1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
| close all;clc;clear all;
warning off;
%Chargement de l'image
f=medfilt2(rgb2gray(imread('car5.jpg')), [7 7]);
figure(1), imshow(f), title('filtered image by 7x7');
%seuil de l'image
seuil = graythresh(f);
%conversion en binaire
B=im2bw(f,seuil);
figure(2), imshow(B), title('Binary image');
%le fond en blanc et la forme en noir
Bi=~B;
figure(3), imshow(Bi), title('Inverse binary image');
%Element structurant de rectangle
se=strel('rectangle',[5 14]);
%vertical erosion
Ber=imerode(Bi,se);
figure(4), imshow(Ber), title('erosion vertical de l image binaire inverse');
%horizontal erosion
Ber2=imerode(Ber,se);
figure(5), imshow(Ber2), title('horizontal erosion');
%Dilate back
Bdi=imdilate(imdilate(Ber2,se),se);
figure(6), imshow(Bdi), title('image dilate');
%Canny edge detector
edg=edge(Ber2,'canny',0.8); %or canny, 0.3 instead of sobel
figure(4), imshow(edg), title('canny edge detector');
%Hough Transformation
[H, theta, rho] = hough(edg);
figure(7), imshow(H, [],'XData',theta,'YData',rho,'InitialMagnification','fit');
xlabel('\theta'), ylabel('\rho');
axis on, axis normal, hold on;
peaks = houghpeaks(H,18, 'threshold',ceil(0.1*max(H(:))));
x=theta(peaks(:,2)); y=rho(peaks(:,1));
plot(x,y,'s','Color','white');
t1 = title(['Hough Transform and detected peaks (',num2str(length(peaks)),')']);
%find lines and plot them
lines = houghlines(f,theta,rho,peaks,'FillGap',15,'MinLength',7);
figure(8), imshow(f), hold on;
max_len = 0;
for k=1:length(lines)
if isfield(lines,'point1') == 1
xy=[lines(k).point1; lines(k).point2];
plot(xy(:,1),xy(:,2),'LineWidth',2,'Color','red');
%plot the 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','green');
%text(xy(1,1),xy(1,2),(num2str(k)),'HorizontalAlignment','center','BackgroundColor',[.7 .9 .7]);
%determine the endpoints of the highest line segment
len = norm(lines(k).point1 - lines(k).point2);
if(len > max_len)
max_len = len;
xy_long = xy;
end
end
end
% highlight the longest line segment
plot(xy_long(:,1),xy_long(:,2),'LineWidth',2,'Color','cyan'); |
Partager