Muaz Usmani
Muaz Usmani

Reputation: 1318

Matlab error while finding log

I am trying to find log of base 10 of each pixel of an image in matlab using following code

m1 = imread('owl','pgm');
for x = 1:size(m1,1)
    for y = 1:size(m1,2)
        m1(x,y) = log10(m1(x,y));  
    end
end

here m1 is a 2-D array of order 221 X 201. but I am facing this error

??? Undefined function or method 'log2' for input arguments of type 'uint8'.

Error in ==> log10 at 20
y = log2(x);

Error in ==> q2 at 38
        m1(x,y) = log10(m1(x,y));

but when I debug log function using following code

fprintf('log of 190 is %d', log10(190));

it gives me right output I dont know what happened when I use the same code in the loop.

Upvotes: 1

Views: 3810

Answers (1)

High Performance Mark
High Performance Mark

Reputation: 78324

The error message tells you what the problem is, you've tried to apply the log10 function to a value of type uint8 and the function is not defined for that type of number. What you haven't realised is that imread, when an image file meets certain criteria (read the documentation for what those criteria are) will capture the pixel data into an array of uint8s, not real numbers.

If you want to take the logarithm of a uint8 you'll either have to define a logarithm function of your own which takes such inputs, or, more straightforward, cast the uint8 to a type which log10 is happy with. For example, you could write:

log10(double(m1(x,y)))

And by now you'll have realised why your diagnostic test didn't tell you anything useful, when you execute the command log10(190) Matlab, by default, decides that 190 is of type double and computes the logarithm without complaint. log10(uint8(190)) tells a different story.

Upvotes: 3

Related Questions