我有一个用循环创建的 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 显然没有效果(cols
和 rows
保持不变):
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/