本文实例为大家分享了opencv摄像头捕获识别颜色的具体代码,供大家参考,具体内容如下
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
|
#include "highgui.h"
#include "iostream"
using namespace std;
int main()
{
CvCapture* pCap = cvCreateCameraCapture(1 ); //这里-1也可以,不过我的电脑装的有CyberLink YouCam软件,
int flag=0; //OpenCV会默认调用该摄像头,而不调用系统的驱动
IplImage* frame =NULL;
if (cvCreateCameraCapture == NULL)
{
return (0);
}
cvNamedWindow( "Camera" ,CV_WINDOW_FULLSCREEN);
while ((frame = cvQueryFrame(pCap)) != 0 && cvWaitKey(20) != 27)
{
frame = cvQueryFrame(pCap);
//cout<<frame->width<<" "<<frame->height<<endl;
flag=0;
for ( int x=0;x<frame->height;x++)
{
for ( int y=0;y<frame->width*3;y++)
{
//CV_IMAGE_ELEM(frame,uchar,x,y)=255;
if ( ((uchar*)(frame->imageData+x*frame->widthStep))[y*frame->nChannels+0]<30 //b
&&((uchar*)(frame->imageData + x*frame->widthStep))[y*frame->nChannels + 1]<30 //g
&&((uchar*)(frame->imageData + x*frame->widthStep))[y*frame->nChannels + 2]>150) //r
flag++;
}
}
cout<<flag<<endl;
if (flag>70000)
cout<< "red" ;
else
cvShowImage( "Camera" ,frame);
}
aaa: cvReleaseCapture(&pCap);
cvDestroyWindow( "Camera" );
system ( "pause" );
return (0);
}
|
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。