templom = imread('templom.jpg');
templom_gray = rgb2gray(templom);
templom_Canny = edge(templom_gray, 'canny');

[HM, theta, rho] = hough(templom_Canny);
peaks = houghpeaks(HM, 10);
lines = houghlines(templom_Canny, theta, rho, peaks);

%Display the original image.
subplot(2,1,1);
imshow(templom);
title('Templom');

% Display the Hough matrix.
subplot(2,1,2);
imshow(imadjust(mat2gray(HM)),'XData',theta,'YData',rho,'InitialMagnification','fit');
      
title('Hough Transform of Gantrycrane Image');
xlabel('\theta'), ylabel('\rho');
axis on, axis normal, hold on;
colormap(hot);

figure, imshow(templom_gray); hold on;
for k = 1:length(lines)
   xy = [lines(k).point1; lines(k).point2];
   plot(xy(:,1),xy(:,2),'LineWidth',3,'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');

   max_len = 0;
   % 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','red');