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.
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.
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
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)
ah yes, imread does autoconversion to BGR, if I remember correctly… IMREAD_UNCHANGED
would be useful.
or what sturkmen recommended.
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
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.
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)
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…
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)
What should I change to the above code for running it in C++ ? I need the related code in C++ Thanks!!
I have installed opencv library and I am using it on python. Do I need to install a different library If I want to use it on C++ ? I am trying to accelerate the above mentioned python code. But as I have read C++ does not make something more than python code, because python calls C++ code on the above example…Am I right? Any ideas how to accelerate the above code??
yes, most operations in Python just call compiled C/C++ code.
you can’t “accelerate” infinitely. this is always true.
you can only accelerate something that (1) is inefficient and (2) can feasibly be made more efficient.
first you need to be aware of why you want to accelerate. not everything is worth optimizing. then you should explain your reasoning so others understand too.
then you need to measure. without numbers to tell you what to scrutinize for performance, you would likely waste effort optimizing the wrong things.