如何使用OpenCV获得实时视频输入的实时直方图?

问题描述 投票:0回答:2

我正试图通过网络摄像头的视频输入获得实时直方图。但是我被困在只捕获视频第一帧并且显示等效直方图的点上。另外,我在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;
}

This image显示了我的代码的输出。任何人都可以建议我如何继续这个代码,以实现实时直方图?

c++ opencv qt5 ubuntu-16.04 histogram
2个回答
0
投票

在捕获循环结束时,您使用的是waitKey(0),它是一个OpenCV函数,等待按键。整数参数表示函数等待按键的时间(以毫秒为单位)。值为0表示等待按键发生。这就是您的程序在第一帧之后停止的原因。您必须按任意键才能观察下一帧(在删除终止程序的exit(0)之后)。

如果您想要查看视频流,则可以使用较小的值,例如10,仅等待10 ms,允许帧速率高达100 FPS。

你也应该删除exit函数,因为这个函数将在waitKey函数完成等待按键后终止你的程序。

作为旁注:waitKey函数还返回一个数字,指示按下了哪个键。您可以使用它来触发程序的不同行为,例如,终止您的程序。


0
投票

你的waitKey(0)循环中有exit(0)while(true)。在没有查看waitKey的来源的情况下,我可以做出以下猜测:你得到直方图一次,然后按一个按钮,然后退出。

我怀疑,如果你从你的程序中取出waitKeyexit,你会得到你想要的东西。

© www.soinside.com 2019 - 2024. All rights reserved.