Monday, June 21, 2010

Open CV Tutorial - Advanced operations on images ' Filter color in Image'

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.

/******************** 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;i100 && 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 :- 


Sunday, June 20, 2010

Open CV Tutorial - Basic operations for images Rotate and Scale

In this tutorials of OpenCV we will do some basic but interesting operartion on images. We rotate the image and scale it down.

For this we will use a matrix and rotate it. 

/************************************************/


#include "cv.h"
#include "highgui.h"
#include "math.h"
int main()
{
IplImage* src;
IplImage* dst;
int delta;
int angle;
src = cvLoadImage("apple.bmp", 1);
dst = cvCloneImage( src );
delta = 1; angle = 0;
cvNamedWindow( "src", 1 );
cvShowImage( "src", src );
for(;;)
{
float m[6];
double factor = (cos(angle*CV_PI/180.) + 1.1)*3;
CvMat M = cvMat( 2, 3, CV_32F, m );
int w = src->width;
int h = src->height;
m[0] = (float)(factor*cos(-angle*2*CV_PI/180.));
m[1] = (float)(factor*sin(-angle*2*CV_PI/180.));
m[2] = w*0.5f;
m[3] = -m[1];
m[4] = m[0];
m[5] = h*0.5f;
cvGetQuadrangleSubPix( src, dst, &M, 1, cvScalarAll(0));
cvNamedWindow( "dst", 1 ); cvShowImage( "dst", dst );
if( cvWaitKey(5) == 27 )
break;
angle = (angle + delta) % 360;
}
return 0;
}

OUTPUT:- 

ALSO READ :- 


OPENCV Tutorial - Basic operations for images Dilate/Erode

Let us do some more image processing in openCV. In this turorial we will do Dilate/Erode operation. This is a basic Morphological Operations.  To learn more about  Morphological Operations I have found a very good tutorial. You can find it here

Now we will use function Erode ( - Erodes image by using arbitrary structuring element and) Dilate -(Dilates image by using arbitrary structuring element).

lets learn a bit about functions -


void cvErode( const CvArr* src, CvArr* dst, IplConvKernel* element=NULL, int iterations=1 );

src
Source image.
dst
Destination image.
element
Structuring element used for erosion. If it is NULL, a 3×3 rectangular structuring element is used.
iterations
Number of times erosion is applied.
The function cvErode erodes the source image using the specified structuring element that determines the shape of a pixel neighborhood over which the minimum is taken:



void cvDilate( const CvArr* src, CvArr* dst, IplConvKernel* element=NULL, int iterations=1 );

src
Source image.
dst
Destination image.
element
Structuring element used for erosion. If it is NULL, a 3×3 rectangular structuring element is used.
iterations
Number of times erosion is applied.
The function cvDilate dilates the source image using the specified structuring element that determines the shape of a pixel neighborhood over which the maximum is taken:

NOTE:- Both functions supports the in-place mode. Erosion can be applied several (iterations) times. In case of color image each channel is processed independently.


After enough learning lets write a program :-


#include "cxcore.h"
#include "highgui.h"
int main()
{
            IplImage* newImg = NULL;
IplImage* dilateImg = NULL;
IplImage* erodeImg = NULL;

cvNamedWindow("src", 1);
cvNamedWindow("dilate",1);
cvNamedWindow("erode",1);

//load original image
newImg = cvLoadImage("apple.bmp",1);
cvShowImage( "src", newImg );

//make a copy of the original image
dilateImg=cvCloneImage( newImg );
erodeImg=cvCloneImage( newImg );

//dilate image
cvDilate(newImg,dilateImg,NULL,4);

//erode image
cvErode(newImg,erodeImg,NULL,4);
cvShowImage( "dilate", dilateImg );
cvShowImage( "erode", erodeImg );
cvWaitKey(0);
cvDestroyWindow( "src" ); cvDestroyWindow( "dilate" ); cvDestroyWindow( "erode" );
cvReleaseImage( &newImg ); cvReleaseImage( &dilateImg ); cvReleaseImage( &erodeImg );
return 0;

}



Open CV Tutorial - Basic operations for images ' Canny edge detection'

 In the last tutorial I have discussed how to setup workspace for different IDEs and basic tutorials about Opencv. Now let us make some more good programs. Lets us make a program for canny edge detection. Canny edge is one of the most widely used edge detection programs used along with other edge detection like Sobel. Read more about Canny algorithm here.


In OpenCV we get a very good implementation of Canny algorithm. The function cvCanny finds the edges on the input image image and marks them in the output image edges using the Canny algorithm. The smallest of threshold1 and threshold2 is used for edge linking, the largest - to find initial segments of strong edges.A brief intro of function is as:-


void cvCanny( const CvArr* image, CvArr* edges, double threshold1,
              double threshold2, int aperture_size=3 );


Explanation of function:-

image

Input image.

edges

Image to store the edges found by the function.

threshold1

The first threshold.

threshold2

The second threshold.

aperture_size ( a bit complicated :( but try to grasp it or use default-3 in most cases)

    

In all cases except 1, aperture_size ×aperture_size separable kernel
    will be used to calculate
    the derivative. For aperture_size=1 3x1 or 1x3 kernel is
    used (Gaussian smoothing is not done).
    There is also special value CV_SCHARR (=-1) that
    corresponds to 3x3 Scharr filter that may
    give more accurate results than 3x3 Sobel. Scharr aperture is:
| -3 0  3|
|-10 0 10|
| -3 0  3|
for x-derivative or transposed for y-derivative.
Now we will write a sample program :-


#include "cv.h"
#include "highgui.h"
int main()
{
IplImage* newImg; // original image
IplImage* grayImg; // gray image for the conversion of the original image
IplImage* cannyImg; // gray image for the canny edge detection
//load original image
newImg = cvLoadImage("apple.bmp",1);
//create a single channel 1 byte image (i.e. gray-level image)
grayImg = cvCreateImage( cvSize(newImg->width, newImg->height), IPL_DEPTH_8U, 1 );
//convert original color image (3 channel rgb color image) to gray-level image
cvCvtColor( newImg, grayImg, CV_BGR2GRAY );
cannyImg = cvCreateImage(cvGetSize(newImg), IPL_DEPTH_8U, 1);
// canny edge detection
cvCanny(grayImg, cannyImg, 50, 150, 3);
cvNamedWindow("src", 1);
cvNamedWindow("canny",1);
cvShowImage( "src", newImg );
cvShowImage( "canny", cannyImg );
cvWaitKey(0);
cvDestroyWindow( "src" );
cvDestroyWindow( "canny" );
cvReleaseImage( &newImg );
cvReleaseImage( &grayImg );
cvReleaseImage( &cannyImg );
return 0;
}


OUTPUT :- 

Also Read :- 

Open CV Tutorial and Introduction

Introduction: -
For past few years I am using OpenCV for my image processing library. Initially I stared with Matlab. But the
as usual the programing in Matlab was easy but the execution was really slow. For my real time image processing project I stared to search for a good Image processing library and found OpenCv. It was not only fast but I can built exe files unlike m files in Matlab. I this tutorial along with a brief intorduction, we will a very simple OpenCV program.

You can download OpenCv library form :- http://sourceforge.net/projects/opencvlibrary/ Latest version is 2.1. You will also have to setup the workspace. You can find it  here. For DevC++ user the setup part is here.

Lets see some important features of OpenCV:-

  • Open source computer vision library in C/C++.
  • Optimized and intended for real-time applications.
  • OS/hardware/window-manager independent.
  • Generic image/video loading, saving, and acquisition.
  • Both low and high level API.
  • Provides interface to Intel's Integrated Performance
  • Primitives (IPP) with processor specific optimization (Intel processors).


OpenCV modules
There are mainly 4 modules in OpenCV. These are

  • cv - Main OpenCV functions.
  • cvaux - Auxiliary (experimental) OpenCV functions.
  • cxcore - Data structures and linear algebra support.
  • highgui - GUI functions.



Image data structure in OpenCV and a Sample Program:-
Now we have done enough reading, Lets try our hands at some programing. In this simple program we will load a image file and show in window. I assume you have sample image :- 
sample.bmp. 


#include "cv.h" //main OpenCV functions
#include "highgui.h" //OpenCV GUI functions¯include <stdio.h> so no need to re include it.
int main()
{
       /* declare a new IplImage pointer, the basic
          image data structure in OpenCV */
          IplImage* newImg;
        /* load an image named "sample.bmp", 1 means
           this is a color image */
            newImg = cvLoadImage("sample.bmp",1);
         //create a new window
             cvNamedWindow("Window", 1);
          //display the image in the window
             cvShowImage("Window", newImg);
             //wait for key to close the window
            cvWaitKey(0);
            cvDestroyWindow( "Window" ); //destroy the window
            cvReleaseImage( &newImg ); //release the memory for the image
    return 0;
}