4

I have a 4 band multichannel image (TIF) which is an unsigned 16 bit image. I am using the following:

    opencv2 
    python 

I have done the following at the beginning of my code

    import cv2 

When I use

  i = cv2.imread('img.tif') 

it is reading all the four bands but as UINT8. When I use

  i = cv2.imread('img.tif', cv2.CV_16U)

it reads only one channel of the image. What should be done to read all the four channels in 16bit unsigned?

2
  • 2
    stackoverflow.com/a/9781142/749973 Commented Jul 8, 2013 at 20:00
  • Thanks for the help, but I asked a Python related question! Commented Jul 9, 2013 at 1:48

1 Answer 1

1

Try passing the flag cv2.IMREAD_ANYDEPTH. The documentation states this flag will:

If set, return 16-bit/32-bit image when the input has the corresponding depth, otherwise convert it to 8-bit.

i = cv2.imread('img.tif', flags=cv2.IMREAD_ANYDEPTH)

If that doesn't work, try the cv2.IMREAD_UNCHANGED flag instead. It's not documented, but it looks like it may also work.

Sign up to request clarification or add additional context in comments.

3 Comments

Nope this does not work. My image loads but only one channel! I have cross verfied this with Matlab where my image loads as UINT16 / 4 channel.
It appears that GDAL from osgeo works better in terms of reading images.
@Indian Did you try the IMREAD_UNCHANGED flag? It works for me.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.