Split 4-channel .tif UAV image to separate files according to the channel

Hello, I’ m trying to separate a UAV .tif (containing four channels: green, red, red-edge, NIR) to four different images, one for each channel. The code that I used for normal RGB (3 channel .tif) worked perfectly but I when I try to implement it to the 4-channel UAV .tif image with the related modifications in the code, I get this message…

  File "code.py", line 4, in <module>
    g,r,re,nir = cv2.split(img)
ValueError: not enough values to unpack (expected 4, got 0)

Any idea how to fix this?

  1. check img.shape
    you probably loaded the image without IMREAD_UNCHANGED, which means OpenCV automatically converts it to (some type of) BGR, losing the extra channel
    it may still reorder the channels. I don’t know exactly how it’ll behave. check the docs and/or run the function and check the results to be sure.

  2. you’re using python. you can also use numpy means:
    (ch1, ch2, ch3, ch4) = np.transpose(img, axes=(2,0,1))

1 Like

Thanks, solution 1 works! But how can be sure which channel is which? With gdalinfo I get only the Green correctly and other 3 as undefined…

don’t expect OpenCV to give you the original tiff data.

you may have to use the tifffile package

1 Like

I tried both openCV and tifffile but I lose the real coordinates in the end result… How can I avoid this?

this is the first time you mentioned coordinates.

1 Like

Yes, this is true! I did not know that the coordinates are affected though!

do you think you should explain what you mean?

Ok, the idea is to have unaffected the coordinates after the calculations with the images… How do I achieve this?

maybe you never meant “coordinates”,
but that the “order/meaning of the channels” has changed ?

Ok! How do I maintain the coordinates?