Matlab统计棋盘连通分量(空值区域)的大小和个数

一、基础设定

在20x20的棋盘矩阵中,设定黑子为1,空值为0

Matlab统计棋盘连通分量(空值区域)的大小和个数_第1张图片

Matlab统计棋盘连通分量(空值区域)的大小和个数_第2张图片

现需要统计空值(连通分量)的大小和个数并标记,如下图所示:

Matlab统计棋盘连通分量(空值区域)的大小和个数_第3张图片

Matlab统计棋盘连通分量(空值区域)的大小和个数_第4张图片

Matlab统计棋盘连通分量(空值区域)的大小和个数_第5张图片

以此类推

其中,最大的连通分量如下:

Matlab统计棋盘连通分量(空值区域)的大小和个数_第6张图片

二、Matlab程序

以下是红色标记连通分量大小为6的Matlab程序

clear,clc,close all
m = 20;
n = 20;
markersize = floor(80-(max([m n])));
rng('default');
A = randn(m,n);
A(abs(A)<=1) = 1;
A(abs(A)>1) = 0;
Ar = 1-A;
cc4 = bwconncomp(Ar,4);
L4 = labelmatrix(cc4);

counts = [];
for i = 1:numel(cc4.PixelIdxList)
    idx = cc4.PixelIdxList{1,i};
    Ar(idx) = numel(cc4.PixelIdxList{1,i});
    counts(i) = numel(cc4.PixelIdxList{1,i});
end
size=flip(unique(counts));

fprintf('编号\t\t连通分量大小\t\t个数\n');
for i = 1:numel(size)
    fprintf('%d\t\t\t%d\t\t\t%d\n',i,size(i),sum(counts==size(i)));
end
fprintf('连通分量总个数:%d\n',cc4.NumObjects);

mark = 6; % 需要标记的连通分量大小
Ar(Ar~=mark)=0;
fprintf('原始棋盘矩阵:\n');
disp(A); % 黑子为1,空值为0
fprintf('红色标记的棋盘矩阵:\n');
disp(Ar); % 红子为1,其余为0
go(A,markersize,m,n,[],[],[])
go(A,markersize,m,n,Ar,mark,sum(counts==mark))

function go(A,markersize,m,n,Ar,mark,count)
    figure('color',[1 1 1],'position',[600,100,400*1.5,400*1.5]);
    hold on
    spy(A,'k',markersize);
    spy(Ar,'r',markersize);
    axis([1 n 1 m]);
    xticks([1:n]);
    yticks([1:m]);
    xlabel("红色标记的连通分量大小为:"+mark+",个数为:"+count);
    ax = gca;
    ax.GridColor = [0 .5 .5];
    ax.GridLineStyle = '-';
    ax.GridAlpha = 0.5;
    ax.Layer = 'top';
    ax.XRuler.TickLabelGapOffset = 20;
    ax.YRuler.TickLabelGapOffset = 20;
    ax.YAxis.Label.Visible = 'off';
    grid on
end

三、输出结果

编号		连通分量大小		个数
1			10			1
2			8			1
3			7			2
4			6			4
5			5			2
6			4			2
7			3			3
8			2			12
9			1			22
连通分量总个数:49
原始棋盘矩阵:
     1     1     1     0     0     1     0     1     1     0     1     0     0     0     1     1     0     0     1     0
     0     0     1     1     1     1     1     1     1     1     0     0     1     0     1     1     1     1     1     1
     0     1     1     1     1     1     0     0     0     1     1     0     1     1     1     1     1     1     0     1
     1     0     1     0     0     1     0     1     0     1     1     1     1     1     0     1     1     1     1     1
     1     1     1     0     1     1     1     1     0     1     1     0     1     1     1     1     1     1     1     0
     0     0     1     1     1     1     1     1     1     0     1     1     0     0     1     1     1     0     1     0
     1     1     1     0     1     1     0     0     0     0     1     1     1     1     1     1     1     1     1     1
     1     1     1     1     1     1     1     1     1     0     1     1     0     0     1     1     1     1     0     1
     0     1     0     0     1     0     1     0     1     1     1     1     1     1     0     1     0     1     1     1
     0     1     0     0     0     1     0     1     1     1     1     1     1     1     0     1     0     1     1     1
     0     1     1     1     0     0     1     1     0     1     1     1     1     1     1     1     1     1     0     1
     0     0     1     1     1     1     1     1     0     1     0     0     1     0     0     1     0     1     1     1
     1     0     0     1     1     0     0     1     0     1     1     1     1     0     0     1     1     1     1     1
     1     1     0     1     0     0     1     1     1     1     1     1     1     1     1     0     0     0     1     0
     1     0     1     1     1     1     1     0     1     1     1     1     1     1     1     0     1     0     1     1
     1     0     0     0     1     1     1     1     1     1     0     1     1     1     1     1     1     1     1     1
     1     1     1     0     1     0     0     1     1     0     1     1     0     0     0     0     0     1     1     0
     0     1     1     1     0     0     0     0     1     1     1     0     0     1     1     1     0     1     1     1
     0     0     1     1     1     1     1     1     1     1     0     1     0     1     0     1     0     1     0     1
     0     0     0     1     0     0     1     1     1     1     1     0     1     1     0     1     1     1     0     1

红色标记的棋盘矩阵:
     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0
     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0
     0     0     0     0     0     0     6     6     6     0     0     0     0     0     0     0     0     0     0     0
     0     0     0     0     0     0     6     0     6     0     0     0     0     0     0     0     0     0     0     0
     0     0     0     0     0     0     0     0     6     0     0     0     0     0     0     0     0     0     0     0
     0     0     0     0     0     0     0     0     0     6     0     0     0     0     0     0     0     0     0     0
     0     0     0     0     0     0     6     6     6     6     0     0     0     0     0     0     0     0     0     0
     0     0     0     0     0     0     0     0     0     6     0     0     0     0     0     0     0     0     0     0
     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0
     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0
     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0
     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0
     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0
     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0
     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0
     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0
     0     0     0     0     0     6     6     0     0     0     0     0     0     0     0     0     0     0     0     0
     6     0     0     0     6     6     6     6     0     0     0     0     0     0     0     0     0     0     0     0
     6     6     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0
     6     6     6     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0

Matlab统计棋盘连通分量(空值区域)的大小和个数_第7张图片

Matlab统计棋盘连通分量(空值区域)的大小和个数_第8张图片

Matlab统计棋盘连通分量(空值区域)的大小和个数_第9张图片

你可能感兴趣的:(matlab,棋盘,连通分量,Matlab)