c++ - OpenCV:无法访问 Mat 元素

标签 c++ opencv mat

我有一个用循环创建的 Mat 对象:

cluster_centre = cv::Mat(num_clusters,num_dimensions,cv::DataType<double>::type) 
// <double> is essential

for (j = 0; j < num_clusters; j++) {
        for (k = 0; k < num_dimensions; k++) {
          ...
          cluster_centre.at<double>(j,k) = ...
          }
}

// rounding numbers 0...255
cluster_centre.convertTo(cluster_centre, CV_32S);

cout << cluster_centre << endl 的输出没问题:

 [79, 99, 148;
 73, 29, 14;
 254, 254, 254;
 171, 70, 3;
 178, 189, 211]

而且似乎 reshape 显然没有效果(colsrows 保持不变):

cluster_centre.reshape(3,1); // storing as 1-D array of 3-channel vectors
cout << cluster_centre.cols //output 3;

当我尝试访问我的元素并进一步绘制 BGR 颜色时,我得到:

cout << Scalar(
    mycolors.at<uchar>(0,0), 
    mycolors.at<uchar>(0,1), 
    mycolors.at<uchar>(0,2))<<endl;

[79, 0, 0, 0] // ??

cout << Scalar(
    mycolors.at<uchar>(0,0), 
    mycolors.at<uchar>(1,0), 
    mycolors.at<uchar>(2,0))<<endl;

[79, 73, 254, 0] //vertical

编辑:矩阵 isContinuous , 检查。

最佳答案

函数 Mat::reshape 对 mat 对象本身没有影响。它返回一个 reshape 的 cv::Mat 对象。正确的函数调用是:

cluster_centre = cluster_centre.reshape(3,1);

请注意,返回的对象数据指向源对象的数据,即只有 header 发生了变化。

关于c++ - OpenCV:无法访问 Mat 元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40216279/

相关文章:

c++ - 不使用 STL 的原因是什么?

c# - 如何编写为模板化基指定类型参数的派生类

c++ - 从源代码构建编译器是否会带来更好的优化?

c++ - 使用 cv::ml::StatModel::train 以使用 KNN 的 Opencv 错误

image-processing - DirectX 和 OpenCV

c++ - #error 指令中是否允许使用非拉丁字符?

c++ - 为什么我手动调整的、启用 SSE 的代码这么慢?

c++ - 自动释放 Mat 中的用户数据。是否可以?

android - Android jni cpp 文件中的垫到位图

c++ - C++字符串中的ROS cv_bridge ImagePtr转换