Home > Backend Development > C++ > body text

How to Improve Red Object Detection Accuracy in HSV Color Space with OpenCV?

Linda Hamilton
Release: 2024-11-15 04:43:02
Original
980 people have browsed it

How to Improve Red Object Detection Accuracy in HSV Color Space with OpenCV?

Optimized HSV Color Space Object Detection with OpenCV

Problem:

Given an image containing a red rectangle, the task is to enhance the detection accuracy of the red color using OpenCV's cv::inRange method within the HSV color space.

Original Approach:

int H_MIN = 0;
int H_MAX = 10;
int S_MIN = 70; 
int S_MAX = 255;
int V_MIN = 50;
int V_MAX = 255;

cv::inRange( imageHSV, cv::Scalar( H_MIN, S_MIN, V_MIN ), cv::Scalar( H_MAX, S_MAX, V_MAX ), imgThreshold0 );
Copy after login

This approach provides unsatisfactory results.

Improved Solution:

The original approach fails to account for the "wrapping" of red color around 180 degrees in the HSV space. To address this, the H range needs to include both [0,10] and [170, 180].

inRange(hsv, Scalar(0, 70, 50), Scalar(10, 255, 255), mask1);
inRange(hsv, Scalar(170, 70, 50), Scalar(180, 255, 255), mask2);

Mat1b mask = mask1 | mask2;
Copy after login

This updated approach yields improved detection results.

Alternative Approach:

Another efficient method is to:

  1. Invert the BGR image.
  2. Convert to HSV.
  3. Search for cyan color.
Mat3b bgr_inv = ~bgr;
inRange(hsv_inv, Scalar(90 - 10, 70, 50), Scalar(90 + 10, 255, 255), mask); // Cyan is 90
Copy after login

This alternative approach provides a single range check and produces satisfactory results.

The above is the detailed content of How to Improve Red Object Detection Accuracy in HSV Color Space with OpenCV?. 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