zoukankan      html  css  js  c++  java
  • 用OpenCV读取摄像头

     首先插入摄像头

    在电脑中查看

    #include <opencv2/highgui/highgui.hpp>
    #include <opencv2/imgproc/imgproc.hpp>
    #include <opencv2/core/core.hpp>
    
    using namespace cv;
    
    
    int main()
    {
    	VideoCapture cap(0);
    	if (!cap.isOpened())
    	{
    		return -1;
    	}
    	Mat frame;
    	Mat edges;
    
    	bool stop = false;
    	while (!stop)
    	{
    		cap >> frame;
    		cvtColor(frame, edges, CV_BGR2GRAY);
    		GaussianBlur(edges, edges, Size(7, 7), 1.5, 1.5);
    		Canny(edges, edges, 0, 30, 3);
    		imshow("当前视频", edges);
    		if (waitKey(30) != 255)
    			stop = true;
    	}
    	return 0;
    }
    

    最开始电脑配置的opencv2.x版本,上述代码打开摄像头没有问题;后来使用opencv3.x版本,重新编译了contrib包,发现摄像头打开后一闪而过。

    原因是 waitkey() 如果不按键的时候是返回 oxff,oxff无符号时是255,有符号时是-1。windows vs 的环境默认了这个为非符号数 即255,而opencv的有些配置环境中是-1。

    解决方案:把原始代码中循环读取帧的

    if (waitKey(20)>=0)  break;

    改为

    if (waitKey(20) != 255)  break;

    或者把waitkey的返回值用有符号数去读取。

    uchar c=waitkey(20);  if(c!=255)  break;

    #include <opencv2/highgui/highgui.hpp>
    #include <opencv2/imgproc/imgproc.hpp>
    #include <opencv2/core/core.hpp>
    
    
    int main(int argc, char** argv) {
    	cv::namedWindow("Example 2-10", cv::WINDOW_AUTOSIZE);
    	cv::VideoCapture cap;
    	cap.open(0); // open the first camera
    	if (!cap.isOpened()) { // check if we succeeded
    		return -1;
    	}
    	cv::Mat frame;
    	for (;;) {
    		cap >> frame;
    		if (frame.empty()) break; // Ran out of film
    		cv::imshow("Example 2-10", frame);
    		if ((char)cv::waitKey(33) >= 0) break;
    	}
    	return 0;
    }
    

      

  • 相关阅读:
    CodeForces 785D Anton and School
    CodeForces 785C Anton and Fairy Tale
    CodeForces 785B Anton and Classes
    CodeForces 785A Anton and Polyhedrons
    爱奇艺全国高校算法大赛初赛C
    爱奇艺全国高校算法大赛初赛B
    爱奇艺全国高校算法大赛初赛A
    EOJ 3265 七巧板
    EOJ 3256 拼音魔法
    EOJ 3262 黑心啤酒厂
  • 原文地址:https://www.cnblogs.com/fcfc940503/p/11327436.html
Copyright © 2011-2022 走看看