Matlab EDGE detection

Thread Starter

Mazaag

Joined Oct 23, 2004
255
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 ...

Rich (BB code):
clear ;
imagergb = imread('Block.jpg');
imagegray = rgb2gray(imagergb);
size = size(imagegray);
row = size(1);
column = size(2);
for i=1:row 
   for j=1:column-1 
        if (abs(imagegray(i,j+1) - imagegray(i,j)) > 100) 
            imagenew(i,j)= 0; 
        else 
             imagenew(i,j) = 255; 
        end 
   end
end
 
imshow(imagergb)
pause(5)
imshow(imagenew)
 

scubasteve_911

Joined Dec 27, 2007
1,203
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
 

Thread Starter

Mazaag

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

could someone explain the reason why matlab does this ?

Rich (BB code):
>> image(8,8)
ans =    0
 
>> image(8,7)
ans =    6
 
>> image(8,8) - image(8,7)
 
ans =    0
??
 

Dave

Joined Nov 17, 2003
6,970
I think i found the problem...

could someone explain the reason why matlab does this ?

Rich (BB code):
>> image(8,8)
ans =    0
 
>> image(8,7)
ans =    6
 
>> image(8,8) - image(8,7)
 
ans =    0
??
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
 

Thread Starter

Mazaag

Joined Oct 23, 2004
255
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
 

Dave

Joined Nov 17, 2003
6,970
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:

Rich (BB code):
for j=1:column-1 
    if ((imagegray(i,j+1) - imagegray(i,j)) > 0)
        if (abs(imagegray(i,j+1) - imagegray(i,j)) > 100) 
            imagenew(i,j)= 0; 
        else 
             imagenew(i,j) = 255; 
        end 
    else
        if (abs(imagegray(i,j) - imagegray(i,j+1)) > 100) 
            imagenew(i,j)= 0; 
        else 
             imagenew(i,j) = 255; 
        end 
   end
end
Dave
 

Thread Starter

Mazaag

Joined Oct 23, 2004
255
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
 

Thread Starter

Mazaag

Joined Oct 23, 2004
255
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
 

Dave

Joined Nov 17, 2003
6,970
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
 

Dave

Joined Nov 17, 2003
6,970
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
 
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
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
 
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.


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 ...

Rich (BB code):
clear ;
imagergb = imread('Block.jpg');
imagegray = rgb2gray(imagergb);
size = size(imagegray);
row = size(1);
column = size(2);
for i=1:row 
   for j=1:column-1 
        if (abs(imagegray(i,j+1) - imagegray(i,j)) > 100) 
            imagenew(i,j)= 0; 
        else 
             imagenew(i,j) = 255; 
        end 
   end
end
 
imshow(imagergb)
pause(5)
imshow(imagenew)
_____________________________________-

http://edge.kitiyo.com
Sobel Hardware Edge Detector
 
Top