分享

简单的图像变换

 absolutely113 2013-07-19
1)载入一副图像并进行平滑处理

#include "stdafx.h"
#include "highgui.h"
#include "cv.h"
#include "cxcore.h"

int main(int argc,char *argv[])
{
      IplImage* src = cvLoadImage( "D:\\Demo.jpg");
      cvNamedWindow( "show_image");
      cvNamedWindow( "show_out");

      cvShowImage( "show_image",src);
      IplImage* out=cvCreateImage(cvGetSize(src),IPL_DEPTH_8U,3);
      cvSmooth(src,out,CV_GAUSSIAN,3,3);
      cvShowImage( "show_out",out);
                 
      cvWaitKey(0);
      cvReleaseImage(&src);
      cvReleaseImage(&out);
      cvDestroyWindow( "show_out");
      cvDestroyWindow( "show_image");
      return 0;

}

(2)使用cvPyrDown()创建一副宽度和高度为输入图像一半尺寸的图像

#include "stdafx.h"
#include "highgui.h"
#include "cv.h"
#include "cxcore.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 );
};


int main(int argc,char *argv[])
{
      IplImage* src = cvLoadImage( "D:\\Demo.jpg");
                  IplImage* out = cvCreateImage( cvSize( src->width/2,src->height/2 ), src->depth, src->nChannels);
    
                  cvNamedWindow( "show_image");
                  cvNamedWindow( "show_out");

      cvShowImage( "show_image",src);
                 
                  out = doPyrDown( src );
                  cvShowImage( "show_out",out);
                 
      cvWaitKey(0);
      cvReleaseImage(&src);
                  cvReleaseImage(&out);
                  cvDestroyWindow( "show_out");
      cvDestroyWindow( "show_image");
      return 0;

}

(3)Canny边缘检测将输出写入一个单通道(灰度级)图像


#include "stdafx.h"
#include "highgui.h"
#include "cv.h"
#include "cxcore.h"

IplImage* doCanny(
    IplImage* in,
    double    lowThresh,
    double    highThresh,
    double    aperture)
{
    if (in->nChannels != 1)
        return(0); // Canny only handles gray scale images
    IplImage* out = cvCreateImage(
        cvGetSize( in ),
        in->depth, //IPL_DEPTH_8U,   
        1);
    cvCanny( in, out, lowThresh, highThresh, aperture );
    return( out );
};



int main(int argc,char *argv[])
{
      IplImage* src = cvLoadImage( "D:\\Demo.jpg");
                  IplImage* img_gry = cvCreateImage( cvSize( src->width,src->height ), src->depth, 1);
                  cvCvtColor(src, img_gry ,CV_BGR2GRAY);

                  cvNamedWindow( "show_gray");
                  cvNamedWindow( "show_canny");

      cvShowImage( "show_gray",img_gry);
                 
                  IplImage* img_cny = doCanny( img_gry, 10, 100, 3 );
                 
                  cvShowImage( "show_canny",img_cny);
                 
      cvWaitKey(0);
      cvReleaseImage(&src);
                  cvReleaseImage(&img_gry);
                  cvReleaseImage(&img_cny);
                  cvDestroyWindow( "show_gray");
      cvDestroyWindow( "show_canny");
      return 0;

}

(4)读取一个视频文件,将每一帧图像转换为对数极坐标格式(就像你的眼镜真正看到的),最后将转换后的图像序列写入新的视频文件中


#include "stdafx.h"
#include "highgui.h"
#include "cv.h"
#include "cxcore.h"
#include <stdio.h>

// Convert a video to grayscale
// argv[1]: input video file
// argv[2]: name of new output file
//

//#define NOWRITE 1;   //Turn this on (removed the first comment out "//" if you can't write on linux

int main( int argc, char* argv[] ) {
                cvNamedWindow( "Example2_10", CV_WINDOW_AUTOSIZE );
                cvNamedWindow( "Log_Polar", CV_WINDOW_AUTOSIZE );
                CvCapture* capture = cvCreateFileCapture(  "D:\\sample.avi" );
                 if (!capture){
                                 return -1;
                }
                IplImage* bgr_frame;
                 double fps = cvGetCaptureProperty (
                                capture,
                                CV_CAP_PROP_FPS
                                );
                printf( "fps=%d\n",(int )fps);

                CvSize size = cvSize(
                                ( int)cvGetCaptureProperty( capture, CV_CAP_PROP_FRAME_WIDTH),
                                ( int)cvGetCaptureProperty( capture, CV_CAP_PROP_FRAME_HEIGHT)
                                );

                printf( "frame (w, h) = (%d, %d)\n" ,size.width,size.height);
#ifndef NOWRITE
                CvVideoWriter* writer = cvCreateVideoWriter(  // On linux Will only work if you've installed ffmpeg development files correctly,
                                 "D:\\sampleout.avi"                              // otherwise segmentation fault.  Windows probably better.
                                CV_FOURCC( 'M','J' ,'P', 'G'),   
                                fps,
                                size
                                );
#endif
                IplImage* logpolar_frame = cvCreateImage(
                                size,
                                IPL_DEPTH_8U,
                                3
                                );

                IplImage* gray_frame = cvCreateImage(
                                size,
                                IPL_DEPTH_8U,
                                1
                                );

                 while( (bgr_frame=cvQueryFrame(capture)) != NULL ) {
                                cvShowImage( "Example2_10", bgr_frame );
                                cvConvertImage(   //We never make use of this gray image
                                                bgr_frame,
                                                gray_frame,
                                                CV_RGB2GRAY
                                                );
                                cvLogPolar( bgr_frame, logpolar_frame,  //This is just a fun conversion the mimic's the human visual system
                                                cvPoint2D32f(bgr_frame->width/2,
                                                bgr_frame->height/2),
                                                40,
                                                CV_INTER_LINEAR+CV_WARP_FILL_OUTLIERS );
                                cvShowImage( "Log_Polar", logpolar_frame );
                                 //Sigh, on linux, depending on your ffmpeg, this often won't work ...
#ifndef NOWRITE
                                cvWriteToAVI( writer, logpolar_frame );
#endif
                                 char c = cvWaitKey(10);
                                 if( c == 27 ) break ;
                }
#ifndef NOWRITE
                cvReleaseVideoWriter( &writer );
#endif
                cvReleaseImage( &gray_frame );
                cvReleaseImage( &logpolar_frame );
                cvReleaseCapture( &capture );
}

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多