Borrar filtros
Borrar filtros

Remove image region inside another region

3 visualizaciones (últimos 30 días)
MARIA RODRIGUEZ SANZ
MARIA RODRIGUEZ SANZ el 27 de Nov. de 2018
Comentada: MARIA RODRIGUEZ SANZ el 10 de Dic. de 2018
Hello!
Does anyone have an idea of how can I remove some regions surrounded by another region? I would like to set the value of these pixels to 0. Both are already segmented and labelled.
This is an example imagesc:
Captura de pantalla 2018-11-27 a las 17.05.14.png
Thank you very much in advanced!
Maria

Respuestas (3)

Image Analyst
Image Analyst el 27 de Nov. de 2018
You could just fill holes in the binarized labeled image, then relabel if you want
binaryImage = imfill(labeledImage > 0, 'holes');
[binaryImage, numberOfBlobs] = bwlabel(binaryImage);
Change connectivity from 4 to 8 or vice versa to see what effect that has.
Attach your labeled image in a .mat file if you need more help.
  7 comentarios
MARIA RODRIGUEZ SANZ
MARIA RODRIGUEZ SANZ el 7 de Dic. de 2018
No way! I tried 'holes' and 'noholes' options, but none of them gives the expected output. :(
I can not believe that there is no option for what I'm looking for...it seems so simple...it would give me an excellent ouptput for my study.
Just to remember, I just want to set the child blobs of bwboundaries (blobs inside blobs) to black. Or, what is the same, fill them with zeros!.
Any other ideas? Please...

Iniciar sesión para comentar.


MARIA RODRIGUEZ SANZ
MARIA RODRIGUEZ SANZ el 27 de Nov. de 2018
Dear Image Analyst,
First of all, thanks a lot for your reply!
In fact, I don't want to fill these holes, but quite the opposite.
In my original grayscale image these subregions correspond -in general- to low intensity regions.
First, I find the edges (The best method I have found to segment adjacent objects in my images. It produces really good results!). Then I delimitate objects by bwboundaries(). (That's because I haven't found any method to fill edges regions...), but it's ok.... The resulting image contains lots of subregions that difficult a lot the subsequent steps (My final purpose is to skeletonize the image and I'm getting messy results because of these undesireable subregions.)
I have to be vey careful playing with thresholds, because I have objects with low intensities that I don't want to loose. That's why it is also complicated to remove those regions with a certain intensity in the original image... (bwpropfilt...).
I know it would not be perfect! but just setting this subregions to 0 would help me a lot!
Captura de pantalla 2018-11-27 a las 18.06.30.png
I attach both my original and labbeled image!
Thank you in advanced again!

Image Analyst
Image Analyst el 7 de Dic. de 2018
Editada: Image Analyst el 7 de Dic. de 2018
I've take your simple blob and made a demo image with some similar ones with different numbers of nested blobs and different levels of nesting. I then made a demo specially for you that removes all inner blobs and leaves only the outermost blob. See attached demo.
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;
%===============================================================================
% Read in gray scale demo image.
folder = pwd; % Determine where demo folder is (works with all versions).
baseFileName = 'index.png';
% Get the full filename, with path prepended.
fullFileName = fullfile(folder, baseFileName);
% Check if file exists.
if ~exist(fullFileName, 'file')
% The file doesn't exist -- didn't find it there in that folder.
% Check the entire search path (other folders) for the file by stripping off the folder.
fullFileNameOnSearchPath = baseFileName; % No path this time.
if ~exist(fullFileNameOnSearchPath, 'file')
% Still didn't find it. Alert user.
errorMessage = sprintf('Error: %s does not exist in the search path folders.', fullFileName);
uiwait(warndlg(errorMessage));
return;
end
end
rgbImage = imread(fullFileName);
% Display the image.
subplot(2, 3, 1);
imshow(rgbImage, []);
title('Original Image', 'FontSize', fontSize, 'Interpreter', 'None');
axis('on', 'image');
hp = impixelinfo();
% Get the dimensions of the image.
% numberOfColorChannels should be = 1 for a gray scale image, and 3 for an RGB color image.
[rows, columns, numberOfColorChannels] = size(rgbImage)
if numberOfColorChannels > 1
% It's not really gray scale like we expected - it's color.
% Use weighted sum of ALL channels to create a gray scale image.
% grayImage = rgb2gray(rgbImage);
% ALTERNATE METHOD: Convert it to gray scale by taking only the green channel,
% which in a typical snapshot will be the least noisy channel.
grayImage = rgbImage(:, :, 2); % Take green channel.
else
grayImage = rgbImage; % It's already gray scale.
end
% Now it's gray scale with range of 0 to 255.
% Display the histogram of the image.
subplot(2, 3, 2);
[counts, binLocations] = imhist(grayImage);
% Suppress bin 1 because it's so tall
counts(1) = 0;
bar(binLocations, counts);
grid on;
title('Histogram of Image', 'FontSize', fontSize, 'Interpreter', 'None');
%------------------------------------------------------------------------------
% Set up figure properties:
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0, 0.04, 1, 0.96]);
% 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')
drawnow;
% Binarize the image
% Get the mask where the region is solid.
binaryImage1 = grayImage > 210;
% Display the image.
subplot(2, 3, 3);
imshow(binaryImage1, []);
title('Initial Binary Image', 'FontSize', fontSize, 'Interpreter', 'None');
axis('on', 'image');
hp = impixelinfo();
drawnow;
% Clear borders
binaryImage2 = imclearborder(~binaryImage1);
% Fill holes.
binaryImage2 = imfill(binaryImage2, 'holes');
% Display the image.
subplot(2, 3, 4);
imshow(binaryImage2, []);
title('Filled Binary Image', 'FontSize', fontSize, 'Interpreter', 'None');
axis('on', 'image');
drawnow;
% Get the background color from the upper left pixel
backgroundIntensity = grayImage(1,1);
% Get the final binary image by erasing the first binary image with the latest mask.
finalMask = binaryImage1 & ~binaryImage2;
% Display the image.
subplot(2, 3, 5);
imshow(finalMask, []);
title('Final Binary Image', 'FontSize', fontSize, 'Interpreter', 'None');
axis('on', 'image');
drawnow;
% Use it to mask the original image.
finalImage = grayImage; % Initialize
finalImage(~finalMask) = backgroundIntensity; % Erase outside the mask.
% Display the image.
subplot(2, 3, 6);
imshow(finalImage, []);
title('Final, Masked Image', 'FontSize', fontSize, 'Interpreter', 'None');
axis('on', 'image');
  1 comentario
MARIA RODRIGUEZ SANZ
MARIA RODRIGUEZ SANZ el 10 de Dic. de 2018
Thank you very much Image Analyst!
It helped me a lot! The secret was in imclearborder!. I love this function! hehe!
thanks! thanks! thanks!

Iniciar sesión para comentar.

Community Treasure Hunt

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

Start Hunting!

Translated by