0

So I'm trying to do a full-scale contrast stretch to a grayscale image, and I'm getting some really strange answers. I've identified the problem, just not the solution.

Taking a test point, I(2,2) = 108.

(I(2,2) - 57)

Ans = 51, as you'd expect. Now here's the weird part:

(I(2,2) - 57)*255

Ans = 255.

If I do 51 * 255 explicitly, I get the correct answer (13005). What am I doing wrong?

Divakar
  • 218,885
  • 19
  • 262
  • 358
Andrew M
  • 85
  • 1
  • 7
  • 7
    You are using `uint8`, so its clipping at 255 (max for UINT8). – Divakar Feb 11 '15 at 19:04
  • So how do I tell the program to not use uint8 for that line? – Andrew M Feb 11 '15 at 19:07
  • 3
    Convert it to `double` and then do those operations? – Divakar Feb 11 '15 at 19:10
  • @rayryeng: That doesn't make it off-topic. Either answer it or close it as a duplicate of another question about saturating arithmetic with MATLAB integral types. Or just ignore it. But don't close it with a reason that doesn't apply. – Ben Voigt Feb 11 '15 at 20:20
  • @BenVoigt - I understand. My apologies. I'll find a suitable duplicate then. – rayryeng Feb 11 '15 at 20:23
  • 1
    Do what @Divakar suggested and cast your array / image to double via `I = double(I);`, then do the calculations. You are undergoing what is known as clipping in MATLAB. – rayryeng Feb 11 '15 at 20:28
  • Possible duplicate of http://stackoverflow.com/questions/25771469/multipage-tiff-write-in-matlab-doesnt-work – Divakar Feb 11 '15 at 20:31

0 Answers0