关于一张图像的Hough完整处理过程
首先载入图片
A=imread(‘DSC9342.bmp’);
由于图片可能不是二值图片,需要进行转换
X=rgb2gray(A);
之后对图片进行边缘检测
BW=edge(X,‘sobel’,0.1);
imshow(A),figure;imshow(BW),figure;
对检测结果进行Hough变换
[H,T,R]=hough(BW,‘ThetaResolution’,1,‘RhoResolution’,10);
imshow(H,[],‘Xdata’,T,‘Ydata’,R,‘Initialmagnification’,‘fit’);
xlabel(’\theta’),ylabel(‘r’);
axis on,axis normal,hold on;
对Hough变换中的密集交点进行检测
P=houghpeaks(H,20,‘threshold’,ceil(0.15*max(H(?)),‘NHoodSize’,[101,101]);
x=T(P(:,2));y=R(P(:,1));
plot(x,y,‘s’,‘color’,‘white’);
lines=houghlines(BW,T,R,P,‘FillGap’,20,‘MinLength’,20);
figure,imshow(X),hold on
max_len=0;
for k=1:length(lines)
xy=[lines(k).point1;lines(k).point2];
plot(xy(:,1),xy(:,2),‘LineWidth’,4,‘Color’,‘black’);
end
得到最后的结果为:
可见其中主要的线条已经被检测出来了,之后需要的就是读取线条的数据,得到倾斜角度,最后将图片旋转就完成了。