【发布时间】:2018-01-01 23:07:46
【问题描述】:
我想用这样的图像提取类似矩形的形状(有些可能在一侧有三角形延伸);
我在 MATLAB 中所做的是;
BW=imread('Capture2.JPG');
BW=~im2bw(BW);
SE = strel('rectangle',[1 6]);
BW = ~imclose(BW,SE);
BW2 = imfill(~BW,'holes');
figure
imshow(~BW)
figure
imshow(BW2);
s = regionprops(BW,'BoundingBox','Area','PixelIdxList');
s = s(2:end);
[lab, numberOfClosedRegions] = bwlabel(BW);
figure
imshow(BW)
for i=1:numel(s)
rec = s(i);
ratio = rec.BoundingBox(4)/rec.BoundingBox(3);%height/width
% ratio > 0.3 && ratio < 51.6 && rec.Area > 1100 && rec.Area < 22500
% if ratio > 0.16
rectangle('Position',s(i).BoundingBox,'EdgeColor','r','LineWidth',2);
text(rec.BoundingBox(1),rec.BoundingBox(2),num2str(i),'fontsize',16);
% end
end
我想出的是;
正如所见,有一些区域作为文本的一部分找到,块内部的形状(索引 = 3)和非块区域(索引 = 11)。我需要丢弃内部区域和非阻塞区域。
另一个问题是,由于区域是由白色区域定义的,我需要获取块的黑色边框,以便我可以捕获块本身,而不是内部的白色区域。我该如何解决这些问题?
我都试过反转图像并使用方法但没有成功。
编辑:代码改进和附加图片
其中一个图像可以是这样的,包括非矩形但感兴趣的对象形状(最左边)。
另一个问题,如果图像不如应有的好,线被认为是开放的,尤其是对角线和 1px 宽的线,这会导致 regionprops 错过它们。
代码改进;
close all;
image=imread('Capture1.JPG');
BW = image;
BW = ~im2bw(BW);
SE = strel('rectangle',[3 6]);
BW = ~imclose(BW,SE); % closes some caps to be closed region
r = regionprops(BW,'PixelIdxList');
BW(r(1).PixelIdxList) = 0; %removes outermost white space allowing to connection lines disapear
se = strel('rectangle',[6 1]);
BW = imclose(BW,se);% closes some caps to be closed region
BW = imfill(BW,'holes');
s = regionprops(BW,{'Area', 'ConvexArea', 'BoundingBox','ConvexHull','PixelIdxList'});
%mostly the area and convex area are similar but if convex area is much greater than the area itself it is a complex shape like concave intermediate sections then remove
noidx = [];
for i=1:numel(s)
rec = s(i);
if rec.Area*1.5 < rec.ConvexArea
BW(rec.PixelIdxList) = 0;
noidx(end+1)=i;
end
end
s(noidx)=[];
%没有剩余区域的条件 数字 显示(BW)
for i=1:numel(s)
rec = s(i);
ratio = rec.BoundingBox(4)/rec.BoundingBox(3);%height/width
% ratio > 0.3 && ratio < 51.6 && rec.Area > 1100 && rec.Area < 22500
% if ratio > 0.16
rectangle('Position',s(i).BoundingBox,'EdgeColor','r','LineWidth',2);
text(rec.BoundingBox(1),rec.BoundingBox(2),num2str(i),'fontsize',16,'color','red');
% end
end
结果是;
优势是所有剩余区域都是区域,如果兴趣没有例外并且没有区域限制等条件,因为图像大小可以不同因此区域。
但即使这样也不适用于第二张图片。由于块下方的文本(总是如此 -> 第一张图像被清除以上传)并且最左侧块的对角线尖端被视为开放线。
【问题讨论】:
-
矩形的线条是否可能比噪点更粗?
-
别想了。这个图像不是我的创作。我需要为此努力。所以它就是这样。 ://
-
部分解决方案是迭代检查中心是否在另一个框内。
-
您还可以将矩形扩展 1 个像素,然后检查有多少黑色像素与您的矩形相交。如果 nbr_of_black_pixel 删除矩形。
-
@obchardon:无法获得它的用途。这对我说的是什么 nbr_of_black_pixel
标签: matlab image-processing image-segmentation regions