How to fill inside of the object?

Hello every one,
I have one hand image(binary), which after doing segmentation i got the image with edges and now i want to fill inside of the hand which can see in the second following image.
I used imfill but it didnt change anything.
I will appreciate if anyone can help me to do it.
thanks

 Respuesta aceptada

Image Analyst
Image Analyst el 6 de Jul. de 2015
You need to draw a line across the bottom to close off the arm before you fill it.
% Make tempoprary line.
binaryImage(end, :) = true;
% arm is sealed off. Now we can fill.
binaryImage = imfill(binaryImage, 'holes');
% Erase temporary line.
binaryImage(end,:) = false;

6 comentarios

Try this:
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;
binaryImage = imread('low.jpg');
[rows, columns, numberOfColorChannels] = size(binaryImage);
if numberOfColorChannels > 1
% It's not really gray scale like we expected - it's color.
% Convert it to gray scale by taking only the green channel.
binaryImage = binaryImage(:, :, 2); % Take green channel.
end
% Display the original gray scale image.
subplot(2, 2, 1);
imshow(binaryImage, []);
title('Original Binary Image', 'FontSize', fontSize, 'Interpreter', 'None');
% Crop image to get rid of white border.
binaryImage = binaryImage(33:585, 88:801) > 128;
subplot(2, 2, 2);
imshow(binaryImage, []);
title('Cropped Binary Image', 'FontSize', fontSize, 'Interpreter', 'None');
% Make temporary line.
binaryImage(end, :) = true;
% arm is sealed off. Now we can fill.
binaryImage = imfill(binaryImage, 'holes');
subplot(2, 2, 3);
% Erase temporary line.
binaryImage(end,:) = false;
imshow(binaryImage)
title('Filled Binary Image', 'FontSize', fontSize, 'Interpreter', 'None');
If this works, you can officially "Accept this answer".
Image Analyst
Image Analyst el 8 de Jul. de 2015
If it has no white border, simply get rid of the imcrop() call. I think that perhaps the white border same from how you saved the image and won't be in there in your original code. If the border will sometimes be there and sometimes not, and if it's there might be of varying size, then you just have to sum vertically and horizontally and see if the sum is 255 times the number of rows or columns in the image, and then crop based on that, but I don't think you'll need to do that.
This seems to work fine. I'm just taking the whole image instead of cropping out the middle portion of it:
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;
binaryImage = imread('IM.jpg');
% Display the original color image.
subplot(2, 2, 1);
imshow(binaryImage, []);
title('Original Color Image', 'FontSize', fontSize, 'Interpreter', 'None');
[rows, columns, numberOfColorChannels] = size(binaryImage);
if numberOfColorChannels > 1
% It's not really gray scale like we expected - it's color.
% Convert it to gray scale by taking only the green channel.
binaryImage = binaryImage(:, :, 2); % Take green channel.
end
% Threshold gray scale image to make it binary.
binaryImage = binaryImage > 128;
subplot(2, 2, 2);
imshow(binaryImage, []);
title('Binary Image', 'FontSize', fontSize, 'Interpreter', 'None');
% Make temporary line.
binaryImage(end, :) = true;
% arm is sealed off. Now we can fill.
binaryImage = imfill(binaryImage, 'holes');
subplot(2, 2, 3);
% Erase temporary line.
binaryImage(end,:) = false;
imshow(binaryImage)
title('Filled Binary Image', 'FontSize', fontSize, 'Interpreter', 'None');
farhad:
See code below that starts with your grayscale image in your mat file:
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 = 20;
% Load "original" image. Evidently it's not really the original.
s = load('grayscale.mat')
grayImage = s.IBWS;
% Display the original gray scale image.
subplot(2, 2, 1);
imshow(grayImage, []);
title('Original Grayscale Image', 'FontSize', fontSize, 'Interpreter', 'None');
% Set up figure properties:
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0 0 1 1]);
% Get rid of tool bar and pulldown menus that are along top of figure.
set(gcf, 'Toolbar', 'none', 'Menu', 'none');
% Give a name to the title bar.
set(gcf, 'Name', 'Demo by ImageAnalyst', 'NumberTitle', 'Off')
% Let's compute and display the histogram.
[pixelCount, grayLevels] = imhist(uint8(grayImage));
subplot(2, 2, 2);
bar(grayLevels, pixelCount); % Plot it as a bar chart.
grid on;
title('Histogram of original image', 'FontSize', fontSize, 'Interpreter', 'None');
xlabel('Gray Level', 'FontSize', fontSize);
ylabel('Pixel Count', 'FontSize', fontSize);
xlim([0 grayLevels(end)]); % Scale x axis manually.
% Threshold the image
binaryImage = grayImage > 20;
% Display the image.
subplot(2, 2, 3);
imshow(binaryImage, []);
title('Binary Image', 'FontSize', fontSize, 'Interpreter', 'None');
% Make temporary line.
binaryImage(end, :) = true;
% The arm is sealed off. Now we can fill.
binaryImage = imfill(binaryImage, 'holes');
% There are small noise blobs. Extract the largest one.
binaryImage = bwareafilt(binaryImage, 1);
subplot(2, 2, 4);
% Erase temporary line.
binaryImage(end,:) = false;
% Display the image.
imshow(binaryImage)
title('Filled Binary Image', 'FontSize', fontSize, 'Interpreter', 'None');
Image Analyst
Image Analyst el 9 de Jul. de 2015
You must have an old version of MATLAB where bwareafilt() had not been introduced yet. See my attached demo where you can use your version and use my function ExtractLargestNBlobs.
To smooth the boundary you can use imclose() or activecontour() (demo attached).
Image Analyst
Image Analyst el 10 de Jul. de 2015
Not sure what you did but it works just great with my ExtractNLargestBlobs() function. No problems whatsoever. Working code is attached.

Iniciar sesión para comentar.

Más respuestas (1)

Jórdan Venâncio Leite
Jórdan Venâncio Leite el 28 de Feb. de 2020

0 votos

Hello ImageAnalyst! how to draw a line across the top of a figure as you did with the bottom side?

4 comentarios

Image Analyst
Image Analyst el 28 de Feb. de 2020
Editada: Image Analyst el 4 de Mzo. de 2020
Just use 1 instead of end
% Make temporary line along the top of the image.
binaryImage(1, :) = true; % Set all of row 1 to true/white/1
Jórdan Venâncio Leite
Jórdan Venâncio Leite el 4 de Mzo. de 2020
Thanks ImageAnalyst! God bless you!
Image Analyst
Image Analyst el 4 de Mzo. de 2020
Editada: Image Analyst el 4 de Mzo. de 2020
Sometimes you need to save the line, then do your fill or whatever, then set it back:
% Save existing line:
originalLine = binaryImage(1, :);
% Make temporary line along the top of the image.
binaryImage(1, :) = true; % Set all of row 1 to true/white/1
% Now do some processing, such as imfill() or whatever...
% Finally restore original line:
binaryImage(1, :) = originalLine;
Jórdan Venâncio Leite
Jórdan Venâncio Leite el 11 de Mzo. de 2020
You're the guy!

Iniciar sesión para comentar.

Categorías

Community Treasure Hunt

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

Start Hunting!

Translated by