簡體   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