How to open float 32-bit images?

Hello, I am reading .tif images with this:

view = cv2.imread(path_name, cv2.IMREAD_UNCHANGED)
self.image = cv2.cvtColor(view , cv2.COLOR_BGR2RGB)

However when I try to open 32-bit or 64-bit float images, I get this error:


cv2.error: OpenCV(3.4.14) /tmp/pip-req-build-m7h7rfem/opencv/modules/imgproc/src/color.simd_helpers.hpp:88: error: (-2:Unspecified error) in function 'cv::impl::{anonymous}::CvtHelper<VScn, VDcn, VDepth, sizePolicy>::CvtHelper(cv::InputArray, cv::OutputArray, int) [with VScn = cv::impl::{anonymous}::Set<3, 4>; VDcn = cv::impl::{anonymous}::Set<3, 4>; VDepth = cv::impl::{anonymous}::Set<0, 2, 5>; cv::impl::{anonymous}::SizePolicy sizePolicy = cv::impl::<unnamed>::NONE; cv::InputArray = const cv::_InputArray&; cv::OutputArray = const cv::_OutputArray&]'
> Invalid number of channels in input image:
>     'VScn::contains(scn)'
> where
>     'scn' is 1

that would mean the image is single-channel, i.e. grayscale, and you can’t apply channel reordering (COLOR_BGR2RGB) on something that has a single channel.

what kind of TIFF is this? something weird, a stack of images? if you probe it with imagemagick’s magick identify ..., what does it say?

1 Like

From QGIS…
tif_info

But I need to read it with opencv inside the rest of the python code I have written…

that QGIS info doesn’t hint at there being multiple channels, so the best assumption is that it is actually single-channel.

why did you need cvtColor? do you want it to be 3-channel instead of 1-channel?

1 Like

With QGIS I have read it! But I need to read it with opencv to do some calculations with its pixels. It is 32-bit float grayscale, 1 channel geotif. Any idea what to change to the above mentioned lines of code? Just to read the file, nothing to convert to 3-channels and things like this…

you’re not answering my question.

so far you have shown nothing that requires 3-channel data. 1-channel data is perfectly usable for all kinds of purposes.

if you just want to convert the loaded data into 3-channel, use cvtColor with COLOR_GRAY2BGR.

note that BGR is OpenCV’s favorite channel order. other libraries might assume/prefer RGB order instead.

1 Like

This is code found by google searching…:slight_smile: I don’t know if I need cvtColor. I need to read that 1-channel grayscale 32-bit float image… Can I load it as 1-channel and not converting it to RGB?

your image IS already loaded.
now, what do you want to do with it ?

1 Like

Calculations with pixels. Can I access the pixels without need to display the image somewhere? If yes that’s what I need to do…

yes (maybe you want to read some numpy / opencv tutorials now ?)

1 Like

I am using this code (source: python - How to save 2D float numpy arrays losslessly into a grayscale image while preserving resolution? - Stack Overflow)

import numpy as np                                               
from PIL import Image

reloaded = np.array(Image.open('uav_image.tif'))

But I got this error:

  File "/home/test/.local/lib/python3.9/site-packages/PIL/Image.py", line 3008, in open
    raise UnidentifiedImageError(
PIL.UnidentifiedImageError: cannot identify image file 'uav_image.tif'

Is this problem of my image? Because other uav images i tested (but RGB, not grayscale) are ok with the above code…

How can I transform the 32 bit float image to 8bit int with normalized negative pixel values? I don’t see something else to do… I have spent all day trying to do something…