matlab图像处理之连通区域求质心

一.确定不规则连通区域的质心

原样本


matlab处理

clear;clc;close all
I=imread('img.jpg'); 

I_gray=rgb2gray(I);
level=graythresh(I_gray);

[height,width]=size(I_gray);
I_bw=im2bw(I_gray,level);

for i=1:height %%循环中进行反色
for j=1:width
    if I_bw(i,j)==1
        I_bw(i,j)=0;
    else I_bw(i,j)=1;
    end
    
end
end

[L,num]=bwlabel(I_bw,8);
plot_x=zeros(1,num);%%用于记录质心位置的坐标
plot_y=zeros(1,num);

for k=1:num  %%num个区域依次统计质心位置
    sum_x=0;sum_y=0;area=0;
    for i=1:height
    for j=1:width
       if L(i,j)==k
        sum_x=sum_x+i;
        sum_y=sum_y+j;
        area=area+1;   
       end
    end
    end
    plot_x(k)=fix(sum_x/area);
    plot_y(k)=fix(sum_y/area);
end

figure(1);
imshow(I_bw);
for i=1:num
hold on
plot(plot_y(i) ,plot_x(i), '*');
end
处理后的样本



至此,使用matlab找出了不规则连通区域的质心点.

转载请注明出处.

  • 11
    点赞
  • 87
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值