0

I am extracting the blue color using OpenCV inRange(), and the code is written in C++.

My problem is my range doesn't cover the varying shades of blue, for example 1st Image blue color was extracted perfectly as shown 1st image resultOn the other hand, 2nd Image was not extracted as shown 2nd image result

The masking code

+ (UIImage *)detectFourCorners:(UIImage *)image{

cv::Mat mat;
UIImageToMat(image, mat);

// Convert input image to into BGR
cv::Mat bgr_image;
cv::cvtColor(mat, bgr_image, cv::COLOR_RGB2BGR);

// Convert input image to HSV
cv::Mat hsv_image;
cv::cvtColor(bgr_image, hsv_image, cv::COLOR_BGR2HSV);

cv::Mat mask;

// original
cv::inRange( hsv_image, cv::Scalar(100,150,0), cv::Scalar(140,255,255), mask);

cv::Mat result_blue;
cv::bitwise_and(mat,mat,result_blue,mask);

return MatToUIImage(result_blue); }

I believe I'm not covering all the needed ranges of blue, but I don't know how to get my own ranges. If anyone could help, it would be appreciated !

Christoph Rackwitz
  • 11,317
  • 4
  • 27
  • 36
MadawiAh
  • 27
  • 3
  • 2
    Try creating a color tracker similar to this: https://stackoverflow.com/questions/10948589/choosing-the-correct-upper-and-lower-hsv-boundaries-for-color-detection-withcv/ – Jeru Luke May 04 '22 at 15:12
  • 2
    To be honest, I'm having trouble telling that those squares are blue. If a person can't do it, how can a computer? – Mark Ransom May 04 '22 at 15:15
  • 1
    I increased the range for the saturation (`S`) chanel to 100..255 (instead of 150..255) and the second image get a lot better result. It might introduce fasle positives with other images, but anyway how did you calibrate your range thresholds ? – wohlstad May 04 '22 at 15:24
  • Im still going to do some tests, but at this point changing "S" channel to 90 solved the problem!, I wasn't able to understand the ranges-basically brute forcing them- but after checking @JeruLuke attached answer it makes a lot of sense now!, THANK YOU ALL – MadawiAh May 04 '22 at 18:56

0 Answers0