Color a black/white .tif image with 3 colors

So, I have a black/white UAV .tif image and I want to group the values of the pixels (ranging from 0 - 300) to 3 groups of colors. So for instance pixels with values from 0-100 to transformed to Green color, pixels from 101-200 to transformed to Red color, and pixels from 201-300 to transformed to Blue color. I am searching on google but I do not find something similar… Any idea how to do this? I am working in python 3.

See OpenCV: ColorMaps in OpenCV

1 Like

or simply numpy:

assert input.shape == (height, width) # single channel

output = np.zeros((height, width, 3), dtype=np.uint8)

output[(input <= 50)] = (255,0,255)
output[(input > 100) & (input <= 150)] = (255,255,0)
output[(input > 200)] = (0,255,255)

always work through the documentation and tutorials of the technologies you use. numpy is a part of that.

1 Like

This is the code:

import cv2
 
input = cv2.imread('UAV_image.jpg')

height = 1390
width = 1300

assert input.shape == (height, width) # single channel

output = np.zeros((height, width, 3), dtype=np.uint8)

output[(input <= 50)] = (255,0,255)
output[(input > 100) & (input <= 150)] = (255,255,0)
output[(input > 200)] = (0,255,255)


cv2.imwrite('UAV_image_3_colors.jpg', image_gray)

Any idea why it prints this??:

    assert input.shape == (height, width) # single channel
AssertionError

Sorry I am very new…all day I am over the google…I am testing it firstly with a jpeg…

your picture isn’t single-channel

or the width and height don’t match

look at input.shape

these are basic debugging skills

1 Like

I changed 2-3 images. The image is black/white so 1 channel and the height/width correct… I cannot figure out what is wrong!

input = cv2.imread(‘UAV_image.jpg’,cv2. IMREAD_GRAYSCALE)

2 Likes

ah yes, imread does autoconversion to BGR, if I remember correctly… IMREAD_UNCHANGED would be useful.

or what sturkmen recommended.

1 Like

I changed the last line to this:

cv2.imwrite('UAV_image_3_colors.jpg',0)

because the previous didn’t run, but I do not see anywhere in the folder the output (stored) image…

no sense at all in that line

1 Like

Thanks to all! You are amazing… It works!

Why did you use bitwise and (&) and not logical and (and)?

those are numpy arrays. they don’t respond well to the and operator. try it.

1 Like

When I increase the code for more colors, 5, I lose the 2 last colors…I do not know why! Any ideas??

I am using this code after so many months, and it prints me the same error:

    assert input.shape == (height, width, 4) # single channel
AssertionError

I checked shape and returns me () What could be possibly wrong? I checked the path it is correct. I used absolute path, the same error!!!

it means, your image is empty / invalid / was not read

besides that:

no you’re assuming 4 channels here, not a single one
(mean what you say, say what you mean)

1 Like

Sorry for the wrong comment. I forgot it there. The UAV .tif has RGBA (4 channels)… What is wrong with the image? I checked another one, the same error…