In this tutorial we will learn to filter images, and separate the color. For this I am using Opencv library. In my earlier posts I have discussed how to link opencv in devC++ (here) and in Visual Studio (here). Here I suppose that you know the basic of image processing and programing using opencv.
Here I have given a very basic algo to filter color which will not work with real life images, but will give a insight how to loop through each and every pixel of a image. In this code we will loop through each pixel, find the value of each component ( RGB) and for a specific condition we will determine that pixel is of which color. Here I have Used two functions
CvScalar cvGet2D( const CvArr* arr, int idx0, int idx1 )
->
void cvSet2D( CvArr* arr, int idx0, int idx1, CvScalar value );
-> Note that cvSet*D function can be used safely for both single-channel and multiple-channel arrays though they are a bit slower.
Here I have given a very basic algo to filter color which will not work with real life images, but will give a insight how to loop through each and every pixel of a image. In this code we will loop through each pixel, find the value of each component ( RGB) and for a specific condition we will determine that pixel is of which color. Here I have Used two functions
CvScalar cvGet2D( const CvArr* arr, int idx0, int idx1 )
->
void cvSet2D( CvArr* arr, int idx0, int idx1, CvScalar value );
-> Note that cvSet*D function can be used safely for both single-channel and multiple-channel arrays though they are a bit slower.
/******************** CODE TO FILTER COLORS ****************/ #include#include #include #include int main() { // Decleare image variables IplImage* img=0; // original image IplImage* white; IplImage* blue; IplImage* green; IplImage* red; //load original image img = cvLoadImage("test.jpg",-1); if(img==0) { printf(" ERORR IN LOADING IMAGE !!"); exit(-1); } // get height and width for image int height=img->height; int width=img->width; // create four images white=cvCreateImage(cvSize(img->width,img->height),8,1); blue=cvCreateImage(cvSize(img->width,img->height),8,3); green=cvCreateImage(cvSize(img->width,img->height),8,3); red=cvCreateImage(cvSize(img->width,img->height),8,3); // Two scalar variable to store a pixle data. // wt -> white CvScalar s,wt; wt.val[0]=255; // A very simple filter operation // This loops through each and every pixle of image and filter accordingly for(int i=0;i 100 && s.val[1]>100 && s.val[2]>100) // filter white cvSet2D(white,i,j,wt); else if(s.val[0]>100 && s.val[1]<100 && s.val[2]<100) // filter blue cvSet2D(blue,i,j,s); else if(s.val[0]<100 && s.val[1]>100 && s.val[2]<100) // filter green cvSet2D(green,i,j,s); else if(s.val[0]<100 && s.val[1]<100 && s.val[2]>100) // filter red cvSet2D(red,i,j,s); } // Create windows and show them cvNamedWindow("img", 1); cvShowImage( "img", img ); cvNamedWindow("white", 1); cvShowImage( "white", white ); cvNamedWindow("blue", 1); cvShowImage( "blue",blue); cvNamedWindow("green", 1); cvShowImage( "green",green); cvNamedWindow("red", 1); cvShowImage( "red",red); // Wait for any to be pressed cvWaitKey(0); // Detroy all created windows cvDestroyAllWindows(); // Release the memory occupied by images cvReleaseImage( &img ); cvReleaseImage( &white); cvReleaseImage( &blue); cvReleaseImage( &green); cvReleaseImage( &red); return 0; }
OUTPUT:-
test.jpg :- red image:- blue image :-
Green Image White Image
ALSO READ :-
- Open CV Tutorial and Introduction
- Linking OpenCV library in DEVC++
- OPENCV Tutorial - Basic operations for images Dilate/Erode
Open CV Tutorial - Basic operations for images ' Canny edge detection'