c++ - 在 openCV 中访问 rgbMat 值

标签 c++ opencv image-processing

我试图在映射后打印出 rgbMat 的值(在下面的代码中),但出现了一些错误。谁能告诉我哪里出错了?它向我显示以下错误:

g++ `pkg-config --cflags --libs opencv` line1.cpp 
l1.cpp: In function ‘int main(int, char**)’:
l1.cpp:28:50: error: request for member ‘at’ in ‘rgbMat’, which is of non-class type ‘CvMat*’
l1.cpp:28:58: error: expected primary-expression before ‘>’ token
l1.cpp:28:62: error: name lookup of ‘x’ changed for ISO ‘for’ scoping
l1.cpp:28:62: note: (if you use ‘-fpermissive’ G++ will accept your code)

我想我在以下行中出错了:

  printf("matrix is %u: \n", rgbMat.at<uchar>(y,x));

我的代码是:

     IplImage* rgb[3];

     float L0[]={
         -1,-1,-1,-1,-1,
        0, 0, 0, 0, 0,
        2, 2, 2, 2, 2,
        0, 0, 0, 0, 0,
        -1,-1,-1,-1,-1 };

     CvMat*  rgbMat = cvCreateMat(5, 5, CV_32FC1);

     for (int y = 0; y < 5; y++)
     {
         for (int x = 0; x < 5; x++)
             cvmSet(rgbMat, y, x, L0[y*5 + x]);
           printf("matrix is %u: \n", rgbMat.at<uchar>(y,x));
     }

最佳答案

总结讨论:

建议将所有 OpenCV 矩阵的使用移植到新的 cv::Mat 格式。但是,OpenCV 提供了 convert between the old and new formats 的功能:

Partial yet very common cases of this user-allocated data case are conversions from CvMat and IplImage to Mat. For this purpose, there are special constructors taking pointers to CvMat or IplImage and the optional flag indicating whether to copy the data or not. Backward conversion from Mat to CvMat or IplImage is provided via cast operators Mat::operator CvMat() const and Mat::operator IplImage(). The operators do NOT copy the data.

在您的代码中,首先转换 rgbMatcv::Mat格式使用 cv::Mat rgbMatcpp(rgbMat) .现在您应该能够使用 at 访问元素.例如,rgbMatcpp.at<float>(y, x) .接下来,注意变量 xy范围仅限于 for它们在其中运行的循环。因此,对内部 for 使用大括号循环并移动 printf进入循环体。此外,在您尝试使用 at 时,您已将类型指定为 uchar ,这是错误的,因为 rgbMat 的类型是 CV_32FC1 .所以改变你对at的使用至 rgbMatcpp.at<float>(y, x) .

关于c++ - 在 openCV 中访问 rgbMat 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17472569/

相关文章:

python - Python:OpenCv函数cvHaarDetectObjects返回什么?

python - 为什么imread返回NULL?

matlab - 图像无法正确显示

c++ - libcxx 中 std::is_function 的实现如何工作?

c++ - 如何在二维数组中绘制 "line"(屏幕模拟)

c++ - 迭代包含在两个范围的交集中的对象

c++ - 有函数模板模板参数这种东西吗?

c++ - 如何只训练我的数据一次

python - 如何使用python曲折排序并连接每行的值

php - 使用 PHP 从 URL 保存图像(保存为 0KB 文件大小)