目录
1 概述
2 运行结果
3 参考文献
4 Matlab代码
该程序实现了一些基本的图像处理算法,并将它们组合在一起构建了一个基于霍夫变换的直线检测器。该程序能够找到图像中直线段的起点和终点。与大多数视觉算法一样,霍夫变换使用了许多参数,这些参数的最佳值与数据有关(即,一组在一幅图像上非常有效的参数值可能不适合另一幅图像)。通过在测试图像上运行代码和调整参数,它获得了每个图像的最优值,从而获得了良好的性能。
主函数部分代码:
clc;
clear;
datadir = '../data'; %the directory containing the images
resultsdir = '../results'; %the directory for dumping results
%parameters
sigma = 2;
threshold = 0.03;
rhoRes = 2;
thetaRes = pi/90;
nLines = 50;
%end of parameters
imglist = dir(sprintf('%s/*.jpg', datadir));
for i = 1:numel(imglist)
%read in images%
[path, imgname, dummy] = fileparts(imglist(i).name);
img = imread(sprintf('%s/%s', datadir, imglist(i).name));
if (ndims(img) == 3)
img = rgb2gray(img);
end
img = double(img) / 255;
%actual Hough line code function calls%
[Im Io Ix Iy] = myEdgeFilter(img, sigma);
[H,rhoScale,thetaScale] = myHoughTransform(Im, threshold, rhoRes, thetaRes);
[rhos, thetas] = myHoughLines(H, nLines);
lines = houghlines(Im>threshold, 180*(thetaScale/pi),...
rhoScale, [rhos,thetas],'FillGap',5,'MinLength',10);
%everything below here just saves the outputs to files%
fname = sprintf('%s/%s_01edge.png', resultsdir, imgname);
imwrite(Im/max(Im(:)), fname);
fname = sprintf('%s/%s_02threshold.png', resultsdir, imgname);
imwrite(Im > threshold, fname);
fname = sprintf('%s/%s_03hough.png', resultsdir, imgname);
imwrite(H/max(H(:)), fname);
fname = sprintf('%s/%s_04lines.png', resultsdir, imgname);
img2 = img;
for j=1:numel(lines)
img2 = drawLine(img2, lines(j).point1, lines(j).point2);
end
imwrite(img2, fname);
end
[1]唐佳林,王镇波,张鑫鑫.基于霍夫变换的直线检测技术[J].科技信息,2011,No.370(14):33+35.
部分理论引用网络文献,若有侵权联系博主删除。