[英]OpenCV not recognizing Mat size
I'm trying to print an image using OpenCV defining a 400x400 Mat: 我正在尝试使用定义400x400垫的OpenCV打印图像:
plot2 = cv::Mat(400,400, CV_8U, 255);
But when I try print the points, something strange happens. 但是当我尝试打印点时,会发生一些奇怪的事情。 The y coordinate only prints to the first 100 values.
y坐标仅打印到前100个值。 That is, if I print the point (50,100), it does not print it in the 100/400th part of the columns, but at the end.
也就是说,如果我打印点(50,100),它不会在列的100/400部分打印,而是在最后打印。 Somehow, 400 columns have turned into 100.
不知何故,400列已变为100。
For example, when running this: 例如,运行时:
for (int j = 0; j < 95; ++j){
plot2.at<int>(20, j) = 0;
}
cv::imshow("segunda pared", plot2);
Shows this (the underlined part is the part corresponding to the code above): 显示这个(带下划线的部分是与上面代码对应的部分):
A line that goes to 95 almost occupies all of the 400 points when it should only occupy 95/400th of the screen. 当它应该仅占据屏幕的95/400时,到95的线几乎占据所有400点。
What am I doing wrong? 我究竟做错了什么?
When you defined your cv::Mat
, you told clearly that it is from the type CV_8U
: 当你定义你的
cv::Mat
,你清楚地告诉它它来自CV_8U
类型:
plot2 = cv::Mat(400,400, CV_8U, 255);
But when you are trying to print it, you are telling that its type is int
which is usually a signed 32 bit not unsigned 8 bit. 但是当你试图打印它时,你告诉它的类型是
int
,它通常是带符号的32位而不是无符号的8位。 So the solution is: 所以解决方案是:
for (int j = 0; j < 95; ++j){
plot2.at<uchar>(20, j) = 0;
}
Important note: Be aware that OpenCV uses the standard C++ types not the fixed ones. 重要说明:请注意OpenCV使用标准C ++类型而不是固定类型。 So, there is no need to use fixed size types like
uint16_t
or similar. 因此,不需要使用像
uint16_t
或类似的固定大小类型。 because when compiling OpenCV & your code on another platform both of them will change together. 因为在另一个平台上编译OpenCV和你的代码时,它们会一起改变。
BTW, one of the good way to iterate through your cv::Mat
is: 顺便说一句,迭代你的
cv::Mat
的好方法之一是:
for (size_t row = 0; j < my_mat.rows; ++row){
auto row_ptr=my_mat.ptr<uchar>(row);
for(size_t col=0;col<my_mat.cols;++col){
//do whatever you want with row_ptr[col] (read/write)
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.