OpenCV实现好玩的实用的小程序

1、读取AVI文件并显示

#include 

using namespace cv;

int main()
{
    Mat img_src;
    VideoCapture vido_file("vido.avi");
    for (;;)
    {
        vido_file >>img_src;
        imshow("video_src",img_src);//可以事先不用新建一个窗口
        char c=(char)waitKey(47);
        if (c==27)
        {
            break;    
        }
    }
    return 0;
}

2、驱动摄像头

#include 

using namespace cv;

int main()
{
    Mat img_src;
    VideoCapture cam(0);
    for (;;)
    {
        cam >>img_src;
        imshow("camera",img_src);//可以事先不用新建一个窗口
        char c=(char)waitKey(30);
        if (c==27)
        {
            break;    
        }
    }
    return 0;
}

3、驱动摄像头并进行Canny边缘检测

#include 
#include 
#include 
#include 

using namespace cv;
using namespace std;

int main( int argc, const char **argv )
{

    VideoCapture cap(0); // open the default camera
    if(!cap.isOpened()) // check if we succeeded
        return -1;
    Mat edges;
    namedWindow("edges",1);
    for(;;)
    {
        Mat frame;
        cap >> frame; // get a new frame from camera
        cvtColor(frame, edges, CV_BGR2GRAY);
        GaussianBlur(edges, edges, Size(7,7), 1.5, 1.5);
        Canny(edges, edges, 0, 30, 3);
        imshow("edges", edges);
        if(waitKey(30) >= 0) break;
    }
    // the camera will be deinitialized automatically in VideoCapture destructor
    return 0;
}

环境:VS2012 + OpenCV2.4.4

参考:http://www.cnblogs.com/tornadomeet/archive/2012/04/23/2467153.html

你可能感兴趣的:(OpenCV)