MATLAB Answers

Remove parts from Image and Fill Parts

1 view (last 30 days)
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

Sign in to comment.

Accepted Answer

Image Analyst
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

  1 Comment

Sign in to comment.

More Answers (0)

Sign in to answer this question.