【问题标题】:How to get a realtime Histogram of a live video feed using OpenCV?如何使用 OpenCV 获取实时视频源的实时直方图?
【发布时间】:2019-03-05 06:17:33
【问题描述】:

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

显示我的代码的输出。谁能建议我如何处理此代码以实现实时直方图?

【问题讨论】:

    标签: c++ opencv qt5 ubuntu-16.04 histogram


    【解决方案1】:

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

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

    您还应该删除exit 函数,因为此函数将在waitKey 函数完成等待按键后终止您的程序。

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

    【讨论】:

      【解决方案2】:

      您的while(true) 循环中有waitKey(0)exit(0)。不看waitKey的来源,我可以做出如下猜测:你得到一次直方图,然后你按下一个按钮,然后它就退出了。

      我怀疑如果你从你的程序中去掉waitKeyexit 调用,你会得到你想要的。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2018-07-30
        • 2017-03-10
        • 2018-04-29
        • 1970-01-01
        • 2021-12-28
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多