1

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?

Zetland
  • 569
  • 2
  • 11
  • 25
  • 2
    Could you post sample input and output image as well, please? Do it so that people can just copy and paste your code to run it. Make it easy to run your code. You will have a much higher chance of getting a useful answer if you do.. – kkuilla Mar 09 '15 at 11:41
  • @Zetland - what are the datatypes of your image? Are they double (pixels values 0..1) or uint8 (pixel values 0..255)? Are thresh, meanpic, stdpic, IMAGE all the same type or different types? – cxw Mar 09 '15 at 18:12
  • @cxw They're from 0 to 255. I've since edited the above code to turn `t = zeros(size(IMAGE));` into `t = uint8(zeros(size(IMAGE)));`, although now the program is producing an image which appears to be identical to the one produced by `immean()`... – Zetland Mar 09 '15 at 20:04
  • Got it. The problem was caused by all the casting I was doing. Once I removed all the `uint8`s and `uint32`s and added `Agrey = double(Agrey);`, the program worked. – Zetland Mar 13 '15 at 17:21
  • @Zetland glad to hear it! Would you please hit the checkmark by the answer to mark it as Accepted? Thanks! – cxw Mar 16 '15 at 15:23

1 Answers1

1

The thresh is reassigned each time through the loop. Thus only one pixel in the neighborhood contributes to each output pixel. If changing thresh doesn't help, would you please post the whole file here or on pastebin? The walkthrough format is very useful, but it's always possible the bug is in the code not shown.

Edit: at lines 163-165 of the paste, change uint8to double. At line 211, add code to use a pixel count or something similar. I think the division by R is happening in uint and truncating to zero. Change back to uint at the very end, after the math, or else imshow won't give you the results you expect. Hope this helps!

cxw
  • 16,685
  • 2
  • 45
  • 81
  • Thank you very much. I have now posted it to Pastebin here: http://pastebin.com/SuyrsSQa – Zetland Mar 12 '15 at 10:43
  • Just noticed an error in the `main` method. `imshow(imthreshold(Agrey, 2, 0.5))` should not be commented out, whereas the `subplot` ones should be... – Zetland Mar 12 '15 at 10:44
  • Thank you. Did you find anything? – Zetland Mar 12 '15 at 15:19
  • Didn't have a chance to read the whole thing today. I did notice immean appears to be doing math in uint8, which will truncate fractions. I'd say slap a `whos` at the end of `immean` and `imadaptive` and make sure anything that you want to carry a fractional value is `double`. I'll keep looking over the next day or two. – cxw Mar 13 '15 at 00:28
  • @zet Updated. Any news on your end? – cxw Mar 14 '15 at 10:11