首页
学习
活动
专区
圈层
工具
发布
社区首页 >专栏 >选出在正方形中含有不同大小的圆形的图像。

选出在正方形中含有不同大小的圆形的图像。

作者头像
裴来凡
发布于 2022-05-28 15:19:46
发布于 2022-05-28 15:19:46
1.4K0
举报

answerQ7.m

代码语言:javascript
复制
function noCircleWithNoHole = answerQ7( img )
%answerQ7 This function calculate number of circle object with no holes in image
img=imread('C:\Users\xpp\Desktop\FCIS-Image-Processing-Course-s-Tasks--master\Morphology\test.png');
i = rgb2gray(img);
BI = im2bw(i);%convert img to binary

%object  that has hole
n=bwconncomp(BI);%return struct with connected components in binary image
s=regionprops(n,'Eulernumber');%returns a scalar that specifies the number 
%of objects in the region minus the number of holes in those objects 
arr2 = struct2array(s);

%square
BITest = imfill(BI,'holes');
n=bwconncomp(BITest);%return struct with connected components in binary image
s=regionprops(n,'Extent');%divide area of object overarea of bounding box so if object is square then it's 1
arr1 = struct2array(s);
[rows,si] = size(arr1);%rows always 1
noCircleWithNoHole=0;
for i=1:si
    if arr1(i)~=1 && arr2(i)==1
        noCircleWithNoHole = noCircleWithNoHole + 1;
    end
end

end

answerQ6.m

代码语言:javascript
复制
function noSquareWithHole = answerQ6( img)
%answerQ6 This function calculate number of square object with holes in image
img=imread('C:\Users\xpp\Desktop\FCIS-Image-Processing-Course-s-Tasks--master\Morphology\test.png');
i = rgb2gray(img);
BI = im2bw(i);%convert img to binary
%object  that has hole
n=bwconncomp(BI);%return struct with connected components in binary image
s=regionprops(n,'Eulernumber');%returns a scalar that specifies the number 
%of objects in the region minus the number of holes in those objects 
arr2 = struct2array(s);

%square
BITest = imfill(BI,'holes');
n=bwconncomp(BITest);%return struct with connected components in binary image
s=regionprops(n,'Extent');%divide area of object overarea of bounding box so if object is square then it's 1
stat = regionprops(BITest,'boundingbox');%Returns the smallest rectangle containing the region, specified as a 1-by-Q*2 vector, where Q is the number of image dimensions
arr1 = struct2array(s);
noSquareWithHole = 0;
imshow(img); hold on;
for i=1:numel(stat)
    if arr1(i)==1 && arr2(i)~=1
        noSquareWithHole = noSquareWithHole + 1;
        bb = stat(i).BoundingBox;
        rectangle('position',bb,'edgecolor','r','linewidth',2);
    end
end

end
本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2020-05-14,如有侵权请联系 cloudcommunity@tencent.com 删除
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档