2015-10-07 40 views
0

我想显示从视频文件中的每个帧的更新直方图,显示更新的视频直方图与每个FRAM

视频文件被捕获,然后传递到直方图计算功能

我已经做了一些改变,以下面的代码。但它不工作,因为我想要的。

我感谢所有帮助

问候

#include "opencv2/core/core.hpp" 
#include "opencv2/highgui/highgui.hpp" 
#include "opencv2/imgproc/imgproc.hpp" 
#include "iostream" 
#include "opencv2/highgui/highgui.hpp" 
#include "opencv2/imgproc/imgproc.hpp" 
#include <iostream> 
#include <stdio.h> 

using namespace cv; 
using namespace std; 

void histogramcalculation(const Mat &Image, Mat &histoImage) 
{ 
int histSize = 255; 
// 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(&Image, 1, 0, Mat(), b_hist, 1, &histSize, &histRange, uniform, accumulate); 


// Draw the histogram 

    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()); 


    // Draw 

    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); 



    } 
histoImage= histImage; 

} 

int main() 
{ 

Mat histImage; 

VideoCapture cap("eye.mp4"); // open video 
if(!cap.isOpened()) // check if we succeeded 
    return -1; 

namedWindow("Video",1); 
namedWindow("ycbcr",1); 
while(1) 
{ 
    Mat frame; 

    cap >> frame; // get a new frame 

    imshow("video", frame); 

// Calculate the histogram 

histogramcalculation(frame, histImage); 

// Display the histogram 

imshow("Colour Image Histogram", histImage); 

// Wait until user exits the program 

waitKey(); 

    } 
return 0; 
} 
+1

”但它没有按我想要的工作。“ <<<请澄清和阅读:http://stackoverflow.com/help/mcve – Mailerdaimon

回答

0

下面的代码将绘制灰度图像的直方图。如果您想绘制彩色图像的直方图,则必须将图像分成单独的通道,并且可以通过这些单独通道分别绘制直方图。 “

#include "opencv2/core/core.hpp" 
#include "opencv2/highgui/highgui.hpp" 
#include "opencv2/imgproc/imgproc.hpp" 
#include "iostream" 
#include "opencv2/highgui/highgui.hpp" 
#include "opencv2/imgproc/imgproc.hpp" 
#include <iostream> 
#include <stdio.h> 

using namespace cv; 
using namespace std; 

void histogramcalculation(const Mat &Image, Mat &histoImage) 
{ 
int histSize = 255; 
// 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(&Image, 1, 0, Mat(), b_hist, 1, &histSize, &histRange, uniform, accumulate); 


// Draw the histogram 

    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()); 


    // Draw 

    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); 



    } 
histoImage= histImage; 

} 

int main() 
{ 

Mat histImage; 

VideoCapture cap("test1.avi"); // open video 
if(!cap.isOpened()) // check if we succeeded 
    return -1; 

while(1) 
{ 
    Mat frame; 

    cap >> frame; // get a new frame 


    imshow("video", frame); 
    cvtColor(frame, frame, CV_BGR2GRAY); 

// Calculate the histogram 

histogramcalculation(frame, histImage); 

// Display the histogram 

imshow("Gray Scale Image Histogram", histImage); 

// Wait until user exits the program 

waitKey(5); 

    } 
return 0; 
} 
+0

谢谢,它的工作原理我的代码出了什么问题? –

+0

@sarmadm你忘了将图像转换为灰度。请接受upvote的答案! – Arjun