I'm currently working on a piece of code which prepares an image for thresholding, although there seems to be a mismatch between the output image and the pixel data which is returned.
I'll talk you through the code. First, I define the function which takes three parameters - IMAGE
, the image that will be thresholded; r
the radius which defines a neighbourhood around a central pixel, from which pixel data can be calculated; and k
, the 'kernel' which determines the detail of the thresholding.
function [t] = imadaptive ( IMAGE, r, k )
I then call functions I've previously defined, which create images in which the mean and standard deviation of each pixel is stored:
meanpic = immean (IMAGE, r);
stdpic = imstd (IMAGE, r);
Next, I create a new image t
, where each pixel has an intensity of 0. This will be used to store the appropriate thresholding values for each pixel:
t = zeros(size(IMAGE));
I then work out the size of the image to tell the for-loop when to stop:
[nx, ny] = size(IMAGE);
Next, I start the for-loop and run a series of if-statements to stop the program from trying to inspect pixel values in positions like (-2,-2):
if x-r <= 0
startx = 1;
else
startx = x-r;
end
if x+r > nx
endx = nx;
else
endx = x+r;
end
if y-r <= 0
starty = 1;
else
starty = y-r;
end
if y+r > ny
endy = ny;
else
endy = y+r;
end
Finally, I run the code to work out the work out the appropriate threshold values for each pixel and then add that value to the image t
:
R = 128;
for xp = startx : endx
for yp = starty : endy
if (x-xp)^2 + (y-yp)^2 <= r^2
thresh = meanpic(xp,yp) * (1 + (k * (((stdpic(xp,yp) / R) - 1))));
end
end
end
t(x,y) = thresh;
The problem is: when I run this code, I get an image identical to my stdpic
: the picture which shows the standard deviation values of each image. I can't figure out why this is, though. The equation seems to be correct - and when I remove the ;
from the equation, I see that the values it outputs are very varied and between 0 and 255, so the output image t
should look wildly different.
Can anyone point out what I've got wrong here?