Learn Before
Color Extraction
To extract a color from an image we need to convert the BGR file into a HSV file.
hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
Once we have converted the file into an HSV format, we need to pick the lower bound and upper bound values to determine the color we are extracting. However, we would need to find the associated HSV values. We can search it up online with a color picker , we can take a pixel from the original image or pass it BGR values and convert that into HSV values using the same method as above.
BGR_color = np.array([[[255,0,0]]]) print(cv2.cvtColor(BGR_color, cv2.COLOR_BGR2HSV)
For example if we wanted to extract blue we would use the following code:
lower_blue = np.array([90, 50, 50]) upper_blue = np.array([130, 255, 255]) mask = cv2.inRange(hsv, lower_blue, upper_blue) result = cv2.bitwise_and(frame, frame, mask = mask) cv2.imshow('frame', result)
Mask will return a layer that only has blue pixels within the range. Otherwise everything else is going to be blacked out. The mask should be fine tuned to more perform a better job at preventing other colors from appearing.
0
1
Tags
Python Programming Language
Data Science