如何在matlab中对RGB矩阵执行power()函数

时间:2013-12-28 10:36:53

标签: image-processing matlab matlab-figure

在一个项目中,我需要在matlab GUI程序中对RGB矩阵执行power()函数,但是matlab会不断返回错误消息。 以下是代码和错误消息

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);

这是错误消息

Integers can only be raised to positive integral powers.

但是,当我尝试在命令窗口中使用power()函数时,可以这样做。

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

A =

     2     2
     2     2

>> power(A,0.4)

ans =

    1.3195    1.3195
    1.3195    1.3195

请告诉我你是否有人得到解决方案,谢谢。

1 个答案:

答案 0 :(得分:2)

可能你的RGB矩阵是例如格式为uint8uint16,因为这是许多文件类型的图像导入功能的输出格式。由于power打算不违反格式定义(它对分数幂而言),它会抛出错误。

所以基本上你只需要将2-4行改为:

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

并且您的代码应该按照需要运行。