C++ OpenCV直方图不';t更新,尽管有新的视频数据
我试图为来自单通道黑白视频流的每一帧计算一个直方图。最后,我将把这些直方图的投影反馈给OpenCV的camshift函数 这是我的密码:C++ OpenCV直方图不';t更新,尽管有新的视频数据,c++,opencv,image-processing,histogram,C++,Opencv,Image Processing,Histogram,我试图为来自单通道黑白视频流的每一帧计算一个直方图。最后,我将把这些直方图的投影反馈给OpenCV的camshift函数 这是我的密码: #include "opencv2/highgui/highgui.hpp" #include "opencv2/imgproc/imgproc.hpp" #include "opencv2/opencv.hpp" #include "opencv2/core/core.hpp" #include <iostream> #include <v
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/opencv.hpp"
#include "opencv2/core/core.hpp"
#include <iostream>
#include <vector>
using namespace std;
using namespace cv;
int main(int argc, char* argv[]) {
Mat frame, back, fore, hist, image, v_hist;
BackgroundSubtractorMOG2 bgs;
vector<vector<Point> > contours;
vector<Vec4i> hierarchy;
int bins = 8;
int histSize[] = { bins };
float range[] = { 0, 255 }; //values will range from 0 to 256
const float* histRange = { range };
bool uniform = true, accumulate = true;
VideoCapture cap(0);
if (!cap.isOpened()) {
cout << "Cannot open the video cam" << endl;
return -1;
}
namedWindow("histogram",CV_WINDOW_AUTOSIZE);
namedWindow("Foreground",CV_WINDOW_AUTOSIZE);
for (;;) {
if (!cap.read(frame)){
cout << "No frame" << endl;
break;
}
frame.copyTo(image);
//***start mask computation***
bgs.operator()(image, fore);
bgs.getBackgroundImage(back);
findContours(fore, contours, hierarchy, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE);
int idx = 0;
for( ; idx >= 0; idx = hierarchy[idx][0] ){
Scalar color( 255, 255, 255 );
drawContours( fore, contours, idx, color, CV_FILLED, 8, hierarchy );
}
erode(fore, fore, Mat());
erode(fore, fore, Mat());
dilate(fore, fore, Mat());
dilate(fore, fore, Mat());
//****end mask computation****
calcHist( &fore, 1, 0, Mat(), v_hist, 1, histSize, &histRange, uniform, accumulate);
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( v_hist, v_hist, 0, histImage.rows, NORM_MINMAX, -1, Mat() );
//draw histogram
for (int i = 1; i < *histSize; i++)
line ( histImage,
Point(bin_w*(i-1), hist_h - cvRound(v_hist.at<float>(i-1)) ),
Point( bin_w*(i), hist_h - cvRound(v_hist.at<float>(i)) ),
Scalar( 255, 255, 255), 2, 8, 0 );
if(!histImage.empty()) imshow("histogram", histImage);
if(!fore.empty()) imshow("Foreground", fore);
if (waitKey(30) >= 0) break;
}
return 0;
}
#包括“opencv2/highgui/highgui.hpp”
#包括“opencv2/imgproc/imgproc.hpp”
#包括“opencv2/opencv.hpp”
#包括“opencv2/core/core.hpp”
#包括
#包括
使用名称空间std;
使用名称空间cv;
int main(int argc,char*argv[]){
垫子框架,背部,前部,历史,图像,v_历史;
背景:MOG2 bgs;
矢量等值线;
向量层次;
int bins=8;
int histSize[]={bins};
浮点范围[]={0,255};//值的范围为0到256
常量浮点*histRange={range};
布尔均匀=真,累加=真;
视频捕获上限(0);
如果(!cap.isOpened()){
cout虽然我还没有找到绘制的直方图没有更新的确切原因,但我已经做了一些测试,现在我确信直方图在不同帧之间发生了变化
以下是我在以前的程序中添加的代码:
//new declarations
Mat last_hist;
bool first = true;
//code is the same, leading up to...
normalize( v_hist, v_hist, 0, histImage.rows, NORM_MINMAX, -1, Mat() );
if (!first){
eq = std::equal( v_hist.begin<uchar>(), v_hist.end<uchar>(), last_hist.begin<uchar>() );
if (eq)
cout << "hists equal" << endl;
else
cout << "hists not equal" << endl;
}
else
first = false;
//draw histogram
for (int i = 1; i < *histSize; i++)
//...
if(!frame.empty())imshow("Frame", frame);
v_hist.copyTo(last_hist);
if (waitKey(30) >= 0) break;
//from here to end is the same
//新声明
最后一次;
bool first=true;
//代码是相同的,导致。。。
规范化(v_hist,v_hist,0,histImage.rows,NORM_MINMAX,-1,Mat());
如果(!第一个){
eq=std::equal(v_hist.begin()、v_hist.end()、last_hist.begin());
if(eq)
库特