【发布时间】:2019-05-24 17:17:36
【问题描述】:
下面的代码使用bwboundaries根据对象的圆度对对象进行分类。
它估计每个物体的面积和周长,并使用这些结果形成一个简单的指标,指示物体的圆度,指标如下:
metric = 4*pi*area/perimeter^2
此度量仅对于圆形等于 1,对于任何其他形状都小于 1。但是在这段代码中,我使用了 0.80 的阈值,这样只有度量值大于 0.80 的对象才会被归类为圆形。
我的问题是,当给定对象归类为圆形时,如何从原始图像img(不是I 或bw)中将其裁剪并保存为新图像?
我认为使用标签矩阵和边界矩阵就足够了,但仍然不知道如何操作它们。
img=imread('cap.png');
I = rgb2gray(img);
% Step 2: Threshold the Image
bw1 = imbinarize(I);
bw = imcomplement(bw1);
% Step 3: Remove the Noise
bw = bwareaopen(bw,30); % remove small objects
bw = imfill(bw,'holes');
% Step 4: Find the Boundaries
[B,L] = bwboundaries(bw,'noholes');
imshow(label2rgb(L,@jet,[.5 .5 .5]))
hold on
for k = 1:length(B)
boundary = B{k};
plot(boundary(:,2),boundary(:,1),'w','LineWidth',2)
end
% Step 5: Determine which Objects are Round
stats = regionprops(L,'Area','Centroid');
threshold = 0.80;
% loop over the boundaries
for k = 1:length(B)
% obtain (X,Y) boundary coordinates corresponding to label 'k'
boundary = B{k};
% compute a simple estimate of the object's perimeter
delta_sq = diff(boundary).^2;
perimeter = sum(sqrt(sum(delta_sq,2)));
% obtain the area calculation corresponding to label 'k'
area = stats(k).Area;
% compute the roundness metric
metric = 4*pi*area/perimeter^2;
% display the results
metric_string = sprintf('%2.2f',metric);
% Test if the current object classified as a round
if metric > threshold
% HERE, I want to crop the current object from the 'img'
% and save it as a new image
end
end
title(['Metrics closer to 1 indicate that ',...
'the object is approximately round'])
【问题讨论】:
标签: image matlab image-processing