1

I tried the following code to print all the white pixels of this binary image without success:

Mat grayImage;
Mat rgb_Image;
int Max_value = 255;
int Global_Threshold = 155;

rgb_Image = imread( "../Tests/Object/Object.jpg", CV_LOAD_IMAGE_COLOR);   // Read the file

if(! rgb_Image.data )  // Check for invalid input
{
        cout <<  "Could not open or find the image" << endl ;
        return -1;
}

//Convert to Grayscale.
cvtColor(rgb_Image, grayImage, CV_BGR2GRAY);

//Binarize the image with a fixed threshold.
threshold( grayImage, binImage, Global_Threshold, Max_Value, 0);

//Printing white pixels
for(int i = 0; i < 640; i++)
{
    for(int j = 0; j < 480; j++)
    {
        if(255 == binImage.at<float>(i,j))
        {
            cout << binImage.at<float>(i,j) << endl;
        }
    }
}

If I print the values that are not zeroes I get strange values but not 255.

Thanks,

4

2 回答 2

4

cvtColor will create a uchar image, and threshold will keep the data format, so your binImage is made up of uchars, and not float's.

Change

binImage.at<float>(i,j)

to

binImage.at<uchar>(i,j)

and keep in mind that comparing floats with == is usually a bad idea (even when you work with floats), because of floating-point representation errors. You may end up having a matrix full of 254.9999999999, and never meet the image(i,j)==255 condition

于 2012-07-11T14:24:38.223 回答
1

You need to cast it to float.

Although the image is an openCV float it's still stored in a unsigned char* block of data (so it can be easily converted) so "<<" thinks it's receiving a C string and prints the binary data until it sees a '0'

于 2012-07-11T14:01:15.567 回答