Color detection in opencv

帅比萌擦擦* 提交于 2019-12-17 17:59:39

问题


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;
}

回答1:


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 output:



来源:https://stackoverflow.com/questions/19189482/color-detection-in-opencv

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!