首頁  >  文章  >  後端開發  >  如何使用 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