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

      作者:tornadomeet 出处:http://www.cnblogs.com/tornadomeet 

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

  工程环境为vs2010+opencv2.3.1                                     

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

  代码为:

#include "stdafx.h"
#include <opencv2/highgui/highgui.hpp>
 
using namespace cv;
 
 int main(int argc,unsigned char* argv[])
{
    Mat img_src;
     for (;;)
    {
        img_src=imread("lena.jpg");
        imshow("lena_show",img_src);
         waitKey(30);
     }
     return 0;
}

二、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 }











  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值