c++ - 如何设置 CvPoint 的值

标签 c++ opencv

我在 OpenCV 中使用 CvPoint 结构,我需要为该结构的 xy 字段赋值。

这是我的代码:

CvPoint* P1;
P2[0].x=32;

但是程序总是在尝试设置值时阻塞。

知道如何设置这些值吗?

最佳答案

首先,P1 是指向 P1 类型对象的指针。为了通过对象的指针将某些内容分配给对象的成员,您需要使用 -> 运算符。如果此指针指向数组的开头,您可以使用 operator[] 访问单个元素。此运算符返回给定索引的引用,在本例中为 CvPoint&

<强>1。单个对象的动态分配

CvPoint* P1 = new CvPoint(); // default construction of an object of type CvPoint
P1->x = 32;

// do something with P1

// clean up 
delete P1;

<强>2。动态分配或数组

CvPoint* points = new CvPoint[2]; // array of two CvPoints
points[0].x = 32; // operator[] returns a reference to the CvPoint at the given index
points[1].x = 32;

// do something with points

// clean up
delete[] points;

由于在两个示例中都使用了 new 运算符,因此在数组的情况下,必须将它们与对 deletedelete[] 的匹配调用配对。

关于c++ - 如何设置 CvPoint 的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7927396/

相关文章:

python - OpenCV光流示例代码错误

c++ - 这是一个好的设计选择吗?

c++ - 临时对象的复制省略

ios - Xcode 5.1 构建 opencv 在 64 位模拟器上失败

C++ - 停止无限循环 - 追踪轮廓

android - Nexus 4手机上没有调用onPreviewFrame…而是在模拟器上

c++ - SDL 无法使用文件和 header 加载媒体

c++ - setsockopt 在多播成员资格上将错误号设置为 ENOPROTOOPT

c++ - (V)C++ (2010) 正则表达式, "recursive captures"

python - 基于通过欧氏距离(或任何其他距离计算技术)提取的 SIFT 描述符估计两幅图像的相似性分数