caozuo.m

来自「用matlab语言 分别判断出三个圆的圆心」· M 代码 · 共 110 行

M
110
字号
RGB=imread('D:\test2.jpg');
BW=im2bw(RGB,0.4);
BW2=bwmorph(BW,'close',Inf);
BW3=bwfill(BW,'holes');
BW4=bwmorph(BW3,'dilate');
subplot(2,2,1);
imshow(BW);title('原始图像');
subplot(2,2,2);
imshow(BW2);title('闭合运算图像');
subplot(2,2,3);
imshow(BW3);title('运算图像');
subplot(2,2,4);
imshow(BW4);title('运算图像2');

PicSize = size(BW4);
iTypePix = zeros(PicSize(1),PicSize(2));
nPointClass = 0;
nPointCount = zeros(100,1);

for i = 11 : PicSize(1) - 10
    for j = 11 : PicSize(2) - 10
        if BW4(i, j) == 1
            Type = GetNearType(iTypePix, i, j);
            if Type == 0
                nPointClass = nPointClass + 1;
                iTypePix(i, j) = nPointClass;
                nPointCount(nPointClass) = 1;
            else
                iTypePix(i, j) = Type;
                nPointCount(int16(Type)) = nPointCount(Type) + 1;
            end
        end
    end
end

MaxNumber = max(nPointCount);
for i = 1:100
    if nPointCount(i) == MaxNumber
        MaxCount = i;
        nPointCount(i) = 0;
    end
end

SubMaxNumber = max(nPointCount);
for i = 1:100
    if nPointCount(i) == SubMaxNumber
        SubMaxCount = i;
        nPointCount(i) = 0;
    end
end

TribMaxNumber = max(nPointCount);
for i = 1:100
    if nPointCount(i) == TribMaxNumber
        TribMaxCount = i;
        nPointCount(i) = 0;
    end
end

TotalX1 = 0;
TotalY1 = 0;
TotalX2 = 0;
TotalY2 = 0;
TotalX3 = 0;
TotalY3 = 0;

for i = 1 : PicSize(1)
    for j = 1 : PicSize(2)
        if iTypePix(i, j) == MaxCount
            TotalX1 = TotalX1 + j;
            TotalY1 = TotalY1 + i;
        elseif iTypePix(i, j) == SubMaxCount
            TotalX2 = TotalX2 + j;
            TotalY2 = TotalY2 + i;
        elseif iTypePix(i, j) == TribMaxCount
            TotalX3 = TotalX3 + j;
            TotalY3 = TotalY3 + i;
        end
    end
end

X1 = TotalX1 / MaxNumber;
Y1 = TotalY1 / MaxNumber;

X2 = TotalX2 / SubMaxNumber;
Y2 = TotalY2 / SubMaxNumber;

X3 = TotalX3 / TribMaxNumber;
Y3 = TotalY3 / TribMaxNumber;

Distance1 = sqrt((X2 - X1)*(X2 - X1) + (Y2 - Y1)*(Y2 - Y1));
Distance2 = sqrt((X3 - X2)*(X3 - X2) + (Y3 - Y2)*(Y3 - Y2));
Distance3 = sqrt((X3 - X1)*(X3 - X1) + (Y3 - Y1)*(Y3 - Y1));
















⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?