Color Detection in video from a webcam using OpenCV

Camera : Sony PS3 eye
Opencv 2.4 on Ubuntu 10.10

#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>


#include <iostream>

using namespace cv;
using namespace std;

int main(int, char**)
{
    Size dim;
    VideoCapture cap(1); 
    if(!cap.isOpened())  
        return -1;

    Mat result;
    
    namedWindow("frame",CV_WINDOW_AUTOSIZE);
    for(;;){
      Mat frame;
      cap >> frame; // get a new frame from camera
      result=frame.clone();
      dim=frame.size();
      //cout<<"width = "<<dim.width << " height = "<<dim.height;
      for(int i=0;i<dim.width;i++){
	  for(int j=0;j<dim.height;j++){
	    Vec3b col=frame.at<Vec3b>(i,j);
	    if(col[0]>75 && col[1]<75 && col[2]<75){
	      circle(result,Point(j,i),1,Scalar(0,0,0),-1);
	    }	    
	  }
      }
      imwrite("camera.jpg",result);
      imshow("frame",result);
      if(waitKey(30) >= 0) break;
    }
    return 0;
}

Its a wire clipper with blue handles. Results are noisy as you can see. A lot of stray colors are detected as blue. Need to fix that. Good for a start though.

Advertisements

One thought on “Color Detection in video from a webcam using OpenCV

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s