图像处理:连通域质心求取

本文介绍了如何使用MATLAB计算图像中的单连通域和多连通域质心,通过`bwlabel`函数确定连通域并统计每个区域的质心坐标。首先处理单连通域,然后扩展到处理多连通域的情况。
摘要由CSDN通过智能技术生成

第一个例子:求取单连通域的质心
img=imread('bw.tif');
[m,n]=size(img);

L=bwlabel(img);
sum_x=0;sum_y=0;area=0;
for i=1:m
    for j=1:n
        if L(i,j)==1
            sum_x=sum_x+i;  %所有x*f(x)的和
            sum_y=sum_y+j;  %所有y*f(y)的和
            area=area+1;    %面积求和
        end
    end
end

plot_x=fix(sum_x/area);  
plot_y=fix(sum_y/area);
figure();
imshow(img);
hold on
plot(plot_y,plot_x,'*');
第2个例子:求取单连通域的质心

## 例2:多连通域的质心
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);  %num是连通域个数
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
 

  • 7
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值