I would need to verify if and how much of certain predefined color ranges are present in a image file.
each color range is defined by 6 variables and a counter in a colorRange
class:
hS (hue start)
hE (hue end)
sS (saturation start)
sE (saturation end)
lS (lightness start)
lE (lightness end)
colorCounter
The image can either be a file, or loaded from the camera. Following code loads the image from the camera buffer:
img1 = np.ndarray(buffer=component.data.copy(), dtype=np.uint8,
shape=(component.height, component.width, 1))
img2 = cv2.cvtColor(img1, cv2.COLOR_BayerBG2BGR)
what I would need to do is to scan every n-th pixel (5 is a good starting value), and compare it to every color range. if it falls into that color range, then add +1 to that specific colorCounter
. In the end I go over the Counter of each color range and calculate the presence of that color range in %. The goal is to check the % of analized pixels that fall into each color range. The sum of the colorranges can be greater than 100 since a pixel can fall into multiple colorranges as tehy can be overlapping. (for example a color ragne could be all the reds, and another could be only the dark reds.. a dark red pixel would fall into both ranges, a bright red only in the first one.)
The way I would do it is to check every fifth pixel one by one, convert its rgb value to HSL, and then compare it with all the color ranges. (where if hS > hE then it is wrapping around in the reds)
But it seems a very complicated way to do it and was wondering if there are some premade functions that can do this, or at least partially do this.
So the question is: How can this be done in a smart way?
UPDATE:
this is what I have so far:
hls = cv2.cvtColor(img2, cv2.COLOR_BGR2HLS)
GreenLo = np.array([75, 0, 0])
GreenHi = np.array([155, 1, 1])
pxGreen = cv2.inRange(hls[0:620:5, 0:620:5], GreenLo, GreenHi)
cGreen = cv2.countNonZero(pxGreen)