% read image, find edges and Hough transform I = imread('pillows.jpg'); I = imread('chip.tif'); BW = edge(I,'canny'); % extract edges [H,theta,rho] = hough(BW,'RhoResolution',0.5,'ThetaResolution',0.5); % display the original image subplot(2,1,1); %imshow(RGB); imshow(I); % display the hough matrix subplot(2,1,2); imshow(imadjust(mat2gray(H)),'XData',theta,'YData',rho,... 'InitialMagnification','fit'); title('Hough transform of gantrycrane.png'); xlabel('\theta'), ylabel('\rho'); axis on, axis normal, hold on; colormap(gray(256)); % find peaks in Hough Transform P = houghpeaks(H,5,'threshold',ceil(0.3*max(H(:)))); hold on; x = theta(P(:,2)); y = rho(P(:,1)); plot(x,y,'s','color','red'); % find the lines lines = houghlines(BW,theta,rho,P,'FillGap',5,'MinLength',7); % plot lines on image figure, imshow(I), hold on max_len = 0; for k = 1:length(lines) xy = [lines(k).point1; lines(k).point2]; plot(xy(:,1),xy(:,2),'LineWidth',2,'Color','green'); % Plot beginnings and ends of lines plot(xy(1,1),xy(1,2),'x','LineWidth',2,'Color','yellow'); plot(xy(2,1),xy(2,2),'x','LineWidth',2,'Color','red'); % Determine the endpoints of the longest line segment len = norm(lines(k).point1 - lines(k).point2); if ( len > max_len) max_len = len; xy_long = xy; end end % highlight the longest line segment plot(xy_long(:,1),xy_long(:,2),'LineWidth',2,'Color','cyan');