Wednesday, May 13, 2009

Color Filter with OpenCV

This is a color filter test coded with OpenCV. There are six track bars to set the minimum and the maximum ranges of color. here cvInRangeS( ) openCV function has used to filter out the color according to the MAX and MIN values. This is just an one method to do filtering color.


nput image


Output image

// filter.cpp : Defines the entry point for the console application.



#include "stdafx.h"
#include
#include
#include

#define IN_RANG_FILTER

void in_rang_filter();
void Allocate_imgs();


IplImage *img = NULL,*test,*imgHsv,*imgResult;
int mR_val=19,mG_val=13,mB_val=18,MAR_val=154,MAG_val=256,MAB_val=79;//default green .ctrl BLUE to find color


void set_RGB(int pos)
{
#ifdef IN_RANG_FILTER
in_rang_filter();
#endif
}


int main(int argc, char** argv)
{

CvSeq* find_contour = NULL;
bool isStop = false;
img = cvLoadImage("test.jpg");
if(img == NULL)
{
printf("capture device not found!!");
return -1;
}


Allocate_imgs();

// creat windows
cvNamedWindow("Capture", CV_WINDOW_AUTOSIZE);
cvNamedWindow("Result", CV_WINDOW_AUTOSIZE);


//To in_rang_filter() MIN , MAX RGB value set
cvCreateTrackbar( "minR","Result",&mR_val, 256,set_RGB);
cvCreateTrackbar( "minG","Result",&mG_val, 256,set_RGB);
cvCreateTrackbar( "minB","Result",&mB_val, 256,set_RGB);

cvCreateTrackbar( "maxR","Result",&MAR_val, 256,set_RGB);
cvCreateTrackbar( "maxG","Result",&MAG_val, 256,set_RGB);
cvCreateTrackbar( "maxB","Result",&MAB_val, 256,set_RGB);
//


cvCvtColor( img, imgHsv, CV_BGR2HSV);//convert the color space
in_rang_filter();


cvShowImage("Capture", img);
cvShowImage("Result", test);
cvWaitKey(0);
cvReleaseImage(&imgResult);
cvReleaseImage(&imgHsv);
cvDestroyAllWindows();

}


void in_rang_filter()
{
IplImage* test=cvCreateImage(cvGetSize(img), IPL_DEPTH_8U, 3);
CvScalar min_color = CV_RGB(mR_val,mG_val,mB_val);
CvScalar max_color = CV_RGB(MAR_val,MAG_val,MAB_val);
cvInRangeS(imgHsv, min_color,max_color, imgResult);//search for the color in image

cvCvtColor( imgResult, test,CV_GRAY2RGB);
cvShowImage("Result", test);
cvReleaseImage(&test);
}

void Allocate_imgs()
{
const int w = img->width;
const int h = img->height;
imgResult = cvCreateImage(cvSize(w, h), IPL_DEPTH_8U,1);
imgHsv=cvCreateImage(cvSize(w, h), IPL_DEPTH_8U, 3);
}

code can be downloaded here (.rar).

4 comments:

  1. Hi it's impossible to access to dl your file, how can I dl it?

    Regards

    Paolo

    ReplyDelete
  2. your download link appears to be broken

    ReplyDelete
  3. Can you explain why it only work with the blue control? The other min and max doesn't seem to do anything...

    ReplyDelete
  4. I figure it out. For anyone who try this code, it's a little bit misleading as you convert the image to HSV encoding but you still use RGB notation for your variable. So to better understand all of this, just replace the trackbar names with HSV min and HSV max. Also, if you want something simpler, just use the Hue channel with only 2 trackbar. Like this:

    Code in C#:

    IplImage imageTemporaire = cvlib.cvCreateImage(cvlib.cvGetSize(_image.IplImage), 8, 3);
    cvlib.cvCvtColor(_image.IplImage, imageTemporaire, cvlib.CV_BGR2HSV);
    IplImage imageH = cvlib.cvCreateImage(cvlib.cvGetSize(imageTemporaire), 8, 1);

    cvlib.cvSplit(imageTemporaire, imageH, null, null, null);
    IplImage imageVerte = cvlib.cvCreateImage(cvlib.cvGetSize(_image.IplImage), 8, 1);

    //On sort ce qui est dans le range
    CvScalar min_color = cvlib.CV_RGB(0, 0, Hmin);
    CvScalar max_color = cvlib.CV_RGB(0, 0, Hmax);

    cvlib.cvInRangeS(imageH, min_color, max_color, imageVerte);
    cvlib.cvShowImage("Vert", imageVerte);
    cvlib.cvReleaseImage(imageTemporaire);
    cvlib.cvReleaseImage(imageVerte);

    ReplyDelete