opencv中的颜色检测 [英] Color detection in opencv

查看:721
本文介绍了opencv中的颜色检测的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从实时视频流中检测特定颜色,蓝色。
我写了以下代码,显示实时视频流,并将其更改为HSV和灰度。因为我是opencv的新手,我不知道下一步该做什么。

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. 载入框。

  2. 将BGR转换为HSV颜色空间。

  3. 范围的颜色范围与检测。

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

编辑

代码可从源图片中找到任何像素的HSV值。您可以在这里查看有关HSV色彩空间的良好说明,从那里下载HSV色轮手动找出HSV范围。

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.

以下范围可用于评论

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

代码可以使用:

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

这是输出:

img src =https://i.stack.imgur.com/xvGMz.jpgalt =输入图片说明here>

这篇关于opencv中的颜色检测的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆