繁体   English   中英

OpenCV C++ 实时图像过滤(由按键定义的灰度、模糊等)但除非您按住按键,否则相机会冻结

[英]OpenCV C++ real-time image filtering (grayscale, blur, etc. defined by keys) but the camera freezes unless you hold keypress

我遇到的另一个问题是我必须按“0”(这是为默认视频定义的按键情况),以便视频 window 一开始就弹出。 我对 OpenCV 还是新手,我认为这可能是我在使用 cv::waitKey() function 以及如何设置它以与我的开关循环一起工作时遇到的问题。

#include <cstdio>
#include <opencv2/opencv.hpp>
#include <opencv2/imgproc.hpp>
#include <opencv2/highgui.hpp>
#include <iostream>

using namespace cv;
using namespace std;


int main(int argc, char *argv[]) {
    // declare variables
    char label[256]; // a string for image capture file
    int quit = 0;
    int frameid = 0;
    char buffer[256];
    std::vector<int> pars;
    pars.push_back(5);

    // initialize video
    cv::VideoCapture *capdev;
    const char* window = "Main Window";
        cv::namedWindow(window, cv::WINDOW_AUTOSIZE);

    // open the video device
    capdev = new cv::VideoCapture(0);
    if( !capdev->isOpened() ) {
        printf("Unable to open video device\n");
        return(-1);
    }

    // get some properties of the image
    cv::Size refS( (int) capdev->get(cv::CAP_PROP_FRAME_WIDTH ),
               (int) capdev->get(cv::CAP_PROP_FRAME_HEIGHT));

    printf("Expected size: %d %d\n", refS.width, refS.height);
    
    // create destination output
        cv::Mat convertedImage;

    // loop for various filter keys
    for(;!quit;) {
        cv::Mat frame;
                *capdev >> frame; // get a new frame from the camera, treat as a stream


        // test if video capture  was successful
                if( frame.empty() ) {
                  printf("frame is empty\n");
                  break;
                }
        
        /*  
        // loop over all rows
        for(i=0; i<src.rows; i++) {
          // loop over all of columns
          for (j=0; j<src.cols, j++) {
            // apply filter and write result to destination
            dst.at<cv::Vec3b>(i, j) = applyFilter( src, i, j ); // fxn returns pixel (cv::Vec3b)
          }
        } 
        */
        
        int last = waitKey(0);  
    
        switch(last) {
        case 'q':
        { 
          quit = 1;
          break;
        }
        case 's': // capture a photo if the user hits s
        {
          sprintf(buffer, "%s.%03d.png", label, frameid++);
          cv::imwrite(buffer, convertedImage, pars);
          printf("Image written: %s\n", buffer);
          break;
        }
        case 'g': // display greyscale live video
        {
          cv::cvtColor(frame, convertedImage, cv::COLOR_BGR2GRAY);
          break;
        }
        default: // 0 to get to original video
        {
          convertedImage = frame;
        }
        }
        
        cv::imshow(window, convertedImage);
        
        int key = waitKey(10);
            if (key != -1)
            {
          last = key;
        }
    }
    
    // terminate the video capture
    printf("Terminating\n");
    delete capdev;
    
    return(0);
}

您只需要从 waitKey 中获取一个字节:

char last = waitKey(1);  

并可能删除对 waitKey 的第二次调用,如下所述。

对于灰色模式,您可以管理切换 state,因为即使按住 g 键,也可能会显示一些色框。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM