dimanche 29 mars 2015

implementing matlab hist() in c++


here is the line of code I want to implement



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);


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);


I can use openCV 2.4.10 as well but calcHist() isn't really the same, I need to create 2 bins.


my input is 1D array:



0.00598881028540019 1.56120677124307 0.00598881028540019 0.00669537049433832 1.37723800334516 1.37723800334516 1.36424594043624 1.56120677124307 0.0152220988707370


the output is:



X= 0.394793300524817 1.17240228100365
N = 4 5



Aucun commentaire:

Enregistrer un commentaire