Reputation: 41
import cv2
import numpy as np
img = cv2.imread('image.jpg')
res = img.copy()
hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
lower_green = np.array([40,50,50])
upper_green = np.array([80,255,255])
r2, g2, b2 = 255, 0, 0
mask = cv2.inRange(hsv, lower_green, upper_green)
mask = mask/255
mask = mask.astype(np.bool)
res[:,:,:3][mask] = [b2, g2, r2] # opencv uses BGR
cv2.imshow('image', img)
cv2.imshow('Result', res)
cv2.waitKey(0)
cv2.destroyAllWindows()
I want to detect lower to upper values of colors based on click in photo. For example in this code, I used [40,50,50]
as a lower value of green and [80,255,255]
as an upper value of green, but I want to automate this process. For instance, you clicked the green side of the photo and this return you the
[40,50,50]
and [80,255,255]
Upvotes: 0
Views: 461
Reputation: 51
You might use lists or dictionaries for the values of each shade (R,G,B), then by an event for the click you could get the value of BGR on the picture (like: 40,50,50) and by comparing each value by conditionals:
(if valueB > 40 and valueB < 80 and valueG > 50 and valueG < 255 and valueR > 50 and valueR < 255: print("the values of the color are [40,50,50] and [80,255,255]") )
and in that way for the colors you want to get.
Upvotes: 1