My third day of OpenCV

    今日依旧偷懒,只看了几页纸照着书做了两个例程。思路依旧简单:

    (一)显示结果图像代码写在主函数中:(如2.)

   (a)在主函数中创建IplImage格式变量,利用cvLoadImage加载图像

   (b)在主函数中调用所写的图像处理函数

          功能函数注意选择需要传递的参数。

          功能函数中一般先确保该函数需要满足的条件(某些操作或者待用的OpenCV函数需要图像满足一定条件,如Canny

          算子需要灰度图才行),然后定义IplImage格式的输出变量,调用OpenCV函数执行功能(一般为xxx(in,

          out))

   (c)在主函数中cvNamedWindow创建窗口,cvShowImage显示图像,cvWaitKey( 0 )按键等待,cvReleaseImage 

          释放资源,cvDestroyWindow销毁窗口

   (二)显示结果图像代码写在功能函数中:(如1.)

   (a)在主函数中创建IplImage格式变量,利用cvLoadImage加载图像

   (b)在主函数中调用所写的图像处理函数

   (c)在功能函数中cvNamedWindow创建窗口,cvShowImage显示图像,cvWaitKey( 0 )按键等待,cvReleaseImage 

          释放资源,cvDestroyWindow销毁窗口

两种方法没有本质区别,博主倾向方法(一)

1.图像高斯平滑

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

void example2_4( IplImage* image )
{
// Create some windows to show the input
// and output images in.
//
cvNamedWindow( "Example4-in" ,0);
cvNamedWindow( "Example4-out",0 );
// Create a window to show our input image
//
cvShowImage( "Example4-in", image );
// Create an image to hold the smoothed output
//
IplImage* out = cvCreateImage(cvGetSize(image),IPL_DEPTH_8U,);
// Do the smoothing
//
cvSmooth( image, out, CV_GAUSSIAN, 3, 3 );
// Show the smoothed image in the output window
//
cvShowImage( "Example4-out", out );
// Be tidy
//
cvReleaseImage( &out );
// Wait for the user to hit a key, then clean up the windows
//
cvWaitKey( 0 );
cvDestroyWindow( "Example4-in" );
cvDestroyWindow( "Example4-out" );
}
void main()
{
IplImage* img = cvLoadImage( "1.jpg", CV_LOAD_IMAGE_COLOR);
example2_4( img );
}

2.Canny平滑与图像缩小

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

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 );
};


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

   IplImage* out = cvCreateImage(cvGetSize( in ) ,IPL_DEPTH_8U,1);

      cvCanny( in, out, lowThresh, highThresh, aperture );
     return( out );
};

void main()
{
cvNamedWindow( "in" ,0);
cvNamedWindow( "img1" ,0);
cvNamedWindow( "img2" ,0);
cvNamedWindow( "img3" ,0);
IplImage* in = cvLoadImage( "1.jpg", CV_LOAD_IMAGE_COLOR);
IplImage* img1 = doPyrDown( in, IPL_GAUSSIAN_5x5 );
IplImage* img2 = doPyrDown( img1, IPL_GAUSSIAN_5x5 );
IplImage* img3 = doCanny( img2, 10, 100, 3 );
cvShowImage( "in", in );
cvShowImage( "img1", img1 );
cvShowImage( "img2", img2 );
cvShowImage( "img3", img3 );
cvWaitKey( 0 );
cvReleaseImage( &img1 );
cvReleaseImage( &img2 );
cvReleaseImage( &img3 );
cvDestroyWindow( "in" );
cvDestroyWindow( "img1" );
cvDestroyWindow( "img2" );
cvDestroyWindow( "img3" );
}

posted @ 2013-01-27 23:26  傻呵呵的日子  阅读(240)  评论(0编辑  收藏  举报