在C ++中实现matlab hist() [英] implementing matlab hist() in c++
本文介绍了在C ++中实现matlab hist()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是我要实现的代码行
kb = [];
for k = 1:length(nRef)
for n=1:length(dCmpInd{k})
x = [centroid(nRef{k}, 1), centroid(dCmpInd{k}(n),1)];
y = [centroid(nRef{k}, 2), centroid(dCmpInd{k}(n),2)];
[x,ind] = sort(x);
y = y(ind);
kb = [kb diff(y) / diff(x)];
end
end
theta = (atan(kb));
[N, X] = hist(abs(theta),2);
这是我的c ++代码:
here is my c++ code:
std::vector<double> kb;
std::vector<double> theta;
for (int k = 0; k < nRef.size(); k++)
{
for (int n = 0; n < dCmpInd[k].size(); n++)
{
double x1 = centroids[nRef[k]].m_X; double x2 = centroids[dCmpInd[k][n]].m_X;
double y1 = centroids[nRef[k]].m_Y; double y2 = centroids[dCmpInd[k][n]].m_Y;
if (x1 <x2)
{
double tempdiff = (y2-y1)/(x2-x1);
kb.push_back(tempdiff);
theta.push_back(abs(atan(tempdiff)));
}
else
{
double tempdiff = (y1-y2)/(x1-x2);
kb.push_back(tempdiff);
theta.push_back(abs(atan(tempdiff)));
}
}
}
有没有一种快速的实现方法:
is there a quick way to implement :
[N,X] = hist(theta,2);
我也可以使用openCV 2.4.10,但是calcHist()并不完全相同,我需要创建2个bin.
I can use openCV 2.4.10 as well but calcHist() isn't really the same, I need to create 2 bins.
我的输入是一维数组:
0.00598881028540019 1.56120677124307 0.00598881028540019 0.00669537049433832 1.37723800334516 1.37723800334516 1.36424594043624 1.56120677124307 0.0152220988707370
输出为:
X= 0.394793300524817 1.17240228100365
N = 4 5
推荐答案
尽管这不是完整的实现,但这是我所做的
Although this is not a full implementation here is what I did
HistogramResults Utilities::Hist(std::vector<double> vector, std::vector<double> x)
{
if (vector.empty())
{
return HistogramResults();
}
if(x.empty())
{
x.push_back(10);
}
std::vector<double> N(x);
std::vector<double>::iterator iter = std::min_element(std::begin(vector),std::end(vector));
double minY = *iter;
iter = std::max_element(std::begin(vector),std::end(vector));
double maxY = *iter;
std::vector<double> xx;
if (x.size() == 1)
{
if (minY == maxY)
{
minY = minY - floor(x[0] /2) - 0.5;
maxY = maxY - ceil(x[0] /2) - 0.5;
}
double binwidth = (maxY - minY)/x[0];
xx.push_back(-1*std::numeric_limits<double>::infinity());
for (int i = 0; i <= x[0]; i++)
{
xx.push_back( minY + binwidth * i);
}
xx[x[0]+1] = maxY;
xx.push_back(std::numeric_limits<double>::infinity());
x.clear();
for (int i = 1; i < xx.size()-2; i++)
{
x.push_back(xx[i] + binwidth/2);
}
}
else
{
}
std::vector<int> nn =HistC(vector,xx);
nn[1] = nn[1] +nn[0];
nn[nn.size()-1] == nn[nn.size()-1] + nn[nn.size()-2];
nn.pop_back();
nn.erase (nn.begin());
return HistogramResults(nn,x);
}
std::vector<int> Utilities::HistC(std::vector<double> y, std::vector<double> edges)
{
std::vector<int> bins(edges.size()-1);
for (int i = 0; i < y.size(); i++)
{
for (int j = 0; j < edges.size()-1; j++)
{
if( (edges[j] < y[i]) && (y[i] <= edges[j+1]))
{
bins[j]= bins[j]+1;
}
}
}
return bins;
}
这是类HistorgramResults
here is the class HistorgramResults
#include "HistogramResults.h"
HistogramResults::HistogramResults(std::vector<int> numberOfElementN, std::vector<double> averageX)
{
m_numberOfElementN = numberOfElementN;
m_averageX = averageX;
}
HistogramResults::HistogramResults(void)
{
}
HistogramResults::~HistogramResults(void)
{
}
这篇关于在C ++中实现matlab hist()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文