计算累积直方图
calculating cumulative histogram
我想计算累积直方图,我已经完成了直方图计算,下面是它的代码。
我已经将图像转换为ycbcr通道,并为Y通道应用直方图
Thanks for help
#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;
vector<Mat> bgr_planes;
split(Image, bgr_planes );
// Compute the histograms:
calcHist( &bgr_planes[0], 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 src, imageq,ycbcr;
Mat histImage;
// Read original image
src = imread( "3.jpg");
if(! src.data )
{ printf("Error imagenn"); exit(1); }
cvtColor(src, ycbcr, CV_RGB2YCrCb);
vector <Mat> planes;
split(ycbcr,planes);
// Separate the image in 3 places ( B, G and R )
// Display results
imshow( "Source image", src );
// Calculate the histogram to each channel of the source image
histogramcalculation(planes[0], histImage);
// Display the histogram for each colour channel
imshow("Colour Image Histogram", histImage );
// Wait until user exits the program
waitKey();
return 0;
}
OpenCV cv::calcHist
具有accumulate
标志,但这并不做累积直方图,它只是不将直方图在开始时设置为零,因此您可以在多个图像上累积直方图。
你要做的是,在获得直方图之后,通过将所有之前的直方图bin的总和加到每个bin中来累积它。
cv::Mat hist;
cv::calcHist(&eyeROI, 1, 0, Mat(), hist, 1, &histSize, &histRange);
cv::Mat accumulatedHist = hist.clone();
for (int i = 1; i < histSize; i++) {
accumulatedHist.at<float>(i) += accumulatedHist.at<float>(i - 1);
std::cout << "Accumulated : " << accumulatedHist.at<float>(i) << ", original = " << hist.at<float>(i) << std::endl;
}
添加了std::cout以便您可以查看结果
相关文章:
- 为什么"do while"循环不断退出,即使条件计算结果为 false?
- 递归函数计算序列中的平方和(并输出过程)
- (C++)分析树以计算返回错误值的简单算术表达式
- 我的字符计数代码计算错误.为什么
- 在计算中使用二的幂有多有利可图
- 如何在OpenCV 3.4.7中计算CV_32FC1 cv::GpuMat图像的(范围)直方图
- 如何计算图像中每个块的直方图,然后将它们连接起来?
- 使用标准库或Boost库从一组数据中计算直方图
- 拥有云pcl::P ointCloud<xyzrgb>,如何独立计算每个颜色层的直方图?
- 如何使用每 n 个像素 OpenCV 计算直方图
- 尝试在没有opencv calcHist()的情况下计算我自己的直方图
- CUDA使用共享内存计算直方图
- 计算彩色图像的HSV直方图是否与H-S直方图不同
- Opencv:如何计算 3D 直方图
- OpencCV:从修改后的直方图计算图像
- 计算定向梯度的直方图
- 为什么尝试计算直方图和背向投影不起作用
- 在C++中计算单通道直方图的平均值和标准偏差
- 如何在OpenCV中计算矩阵的直方图
- 计算累积直方图