gpt4 book ai didi

c++ - 在 C++ 中实现 matlab hist()

转载 作者:搜寻专家 更新时间:2023-10-31 02:23:25 30 4
gpt4 key购买 nike

这是我要实现的代码行

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++ 代码:

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

有没有快速实现的方法:

[N,X] = hist(theta,2);

我也可以使用 openCV 2.4.10,但 calcHist() 并不完全相同,我需要创建 2 个分箱。

我的输入是一维数组:

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

最佳答案

calcHist 工作正常。尝试以下选择 histSizerange 的方法。希望能帮助到你 !。

float ary[9] = { 0.00598881028540019, 1.56120677124307,0.00598881028540019, 0.00669537049433832,1.37723800334516, 1.37723800334516, 1.36424594043624,1.56120677124307, 0.0152220988707370 };
cv::Mat srcMat = cv::Mat(1, 9, CV_32FC1, ary);
int histSize = 2;
float range[] = { 0.0, 2.0 };
const float* histRange = { range };
bool uniform = true;
bool accumulate = false;
cv::Mat histOut;
cv::calcHist(&srcMat, 1, 0, Mat(),histOut, 1, &histSize, &histRange, uniform, accumulate);
cout << histOut << endl;

关于c++ - 在 C++ 中实现 matlab hist(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29329193/

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