I am working on python - Open CV on Ubuntu. I am pretty new in python and I am feeling my coding is not optimized.
The final goal is to change the pixel color to an jpeg image. Let's say that if the red channel value is < 255 I set it at 255.
For that, I transformed the jpeg into a numpy.array. Then using 'for/ in:' loops I go pixel by pixel to check if the red channel is <255. If the condition is met, then I change the value to 255.
My code:
import numpy
import cv2
img=cv2.imread('image.jpeg',1)
y=x=-1 # I use y and x as a counters.
#They will track the pixel position as (y,x)
for pos_y in img:
y=y+1; x=-1 #For each new column of the image x is reset to -1
for pos_x in pos_y:
x=x+1
b, g, r = pos_x # I get the blue, green and red color
# please note that opencv is bgr instead of rgb
if r < 255:
r = 255
pos_x = [b,g,r]
img[y,x] = pos_x
cv2.imshow('Image',img)
cv2.waitKey(0)
cv2.destroyAllWindows()
This code works. However, I feel is neither elegant nor efficient.
How could I optimize the code and make it more efficient?