Question

In a project, I need to perform power() function on RGB matrix in a matlab GUI program, but matlab keeps returning error meesage. Below is the code and the error message

img_src = getappdata(handles.figure_pjimage, 'img_src');
R=img_src(:,:,1);
G=img_src(:,:,2);
B=img_src(:,:,3);
C = 12;
gamma = 0.8;
R1 = C * power(R, gamma);
G1 = C * power(G, gamma);
B1 = C * power(B, gamma);
R2 = power((R1 / C), (1/gamma));
G2 = power((G1 / C), (1/gamma));
B2 = power((B1 / C), (1/gamma));
disp(max(R2));
new_img = cat(3,R2,G2,B2);
axes(handles.axes_dst);
imshow(new_img);

And here is the error message

Integers can only be raised to positive integral powers.

However, when I try to use power() function in command window, this can be done.

>> A = [2,2
2,2]

A =

     2     2
     2     2

>> power(A,0.4)

ans =

    1.3195    1.3195
    1.3195    1.3195

Please tell me if any of you get the solution, thanks.

Was it helpful?

Solution

Probably your RGB-matrices are e.g. in format uint8 or uint16, as this is the output format of image import functions for a lot of file types. As power intends not to violate the format definition, which it would for fractional powers, it throws the error.

So basically you only have to change lines 2-4 to:

R = double( img_src(:,:,1) );
G = double( img_src(:,:,2) );
B = double( img_src(:,:,3) );

and your code should work as desired.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top