初次接觸OpenCV
在用OpenCV做一些東西之前,要先配置一下開發(fā)環(huán)境,由于我的常使用的是Qt ,就以Qt作為例子。
這是我的pro配置:
#------------------------------------------------- # #?Project?created?by?QtCreator?2016-04-09T11:40:17 # #------------------------------------------------- QT???????+=?core QT???????-=?gui TARGET?=?Example4 CONFIG???+=?console CONFIG???-=?app_bundle TEMPLATE?=?app INCLUDEPATH?+=?-L?D:OpenSourceopencvbuildinclude? ???????????????-L?D:OpenSourceopencvbuildincludeopencv? ???????????????-L?D:OpenSourceopencvbuildincludeopencv2 LIBS?+=?D:OpenSourceopencvbuildx86vc12libopencv_core2410.lib LIBS?+=?D:OpenSourceopencvbuildx86vc12libopencv_highgui2410.lib LIBS?+=?D:OpenSourceopencvbuildx86vc12libopencv_imgproc2410.lib LIBS?+=?D:OpenSourceopencvbuildx86vc12libopencv_photo2410.lib SOURCES?+=?main.cpp
INCLUDEPATH 中是必要的頭文件包含,使用的是整體文件夾包含,LIBS是需要的LIB,使用的具體路徑。
下面程序演示了對圖片的一些簡單處理,包括放大,縮小,平滑,和邊緣檢測并輸出一個單通道的圖像
#include#include?"iostream"
#include?"cxcore.h"
#include?"cv.h"
#include?"highgui.h"
//對圖片進(jìn)行縮操作
IplImage*?doPyrDown(IplImage*?in,?int?filter?=?IPL_GAUSSIAN_5x5)
{
????IplImage*?out?=?cvCreateImage(cvSize(in->width/2,in->height/2),
??????????????????????????????????in->depth,
??????????????????????????????????in->nChannels);
????cvPyrDown(in,out);
????return?out;
}
//對圖片進(jìn)行放操作
IplImage*?doPyrUp(IplImage*?in,?int?filter?=?IPL_GAUSSIAN_5x5)
{
????IplImage*?out?=?cvCreateImage(cvSize(in->width?*?2,in->height?*?2),
??????????????????????????????????in->depth,
??????????????????????????????????in->nChannels);
????cvPyrUp(in,out);
????return?out;
}
//進(jìn)行邊緣檢測輸出一個單通道圖像(灰色)
IplImage*?doCanny(IplImage*?in,double?lowThresh,double?highThresh,double?aperture)
{
????if(in->nChannels?!=?1)
????????return?0;
????IplImage*?out?=?cvCreateImage(cvGetSize(in),IPL_DEPTH_8U,1);
????cvCanny(in,out,lowThresh,highThresh,aperture);
????return?out;
}
int?main(int?argc,?char**?argv)
{
????QCoreApplication?a(argc,?argv);
????//Create?four?windows?to?show?our?input?image?and?output?iamge
????cvNamedWindow("Example4_in",CV_WINDOW_AUTOSIZE);
????cvNamedWindow("Example4_out1",CV_WINDOW_AUTOSIZE);
????cvNamedWindow("Example4_out2",CV_WINDOW_AUTOSIZE);
????cvNamedWindow("Example4_out3",CV_WINDOW_AUTOSIZE);
????cvNamedWindow("Example4_out4",CV_WINDOW_AUTOSIZE);
????//show?the?input?image
????IplImage*?inImage?=?cvLoadImage("C:/Users/Administrator/Documents/Example4/debug/test.png");
????cvShowImage("Example4_in",inImage);
????//do?the?pyrdown?and?show?the?image
????IplImage*?outImage1?=?doPyrDown(inImage);
????cvShowImage("Example4_out1",outImage1);
????//do?the?pyrup?and?show?the?image
????IplImage*?outImage2?=?doPyrUp(inImage);
????cvShowImage("Example4_out2",outImage2);
????//do?the?smoothing?and?show?the?smoothed?image
????IplImage*?outImage3?=?cvCreateImage(cvGetSize(inImage),IPL_DEPTH_8U,3);
????cvSmooth(inImage,outImage3,CV_GAUSSIAN,3,3);
????cvShowImage("Example4_out3",outImage3);
????//do?the?canny
????IplImage*?outImage4?=?doCanny(inImage,10,100,3);
????cvShowImage("Example4_out4",outImage4);
????//Be?tidy
????cvWaitKey(0);
????cvReleaseImage(&outImage1);
????cvReleaseImage(&outImage2);
????cvReleaseImage(&outImage3);
????cvReleaseImage(&outImage4);
????cvDestroyWindow("Example4_in");
????cvDestroyWindow("Example4_out1");
????cvDestroyWindow("Example4_out2");
????cvDestroyWindow("Example4_out3");
????cvDestroyWindow("Example4_out4");
????return?a.exec();
}下面程序演示了對于視頻的打開,并復(fù)制
#include#include?"iostream"
//OpenCV
#include"cxcore.h"
#include?"cv.h"
#include?"highgui.h"
/****************************************************************
??首先打開一個視頻文件,讀取文件內(nèi)容,將每一幀圖像轉(zhuǎn)化為對數(shù)
??極坐標(biāo)格式(就像你的眼睛真正能看到的),最后將轉(zhuǎn)化后的圖像
??序列寫入新的視頻文件中
*****************************************************************/
int?main(int?argc,?char?*argv[])
{
????QCoreApplication?a(argc,?argv);
????CvCapture*?capture?=?0;
????capture?=?cvCreateFileCapture(argv[1]);//input?video
????if(!capture)
????{
????????return?-1;
????}
????IplImage*?bgr_frame?=?cvQueryFrame(capture);//init?the?video?read
????double?fps?=?cvGetCaptureProperty(capture,
??????????????????????????????????????CV_CAP_PROP_FPS);
????CvSize?size?=?cvSize(
????????????????(int)cvGetCaptureProperty(capture,CV_CAP_PROP_FRAME_WIDTH),
????????????????(int)cvGetCaptureProperty(capture,CV_CAP_PROP_FRAME_HEIGHT));
????CvVideoWriter*?writer?=?cvCreateVideoWriter(argv[2],
????????????????????????????????????????????????CV_FOURCC('M','J','P','G'),
????????????????????????????????????????????????fps,
????????????????????????????????????????????????size);
?????IplImage*?logpolar_frame?=?cvCreateImage(
?????????????????size,
?????????????????IPL_DEPTH_8U,
?????????????????3);
/*******************************************************************?
?????void?cvLogPolar(?const?CvArr*?src,?
??????????????????????CvArr*?dst,?
??????????????????????CvPoint2D32f?center,?
??????????????????????double?M,?
??????????????????????int?flags=CV_INTER_LINEAR+CV_WARP_FILL_OUTLIERS?);
src?輸入圖像。?dst?輸出圖像。?center?變換的中心,輸出圖像在這里最精確。?M?幅度的尺度參數(shù),見下面公式。?flags?插值方法和以下選擇標(biāo)志的結(jié)合
CV_WARP_FILL_OUTLIERS?-填充輸出圖像所有像素,如果這些點(diǎn)有和外點(diǎn)對應(yīng)的,則置零。
CV_WARP_INVERSE_MAP?-?表示矩陣由輸出圖像到輸入圖像的逆變換,并且因此可以直接用于像素插值。否則,函數(shù)從map_matrix中尋找逆變換。
fillval?用于填充外點(diǎn)的值。
此函數(shù)模仿人類視網(wǎng)膜中央凹視力,并且對于目標(biāo)跟蹤等可用于快速尺度和旋轉(zhuǎn)變換不變模板匹配。
************************************************************************/
??????????????????????
?????while(?bgr_frame?!=?NULL)
?????{
?????????cvLogPolar(bgr_frame,logpolar_frame,
????????????????????cvPoint2D32f(bgr_frame->width/2,
????????????????????????????????bgr_frame->height/2),
????????????????????40,
????????????????????CV_INTER_LINEAR+CV_WARP_FILL_OUTLIERS);
?????????cvWriteFrame(writer,logpolar_frame);
?????}
?????cvReleaseVideoWriter(&writer);
?????cvReleaseImage(&logpolar_frame);
?????cvReleaseCapture(&capture);
????return?a.exec();
}在工程構(gòu)建之后,要想運(yùn)行成功,必須把DLL拷貝到debug中去。





