I am using OpenCV to process an image, and in my code, I have to check / edit each pixel separately:
import cv2, numpy
# we just use an empty image for the purpose of this MCVE
img = cv2.imread("source.jpg")
width = len(img[0])
height = len(img)
empty_img = numpy.zeros((height, width, 3), numpy.uint8)
i = 0
r = 0
c = 0
for line in img:
c = 0
for pixel in line:
blue = pixel[0]
green = pixel[1]
red = pixel[2]
if green != max(red, green, blue) or green < 35:
# this has a greenishy hue
empty_img.itemset((r, c, 0), 255)
c += 1
r += 1
This code works, but is quite slow (it takes about 23 seconds to process the image). How could/should I speed this up?