{matlab}取二值图像centroid几种方法性能比较
试验很简单,取二值图像的质心,三种方法做比较
1.完全采用矩阵性能不做任何循环操作,对find后的值进行除法与取余操作,从而得到centroid
2.完全采用循环操作,最简单明了
3.结合1,2,对每一列进行find,省去与除法与取余的计算时间
分析
试验证明
-
矩阵小的情况下,2是最优的,1与3差不多是2的三倍
-
矩阵中大情况下,三种方法时间一样
-
当处理大矩阵时,方法3具有明显的优势,1,2差不多是3的两倍倍时间
所以我采用了方法2,简单明了,效率也可以。
测试代码
M = 9000; N = 1700; A = randn(M,N); A = A>0.0; %%%%%%%%%%%%%%%%%%%% 方法1 %%%%%%%%%%% fprintf('\n1: '); tic b = find(A>0); c = mod(b,M); x1 = sum(c)+numel(find(c==0))*M; y1 = sum(ceil(b./double(M))); n1 = numel(b); toc %%%%%%%%%%%%%%%%%%%% 方法2 %%%%%%%%%%% fprintf('2: '); tic sx = 0; sy = 0; n2 = 0; for i = 1:M for j = 1:N if A(i,j) > 0 sx = sx + i; sy = sy + j; n2 = n2 + 1; end end end x2 = sx; y2 = sy; toc %%%%%%%%%%%%%%%%%%%% 方法3 %%%%%%%%%%% fprintf('3: '); tic sx = 0; sy = 0; for j = 1:N c = find(A(:,j)>0); sy = sy + numel(c)*j; sx = sx + sum(c); end x3 = sx; y3 = sy; toc fprintf('(%d,%d,%d),(%d,%d,%d),(%d,%d)\n',x1,y1,n1,x2,y2,n2,x3,y3);