% Clear everything clear all close all % Go to your image catalog and read your image (exchange with your own % catalog) cd 'C:\Documents and Settings\aurdal\My Courses\UIO\INF3300' i=double(imread('cuba.jpg')); % Take a look at the image that could have been the end of us all imshow(i,[min(min(i)) max(max(i))]) % Calculate sobel masks sh=fspecial('sobel'); sv=sh'; % Calculate the gradient information igh=imfilter(i,sh); igv=imfilter(i,sv); igm=sqrt(igh.*igh+igv.*igv); figure imshow(igm,[min(min(igm)) max(max(igm))]) min(min(igm)) max(max(igm)) % Can we find edges by thresholding? % Low threshold igmTL=igm>250; figure imshow(igmTL) % High threshold igmTH=igm>350; figure imshow(igmTH) % Find all components resulting from a low threshold that are connected to % a result from a high treshold [lL,nL]=bwlabel(igmTL,8); nL figure imshow(lL,[0 nL]) colormap(rand(nL,3)) [lH,nH]=bwlabel(igmTH,8); figure imshow(lH,[0,nH]) colormap(rand(nH,3)) % Find all components in low thresh image that is connected to segment in % high thresh image tmp=lL&lH; tmp=tmp.*lL; [r,c,v]=find(tmp); v=unique(v); lHyst=ismember(lL,v); figure imshow(lHyst) % Hough transform example 1 clear all close all i=zeros(11); for j=3:9 i(j,j)=1; end figure colormap(gray(2)) imagesc(i); grid on set(gca,'xcolor','w') set(gca,'ycolor','w') axis image theta=0:179; [iH,Xp]=houghradon(i,theta); figure imagesc(theta,Xp,iH) colormap(cool) colorbar xlabel('\theta (degrees)') ylabel('\rho (pixels from center)') grid on % Hough transform example 2 clear all close all i=zeros(11); for j=3:9 i(j,j)=1; end i(5,5)=0; i(7,7)=0; figure colormap(gray(2)) imagesc(i); grid on set(gca,'xcolor','w') set(gca,'ycolor','w') axis image theta=0:179; [iH,Xp]=houghradon(i,theta); figure imagesc(theta,Xp,iH) colormap(cool) colorbar xlabel('\theta (degrees)') ylabel('\rho (pixels from center)') grid on % Hough transform example 3 clear all close all i=imread('corridor.png'); ig=double(rgb2gray(i)); figure imshow(ig,[min(min(ig)) max(max(ig))]) h1=fspecial('sobel'); h2=h1'; igh=imfilter(ig,h1); igv=imfilter(ig,h2); igs=abs(igh)+abs(igv); figure imshow(igs,[min(min(igs)) max(max(igs))]) igsT=igs>170; figure imshow(igsT) theta=0:179; [igsH,Xp]=houghradon(igsT,theta); figure imagesc(theta,Xp,igsH) colormap(cool) colorbar xlabel('\theta (degrees)') ylabel('\rho (pixels from center)') grid on ind=find(igsH>100); [dummy,index]=sort(-igsH(ind)); k=ind(index(1:20)) [r,c]=ind2sub(size(igsH),k); t=-theta(c)*pi/180; rho=Xp(r); lines = [cos(t)' sin(t)' -rho]; cx = size(ig,2)/2-1; cy = size(ig,1)/2-1; lines(:,3) = lines(:,3) - lines(:,1)*cx - lines(:,2)*cy; figure imshow(igs,[min(min(igs)) max(max(igs))]) draw_lines(lines);