I'm trying to extract highlighted text from an image using Python. I obtained the color segmentation code to separate a specific color - in this case green - from an image into its own layer. My question is how can I provide lower and upper limit for the color? It might also be the case that specific color is yellow. Is there any python code to automatically detect lower and upper limits for the color?
def mask_image(img_src, lower, upper):
"""Convert image from RGB to HSV and create a mask for given lower and upper boundaries."""
# RGB to HSV color space conversion
img_hsv = cv2.cvtColor(img_src, cv2.COLOR_BGR2HSV)
hsv_lower = np.array(lower, np.uint8) # Lower HSV value
hsv_upper = np.array(upper, np.uint8) # Upper HSV value
# Color segmentation with lower and upper threshold ranges to obtain a binary image
img_mask = cv2.inRange(img_hsv, hsv_lower, hsv_upper)
return img_mask, img_hsv
Here is the image:
CodePudding user response:
Not 100% sure what you are asking, but if you just want to find coloured regions but don't know (or maybe care) which actual colour, then all you need to know is that blacks, whites and greys all have "zero saturation". So, effectively you are just looking for saturation above zero.
In practice, and especially if your scans (?) are JPEG format which does chroma-subsampling, you may want to allow a few percent tolerance.
Untested, but if you want a concrete suggestion as a starting point for "any colour":
HSVmin = [0, 10, 0]
HSVmax = [255, 255, 255]