我想获得图像的直方图 [英] i want to obtain the histogram of an image

查看:86
本文介绍了我想获得图像的直方图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个图像,我想计算和绘制它的直方图我使用calchist函数,但我很困惑它以前的计算现在我是一个新的回合c函数处理内存所以我需要一个解释这个代码或新的最简单的calchist函数代码



这是我的代码:





Mat Histogram :: BuildHistogram(Mat srcImage,bool accumulate){

Mat histMat;

//计算直方图

int * c =(int *)calloc(sizeof(int),_ channels.size());

for(int i = 0; i< _channels.size(); i ++)

{

c [i] = _ channels [i];



}



int * h =(int *)calloc(sizeof(int),_ channels.size());

for(int i = 0; i< _channels.size(); i ++)

{

h [i] = _ histSize [_channels [i]];



}



float ** ranges =(float **)calloc(sizeof(float *),_ chann els.size());

int size = _channels.size();

for(int i = 0; i< size; i ++)>

{

float * x =(float *)calloc(sizeof(float),2);

int index = 2 * _channels [i];

x [0] = _ histRange [index];

x [1] = _ histRange [index + 1];

范围[i] = x ;



}

if(accumulate == true)

_histMat.copyTo(histMat);



calcHist(&srcImage,1,c,_mask,histMat,_channels.size(),h,(const float **)范围,true,accumulate);

for(int i = 0; i< size; i ++)>

{

free(range [i]);

}

免费(范围);

免费(c);

免费(h);

//规范化直方图

normalize(histMat,_histMat,1,0,NORM_L1);

标量v = cv :: sum(_histMat);

if(accumulate == true)

histMat.copyTo(_histMat);

//返回直方图

返回histMat;

}

i have an image and i want to compute and draw its histogram i use the calchist function but i'm confused about its previous calculations now i'm a new a bout the c function which deal with memory so i need an explain to this code or anew easiest code for calchist function

this is my code:


Mat Histogram::BuildHistogram(Mat srcImage,bool accumulate){
Mat histMat;
//compute histogram
int *c=(int *)calloc(sizeof(int),_channels.size());
for(int i=0;i<_channels.size();i++)
{
c[i]=_channels[i];

}

int *h=(int *)calloc(sizeof(int),_channels.size());
for(int i=0;i<_channels.size();i++)
{
h[i]=_histSize[_channels[i]];

}

float **ranges=(float **)calloc(sizeof(float*),_channels.size());
int size=_channels.size();
for(int i=0;i<size;i++)>
{
float *x=(float *)calloc(sizeof(float),2);
int index=2*_channels[i];
x[0]=_histRange[index];
x[1]=_histRange[index+1];
ranges[i]=x;

}
if(accumulate==true)
_histMat.copyTo(histMat);

calcHist(&srcImage,1,c,_mask,histMat,_channels.size(),h,(const float **)ranges, true, accumulate);
for(int i=0;i<size;i++)>
{
free(ranges[i]);
}
free(ranges);
free(c);
free(h);
//normalize histogram
normalize( histMat, _histMat,1,0,NORM_L1);
Scalar v=cv::sum(_histMat);
if(accumulate==true)
histMat.copyTo(_histMat);
//return histogram
return histMat;
}

推荐答案

有一个非常openCV文档中直方图计算的好例子。你需要的一切都应该在那里:



http://docs.opencv.org/doc/tutorials/imgproc/histograms/histogram_calculation/histogram_calculation.html [ ^ ]
There is a very good example for histogram calculation in the openCV documentation. Everything you need should be there:

http://docs.opencv.org/doc/tutorials/imgproc/histograms/histogram_calculation/histogram_calculation.html[^]


这篇关于我想获得图像的直方图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆