求质心

  1. clear;clc;close all  
  2. I=imread('img.jpg');   
  3.   
  4. I_gray=rgb2gray(I);  
  5. level=graythresh(I_gray);  
  6.   
  7. [height,width]=size(I_gray);  
  8. I_bw=im2bw(I_gray,level);  
  9.   
  10. for i=1:height %%循环中进行反色  
  11. for j=1:width  
  12.     if I_bw(i,j)==1  
  13.         I_bw(i,j)=0;  
  14.     else I_bw(i,j)=1;  
  15.     end  
  16.       
  17. end  
  18. end  
  19.   
  20. [L,num]=bwlabel(I_bw,8);  
  21. plot_x=zeros(1,num);%%用于记录质心位置的坐标  
  22. plot_y=zeros(1,num);  
  23.   
  24. for k=1:num  %%num个区域依次统计质心位置  
  25.     sum_x=0;sum_y=0;area=0;  
  26.     for i=1:height  
  27.     for j=1:width  
  28.        if L(i,j)==k  
  29.         sum_x=sum_x+i;  
  30.         sum_y=sum_y+j;  
  31.         area=area+1;     
  32.        end  
  33.     end  
  34.     end  
  35.     plot_x(k)=fix(sum_x/area);  
  36.     plot_y(k)=fix(sum_y/area);  
  37. end  
  38.   
  39. figure(1);  
  40. imshow(I_bw);  
  41. for i=1:num  
  42. hold on  
  43. plot(plot_y(i) ,plot_x(i), '*');  
  44. end  
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值