霍夫变换 MATLAB - 自定义实现
我正在尝试在 matlab 中实现直线的霍夫变换。我已经为此苦苦挣扎了好几天,我不知道为什么我的代码不能按应有的方式工作。是的,这是作业的一部分,但是请帮助我,因为我完全放弃了。
输入参数:即 - 逻辑“边缘”图像(0 表示不是边缘,1 表示边缘)。
function [out_ro, out_theta]=houghTransform(Ie,nBinsRo,nBinsTheta,tresh)
A = zeros(nBinsRo, nBinsTheta);
theta = 1:nBinsTheta;
theta = scale(theta, nBinsTheta, 1, (pi / 2), - (pi / 2));
D = size(diag(Ie));
D = D(1);
ro = 1:nBinsRo;
ro = scale(ro, nBinsRo, 1, D, -D);
len = size(Ie);
%checks all edges
for i = 1:len(1)
for j = 1:len(2)
%if it is an edge
if ((Ie(i,j) == 1))
%generate all functions x cos(theta) + y sin(theta) = ro
for m=1:nBinsTheta
roVal = i * cos(theta(m)) + j * sin(theta(m));
idx = scale2(roVal, D, -D, nBinsRo, 1);
if (idx > 0 && idx < nBinsRo)
A(idx, m) = A(idx, m) + 1;
end
end
end
end
end
figure(1);
clf;
imagesc(A)
% -------------------------------------------------- %
function idx = scale(val, max_val_in, min_val_in, max_val_out, min_val_out)
skalirni_faktor = (max_val_out - min_val_out) / (max_val_in - min_val_in) ;
idx = min_val_out + (val-min_val_in) .* skalirni_faktor;
% -------------------------------------------------- %
function idx = scale2(val, max_val_in, min_val_in, max_val_out, min_val_out)
skalirni_faktor = (max_val_out - min_val_out) / (max_val_in - min_val_in) ;
idx = min_val_out + round((val-min_val_in) .* skalirni_faktor);
非常感谢您的时间和答复。
I'm trying to implement hough transform for lines in matlab. And I have been struggling with this for days, I don't know why my code isn't working the way it should. Yes, it is part of the homework, but please help me out, because I totally gave up.
Input paramaters: Ie - logical "edge" image (0 means not an edge, 1 means edge).
function [out_ro, out_theta]=houghTransform(Ie,nBinsRo,nBinsTheta,tresh)
A = zeros(nBinsRo, nBinsTheta);
theta = 1:nBinsTheta;
theta = scale(theta, nBinsTheta, 1, (pi / 2), - (pi / 2));
D = size(diag(Ie));
D = D(1);
ro = 1:nBinsRo;
ro = scale(ro, nBinsRo, 1, D, -D);
len = size(Ie);
%checks all edges
for i = 1:len(1)
for j = 1:len(2)
%if it is an edge
if ((Ie(i,j) == 1))
%generate all functions x cos(theta) + y sin(theta) = ro
for m=1:nBinsTheta
roVal = i * cos(theta(m)) + j * sin(theta(m));
idx = scale2(roVal, D, -D, nBinsRo, 1);
if (idx > 0 && idx < nBinsRo)
A(idx, m) = A(idx, m) + 1;
end
end
end
end
end
figure(1);
clf;
imagesc(A)
% -------------------------------------------------- %
function idx = scale(val, max_val_in, min_val_in, max_val_out, min_val_out)
skalirni_faktor = (max_val_out - min_val_out) / (max_val_in - min_val_in) ;
idx = min_val_out + (val-min_val_in) .* skalirni_faktor;
% -------------------------------------------------- %
function idx = scale2(val, max_val_in, min_val_in, max_val_out, min_val_out)
skalirni_faktor = (max_val_out - min_val_out) / (max_val_in - min_val_in) ;
idx = min_val_out + round((val-min_val_in) .* skalirni_faktor);
Thank you very much for your time and answers.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我找不到代码有什么问题,但我认为缩放是有问题的。
如果有人发现这个,这里是霍夫变换的另一个实现。
I couldn't find what was wrong with the code, but I assume that scaling is problematic.
If anyone finds this, here is another implementation of hough transform.
霍夫朴素实现
如果您愿意,我也有 霍夫朴素实现 的要点。
Hough Naive Implementation
If you prefer, I have a gist of the Hough Naive Implementation too.