用matlab找图像的重心可以参考以下的代码:
I = imread('1.jpg');
I = rgb2gray(I);
imshow(I);
I = double(I);
[rows,cols] = size(I);
x = ones(rows,1)*[1:cols];y = [1:rows]'*ones(1,cols);
area = sum(sum(I));
meanx = sum(sum(I.*x))/area;
meany = sum(sum(I.*y))/area;
hold on;
plot(meanx,meany,'r+'); %十字标出重心位置
扩展资料:
常用函数
fix(x):无论正负,舍去小数至相邻整数
floor(x):下取整,即舍去正小数至相邻整数
ceil(x):上取整,即加入正小数至相邻整数
rat(x):将实数x化为多项分数展开
plot: x轴和y轴均为线性刻度(Linear scale)
loglog: x轴和y轴均为对数刻度(Logarithmic scale)
semilogx: x轴为对数刻度,y轴为线性刻度
semilogy: x轴为线性刻度,y轴为对数刻度
sin(x):正弦函数
cos(x):余弦函数
tan(x):正切函数
asin(x):反正弦函数
cosh(x):双曲余弦函数
tanh(x):双曲正切函数
参考资料来源:百度百科-MATLAB
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Function Developed by Fahd A. Abbasi.
% Department of Electrical and Electronics Engineering, University of
% Engineering and Technology, Taxila, PAKISTAN.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% Function takes a picture as an argument (suitably should contain only one
% object whose centroid is to be obtained) and returns the x and y
% coordinates of its centroid.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% USAGE (SAMPLE CODE)
%
% pic = imread('ic.tif');
% [x,y] = ait_centroid(pic);
% x
% y
% imshow(pic); pixval on
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [meanx,meany] = ait_centroid(pic)
[x,y,z] = size(pic); % Checking whether the picture is colored or monochromatic, if colored then converting to gray.
if(z==1)
;
else
pic = rgb2gray(pic);
end
im = pic;
[rows,cols] = size(im);
x = ones(rows,1)*[1:cols]; % Matrix with each pixel set to its x coordinate
y = [1:rows]'*ones(1,cols); % " " " " " " " y "
area = sum(sum(im));
meanx = sum(sum(double(im).*x))/area;
meany = sum(sum(double(im).*y))/area;
参考资料: http://www.mathworks.com/matlabcentral/fileexchange/5457