Home > Backend Development > C++ > How can I achieve more precise red color detection in OpenCV using HSV color space?

How can I achieve more precise red color detection in OpenCV using HSV color space?

Mary-Kate Olsen
Release: 2024-11-22 08:53:10
Original
303 people have browsed it

How can I achieve more precise red color detection in OpenCV using HSV color space?

Improving Red Color Detection with OpenCV using HSV Color Space

In OpenCV, the HSV color space offers an effective approach to detect specific colors, including red. However, due to the circular nature of the hue channel in HSV, red color can wrap around values near 180 degrees. This can pose challenges in detecting red objects accurately.

To address this issue, a more precise detection can be achieved by considering two ranges for the hue component: [0,10] and [170, 180]. By including both ranges, we ensure that the detection covers the entire red color spectrum.

The following Python code demonstrates this approach:

import cv2

# Read the input image
image = cv2.imread("path_to_image")

# Convert BGR to HSV color space
hsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)

# Define HSV values for red color
hue_min1 = 0
hue_max1 = 10
hue_min2 = 170
hue_max2 = 180
sat_min = 70
sat_max = 255
val_min = 50
val_max = 255

# Create masks for the two hue ranges
mask1 = cv2.inRange(hsv, (hue_min1, sat_min, val_min), (hue_max1, sat_max, val_max))
mask2 = cv2.inRange(hsv, (hue_min2, sat_min, val_min), (hue_max2, sat_max, val_max))

# Combine the masks
mask = mask1 | mask2

# Display the mask
cv2.imshow("Mask", mask)
cv2.waitKey(0)
cv2.destroyAllWindows()
Copy after login

This code effectively detects the red rectangle in the image, as shown in the mask output.

Alternative Approach

An alternative method is to invert the BGR image and then convert it to HSV. This approach essentially searches for the complementary color, cyan (90 degrees on the hue channel), allowing you to detect red with a single range.

The following Python code demonstrates this technique:

import cv2

# Read the input image
image = cv2.imread("path_to_image")

# Invert the BGR image
inverted_image = cv2.bitwise_not(image)

# Convert inverted image to HSV color space
hsv_inverted = cv2.cvtColor(inverted_image, cv2.COLOR_BGR2HSV)

# Define HSV values for cyan color (inverted red)
hue_min = 90 - 10
hue_max = 90 + 10
sat_min = 70
sat_max = 255
val_min = 50
val_max = 255

# Create a mask for the cyan color range
mask = cv2.inRange(hsv_inverted, (hue_min, sat_min, val_min), (hue_max, sat_max, val_max))

# Display the mask
cv2.imshow("Mask", mask)
cv2.waitKey(0)
cv2.destroyAllWindows()
Copy after login

Both approaches offer improved red color detection using OpenCV in HSV color space, providing more accurate results for image processing applications.

The above is the detailed content of How can I achieve more precise red color detection in OpenCV using HSV color space?. 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