Reputation: 11
I need to find parts of an image that are of certain brightness (say between 120 and 135) and then set their brightness to 255 and the rest of the image to 0.
I have an image that I converted to grayscale and then blurred so as to reduce noise, and so far I only found this function:
threshold = cv2.threshold(imageGrayscaleBlurred, 120, 255, cv2.THRESH_BINARY)[1]
As far as I know, this function can only take a certain threshold and in my case it will set all parts of the image that are brighter than 120 to 255, and make the rest of the image black. This doesn't fit my goal because the output image looks pretty messy.
Is there a "fancier" way to achieve my goal without using the cv2.threshold()
function twice with a different threshold?
Upvotes: 0
Views: 222
Reputation: 21233
Create a 1-channel mask of the same shape as imageGrayscaleBlurred
, filled with zeros (black):
mask = np.zeros((imageGrayscaleBlurred.shape[0], imageGrayscaleBlurred.shape[1]), np.uint8))
Place the following condition that suits your requirement:
mask[(imageGrayscaleBlurred >= 120) & (imageGrayscaleBlurred <= 135)] = 255
Upvotes: 1