How can imfill a objects when they are in the edges of main image?

17 ビュー (過去 30 日間)
Mohammed Alammar
Mohammed Alammar 2019 年 11 月 26 日
編集済み: Image Analyst 2020 年 11 月 24 日
hi,
I have Two rectangles 200X300 and 300X100 in the edges as shown below. I need to imfill in side the objects edge shape only. I have try use the regular imfill but doesn't work with me. Could you please help me??p.jpg
  4 件のコメント
KALYAN ACHARJYA
KALYAN ACHARJYA 2019 年 11 月 26 日
編集済み: KALYAN ACHARJYA 2019 年 11 月 26 日
Please share the complete code, which generates attached figure.
Mohammed Alammar
Mohammed Alammar 2019 年 11 月 26 日
yes sir, I want to fill the objects to be white.

サインインしてコメントする。

採用された回答

Image Analyst
Image Analyst 2019 年 11 月 26 日
編集済み: Image Analyst 2020 年 11 月 24 日
Close the contours then fill. Assuming the lines reach the edge lines:
% Make top line white:
col1 = find(binaryImage(1, :), 1, 'first')
col2 = find(binaryImage(1, :), 1, 'last')
binaryImage(1, col1:col2) = true;
% Make bottom line white:
col1 = find(binaryImage(end, :), 1, 'first')
col2 = find(binaryImage(end, :), 1, 'last')
binaryImage(end, col1:col2) = true;
% Make right line white:
row1 = find(binaryImage(:, end), 1, 'first')
row2 = find(binaryImage(:, end), 1, 'last')
binaryImage(row1:row2, end) = true;
binaryImage = imfill(binaryImage, 'holes');
If the lines don't reach the edges, then you'd need to use bwmorph() to find endpoints, then use bwlabel() to identify separate/distinct blobs, then construct a line between the two end points of each blob using polyfit(). Then write those lines into the image, then finally call imfill(). This is a more complicated algorithm but certainly doable in a few lines of code.
Post your original image (not a screenshot) if you still have trouble.
  2 件のコメント
Santiago Acosta
Santiago Acosta 2020 年 11 月 24 日
Thank you for the clear answer Image Analyst, it's really helpful!! However, I should note that the function imfind seems to not exist anymore (if it ever did), find( ) works just as well.
Image Analyst
Image Analyst 2020 年 11 月 24 日
Santiago: Sorry, I must have gotten confused. There never was an imfind(). It should be find(). I must just be used to putting im in front because so many of the Image Processing Toolbox functions have im in front of them. Thanks for the catch and I'll make the correction.

サインインしてコメントする。

その他の回答 (2 件)

KALYAN ACHARJYA
KALYAN ACHARJYA 2019 年 11 月 26 日
編集済み: KALYAN ACHARJYA 2019 年 11 月 26 日
Its because of white area touches the border. Apply imclearborder on binary image, then only apply imfill

Turlough Hughes
Turlough Hughes 2019 年 11 月 26 日
You could put a border on the image with value 1, then run the filter and remove the border to get original dimensions:
I=padarray(testiamge,[1 1],1,'both');
test=edge(I,'canny');
outputs = imfill(test,'holes');
outputs([1 end],:)=[]; outputs(:,[1 end])=[];
imshow(outputs)
Also, this might also be an option depending on your requirements:
outputs=~testimage;

カテゴリ

Help Center および File ExchangeFeature Detection and Extraction についてさらに検索

タグ

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by