![0000 Screenshot.png](https://www.mathworks.com/matlabcentral/answers/uploaded_files/253715/0000%20Screenshot.png)
Remove parts from Image and Fill Parts
3 views (last 30 days)
Show older comments
massimiliano de martino
on 9 Dec 2019
Commented: massimiliano de martino
on 10 Dec 2019
Goodmorning,
I would like to have help about that question:
I have the image_1 which I've attached.After having thresholding and binarized that image as result i've Image_2 (attached).Starting from Binary Image (Image_2) ,I would like to remove filled parts and noise, and I would like to fill remaining empty shapes (Filled Parts,Noise and Empty Shape are clearly reported in the Image_3).
Thanks in advance for your support
Attached also the develpoed code
clear
clc
close all
%%
rgbImage = imread('Foto_1.JPG');
Image_Gray = rgb2gray(rgbImage); % Translete in Gray Scale
%%
figure;grid on;hold on;
subplot(121);
imshow(Image_Gray);
subplot(122)
[counts,binLocations] = imhist(Image_Gray);
bar(binLocations,counts)
%%
thresholdValue = 100; % 100 is the correct Value
binaryImage = Image_Gray > thresholdValue; % Bright objects will be chosen if you use >.
figure;
imshow(binaryImage);
hold off
0 Comments
Accepted Answer
Image Analyst
on 10 Dec 2019
Pretty easy. Threshold, fill holes, take the 3 largest.
Solution below. Adapt as needed.
% Initialization steps.
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 14;
filename = fullfile(pwd, 'image_1.jpg');
grayImage = imread(filename);
if ndims(grayImage) == 3
% It's really an RGB image, not a gray scale image.
% Convert to gray scale
grayImage = rgb2gray(grayImage);
end
subplot(2, 3, 1);
imshow(grayImage, []);
title('Original Image', 'FontSize', fontSize);
% Threshold
subplot(2, 3, 2);
imhist(grayImage);
grid on;
title('Histogram', 'FontSize', fontSize);
mask = grayImage < 128; % or whatever value works
subplot(2, 3, 3);
imshow(mask, []);
title('Initial Binary Image', 'FontSize', fontSize);
% Fill Holes
mask = imfill(mask, 'holes');
subplot(2, 3, 4);
imshow(mask, []);
title('After holes filled', 'FontSize', fontSize);
% Take 3 largest blobs then invert intensity.
mask = ~bwareafilt(mask, 3);
subplot(2, 3, 5);
imshow(mask, []);
title('Final Image Showing 3 Largest', 'FontSize', fontSize);
![0000 Screenshot.png](https://www.mathworks.com/matlabcentral/answers/uploaded_files/253715/0000%20Screenshot.png)
More Answers (0)
See Also
Categories
Find more on Image Processing Toolbox in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!