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?