Home > Backend Development > Python Tutorial > How to Effectively Choose Optimal HSV Boundaries for Color Detection in OpenCV using `cv::inRange`?

How to Effectively Choose Optimal HSV Boundaries for Color Detection in OpenCV using `cv::inRange`?

Barbara Streisand
Release: 2025-01-05 09:49:39
Original
804 people have browsed it

How to Effectively Choose Optimal HSV Boundaries for Color Detection in OpenCV using `cv::inRange`?

Choosing Optimal HSV Boundaries for Color Detection with cv::inRange in OpenCV

In image processing tasks, it's often necessary to detect objects based on their color. For this purpose, the cv::inRange function is commonly used in OpenCV to identify pixels within a specified HSV color range. However, selecting the appropriate HSV boundaries can be challenging, especially when different applications utilize varying HSV scales and color formats.

Problem:

Consider the scenario of detecting the orange lid on a coffee can image. Using a gimp tool, the HSV value at the lid's center was found to be (22, 59, 100). However, applying the HSV range (18, 40, 90) - (27, 255, 255) resulted in unsatisfactory detection results.

Solution 1: Adjust HSV Scale

To resolve this issue, it's crucial to be aware that different applications use different HSV scales. In this case, gimp uses the H: 0-360, S: 0-100, V: 0-100 scale, while OpenCV employs H: 0-179, S: 0-255, V: 0-255. For the hue value (22) obtained from gimp, it's necessary to take half of it (11) and adjust the range accordingly. This translates to a new HSV range of (5, 50, 50) - (15, 255, 255).

Solution 2: Convert to BGR Format

Additionally, it's important to consider that OpenCV uses the BGR color format, not RGB. Therefore, in the Python code, the cv::CV_RGB2HSV conversion should be replaced with cv::CV_BGR2HSV.

By implementing these modifications, the detection algorithm should yield improved results. While minor false detections may still occur, the largest contour should correspond to the lid.

Improved Python Code with OpenCV 2:

import cv2

in_image = 'kaffee.png'
out_image = 'kaffee_out.png'
out_image_thr = 'kaffee_thr.png'

ORANGE_MIN = np.array([5, 50, 50], np.uint8)
ORANGE_MAX = np.array([15, 255, 255], np.uint8)

def test1():
    frame = cv2.imread(in_image)
    frameHSV = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
    frame_threshed = cv2.inRange(frameHSV, ORANGE_MIN, ORANGE_MAX)
    cv2.imwrite(out_image_thr, frame_threshed)

if __name__ == '__main__':
    test1()
Copy after login

Enhanced Python Code with OpenCV 4:

import cv2
import numpy as np

in_image = 'kaffee.png'
out_image = 'kaffee_out.png'
out_image_thr = 'kaffee_thr.png'

ORANGE_MIN = np.array([5, 50, 50], np.uint8)
ORANGE_MAX = np.array([15, 255, 255], np.uint8)

def test1():
    frame = cv2.imread(in_image)
    frameHSV = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
    frame_threshed = cv2.inRange(frameHSV, ORANGE_MIN, ORANGE_MAX)
    cv2.imwrite(out_image_thr, frame_threshed)

if __name__ == '__main__':
    test1()
Copy after login

Using these updated codes, it is possible to accurately detect the orange lid on the coffee can image.

The above is the detailed content of How to Effectively Choose Optimal HSV Boundaries for Color Detection in OpenCV using `cv::inRange`?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template