Choosing Color Boundaries for Object Detection with cv::inRange (OpenCV)
When utilizing the cv::inRange function for color detection, selecting appropriate upper and lower HSV boundaries is crucial. This article addresses the question of how to effectively determine these boundaries based on a specific color of interest.
Background
HSV (Hue, Saturation, Value) is a color space commonly used in image processing. The HSV model represents colors as three components:
Choosing Boundaries
Determining proper HSV boundaries is based on the specific color being detected. Here's a step-by-step guide:
Determine Hue:
Adjust Hue Range:
Set Saturation and Value Ranges:
Consider Format:
Example
Let's consider the example of detecting an orange lid in an image.
HSV Values:
Adjusted Boundaries:
Python Code:
import cv2 import numpy as np ORANGE_MIN = np.array([11, 50, 50], np.uint8) ORANGE_MAX = np.array([33, 255, 255], np.uint8) # Read and convert image img = cv2.imread('image.png') hsv_img = cv2.cvtColor(img, cv2.COLOR_BGR2HSV) # Detect orange using inRange mask = cv2.inRange(hsv_img, ORANGE_MIN, ORANGE_MAX) # Display mask cv2.imshow('Mask', mask) cv2.waitKey(0)
The above is the detailed content of How to Effectively Determine HSV Color Boundaries for Object Detection using cv::inRange?. For more information, please follow other related articles on the PHP Chinese website!