Colour segmentation or color filtering is widely used in OpenCV for identifying specific objects/regions having a specific color. The most widely used colour space is RGB color space, it is called an additive color space as the three color shades add up to give the color to the image. To identify a region of a specific colour, put the threshold and create a mask to separate the different colors. HSV color space is much more useful for this purpose as the colors in HSV space is much more localized thus can be easily separated. Color Filtering has many applications and use cases such as in Cryptography, infrared analysis, food preservation of perishable foods etc. In such cases, the concepts of Image processing can be used to find out or extract out regions of a particular color.
For color segmentation, all we need is the threshold values or the knowledge of the lower bound and upper bound range of colors in one of the color spaces. It works best in Hue-Saturation-Value color space.
After specifying the range of color to be segmented, it is needed to create a mask accordingly and by using it, a particular region of interest can be separated out.
Below is the code:
import cv2
import numpy as np
cap = cv2.VideoCapture( 0 )
while ( 1 ):
_, frame = cap.read()
# It converts the BGR color space of image to HSV color space
hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
# Threshold of blue in HSV space
lower_blue = np.array([ 35 , 140 , 60 ])
upper_blue = np.array([ 255 , 255 , 180 ])
# preparing the mask to overlay
mask = cv2.inRange(hsv, lower_blue, upper_blue)
# The black region in the mask has the value of 0,
# so when multiplied with original image removes all non-blue regions
result = cv2.bitwise_and(frame, frame, mask = mask)
cv2.imshow( 'frame' , frame)
cv2.imshow( 'mask' , mask)
cv2.imshow( 'result' , result)
cv2.waitKey( 0 )
cv2.destroyAllWindows() cap.release() |
Original Image-
Masked Image-
Blue Color segmented regions-
Recommended Posts:
- Color Spaces in OpenCV | Python
- Python | OpenCV BGR color palette with trackbars
- Detection of a specific color(blue here) using OpenCV with Python
- Opening multiple color windows to capture using OpenCV in Python
- Display the red, green and blue color planes of a color image in MATLAB
- Difference between Low pass filter and High pass filter
- Gaussian Filter Generation in C++
- Noise removal using Median filter in C++
- Python | Filter the negative values from given dictionary
- Python | Unique dictionary filter in list
- Introduction to OpenCV
- OpenCV - Overview
- Python - Filter float strings from String list
- Python | Filter tuples according to list element presence
- OpenCV | Loading Video
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.