フィルターのクリア

How to close small holes inside each blob?

8 ビュー (過去 30 日間)
Warid Islam
Warid Islam 2024 年 4 月 18 日
編集済み: DGM 2024 年 4 月 18 日
Hi,
I have attached two images above. The 140_multiplied.jpg is the original image. I plan to close the small holes inside the regions. I tried the code below. But the result is shown in 140_multiplied_closed.jpg which is not correct as it changes the shape of the regions. Any suggestions would be appreciated. Thank you.
result_img=imread('140_multiplied.jpg');
% Close the holes in the regions of the result image
result_img_filled = imfill(result_img, 'holes');
result_img_closed = imclose(result_img_filled, strel('disk', 5)); % Adjust the disk size as needed
  1 件のコメント
DGM
DGM 2024 年 4 月 18 日
編集済み: DGM 2024 年 4 月 18 日
Morphological closing with imclose() will change the mask shape dependent on the shape of the structuring element.
You can use imfill() to close all holes that aren't connected to the image edge.
If you only want to fill holes below a certain size, you can try something like
% get rid of holes below a certain size
minimumholearea = 25; % area in pixels
mask = ~bwareaopen(~mask,minimumholearea);

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

採用された回答

Cris LaPierre
Cris LaPierre 2024 年 4 月 18 日
編集済み: Cris LaPierre 2024 年 4 月 18 日
I htink the issue is that your image needs to be converted to a grayscale image first. Here's some code I generated using the Image Segmenter app.
img = imread('140_multiplied.jpg');
% Threshold image with global threshold
BW = imbinarize(im2gray(img));
% Fill holes
BW = imfill(BW, 'holes');
% Show results
imshowpair(img,BW,'montage')
If you need the result to be RGB, then the following code will do that.
RGB = img;
RGB(repmat(BW,[1 1 3]))=255;
imshowpair(RGB,img) % Green shows the difference

その他の回答 (0 件)

製品


リリース

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by