2009年1月5日 星期一

A Not-So-Simple Transformation











左為cvPyrDown()兩次的結果,
  右為cvCanny()一次的結果。

// Example 2-5. Using cvPyrDown() to create a new image that is half the width and height of the input image

// Example 2-6. The Canny edge detector writes its output to a single channel (grayscale) image
// Example 2-7. Combining the pyramid down operator (twice) and the Canny subroutine in a simple image pipeline
// Example 2-8. Simplifying the image pipeline of Example 2-7 by making the individual stages release their intermediate memory allocations

#include "highgui.h"
#include "cv.h"

IplImage *doPyrDown( IplImage * , int );
IplImage *doCanny( IplImage * , double , double , double );

int main( int argc , char **argv )
{
 cvNamedWindow( "Input" , CV_WINDOW_AUTOSIZE );
 cvNamedWindow( "Output1" , CV_WINDOW_AUTOSIZE );
 //cvNamedWindow( "Output2" , CV_WINDOW_AUTOSIZE );
 //cvNamedWindow( "Output3" , CV_WINDOW_AUTOSIZE );

 IplImage *image = cvLoadImage( argv[1] , 0);
 IplImage *out1 = doPyrDown( image , IPL_GAUSSIAN_5x5 );
 //IplImage *out2 = doPyrDown( out1 , IPL_GAUSSIAN_5x5 );
 //IplImage *out3 = doCanny( out2 , 10 , 100 , 3 );
 out1 = doPyrDown( out1 , IPL_GAUSSIAN_5x5 );
 cvShowImage( "Input" , out1 );
 out1 = doCanny( out1 , 10 , 100 , 3 );

 //cvShowImage( "Input" , image );
 cvShowImage( "Output1" , out1 );
 //cvShowImage( "Output2" , out2 );
 //cvShowImage( "Output3" , out3 );

 cvWaitKey( 0 );

 cvReleaseImage( &image );
 cvReleaseImage( &out1 );
 //cvReleaseImage( &out2 );
 //cvReleaseImage( &out3 );

 cvDestroyWindow( "Input" );
 cvDestroyWindow( "Output1" );
 //cvDestroyWindow( "Output2" );
 //cvDestroyWindow( "Output3" );
 return 0;
}

// Example 2-5.
IplImage *doPyrDown( IplImage *in , int filter )
{
 // 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 , filter );
 return out;
}

// Example 2-6.
IplImage *doCanny( IplImage *in , double lowThresh , double highThresh , double aperture )
{
 if( in->nChannels != 1 )
  return 0; // Canny only handles gray scale iamges

 IplImage *out = cvCreateImage( cvGetSize( in ) , IPL_DEPTH_8U , 1 );
 cvCanny( in , out , lowThresh , highThresh , aperture );
 return out;
}

沒有留言: