Matlab EDGE detection

Discussion in 'Programmer's Corner' started by Mazaag, Jan 3, 2008.

  1. Mazaag

    Thread Starter Senior Member

    Oct 23, 2004
    255
    0
    hey guysI was playing aroudn with matlab and images, trying to make a very simple "edge detection" algorithmbasically.. i have a image of a solid 2D block (square).. I read the image and convert it to gray scale, then i go through each pixel in a loop, comparing each pixel with the one adjacent to it ( horizontally) , and if the difference in the values is greater than a threshold, i set that pixel to BLACK (0) , ELSE i set it to WHITE (255).. as to draw an outline on the sides of the block ( not top and bottom)however, when i try the code, I only get an outline on the RIGHT side of the block....i'm staring at the code and I know its something stupid but i can't seem to identify it ...

    Code ( (Unknown Language)):
    1.  
    2. clear ;
    3. imagergb = imread('Block.jpg');
    4. imagegray = rgb2gray(imagergb);
    5. size = size(imagegray);
    6. row = size(1);
    7. column = size(2);
    8. for i=1:row
    9.    for j=1:column-1
    10.         if (abs(imagegray(i,j+1) - imagegray(i,j)) > 100)
    11.             imagenew(i,j)= 0;
    12.         else
    13.              imagenew(i,j) = 255;
    14.         end
    15.    end
    16. end
    17.  
    18. imshow(imagergb)
    19. pause(5)
    20. imshow(imagenew)
    21.  
    22.  
     
  2. scubasteve_911

    Senior Member

    Dec 27, 2007
    1,202
    1
    I think it has something to do with your variable index when you create your new image. Try putting j+1 instead of j for your new image, you'll see that the block was detected, but starting at 1.

    Steve
     
  3. Mazaag

    Thread Starter Senior Member

    Oct 23, 2004
    255
    0
    tried that..still no luck
     
  4. Mazaag

    Thread Starter Senior Member

    Oct 23, 2004
    255
    0
    I think i found the problem...

    could someone explain the reason why matlab does this ?

    Code ( (Unknown Language)):
    1.  
    2. >> image(8,8)
    3. ans =    0
    4.  
    5. >> image(8,7)
    6. ans =    6
    7.  
    8. >> image(8,8) - image(8,7)
    9.  
    10. ans =    0
    11.  
    ??
     
  5. Dave

    Retired Moderator

    Nov 17, 2003
    6,960
    144
    Because the grayscale intensity image cannot be less than 0. What you are doing is saying 0 - 6, which we know should -6, however since the lower limit of the grayscale image is 0 it cannot output a value of -6 (in other words 0 is the closest answer).

    Dave
     
  6. Mazaag

    Thread Starter Senior Member

    Oct 23, 2004
    255
    0
    any ideas on how to go round that?
    I tried creating variables A and B and use B - A but that iddn't work either...

    I believe i should cast the value into a different type ? or is there anohte rway round this
     
  7. Dave

    Retired Moderator

    Nov 17, 2003
    6,960
    144
    You need to check for the condition where the next pixel is less than than the previous -this is the condition that is giving you an error.

    Try:

    Code ( (Unknown Language)):
    1. for j=1:column-1
    2.     if ((imagegray(i,j+1) - imagegray(i,j)) > 0)
    3.         if (abs(imagegray(i,j+1) - imagegray(i,j)) > 100)
    4.             imagenew(i,j)= 0;
    5.         else
    6.              imagenew(i,j) = 255;
    7.         end
    8.     else
    9.         if (abs(imagegray(i,j) - imagegray(i,j+1)) > 100)
    10.             imagenew(i,j)= 0;
    11.         else
    12.              imagenew(i,j) = 255;
    13.         end
    14.    end
    15. end
    16.  
    Dave
     
  8. Mazaag

    Thread Starter Senior Member

    Oct 23, 2004
    255
    0
    okay perfect I got it working

    NOW
    next step..

    i know how to read in images and process them... is it possible to input a video, and treat it as a set of images and process them just like you owuld images ?
    my plan is to take in video input from my webcam (or a mov file to start off with) and perform my edge detection algorithm on it, displaying the output in real time (or close to real time)

    thanks guys
     
  9. Dave

    Retired Moderator

    Nov 17, 2003
    6,960
    144
    What image/video formats are you looking at working with?

    Dave
     
  10. Mazaag

    Thread Starter Senior Member

    Oct 23, 2004
    255
    0
    whichever is easiest (if they're all the same, then the most common format is good)..
    i'm just playing around to experiment..no real application , so whichever is easiest to work with or most common
     
  11. Dave

    Retired Moderator

    Nov 17, 2003
    6,960
    144
    How about MPEGs? You can use MPGREAD. Not quite sure how well it will work with your particular application because I have only had limited exposure to it.

    The other alternative is to use imread (as you have already done) and iteratively read the files one-by-one. If you poll the file directory you can read the images as and when they become available. The imread function is best with JPEG files, but you can also read bitmaps if you like.

    If you are looking for commonality then you cannot go far wrong with JPEGs and MPEGs.

    Dave
     
  12. Dave

    Retired Moderator

    Nov 17, 2003
    6,960
    144
    Further to the above, you could consider AVI files which has the AVIREAD function built into Matlab as opposed to MPGREAD which is a user developed function (please note I am assuming you have access to all the toolboxes I have, which may mean some of these functions are not available to you - if in doubt type "help <function name>" at the Matlab command line).

    Furthermore if you are looking at patching together images into a video you could look at the IM2FRAME function which convert indexed images into a video. You will need to work on how you configure the images before calling IM2FRAME.

    Dave
     
  13. Chronopoils

    New Member

    Mar 30, 2009
    1
    0
    sorry for bumping such an old post but we're using

    mov = mmreader('people2.avi');
    data = read(mov, [100 110]);
    imshow(data);

    and we were wondering how we would be able to call these 10 frames for later use.

    this is for Edge Detection for Motion Tracking which we need to do for a project
     
  14. digitalpbk

    New Member

    Sep 12, 2008
    3
    0
    The code seems to be for horizontal edge detection only.
    You would have to take both horizontal and vertical gradients and then compute the root mean of the value and then threshold. http://edge.kitiyo.com for information on edge detection.


    _____________________________________-

    http://edge.kitiyo.com
    Sobel Hardware Edge Detector
     
  15. isha afzaal

    New Member

    Jun 16, 2009
    1
    0
    i think it would be better if u will add the implementation also in your forum with code
     
  16. digitalpbk

    New Member

    Sep 12, 2008
    3
    0
Loading...