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;
    }
    

      

  • 相关阅读:
    进程间通信的方式——信号、管道、消息队列、共享内存
    exit()与_exit()的区别(转)
    [Google Codejam] Round 1A 2016
    使用shell脚本自定义实现选择登录ssh
    PHP的反射机制【转载】
    PHP set_error_handler()函数的使用【转载】
    PHP错误异常处理详解【转载】
    php的memcache和memcached扩展区别【转载】
    .htaccess重写URL讲解
    PHP数据库扩展mysqli的函数试题
  • 原文地址:https://www.cnblogs.com/fcfc940503/p/11327436.html
Copyright © 2011-2022 走看看