为什么我得到这个误差- MATLAB

时间:2021-04-09 19:44:51

I have the image and the vector

我有图像和向量

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

and this M-file

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

when I'm trying to do this:

当我尝试这么做的时候:

f = Funks(a,v,2)

I am getting this error:

我得到了这个错误:

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

Can anybody help me, with this please?

有人能帮我一下吗?

3 个解决方案

#1


0  

The error is caused because you're trying to raise a number to a vector power. Translated (i.e. replacing formal arguments with actual arguments in the function call), it would be something like:

这个错误是由于你想要把一个数字提升到一个矢量。转换(即在函数调用中用实际的参数替换形式的参数),它会是这样的:

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

Element-wise power .^ won't work either, because you'll try to "stuck" a vector into a scalar container.

Element-wise力量。^也不起作用,因为你会试图“卡”一个向量为一个标量容器。

Later edit: If you want to apply each gamma to your image, maybe this loop is more intuitive (though not the most efficient):

后期编辑:如果你想将每个伽玛应用到你的图像中,也许这个循环更直观(虽然不是最有效的):

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)

Or you may take a look at the other (more efficient if maybe not clearer) solutions posted here.

或者你也可以看看其他发布在这里的解决方案。

Later(er?) edit: If your tiff file is CYMK, then the result of imread is a MxNx4 color matrix, which must be handled differently than usual (because it 3-dimensional).

编辑:如果tiff文件是CYMK,那么imread的结果是一个MxNx4颜色矩阵,它的处理方式必须与通常不同(因为它是三维的)。

#2


0  

There are two ways I would follow:

我可以遵循两种方式:

1) arrayfun

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

2)bsxfun

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

#3


0  

What you're trying to do makes no sense mathematically. You're trying to assign a vector to a number. Your problem is not the MATLAB programming, it's in the definition of what you're trying to do.

你所做的在数学上是没有意义的。你要把一个向量赋给一个数字。你的问题不是MATLAB编程,而是你要做的事情的定义。

If you're trying to produce several images J, each of which corresponds to a certain gamma applied to the image, you should do it as follows:

如果你想要生成几个图像J,每个都对应于应用于图像的某种伽马,你应该这样做:

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

In the end you will get images J(:,:,1), J(:,:,2), etc.

最后你会得到图像J(:,:,1), J(:,:,2)等等。

If this is not what you want to do, then figure out your equations first.

如果这不是你想做的,那么首先计算出你的方程。

#1


0  

The error is caused because you're trying to raise a number to a vector power. Translated (i.e. replacing formal arguments with actual arguments in the function call), it would be something like:

这个错误是由于你想要把一个数字提升到一个矢量。转换(即在函数调用中用实际的参数替换形式的参数),它会是这样的:

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

Element-wise power .^ won't work either, because you'll try to "stuck" a vector into a scalar container.

Element-wise力量。^也不起作用,因为你会试图“卡”一个向量为一个标量容器。

Later edit: If you want to apply each gamma to your image, maybe this loop is more intuitive (though not the most efficient):

后期编辑:如果你想将每个伽玛应用到你的图像中,也许这个循环更直观(虽然不是最有效的):

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)

Or you may take a look at the other (more efficient if maybe not clearer) solutions posted here.

或者你也可以看看其他发布在这里的解决方案。

Later(er?) edit: If your tiff file is CYMK, then the result of imread is a MxNx4 color matrix, which must be handled differently than usual (because it 3-dimensional).

编辑:如果tiff文件是CYMK,那么imread的结果是一个MxNx4颜色矩阵,它的处理方式必须与通常不同(因为它是三维的)。

#2


0  

There are two ways I would follow:

我可以遵循两种方式:

1) arrayfun

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

2)bsxfun

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

#3


0  

What you're trying to do makes no sense mathematically. You're trying to assign a vector to a number. Your problem is not the MATLAB programming, it's in the definition of what you're trying to do.

你所做的在数学上是没有意义的。你要把一个向量赋给一个数字。你的问题不是MATLAB编程,而是你要做的事情的定义。

If you're trying to produce several images J, each of which corresponds to a certain gamma applied to the image, you should do it as follows:

如果你想要生成几个图像J,每个都对应于应用于图像的某种伽马,你应该这样做:

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

In the end you will get images J(:,:,1), J(:,:,2), etc.

最后你会得到图像J(:,:,1), J(:,:,2)等等。

If this is not what you want to do, then figure out your equations first.

如果这不是你想做的,那么首先计算出你的方程。