Yesterday I have written an article about how to use openCV to process the video, including opening, processing and closing. In the processing part we have seen that actually it is just a process of every frame, that is to say, in the final analysis it is about image processing. And here I'd like to introduce some interesting and common image processing applications by OpenCV. Today I just focus on three functions: change the color image into gray image, Gaussian Pyramid Filter and the canny edge detection. What I want to process is the image "lzsNew.jpg".

It is obvious that it is a color image with three color channels R, G and B. Here if we want to achieve the gray image, the mission is to convert the image from color space to a gray space by the function cvCvtColor() with the parameter CV_RGB2GRAY or CV_BGR2GRAY. To generate a canny image in openCV the function cvCanny() is available, which 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. To obtain a Gaussian Pyramid Filter cvPyrDown() and cvPyrUp() are workable. Here we choose the first one to decrease the image size as half. And the functions to load a image, release a image, create a window to show and destroy it, I have already introduced before. And below is the sample code:#include "cv.h"
#include "highgui.h"
IplImage* doCanny(
IplImage* in,
double lowThresh,
double highThresh,
double aperture)
{
IplImage* out = cvCreateImage(
cvGetSize( in ),
in->depth, //IPL_DEPTH_8U,
1);
cvCanny( in, out, lowThresh, highThresh, aperture );
return( out );
};
IplImage* doPyrDown(
IplImage* in,
int filter = IPL_GAUSSIAN_5x5)
{
// Best to make sure input image is divisible by two.
//
assert( in->width%2 == 0 && in->height%2 == 0 );
IplImage* out = cvCreateImage(
cvSize( in->width/2, in->height/2 ),
in->depth,
in->nChannels
);
cvPyrDown( in, out );
return( out );
};
int main( int argc, char** argv )
{
IplImage* img_rgb = cvLoadImage( "lzsNew.jpg" );
IplImage* img_gry = cvCreateImage( cvSize( img_rgb->width,img_rgb->height ),
img_rgb->depth, 1);
cvCvtColor(img_rgb, img_gry ,CV_BGR2GRAY);
IplImage* img_pyr = doPyrDown( img_gry, IPL_GAUSSIAN_5x5 );
IplImage* img_cny = doCanny( img_pyr, 10, 100, 3 );
cvNamedWindow("Example Gray", CV_WINDOW_AUTOSIZE );
cvNamedWindow("Example Pyr", CV_WINDOW_AUTOSIZE );
cvNamedWindow("Example Canny", CV_WINDOW_AUTOSIZE );
cvShowImage("Example Gray", img_gry );
cvShowImage("Example Pyr", img_pyr );
cvShowImage("Example Canny", img_cny );
cvWaitKey(0);
cvSaveImage("lzsGray.jpg",img_gry);
cvSaveImage("lzsGrayDown.jpg",img_pyr);
cvSaveImage("lzsCanny.jpg",img_cny);
cvReleaseImage( &img_rgb);
cvReleaseImage( &img_gry);
cvReleaseImage( &img_pyr);
cvReleaseImage( &img_cny);
cvDestroyWindow("Example Gray");
cvDestroyWindow("Example Pyr");
cvDestroyWindow("Example Canny");
}
And after the program compilation we are able to get then three images below.

Gray Image
Gray Down
Canny Detection
With the same theory which is mentioned at the beginning that video is composed of several frames, so the video is also able to process by a Gaussian Pyramid Filter or canny detection. And yesterday I have posted a sample video, this one is the canny detection effect of that video.
Here is the code of today's image processing.
ontheweg


Posted in: 

1 comments:
do u have a sample progrm to perform gaussian filter and we must be able to specify the sigma(standard deviation) of the gaussian filter and generate the gaussian filter coefficient??
Post a Comment