首页  >  文章  >  后端开发  >  如何使用 OpenCV 提高 HSV 颜色空间中的红色物体检测精度?

如何使用 OpenCV 提高 HSV 颜色空间中的红色物体检测精度?

Linda Hamilton
Linda Hamilton原创
2024-11-15 04:43:02941浏览

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 );

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;

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

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

以上是如何使用 OpenCV 提高 HSV 颜色空间中的红色物体检测精度?的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn