3

I want to detect a specific color say, blue, from a live video stream. I have written the following code which displays the live video stream and change it into HSV and grayscale. Since I am completely new to opencv I have no idea what to do next.

Can someone complete the code for me to detect a specific color.

#include<opencv\cv.h>
#include<opencv\highgui.h>

using namespace cv;


int main(){
Mat image;
Mat gray;
Mat hsv;
VideoCapture cap;
cap.open(0);
namedWindow("window", CV_WINDOW_AUTOSIZE);
namedWindow("gray", CV_WINDOW_AUTOSIZE);
namedWindow("hsv", CV_WINDOW_AUTOSIZE);

while (1){
    cap >> image;
    cvtColor(image, gray, CV_BGR2GRAY);
    cvtColor(image, hsv, CV_BGR2HSV);
    imshow("window", image);
    imshow("gray", gray);
    imshow("hsv", hsv);
    waitKey(33);
}
return 0;
}
BBB
  • 59
  • 1
  • 2
  • 13
  • Please try searching online before posting here. Looking briefly I found this page that explains color detection in OpenCV: http://opencv-srf.blogspot.com/2010/09/object-detection-using-color-seperation.html – fintelia Oct 04 '13 at 20:04
  • Thanks! I had already looked into that website and couldn't understand anything but finally after some more reading I was able to understand the code given in that website. – BBB Oct 05 '13 at 18:43

1 Answers1

10

You can do this in three steps:

  1. Load frame.
  2. Convert BGR to HSV color space.
  3. Inrange between the color range to detect.

Edit

You can use this code to find the HSV value of any pixel from your source image. You can see a good explanation about HSV color space here, download the HSV colour wheel from there and manually find out the HSV range.

The following range can be used for the image supplied in the comments:

hsv_min--> (106,60,90)
hsv_max-->(124,255,255)

The following code can be used:

Mat src=imread("image.jpg");
Mat HSV;
Mat threshold;
    
cvtColor(src,HSV,CV_BGR2HSV);
inRange(HSV,Scalar(106,60,90),Scalar(124,255,255),threshold);
imshow("thr",threshold);     

This is the input:

enter image description here

This is the output:

enter image description here

Masoud Keshavarz
  • 2,166
  • 9
  • 36
  • 48
Haris
  • 13,645
  • 12
  • 90
  • 121
  • I have a question. I can convert to HSV but how exactly do I find the "inRange" for my color which may vary over an image slightly. Take a look at this image: http://i.stack.imgur.com/0xud4.jpg – Sohaib Oct 05 '13 at 18:44
  • Thanks a lot. Is it always hit and trial? How can we say a certain color lies between a range. For example I was using something like (3,50,50) and (19,240,240) as my range. But it would miss a few markers. When I changed the initial range to (3,70,50) some more markers were detected but some earlier ones vanished. I really can't grasp the concept. – Sohaib Oct 07 '13 at 13:48
  • Btw I just changed the range in my program and the range you mention did not function in my program at all. – Sohaib Oct 07 '13 at 13:56
  • Well my fault it seems. It works perfectly on the image you used. But have a look at these: http://sdrv.ms/17bFUnr There are three more similar photos there one of which I sampled using the color range u mentioned and it gave good results but not perfect. Problem is I am sampling a video and in some images I get all 65 markers sampled but in some images I get only 64. – Sohaib Oct 07 '13 at 14:10