The opencv camera captures recognition colors

  • 2020-06-19 11:12:34
  • OfStack

The example of this paper shares the specific code of opencv camera capturing and identifying colors for your reference. The specific content is as follows


#include "highgui.h"  
#include "iostream"
using namespace std;
 
int main() 
{ 
  CvCapture* pCap = cvCreateCameraCapture(1 );// Here, -1 B: That's ok, but my computer has it CyberLink YouCam Software,   
  int flag=0;                       //OpenCV The camera is called by default instead of the system driver   
 
 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); 
} 

Related articles: