home | non-tech | cs | ee | about
Abstract New

OpenCV Tutorial - Getting started with OpenCV - Part 2

Click here for part one of the series.
In this article we shall see how to load an image, convert it from one color space to another, perform edge detection and save the resulting image.

Loading, Processing and Saving an Image

Include the header files mentioned below.
#include "cxtypes.h"
#include "cv.h"
#include "highgui.h"
#include "cvaux.h"
#include "cxmisc.h"

Load the image data

Inside the main program:
//Declare and load the image
//Let us assume you have a image called test1.jpg -
//the one on which you want to perform edge detection
IplImage *originalImage  = cvLoadImage("test.jpg",-1);
Function CvLoadImage - Used to load an image data into an IplImage array cvLoadImage(const char* fileName, int iscolor) where, file name - Name of the image to load iscolor - number of channels of the image If iscolor > 0 the image loaded will always have 3 channels If iscolor < iscolor =" 0"> Channel : A channel is the number of components used to specify a pixel value. For example, an RGB image has three channels. Each pixel's color is specified by it's R, G, B values.

Create an image

Create a grayscale image with the same dimensions as the original image.
//In this case the test.jpg is 320x240.
//Be sure to change it to the size of the image you loaded
IplImage *grayScaleImage = cvCreateImage(cvSize(320,240),8,1);
Function cvCreateImage - Used to create and allocate image data cvCreateImage(CvSize size, int bitDepth, int channels); where, CvSize is a OpenCV structure that contains a width and a height.
typedef struct CvSize
{
int width;
int height;
}
Size - size of the Image bitDepth - the bitDepth of the image - the number of bits used to store information about an image. For the bitDepth one can also use the OpenCV constants like IPL_DEPTH_8U, IPL_DEPTH_16S etc. channels - Number of channels in the image

Convert the original image into grayscale.

We will need to convert the original image into grayscale before performing edge detection.
cvCvtColor(originalImage, grayScaleImage, CV_BGR2GRAY);
Function cvCvtColor - Used to convert Images in one color space to another cvCvtColor(const CvArr* SourceImage, const cvArr* DestinationImage, int code) where, "code" - color conversion operation to be performed. There are numerous conversion operations listed in OpenCV. They include conversion to and from RGB, YCrCb, HSV and Bayer. CvArr - a basic structure in OpenCV. It is used only in functions to specify that the parameter accepts more than one kind of array (there are many types of arrays in OpenCV and IplImage is one such type).

Perform edge detection

Call the function to perform edge detection on the gray scale image.
//Declare an image for holding the results of the edge detection
IplImage * edgeImage; 
//Create Image
edgeImage = cvCreateImage(cvSize(320,240), 8, 1); 
//Perform edge detection using the canny edge detector
cvCanny(grayScaleImage, edgeImage, 0.5, 0.5, 3);
Function cvCanny - Used to perform edge detection with the Canny Operator cvCanny(const cvArr *Image1, const cvArr *Image2, double threshold1, double threshold2, int kernel_size) where, Image1 - Original Image on which edge detection is to be performed. Image2 - The results of the edge detected image. threshold1 and threshold2 - Thresholds for the canny detector. kernel_size - Size of the sobel kernel - Value of 3 gives a 3 X 3 kernel.

Save the image to disk


//If all goes well we need to save the results of the edge detection.
cvSaveImage("Resultant.jpg",edgeImage);
Function cvSaveImage - Used to save an IplImage as a Image cvSaveImage(const char* FileName, const CvArr* Image); where, FileName - Name of the save file Image - Image to be saved

Release memory for images that were created

//Finally release the memory for the images that were created
cvReleaseImage(&edgeImage);
cvReleaseImage(&grayScaleImage);
Function cvReleaseImage - Used to release the image data cvReleaseImage(IplImage **Image) where, Image - Image to be released

Our Final Code

#include "cxtypes.h"
#include "cv.h"
#include "highgui.h"
#include "cvaux.h"
#include "cxmisc.h"

void main() //your main program or function

{
        IplImage *originalImage  = cvLoadImage("test.jpg",-1);
        IplImage *grayScaleImage;
        grayScaleImage = cvCreateImage(cvSize(320,240),8,1);
        cvCvtColor(originalImage, grayScaleImage, CV_BGR2GRAY);
        IplImage * edgeImage; 
        edgeImage = cvCreateImage(cvSize(320,240), 8, 1); 
        cvCanny(grayScaleImage, edgeImage, 0.5, 0.5, 3);                             
        cvSaveImage("Resultant.jpg",edgeImage);
        cvReleaseImage(&edgeImage);
        cvReleaseImage(&grayScaleImage);
}

3 comments:

Anonymous said...

That is good for me.

Anonymous said...

Hikka 2012. wt d f_u_k?????? stupid .h libs. stupid opencv. fk.

Anonymous said...

I am quite surprised that you do not apply some Gaussian blur or Sobel before applying Canny. It's usually a good idea to reduce the noise (by bluring it) before detecting edges.

Post a Comment

© 2014 - 2015 abstract new. All rights reserved.