1)載入一副圖像并進(jìn)行平滑處理
#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()創(chuàng)建一副寬度和高度為輸入圖像一半尺寸的圖像
#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)讀取一個視頻文件,將每一幀圖像轉(zhuǎn)換為對數(shù)極坐標(biāo)格式(就像你的眼鏡真正看到的),最后將轉(zhuǎn)換后的圖像序列寫入新的視頻文件中
#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 "http://" 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 );
|