why does the subtraction gives me a rong matrice?

3 views (last 30 days)
Hello Although no one has helped me yet,but still I am hopeful. Here is another problem I ran into during running a MATLAB code: I got a matrix from a gray image(lets name it A); then, when I tried to do " B=A-A.^2" the answer for B was a zero matrix!! (FYI, elements of A are ranged from 0 to 255).(it gives me a zero matrix and then when I try to make a division like C=K/B, it gives me a matrix which all of its elements are 255!!) The subtraction's result is obviously incorrect which makes my code results to be wrong! I can't get why? Could any one help me on this please? :\

Accepted Answer

Guillaume
Guillaume on 13 Mar 2015
Edited: Guillaume on 13 Mar 2015
Your image is of type uint8 which is an integer type that ranges from 0 to 255. Any operation (such as squaring) on uint8 that results in values above 255 are clamped to 255 and any operation (such as subtraction) that results in values below 0 are clamped to 0. Similarly, division is integer division.
To do the operations you want convert your images to double in the range [0 1] using im2double, or if you want to keep your original intensities in the range [0 255], cast to double with double.
A = im2double(A);
B = A - A.^2;
or
A = double(A);
B = A - A.^2;
I'd go with the first option since matlab expects double images to be in the [0 1] range.

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!