Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OpenCV double mat shows up as all white

Tags:

c++

opencv

I have a type 6 (double-valued, single channel) mat with data ranging from 0 to 255. I can print out the data using the following code:

    double* data  =  result.ptr<double>();

    for(int i = 0; i < rows; i++)
        for(int j = 0; j < cols; j++)
    std::cout<<data[i*step+j]<<"\t";

And this appears perfectly normal--in the range from 0 to 255 and the size that I'd expect. However, when I try to show the image:

imshow(window_name, result);
waitKey();

I just get a white image. Just white pixels. Nothing else.

Loading other images from files and displaying in the window works fine.

Using Windows 7, OpenCV 233

like image 889
Jason Avatar asked Dec 03 '22 04:12

Jason


1 Answers

cv::imshow works in following ways -

  1. If the image is 8-bit unsigned, it is displayed as is.
  2. If the image is 16-bit unsigned or 32-bit integer, the pixels are divided by 256. That is, the value range [0,255*256] is mapped to [0,255].
  3. If the image is 32-bit floating-point, the pixel values are multiplied by 255. That is, the value range [0,1] is mapped to [0,255].

Your matrix lies in the 3rd category where imshow is expecting the values to be between 0 and 1 and so it multiplies it by 255. Since your values are already between 0 and 255, you are getting unwanted result. So normalizing the pixels between 0 and 1 will work.

like image 100
Gaurav Raj Avatar answered Dec 19 '22 02:12

Gaurav Raj