1

I am trying to detect the 3 circles of blue color on the image below, using C++ and openCV.

enter image description here

I use this code

int main(){
     Mat original=imread("img.jpg");
     Mat hsv;
     cvtColor(original,hsv,CV_BGR2HSV);
     Mat bw;
     inRange(hsv,Scalar(110,50,50),Scalar(130,255,255),bw);//detects blue
}

This code does detect the 3 blue circles BUT also detect other blue points. I am thinking it has to do with the range I specified. Is there a way to detect only those RGB blue circles because I do not think there are any other points which are RGB blue in the image. How can I detect only this color (255,0,0)??

Chris
  • 8,030
  • 4
  • 37
  • 56
Steph
  • 609
  • 3
  • 13
  • 32
  • Then directly use inRange() on BGR colour space. – Haris Mar 19 '14 at 08:16
  • but what about the Scalar minimum and maximum values?will they be min=255,0,0 and max=255,0,0? – Steph Mar 19 '14 at 08:17
  • Yes........Like `inRange(original,Scalar(255,0,0),Scalar(255,0,0),bw);` – Haris Mar 19 '14 at 08:21
  • For the image you provided below you won't get accurate result with Scalar(255,0,0) as it has some Green value and G is not 255 every time. You can check both HSV and BGR value of pixel using the code [here](http://stackoverflow.com/questions/22257477/how-to-get-value-of-specific-pixel-after-converting-to-hsv/22265494#22265494) – Haris Mar 19 '14 at 10:01
  • 1
    try to split the image in the RGB color modell, and than work only on the blue channel, run houghcircles, and detect what ever you want to – Engine Mar 19 '14 at 10:06
  • 1
    @Steph Just check this code works fine here `Scalar hsv_l(110,150,150); Scalar hsv_h(130,255,255); cvtColor(original,hsv,CV_BGR2HSV); inRange(hsv,hsv_l,hsv_h,bw);` – Haris Mar 19 '14 at 10:20
  • @Haris you should post this as the answer!it works fine – Steph Mar 20 '14 at 04:17

2 Answers2

3

For the image you provided above, the below thresholds will work fine.

Scalar hsv_l(110,150,150); 
Scalar hsv_h(130,255,255); 
cvtColor(original,hsv,CV_BGR2HSV);
inRange(hsv,hsv_l,hsv_h,bw) 

And you can easily find the HSV value of any pixel using mouse as described here.

Also the HSV color wheel (below) might be helpful to choose any color and get it's HSV value.

HSV Colour wheel

Community
  • 1
  • 1
Haris
  • 13,645
  • 12
  • 90
  • 121
  • Hey if you have a bit of time, can you have a look at my question [here](http://stackoverflow.com/questions/34170856/trying-to-detect-blue-color-from-image-using-opencv-and-getting-unexpected-resu). Actually I have tried to set quite a wide range for blue color, but I am probably missing somethign. – Solace Dec 09 '15 at 04:46
2

If you want to detect the colour represented by (255,0,0) then this is the value you should specify when using the inRange function. Also, if you are interested in the RGB colour, then you don't need to convert your image to hsv.

Note that the relevant page in the OpenCV docs says that the upper and lower bounds in the inRange function are inclusive - you can use the same value for both.

Some minor modifications:

int main()
{
    // Paint a blue square in image
    cv::Mat img = cv::Mat::zeros(100,100,CV_8UC3);
    cv::Scalar blue(255,0,0);

    img(cv::Rect(20,20,50,50)) = blue;
    cv::imshow("Original Image", img);


    // Detect this blue square
    cv::Mat img2;
    cv::inRange(img, blue, blue, img2);
    cv::imshow("Specific Colour", img2);

    cv::imwrite("Input.png",img);
    cv::imwrite("Output.png",img2);
    cv::waitKey(0);

    return 0;
}

Input.png A blue square drawn in an image

Input.png

Output.png The blue square has been detected in this binary image

Output.png

Chris
  • 8,030
  • 4
  • 37
  • 56
  • thanks for this @Chris. But I still cant detect only those 3 blue circles.Actually,I have to detect those blue circles and then draw a contour around it.In all the size of the contours should be 3. Here is my full code http://pastebin.com/5PqQDXGX and here is the image http://i.imgur.com/p2jO5lf.jpg – Steph Mar 19 '14 at 08:41
  • Did you find a solution in the meanwhile? – Jürgen K. Aug 12 '15 at 10:33