能人也辨别出直线,但是图像预处理的效果和边缘检测的效果不好,有时能检测到像素块的小方格,这个如何解决为好。
matlab程序
%% 局部调整
img=imadjust(img,[0.1 0.3],[0 1]);
figure;imshow(img);title('adjust');
thresh=0.5;
sigma=0.9;%定义高斯参数
% f = edge(double(img));
% f = edge(double(img),'roberts',thresh,'thinning');
% f = edge(double(img),'prewitt',thresh,'vertical');
f = edge(double(img),'log',thresh,sigma);
% f = edge(double(img),'canny',thresh,sigma);
figure;imshow(f); title('edge');
%% 直线检测
[H, theta, rho]= hough(f);
peak=houghpeaks(H,2,'threshold',ceil(0.3*max(H(:))));
lines=houghlines(f,theta,rho,peak);
hold on;
num=length(lines)
for k=1:num
xy=[lines(k).point1;lines(k).point2];
slope(k)=(lines(k).point1(2)-lines(k).point2(2))/(lines(k).point1(1)-lines(k).point2(1));
plot(xy(:,1),xy(:,2),'LineWidth',4,'Color',[.98 .4 .2]);
end
hold off;