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:
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.
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!