hough变换用于检测直线,matlab中有hough的实现,不过是用C语言实现的,看不到源码。(matlab-hough)
下面是我自己的实现。
function hough_space = hough_transform(img_binary);
% get the hough transformation
% img_binary: binary image
% hough_space: response in hough space
% x * cos(theta) + y * sin(theta) = r
theta_max = pi;
theta_count = 180;
theta_step = theta_max/theta_count;
img_size = size(img_binary);
center = img_size./2;
r_max = norm(img_size)*2;
r_count = r_max;
r_step = r_max/r_count;
hough_space = zeros(r_count,theta_count);
for i=1:img_size(1)
for j=1:img_size(2)
if ~img_binary(i,j)
continue;
end
for k=1:theta_count
theta = k*theta_step;
r = int16((j-center(2))*cos(theta)-((i-center(1))*sin(theta)));
r = r/r_step;
r = r+r_max/2;
if r>0 && r<r_max
hough_space(r, k) = hough_space(r, k)+1;
end
end
end
end
figure;
imshow(hough_space/max(hough_space(:)));
end
算法参考matlab的实现:matlab-hough algorithm
坐标定义如下:(图片来自matlab doc)
测试如下:
对点的检测
对线的检测