OpenCV: convertTo returns white image (sometimes)

16,833

I believe the result is normal.

When you use convertTo from CV_8U1 to CV32F1, a pixel value, for example, 255 becomes 255.0. But when you try `imshow' the resulting image, the command expects all pixel values to be between 0.0 and 1.0. that's why, without rescaling the image, the image will look all white.

So this will do the trick as zzz pointed out (thanks).

input.convertTo(output, CV_32F, 1.0/255.0)
Share:
16,833

Related videos on Youtube

moatilliatta
Author by

moatilliatta

Updated on July 03, 2022

Comments

  • moatilliatta
    moatilliatta almost 2 years

    I'm relatively new to OpenCV and i've stumpled upon a problem. I've got an input image and want to convert it from Type CV_8U to CV_32F.

    With some images it works just fine via input.convertTo(output, CV_32F) but with other images output would only give an completly white image.

    Number of channels, dims is equal as well as depth. What is the problem?

  • zzzz
    zzzz over 11 years
    Therefore, use the third argument of convertTo as a scalefactor. In your case it would be 1.0/255.0. That way your image gets scaled as it converts. See the documentation at: docs.opencv.org/modules/core/doc/…
  • arvind.mohan
    arvind.mohan over 7 years
    after doing this, my whole image is black now. Every pixel has very low value but looks like the range has not been changed still.
  • Tae-Sung Shin
    Tae-Sung Shin over 7 years
    @arvind.mohan Please try to examine another reason it makes your image black. For example, Is your original image 16 bit? Are you passing an integer instead of float or double for scalingfactor? This method has been working for so long time and may not be the reason your converted image is black.
  • arvind.mohan
    arvind.mohan over 7 years
    @Tae-SungShin there is no difference in absolute values, the original image was 8bit int. I converted that to 64FC3 and scaled 1.0/255.0f. The scaling doesn't seems to be changing when I dumped the converted matrix as jpg image.
  • Tae-Sung Shin
    Tae-Sung Shin over 7 years
    @arvind.mohan Wait, you are saving double-precision image to jpg? (1) you may not save double-precision image with OpenCV. (2) There are not many programs that can display double-precision image even if you could save the image.