Search code examples
matlabimage-processingedge-detection

why this contour detection code doesn't work properly?


I wrote a code based on the first part of this paper (on contour detection). However, the image that my code produces is not like the one shown in the paper. I'm new to image processing and because of this I thought maybe there is something that I don't understand fully.
I'm going to write what paper said and how I implemented it so you can see if there is any misunderstanding.
The paper said:

We propose to use the local method which examines illumination changes within the chosen window n * n . We usually use 3 * 3 window and we divided the image into many overlapping regions of that size. For each of those regions we calculated mean and standard deviation of the pixel intensity values in 8-pixel neighborhood.

For this part I wrote:

e=imread('1.jpg');
p=rgb2gray(e);
p=im2double(p);
h=[1 1 1;
   1 1 1;
   1 1 1;]; 
h=h/9;
u=imfilter(p,h);% average filter
Size=size(e);
n=3;
e=[1 1 1;
   1 1 1;
   1 1 1;]; 
Di=stdfilt(p,e); % standard deviation

I have a problem here: What does 8-pixel neighborhood mean? Is it that (a) I shouldn't use the center pixel of every 3*3 local window, or is it (b) just another term for local window?

Now the rest of algorithm, from the paper:

Then we perform decision if the centre pixel of the examined region belongs to the line or to the background. For the maximum value of the pixel intensity Ihigh, and the minimum value of the pixel intensity in the region Ilow , we calculate the difference S(i,j) such as:
S(i,j)=Ihigh-Ilow
and we compare it to certain threshold value. We propose the usage of mean and standard deviation of pixel intensities in calculation of the threshold value T(i,j) used in contour detection. T=u-k*sd (sd=standard deviation) where k is a certain value.Then the rule for the contour detection is:
g(i,j)=1 if S(i,j)>=T(i,j) and 0 if S(i,j) < T(i,j) In result we obtain the binary image g(i,j) with the detected contours. Moreover, the constant k allows to adjust and change the sensitivity of the edge detection algorithm,"

I wrote this code for this part:

k=1;
Div=k*Di;
t=u-Div;
min=ordfilt2(p,1,ones(3,3));
max=ordfilt2(p,3*3,ones(3,3));
s=max-min;
g=zeros(Size(1),Size(2));
for I=1:Size(1)
    for J=1:Size(2)
        if(s(I,J) >= t(I, J))
            g(I, J) = 1;
        else
            g(I, J) = 0;
        end
    end
end
g=imadjust(g,[0 1],[1,0]);
imshow(g)

I'm not sure about these two lines:

 min=ordfilt2(p,1,ones(3,3));
 max=ordfilt2(p,3*3,ones(3,3);

Based on what paper said, is there anything that I'm missing? or any misunderstandings?

This is the example shown in the paper:

example from paper

and this is what I've got:

own result

original image:

original


Solution

  • I think your result doesn't look that bad. About the differences:

    • You have more noise, but that might be because you have a higher resolution with showing more details on your image. You could try to run a Gaussian filter over your image before finding the contours.
    • You might have many more gray values in your image than in the image of the paper. So more details might be find as contours with this algorithm.
    • Maybe it's just because of a different value for k.

    However, I think you should compare a gray value image of your ear with the result.

    8-pixel-neighborhood in 2d means you don't use the pixel in the middle but all around it. I can not estimate the impact of that to the result compared to using 9 pixels.

    A comment about your code: You could replace

    for I=1:Size(1)
        for J=1:Size(2)
            if(s(I,J) >= t(I, J))
                g(I, J) = 1;
            else
                g(I, J) = 0;
            end
        end
    end
    

    with

    g=zeros(Size);
    g(s>=t)=1;