Color Detection with OpenCV

I recently started learning OpenCV. After struggling with the initial installation hurdles, I seem to be on path and cruising smoothly. This is one program I wrote to detect blue color in an image. Thresholds for the Red,Green and Blue levels were put by me after a little trial and error. Posting it here for reference.
Used 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()
{	
	Mat image,result;
	image = imread("img.jpg",1);
	result=image.clone();	

	for(int i=0;i<700;i++){
		for(int j=0;j<697;j++){
			Vec3b col=image.at(i,j);
			if(col[0]>200 && col[1]<200 && col[2]<200){
				circle(result,Point(j,i),5,Scalar(0,0,0));
			}
		}
	}		
	
	namedWindow("image",CV_WINDOW_AUTOSIZE);
	namedWindow("result",CV_WINDOW_AUTOSIZE);

	imwrite("result.jpg",result);

	if(result.data==NULL)
		cout<<"no data";

	imshow("image",image);
	imshow("result",result);

	waitKey(0);
}

Sample Results

Advertisements

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