我如何在Matlab中实现这些方程?

时间:2015-07-25 15:24:46

标签: image matlab image-processing

我试图实施一篇论文。在其中我需要计算图像的重心和二阶矩。

重心和二阶矩的方程分别如下:

enter image description here

我在Matlab中尝试使用我理解的p(x,y)编码时遇到的问题是图像的像素,但是我遇到了y所代表的问题以及我将如何实现在sum函数中。这是我对第一个等式的实现,但由于我没有将y合并到那里,我确定给出的结果是错误的。

img = imread(path);
m = numel(img);
cog = sum(img(:))/m;

2 个答案:

答案 0 :(得分:1)

我认为,m应该是y的最大值,因为f2x的函数,这意味着在Matlab中它应该是一个向量。

尝试使用此代码实现f2:

img = magic(10)
m = 10;
temp = 0;
for y = 1:m
    temp = temp+y*img(:,y);
   %temp = temp+y*img(y,:);  % depends on your image coordinates system
end
f2 = temp/m

答案 1 :(得分:1)

尝试使用以下使用矢量化匿名函数的代码。

% Read the image into an array (3 dimensions).
% Note: you may need to convert to doubles
img = im2double(imread(path));

% Get the size (may need to switch m and n).
[m, n, o] = size(img);

% Create y vector
y = 1:m;

% Create functions (not sure how you want to handle the RGB values).
f2 = @(x, p) sum(y.*p(x,:,1)/m);
f3 = @(x, p) sum(y.^2.*p(x,:,1)/(m^2));

% Call the functions
x = 10; % Some pixel x position
f2_result = f2(x, img);
f3_result = f3(x, img);

注意:我可能会根据图像的方向切换x和y。如果是这种情况,那么就这样切换:

[n, m, o] = size(img);
...
f2 = @(x, p) sum(y.*p(:,x)/m);
etc...

我没有上班,所以我无法运行im2double功能(没有图书馆),但我认为它会有效。