提取中心线后用hough拟合直线程序有问题,希望大家给指点一下
clc;clear all;
close all;
I=imread('tu1.jpg');%由于图像本身为灰度图像,所以不需要灰度化
% I2=rgb2gray(I);
figure(1),imshow(I);
%%%%提取中心线start%%%%%%%%%%
a=I;
[max_a index]=max(a); %找出a的格式
b=repmat(max_a,size(a,1),1); %按矩阵a格式复制b矩阵,b矩阵全是最大值
a(a<b)=0; %矩阵a中小于最大值的元素全变为0
figure(2),imshow(a);
%%%%%%%提取中心线end%%%%%%%%%%%%
%%%%%%%%%%%hough变换检测直线start%%%%%%%%%%%%
BW=a;
[H,T,R] = hough(BW,'RhoResolution',0.5,'ThetaResolution',0.5);
% display the original image显示原始图像
figure(3),imshow(imadjust(mat2gray(H)),'XData',T,'YData',R,'InitialMagnification','fit');
title('Hough transform of tu1.jpg ');
xlabel('\theta'), ylabel('\rho');
axis on, axis normal, hold on;
colormap(hot);
peaks = houghpeaks(H, 50,'threshold',ceil(0.3*max(H(:))));
lines = houghlines(BW, T, R,peaks);
figure(4),imshow(a);
lines(1).point2(1)=lines(1).point2(1)+315;
hold on
max_len = 0;
for k = 1:length(lines)
% 绘制各条线
xy = [lines(k).point1; lines(k).point2];
plot(xy(:,1),xy(:,2),'LineWidth',1,'Color','red');
end
hold off
%%%%%%%%%%%%%%hough变换检测直线end%%%%%%%%%%%%%%%%