我想用像這樣的圖像提取類似矩形的形狀(一些可能在一邊有三角形延伸)如何在MATLAB中丟棄圖像中的非矩形閉區域?
我在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)的內部。我需要丟棄內部區域和非阻塞區域。
另一個問題是,因爲區域由白色區域定義,我需要獲取塊的黑色邊框,以便我可以捕獲塊本身,而不是內部白色區域。我如何解決這些問題?
我都嘗試顛倒圖像和使用方法,但沒有成功。
EDIT:代碼改進&附加圖像
其中一個圖像可以是這樣的,包括非矩形但興趣形狀的物體(最左邊)。
另一個問題,如果圖像不如它應該是有的,視爲開放特別是對角線和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)=[];
%爲剩餘區域 圖 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,'color','red');
% end
end
結果爲否的條件;
優點是所有剩餘的區域是區域如果興趣也不例外和麪積約束等,因爲圖像尺寸可以是不同的因而區域沒有條件。
但即使這不適用於第二個圖像。由於塊下面的文本(始終是這種情況 - >第一個圖像被清除爲上傳),最左邊塊的對角線頂端被認爲是開放線。
難道矩形可以有比噪聲較粗的線? –
不要以爲。這個圖像不是我的創作。我需要爲此努力。所以它就是這樣。 :/ – freezer
部分解決方案是迭代檢查中心是否在另一個盒子內。 – m7913d