为什么我会得到这个错误 - MATLAB

时间:2013-04-16 20:01:09

标签: vector matlab

我有图像和矢量

a = imread('Lena.tiff');
v = [0,2,5,8,10,12,15,20,25];

和这个M档案

function y = Funks(I, gama, c)
[m n] = size(I);
for i=1:m
    for j=1:n
        J(i, j) = (I(i, j) ^ gama) * c;
    end
end
y = J;
imshow(y);

当我尝试这样做时:

f = Funks(a,v,2)

我收到此错误:

??? Error using ==> mpower
Integers can only be combined with integers of the same class, or scalar doubles.

Error in ==> Funks at 5
        J(i, j) = (I(i, j) ^ gama) * c;

有人可以帮我吗?

3 个答案:

答案 0 :(得分:0)

错误是由于您尝试将数字提升为矢量功率而引起的。翻译(即用函数调用中的实际参数替换形式参数),它将类似于:

J(i, j) = (a(i, j) ^ [0,2,5,8,10,12,15,20,25]) * 2

元素功率.^也不会起作用,因为你会试图将一个矢量“卡住”到一个标量容器中。

稍后修改:如果您想将每个伽玛应用于图片,可能此循环更直观(虽然效率最高):

a = imread('Lena.tiff');        % Pics or GTFO
v = [0,2,5,8,10,12,15,20,25];   % Gamma (ar)ray -- this will burn any picture

f = cell(1, numel(v));  % Prepare container for your results

for k=1:numel(v)
    f{k} = Funks(a, v(k), 2);  % Save result from your function
end;
% (Afterwards you use cell array f for further processing)

或者您可以查看此处发布的其他(更有效,如果可能不是更清晰)解决方案。

稍后(呃?)编辑:如果你的tiff文件是CYMK,那么imread的结果就是MxNx4颜色矩阵,必须以不同的方式处理(因为它是三维的)。

答案 1 :(得分:0)

你想做什么在数学上没有任何意义。您正尝试将向量分配给号码。你的问题不是MATLAB编程,而是你要做的事情的定义。

如果您尝试制作多张图片J,每张图片都对应于应用于图片的某个gamma,则应按以下步骤操作:

function J = Funks(I, gama, c)
[m n] = size(I);

% get the number of images to produce
k = length(gama);

% Pre-allocate the output
J = zeros(m,n,k);

for i=1:m
    for j=1:n
        J(i, j, :) = (I(i, j) .^ gama) * c;
    end
end

最后,您将获得图片J(:,:,1)J(:,:,2)

如果这不是你想要做的,那么先找出你的方程式。

答案 2 :(得分:0)

我会遵循两种方式:

1)arrayfun

results = arrayfun(@(i) I(:).^gama(i)*c,1:numel(gama),'UniformOutput',false);
J = cellfun(@(x) reshape(x,size(I)),results,'UniformOutput',false);

2)bsxfun

results = bsxfun(@power,I(:),gama)*c;
results = num2cell(results,1);
J = cellfun(@(x) reshape(x,size(I)),results,'UniformOutput',false);