[英]OpenCV Mat::data is zero after image operation
我在OpenCV C ++中有问题。 我从文件中读取图像,并使用阈值功能获取二进制图像。 之后,我要访问阈值图像的像素值。
问题:阈值图像的Mat :: data数组为0。
我使用阈值(src,img,220,255,CV_THRESH_BINARY); 与src和img beig cv :: Mat。
之后,在img :: data =“”中调用
这是我的代码:
// read image source
Mat src = imread("image.png", CV_LOAD_IMAGE_COLOR);
if(src.empty())
{
return -1;
}
cvtColor(src,src,CV_RGB2GRAY);
threshold(src, src, 220, 255, CV_THRESH_BINARY );
int line[1200];
int lineCount = 0;
for(int i=src.rows; i>src.rows/2; i-=10)
{
uchar* rowi = src.ptr(i-1);
int columnCount = 0;
// begin on the left of the image
for(int j=0; j<src.cols; j++)
{
uchar grayscale = src.at<uchar>(rowi[j]);
// write data to array
line[columnCount] = grayscale;
columnCount++;
}
}
我认为问题可能与引用和复制Mat :: data的cv :: Mat有关:
谢谢你的帮助!
我想您已经检查过image.png(转换为灰度后)包含的值大于220吗?
您可能想尝试将cvtColor
作为非就地操作执行,因为它可能无法正确支持就地转换(请参阅https://stackoverflow.com/a/15345586/19254 )。 例如,您可以这样做:
Mat dst;
cvtColor(src, dst, CV_RGB2GRAY);
...然后在dst
threshold
。 或者,您可以从灰度模式开始将图像读取到内存中(要imread
第二个参数可以是CV_LOAD_IMAGE_GRAYSCALE
),在这种情况下, cvtColor
调用。
每次调用之间,你可以imwrite
图像到磁盘,以找出哪些步骤无法按预期工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.