Question

I got a code from the book Feature Extraction & Image Processing. As I am a total beginner in Matlab, I don't know how to run these codes to see results. Are they complete?

First one : Hough Transform for Lines

%Polar Hough Transform for Lines
function HTPLine(inputimage)

%image size
[rows,columns]=size(inputimage);

%accumulator
rmax=round(sqrt(rows^2+columns^2));
acc=zeros(rmax,180);

%image
for x=1:columns
  for y=1:rows
    if(inputimage(y,x)==0)
      for m=1:180
        r=round(x*cos((m*pi)/180)+y*sin(m*pi)/180);
        if(r0) acc(r,m)=acc(r,m)+1; end
      end
    end
  end
end

Second one : Hough Transform for Circles

%Hough Transform for Circles
function HTCircle(inputimage,r)

%image size
[rows,columns]=size(inputimage);

%accumulator
acc=zeros(rows,columns);

%image
for x=1:columns
  for y=1:rows
    if(inputimage(y,x)==0)
      for ang=0:360
        t=(ang*pi)/180;
        x0=round(x-r*cos(t));
        y0=round(y-r*sin(t));
        if(x00 & y00)
          acc(y0,x0)=acc(y0,x0)+1;
        end
      end
    end
  end
end

Third one : Hough Transform for Elipses

%Hough Transform for Ellipses
function HTEllipse(inputimage,a,b)

%image size
[rows,columns]=size(inputimage);

%accumulator
acc=zeros(rows,columns);

%image
for x=1:columns
  for y=1:rows
    if(inputimage(y,x)==0)
      for ang=0:360
        t=(ang*pi)/180;
        x0=round(x-a*cos(t));
        y0=round(y-b*sin(t));
        if(x00 & y0< rows & y0>0)
          acc(y0,x0)=acc(y0,x0)+1;
        end
      end
    end
  end
end

I have images (png) that I need to run these programs with. But I cannot seem to run it. I create new script, paste the code, save it and in main window I run the function name sending path to the image as a parameter. It does nothing, no message or so.

Was it helpful?

Solution

Your functions don't return any value, that means that you have to add a return argument to those functions (in case of the hough trafo, you'd like to return the accumulator array acc) and as described in the manual (http://www.mathworks.de/de/help/matlab/ref/function.html), change the functions headers to:

 function acc = HTPLine(inputimage)

and then also call it like this from the commandline (or from another script):

IMG = imread('some_image.jpg');
% e.g. convert to grayscale:
IMG = rgb2gray(IMG);
acc = HTPLine(IMG);

you then still need to find the maximum (or several maxima, depending on how many lines you'd like to fit) in the accumulator and display the fitted line (ellipse/circle) on your own via plot in a figure...

EDIT: Looking at your code, I don't know what the variable r0 should be!? It will def. give you an errormsesage as it's not defined anywhere.

And why do you the if(inputimage(y,x)==0) in your code? The pixel at the current point doesn't have to be black in ordner to calculate the hough transformation for that single pixel (just think of a grey-image with a white line in it -> your code wouldn't do anything as the image wouldn't contain any black pixels). So that's what you need to rethink about.

If you want to, you could also use the built-in hough transformation of MATLAB: http://www.mathworks.de/de/help/images/ref/hough.html // http://www.mathworks.de/de/help/images/ref/houghpeaks.html

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top