l'image de fond

fr       en

Gray Level Image Processing

Hough Transform

This transformation allows lines detection in images. It needs binary images. Every pixel of the binary image is represented by a line in the Hough space. Look at the next image:
hough1
At left, the original image with a single dot to 1. At right, It's transform in Hough space. We can see that the dot has been transformed in line. Now, look at what's happend for several dot belong to one line.
hough2
We note that the pixels trasformation make lines which converge to one single dot (into the red circle). Thanks to this dot, we can find the line which pass trough every dot of original image.
hough3

Method

We start with a edge detection on the original image. tHe aim is to transform the image into binary image and detect the line on edges. The size of the transform matrix will be size of the line for θ and the size of the diagonal of image for rho. as:
s i z e r h o = ( M 2 + N 2 ) 0.5
Where M is the number of raw and N the number of column. This matrix is initialized to 0. Now, for every pixel of image equal to 1, we make the Hough transform. Each dot represent a line in Hough space. In the transform matrix we will increase of 1 for all the line. In that way, every time a line pass to a dot of the matrix, it's value increase. More lines pass to a dot more it's value is high. The compute to find the coordinate to increase is as:
for every pixel of the image to 1 we do for θ from 0 to π
r h o = x . cos ( θ ) + y . sin ( θ )
Where x is coordinate of pixel in raw and y coordinate in column. Thanks to the coordinate (rho, θ ) we know the dot to increase.
Once the transformation is made on the entire imag, we find a result which look like at:
hough4
Now, we have to find the dots with high value. In the program, I used a threshold and local maxima detection.
hough5
As we have the coordinates the most important, we can rebuild lines. We saw above that each dot in Hough space give us a line in x and y of the image. The method is to compute for every pixel of image if it correspond or not to the line coordinate issu. In each coordinate, we have a rho and a θ . we compute for every x and y as x=[1 M] and y=[1 N]:
r h o ' = x . cos ( θ ) + y . sin ( θ )
if r h o = r h o ' then we put the result matrix (x,y) to 1, else we let 0.

Program

function guihoug
clear all;
close all;
figure(  'Name','translation',...
            'NumberTitle','off',...
            'color',[0.3137 0.3137 0.5098]);
       
a(1)=axes('units','normalized',...
    'position',[0.05 0.55 0.35 0.35]);
a(2)=axes('units','normalized',...
    'position',[0.5 0.55 0.35 0.35]);
a(3)=axes('units','normalized',...
    'position',[0.05 0.1 0.35 0.35]);
a(4)=axes('units','normalized',...
    'position',[0.5 0.1 0.35 0.35]);
uicontrol(  'style','pushbutton',...
            'string','load',...
            'Position', [10 10 50 20],...
            'callback',@loadimage);
       
uicontrol(  'style','pushbutton',...
            'string','executer',...
            'Position', [290 10 100 20],...
            'callback',@executer);
uicontrol(  'style','edit',...
            'string','10',...
            'Position', [180 10 50 20],...
            'callback',{@nbline,'nline'});
text(1)=uicontrol(  'style','text',...
            'string','number of line: ',...
            'Position', [80 10 100 20]);
%parametre initial
setappdata(gcf,'x',1);
setappdata(gcf,'type',0);
setappdata(gcf,'nline',10);

     
function loadimage(~,~)
    % appeler quand appui check box
    [filename, pathname] = uigetfile({'*.jpg;*.tif;*.png;*.gif;*.bmp','All Image Files';...
          '*.*','All Files' },'mytitle',...
          'C:\Work\myfile.jpg')

    x = imread(filename);
    if (length(size(x))>2)
        x=x(:,:,1);% on prend une seul plan image noir et blanc chaque plan sont egaux
    end
    k = whos('x');
    if k.class == 'uint8'
        k=8;
    end
    setappdata(1,'k',k);
    setappdata(1,'x',x);
    axes(a(1))
    imshow(x)
    title('original image')

   
end
function executer(hObj,~,Name)

     type = getappdata(1,'type');
     img = getappdata(1,'x');
     nline = getappdata(1,'nline');
     [ligne colonne]=size(img);
     centreligne = round(ligne/2);
     centrecolonne = round(colonne/2);
     img=double(img);
     
     
     %détection de contour
     %edge detection
     %imgedge = edge(img,'canny');prewitt
     imgedge = edge(img,'prewitt');
     axes(a(2))
     imshow(imgedge)
     title('edge detection')
     %
     
     theta_step = pi/(ligne);
     r_max = ceil(((ligne^2 + colonne^2)^0.5 / 2)) ;
     A=zeros(ligne,r_max*2+1);%A(theta, r)
     for i = 1 : ligne
         for j = 1 : colonne
             if (imgedge(i,j))==1
                 x=i-centreligne;
                 y=j-centrecolonne;
                 %p=1;
                 for t = 1 : ligne
                     theta = t * theta_step;
                     r = r_max  + 1+round(x * cos(theta) + y * sin(theta));
                    % h(p)=r;
                     A(t,r)= A(t,r) + 1;
                     %p=p+1
                 end  
             end
         end
     end
     
     axes(a(3))
     imshow(uint8(-(A-max(max(A))*(255/max(max(A))))))
     title('hough tranformation')
     threshold = max(max(A))*0.4;
     A(A<threshold)=0;
     A = wextend(2,'zpd',A,2);
     for i = 3 : size(A,1)-3
         for j = 3 : size(A,2)-3
             maxi=max(max(A(i-2:i+2,j-2:j+2)));
             if A(i,j)~=maxi
                A(i,j)=0;
             end
         end
     end
     A=A(3:end-2,3:end-2);
     %A(A>0)=1;
         
     maxlocaux=[]; %[theta r]
     p=1;
     for i = 1 : size(A,1)
         for j = 1 : size(A,2)
             if A(i,j) ~=0
                 maxlocaux(p,:)=[i,j,A(i,j)];
                 p=p+1;
             end
         end
     end
     
     %sort
     [value tri] = sort(maxlocaux(:,3),1,'descend');
     if nline>length(maxlocaux(:,3));
         nline = length(maxlocaux(:,3));
     end
     for i = 1 : nline
             maxlocauxsort(i,:)=maxlocaux(tri(i),1:2);
     end
     
     
     img2=zeros(ligne,colonne);
     for p = 1 : length(maxlocauxsort)
         pointH=maxlocauxsort(p,:);
         for i = 1 : ligne
             for j = 1 : colonne
                 x=i-centreligne;
                 y=j-centrecolonne;
                 %r = r_max  + 1+round( i* cos(-pointH(1)*theta_step) + j * sin(-pointH(1)*theta_step))
                 r = r_max  + 1+round( x* cos(pointH(1)*theta_step) + y * sin(pointH(1)*theta_step));
                 if r == pointH(2)
                     img2(i,j)=1;
                 end
             end
         end
         
     end
     
     
     axes(a(4))
     imshow(img2)
     title('line')
     figure
     imshow(img2)
     
end
function nbline(hObj,~,Name)
    % Called when user activates popup menu
    val = str2num(get(hObj,'String')); %0|1=ligne, 2=colonne, 3=croix, 4=carre
    setappdata(1,Name,val);
end
end

Example


hough6

Copyright © 2010-2014, all rights reserved, contact: operationpixel@free.fr