我需要找到图像的中心时刻。中心力矩由以下方程给出:

其中x和y是空间图像坐标,

和

是平均x和y (或质心)坐标,p和q是整数,f(x,y)是图像。
此外,我想了解如何处理f(x,y),因为它将保存所有像素值。
发布于 2014-12-29 14:54:32
使用bsxfun
sz = size( img ); %// assuming image is gray scale - 2D array
x = ( 1:sz(2) );
y = ( 1:sz(1) ).'; %'
x = x - mean(x);
y = y - mean(y);
Mpq = sum( reshape( bsxfun( @times, bsxfun( @times, img, x.^p ), y.^q ), [], 1 ) ); %// computing the p-q moment基准测试:
disp('Solution of rayryeng')
tic
[rows, cols] = size(img);
[X, Y] = meshgrid(1:cols, 1:rows);
X = X(:);
Y = Y(:);
Mpq = sum((X - mean(X)).^p .* (Y - mean(Y)).^q .* img(:));
toc
disp('rayryeng with dot product');
tic
[rows, cols] = size(img);
[X, Y] = meshgrid(1:cols, 1:rows);
X = X(:);
Y = Y(:);
Mpq = ((X.' - mean(X)).^p )* ((Y - mean(Y)).^q .* img(:));
toc
disp('this solution - using bsxfun');
tic
sz = size( img ); %// assuming image is gray scale - 2D array
x = ( 1:sz(2) );
y = ( 1:sz(1) ).'; %'
x = x - mean(x);
y = y - mean(y);
Mpq = sum( reshape( bsxfun( @times, bsxfun( @times, img, x.^p ), y.^q ), [], 1 ) );
toc结果为
Solution of rayryeng
Elapsed time is 0.009426 seconds.
rayryeng with dot product
Elapsed time is 0.008374 seconds.
this solution - using bsxfun
Elapsed time is 0.001404 seconds.如您所见,bsxfun比meshgrid-based解决方案快得多。此外,在sum中使用点积而不是元素乘积更快。
发布于 2014-12-29 15:02:18
f(x,y)只是您在每个列位置y和每个行位置x的图像强度。如果你想计算图像的p-q矩,你可以做Shai suggested,这可能会更快。但是,如果您想要不使用bsxfun的可读性更强的图像,您可以简单地假设您的图像是灰度图像并存储在im中
[rows, cols] = size(im);
im = double(im); %// For precision
[X, Y] = meshgrid(1:cols, 1:rows);
X = X(:);
Y = Y(:);
Mpq = sum((X - mean(X)).^p .* (Y - mean(Y)).^q .* im(:));首先,我们将图像转换为double格式,以确保获得尽可能高的精度。您的图像可能是uint8类型,并且任何超过255的计算值都将被裁剪。您可能会得到超过此值的值,因此建议转换为double。然后,我们使用meshgrid生成具有相同图像大小的空间坐标网格,然后将坐标展开为单个向量。这将使计算矩变得更容易。最后一行代码最终根据方程式计算出我们的矩。我们还需要以相同的方式展开图像,以便元素正确排列。
https://stackoverflow.com/questions/27684478
复制相似问题