简体   繁体   English

图像操作后,OpenCV Mat :: data为零

[英]OpenCV Mat::data is zero after image operation

I have a problem in OpenCV C++. 我在OpenCV C ++中有问题。 I read in an image from file and use the threshold-function to get a binary image. 我从文件中读取图像,并使用阈值功能获取二进制图像。 After that I want to access the pixel values of the thresholded image. 之后,我要访问阈值图像的像素值。

PROBLEM: The Mat::data array of the thresholded image is 0. 问题:阈值图像的Mat :: data数组为0。

I use threshold(src, img, 220, 255, CV_THRESH_BINARY); 我使用阈值(src,img,220,255,CV_THRESH_BINARY); with src and img beig cv::Mat. 与src和img beig cv :: Mat。

After that call in the img::data = " " 之后,在img :: data =“”中调用

Here is my code: 这是我的代码:

// 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++;
    }
}

I think the problem might be about cv::Mat with referencing and copying Mat::data: 我认为问题可能与引用和复制Mat :: data的cv :: Mat有关:

Thanks for your help! 谢谢你的帮助!

I suppose you have checked that your image.png (after having been converted to grayscale) contains values higher than 220? 我想您已经检查过image.png(转换为灰度后)包含的值大于220吗?

You may want to try to perform cvtColor as a non-in-place operation, as it might not properly support in-place conversion (see https://stackoverflow.com/a/15345586/19254 ). 您可能想尝试将cvtColor作为非就地操作执行,因为它可能无法正确支持就地转换(请参阅https://stackoverflow.com/a/15345586/19254 )。 This you would do for example like this: 例如,您可以这样做:

Mat dst;
cvtColor(src, dst, CV_RGB2GRAY);

... and then do threshold on dst . ...然后在dst threshold Alternatively, you could read the image into memory in grayscale mode to begin with (second parameter to imread can be CV_LOAD_IMAGE_GRAYSCALE ), in which case the cvtColor call is not necessary. 或者,您可以从灰度模式开始将图像读取到内存中(要imread第二个参数可以是CV_LOAD_IMAGE_GRAYSCALE ),在这种情况下, cvtColor调用。

Between each call, you can imwrite your image to disk in order to find out which step is not working as expected. 每次调用之间,你可以imwrite图像到磁盘,以找出哪些步骤无法按预期工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM