OpenCV:无法访问 Mat 元素
OpenCV: can't access Mat elements
我有一个用循环创建的 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
的输出是OK的:
[79, 99, 148;
73, 29, 14;
254, 254, 254;
171, 70, 3;
178, 189, 211]
并且似乎重塑显然没有效果(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对垫子object本身没有影响。它 returns 一个 cv::Mat object 被重塑。正确的函数调用是:
cluster_centre = cluster_centre.reshape(3,1);
请注意,返回的 object 数据指向源 object 的数据,即只有 header 发生了变化。
我有一个用循环创建的 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
的输出是OK的:
[79, 99, 148;
73, 29, 14;
254, 254, 254;
171, 70, 3;
178, 189, 211]
并且似乎重塑显然没有效果(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对垫子object本身没有影响。它 returns 一个 cv::Mat object 被重塑。正确的函数调用是:
cluster_centre = cluster_centre.reshape(3,1);
请注意,返回的 object 数据指向源 object 的数据,即只有 header 发生了变化。