總結系列_1(opencv需常用的小工程,續...)


  本文中將列出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 camera
21     if(!cap.isOpened()) // check if we succeeded
22         return -1;
23     Mat edges;
24     namedWindow("edges",1);
25     for(;;)
26     {
27         Mat frame;
28         cap >> frame; // get a new frame from camera
29         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 destructor
36     return 0;
37 }

 

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM