总结系列_1(opencv需常用的小工程,续...)

来源:互联网 发布:python开发入门 编辑:程序博客网 时间:2024/06/10 23:58

  本文中将列出opencv需常用的最小工程,以方便今后做测试用。

  工程环境为vs2010+opencv2.3.1

一、opencv读取图片并显示出来:

  代码为:

复制代码
 1 #include "stdafx.h" 2 #include <opencv2/highgui/highgui.hpp> 3  4 using namespace cv; 5  6 int main(int argc,unsigned char* argv[]) 7 { 8     Mat img_src; 9     for (;;)10     {11         img_src=imread("lena.jpg");12         imshow("lena_show",img_src);13         waitKey(30);14     }15     return 0;16 }
复制代码

二、opencv读取avi文件并显示出来:

  注意有些avi格式的视频是读不出来的。

  代码为:

复制代码
 1 #include "stdafx.h" 2 #include <opencv2/highgui/highgui.hpp> 3  4 using namespace cv; 5  6 int main(int argc,unsigned char* argv[]) 7 { 8     Mat img_src; 9     VideoCapture vido_file("tree.avi");10     for (;;)11     {12         vido_file >>img_src;13         imshow("video_src",img_src);//可以事先不用新建一个窗口14         char c=(char)waitKey(47);15         if (c==27)16         {17             break;    18         }19     }20     return 0;21 }
复制代码


三、opencv驱动摄像头并显示出来:

  代码为:

复制代码
 1 #include "stdafx.h" 2 #include <opencv2/highgui/highgui.hpp> 3  4 using namespace cv; 5  6 int main(int argc,unsigned char* argv[]) 7 { 8     Mat img_src; 9     VideoCapture cam(0);10     for (;;)11     {12         cam >>img_src;13         imshow("camera",img_src);//可以事先不用新建一个窗口14         char c=(char)waitKey(30);15         if (c==27)16         {17             break;    18         }19     }20     return 0;21 }
复制代码

 

以下的环境改为:opencv2.4.2+vs2010

四、opencv打开摄像头并对摄像头内视频进行canny边缘检测。 

  代码为:

复制代码
 1 // cam_test.cpp : 定义控制台应用程序的入口点。 2 // 3  4 #include "stdafx.h" 5 #include <opencv2/core/core.hpp> 6 #include <opencv2/highgui/highgui.hpp> 7 #include <opencv2/imgproc/imgproc.hpp> 8 #include <iostream> 9 10 #pragma comment( lib, "opencv_core242.lib" )11 #pragma comment( lib, "opencv_highgui242.lib" )12 #pragma comment( lib, "opencv_imgproc242.lib" )13 14 using namespace cv;15 using namespace std;16 17 int main( int argc, const char **argv )18 {19 20     VideoCapture cap(0); // open the default camera21     if(!cap.isOpened()) // check if we succeeded22         return -1;23     Mat edges;24     namedWindow("edges",1);25     for(;;)26     {27         Mat frame;28         cap >> frame; // get a new frame from camera29         cvtColor(frame, edges, CV_BGR2GRAY);30         GaussianBlur(edges, edges, Size(7,7), 1.5, 1.5);31         Canny(edges, edges, 0, 30, 3);32         imshow("edges", edges);33         if(waitKey(30) >= 0) break;34     }35     // the camera will be deinitialized automatically in VideoCapture destructor36     return 0;37 }
原创粉丝点击