gpt4 book ai didi

c++ - 计算累积直方图

转载 作者:太空宇宙 更新时间:2023-11-03 22:28:34 25 4
gpt4 key购买 nike

我想计算累积直方图,我已经完成了直方图计算,下面是它的代码。

我已将 iamge 转换为 ycbcr channel 并为 Y channel 应用直方图

感谢帮助

#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 imagen\n"); 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::calcHistaccumulate 标志,但这并没有做累积直方图,它只是没有在开始时将直方图设置为零, 因此您可以累积多个图像的直方图。

您想要做的是,在获得直方图之后,通过将所有先前直方图分箱的总和添加到每个分箱来自己累积它。

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 以便您可以查看结果。

关于c++ - 计算累积直方图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32952577/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com