Reputation: 59
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;
}
Upvotes: 3
Views: 30590
Reputation: 14053
You can do this in three steps:
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:
This is the output:
Upvotes: 10