OpenCV学习之2

来源:互联网 发布:软件开发有哪些 编辑:程序博客网 时间:2024/05/21 06:36


初次接触OpenCV


在用OpenCV做一些东西之前,要先配置一下开发环境,由于我的常使用的是Qt ,就以Qt作为例子。


这是我的pro配置:


#-------------------------------------------------## Project created by QtCreator 2016-04-09T11:40:17##-------------------------------------------------QT       += coreQT       -= guiTARGET = Example4CONFIG   += consoleCONFIG   -= app_bundleTEMPLATE = appINCLUDEPATH += -L D:\OpenSource\opencv\build\include \               -L D:\OpenSource\opencv\build\include\opencv \               -L D:\OpenSource\opencv\build\include\opencv2LIBS += D:\OpenSource\opencv\build\x86\vc12\lib\opencv_core2410.libLIBS += D:\OpenSource\opencv\build\x86\vc12\lib\opencv_highgui2410.libLIBS += D:\OpenSource\opencv\build\x86\vc12\lib\opencv_imgproc2410.libLIBS += D:\OpenSource\opencv\build\x86\vc12\lib\opencv_photo2410.libSOURCES += main.cpp


INCLUDEPATH 中是必要的头文件包含,使用的是整体文件夹包含,LIBS是需要的LIB,使用的具体路径。


下面程序演示了对图片的一些简单处理,包括放大,缩小,平滑,和边缘检测并输出一个单通道的图像

#include <QCoreApplication>#include "iostream"#include "cxcore.h"#include "cv.h"#include "highgui.h"//对图片进行缩操作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;}//对图片进行放操作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;}//进行边缘检测输出一个单通道图像(灰色)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();}


下面程序演示了对于视频的打开,并复制


#include <QCoreApplication>#include "iostream"//OpenCV#include"cxcore.h"#include "cv.h"#include "highgui.h"/****************************************************************  首先打开一个视频文件,读取文件内容,将每一帧图像转化为对数  极坐标格式(就像你的眼睛真正能看到的),最后将转化后的图像  序列写入新的视频文件中*****************************************************************/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 幅度的尺度参数,见下面公式。 flags 插值方法和以下选择标志的结合CV_WARP_FILL_OUTLIERS -填充输出图像所有像素,如果这些点有和外点对应的,则置零。CV_WARP_INVERSE_MAP - 表示矩阵由输出图像到输入图像的逆变换,并且因此可以直接用于像素插值。否则,函数从map_matrix中寻找逆变换。fillval 用于填充外点的值。此函数模仿人类视网膜中央凹视力,并且对于目标跟踪等可用于快速尺度和旋转变换不变模板匹配。************************************************************************/                           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();}



在工程构建之后,要想运行成功,必须把DLL拷贝到debug中去。



0 0
原创粉丝点击