簡體   English   中英

如何使用OpenCV獲取實時視頻源的實時直方圖?

[英]How to get a realtime Histogram of a live video feed using OpenCV?

我正在嘗試從網絡攝像頭的視頻提要中獲取實時直方圖。 但是我被困在僅捕獲視頻的第一幀並顯示等效的直方圖的位置。 另外,我在Qt Creator中使用OpenCV2。 這是代碼:

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <opencv2/opencv.hpp>

using namespace cv;
using namespace std;

MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);

 /// Capture video
VideoCapture cap(0);
 if( cap.isOpened() == false )
   { exit(0); }

 while(true)
 {
    Mat frame;
    bool bSuccess = cap.read(frame);
    if (bSuccess == false)
      {
        ui->label->setText("Video camera is disconnected");
        exit(0);
      }

    /// Separate the image in 3 places ( B, G and R )
    vector<Mat> bgr_planes;
    split(frame, bgr_planes );

    /// Establish the number of bins
     int histSize = 256;

    /// Set the ranges ( for B,G,R) )
    float range[] = { 0, 256 } ;
    const float* histRange = { range };

     bool uniform = true; bool accumulate = false;

     Mat b_hist, g_hist, r_hist;

     /// Compute the histograms:
     calcHist( &bgr_planes[0], 1, 0, Mat(), b_hist, 1, &histSize, &histRange, uniform, accumulate );
     calcHist( &bgr_planes[1], 1, 0, Mat(), g_hist, 1, &histSize, &histRange, uniform, accumulate );
     calcHist( &bgr_planes[2], 1, 0, Mat(), r_hist, 1, &histSize, &histRange, uniform, accumulate );

    // Draw the histograms for B, G and R
    int hist_w = 512; int hist_h = 400;
    int bin_w = cvRound( (double) hist_w/histSize );

    Mat histImage( hist_h, hist_w, CV_8UC3, Scalar( 0,0,0) );

    /// Normalize the result to [ 0, histImage.rows ]
    normalize(b_hist, b_hist, 0, histImage.rows, NORM_MINMAX, -1, Mat() );
    normalize(g_hist, g_hist, 0, histImage.rows, NORM_MINMAX, -1, Mat() );
    normalize(r_hist, r_hist, 0, histImage.rows, NORM_MINMAX, -1, Mat() );

     /// Draw for each channel
    for( int i = 1; i < histSize; i++ )
    {
        line( histImage, Point( bin_w*(i-1), hist_h - cvRound(b_hist.at<float>(i-1)) ) ,
                      Point( bin_w*(i), hist_h - cvRound(b_hist.at<float>(i)) ),
                      Scalar( 255, 0, 0), 2, 8, 0  );
        line( histImage, Point( bin_w*(i-1), hist_h - cvRound(g_hist.at<float>(i-1)) ) ,
                      Point( bin_w*(i), hist_h - cvRound(g_hist.at<float>(i)) ),
                      Scalar( 0, 255, 0), 2, 8, 0  );
        line( histImage, Point( bin_w*(i-1), hist_h - cvRound(r_hist.at<float>(i-1)) ) ,
                      Point( bin_w*(i), hist_h - cvRound(r_hist.at<float>(i)) ),
                      Scalar( 0, 0, 255), 2, 8, 0  );
    }

    /// Display
    namedWindow("calcHist Demo", CV_WINDOW_AUTOSIZE );
    imshow("calcHist Demo", histImage );
    namedWindow("Video Capture", CV_WINDOW_AUTOSIZE );
    imshow("Video Capture", frame );

    waitKey(0);

    exit(0);

 }

   }

 MainWindow::~MainWindow()
{
     delete ui;
}

這個圖片 顯示我的代碼的輸出。 誰能建議我該如何繼續進行此代碼以實現實時直方圖?

在捕獲循環的最后,您使用的是waitKey(0) ,它是一個OpenCV函數,等待按鍵。 整數參數指示該功能等待按鍵的時間(以毫秒為單位)。 值為0表示等待按鍵發生。 這就是為什么您的程序在第一幀之后停止的原因。 您將必須按任意鍵才能觀察下一幀(在還刪除了exit(0)程序的exit(0)之后)。

如果要觀看視頻流,可以使用較小的值,例如10,僅等待10毫秒,以使幀速率高達100 FPS。

另外,您還應該刪除exit功能,因為該功能將在waitKey函數完成等待按鍵后終止程序。

附帶說明: waitKey函數還返回一個數字,指示按下了哪個鍵。 您可以使用它來觸發程序的不同行為,例如終止程序。

while(true)循環中有waitKey(0)exit(0) 無需查看waitKey的來源,我可以做出以下猜測:一次獲得直方圖,然后按一個按鈕,然后退出。

我懷疑如果您從程序中exit waitKeyexit調用,您將獲得所需的內容。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM