我正在尝试从网络摄像头的视频提要中获取实时直方图。但是我被困在仅捕获视频的第一帧并显示等效的直方图的位置。另外,我在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;
}
答案 0 :(得分:0)
在捕获循环结束时,您正在使用waitKey(0)
,这是一个OpenCV函数,需要等待按键。整数参数指示该功能等待按键的时间(以毫秒为单位)。值为0表示等待按键发生。这就是为什么您的程序在第一帧之后停止的原因。您必须按任意键才能观察下一帧(在删除exit(0)
后终止程序)。
如果要观看视频流,可以使用较小的值,例如10,仅等待10毫秒,以使帧率达到100 FPS。
另外,您应该删除exit
函数,因为此函数将在waitKey
函数等待按键完成后终止程序。
作为旁注:waitKey
函数还会返回一个数字,该数字指示按下了哪个键。您可以使用它来触发程序的不同行为,例如终止程序。
答案 1 :(得分:0)
您的waitKey(0)
循环中有exit(0)
和while(true)
。无需查看waitKey
的来源,我可以做出以下猜测:一次获得直方图,然后按一个按钮,然后退出。
我怀疑,如果您从程序中退出waitKey
和exit
调用,您会得到想要的。