显示每帧更新的视频直方图

displaying updated video histogram with each fram

本文关键字:的视频 直方图 更新 显示      更新时间:2023-10-16

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

视频文件被捕获,然后传递给直方图计算函数

我已经对下面的代码做了一些修改,但是它不能像我想要的那样工作。

感谢你的帮助

#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;
}

下面的代码将绘制灰度图像的直方图。如果你想为彩色图像绘制直方图,你必须将图像分割成单独的通道,并可以将这些单独的通道单独绘制直方图。

#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;
}