3

I'm taking a matlab programming class, and the instructor told us to always write

img = double(imread('file.tif'));

rather than

img = imread('file.tif');

because omitting the double(...) could "cause errors". I'm confused about why these errors occur.

Here's an example interactive session where I take the square value of each pixel (note - input images are grayscale), and the value is only correct when we use double:

>> img = imread('cat.tif');
>> img_double = double(imread('cat.tif'));
>> img = img .^2;
>> img_double = img_double .^2;
>> img(1:10, 1:10)

ans =

  255  255  255  255  255  255  255  255  255  255
  225  255  255  255  255  255  255  255  255  255
  255  255  255  255  255  255  255  255  255  255
  255  255  255  255  255  255  255  255  255  255
  255  255  255  255  255  255  255  255  255  255
  255  255  255  255  255  255  255  255  255  255
  255  255  255  255  255  255  255  255  255  255
  255  255  255  255  255  255  255  255  255  255
  255  255  255  255  255  255  255  255  255  121
  255  255  255  255  255  255  255  255  255   49

>> img_double(1:10, 1:10)

ans =

  Columns 1 through 5

       16129       16129       16129       16129       16129
         225         361         625        1089        1681
         324         484         961        1681        2209
         400         729        1369        2401        3025
         576        1225        2209        3249        3969
         900        1849        3025        4096        4900
        1369        2809        4096        5184        5625
        2209        3969        5476        6241        6241
        3249        5041        6400        7056        6724
        4489        6241        7569        7569        6724

  Columns 6 through 10

       16129       16129       16129       16129       16129
        2025        2209        2209        1849        1521
        2809        2809        2401        2025        1521
        3249        3249        2809        2025        1369
        3969        3481        2809        1849        1089
        4624        3969        2809        1681         841
        5041        3969        2601        1369         529
        5184        3721        2209         961         289
        5184        3481        1681         576         121
        5041        2809        1225         289          49

Why is this happening?

Cam
  • 14,930
  • 16
  • 77
  • 128
  • 3
    Your instructor should really be giving you better explanations than *it will cause errors*. You're taking the class to learn, not memorize magic recipes. The answer below explains why you need to do the cast, but if I were you I'd still ask the instructor for an explanation. If nothing else, it'll be a gauge of whether he / she knows the reason themselves. – Praetorian Mar 26 '13 at 03:17
  • Not casting the image to double is a common mistake in matlab image processing. Try typing `1.7+uint8(5)` and see what results you get. Matlab will cast the answer automatically to `uint8`. – JustinBlaber Mar 26 '13 at 03:43

1 Answers1

6

You don't need to read images as a double in MATLAB. By default, an image is a matrix of elements of type uint8, which have a range of 0 to 255. Depending on your computation needs, you can cast your image to a variety of types (uint16, uint32, uint64, double, etc…).

In your case, squaring the values of uint8 elements is causing overflow, and that's why you are seeing the maximum value of 255.

ielashi
  • 245
  • 2
  • 8