Remove image region inside another region
Afficher commentaires plus anciens
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:

Thank you very much in advanced!
Maria
Réponses (3)
Image Analyst
le 27 Nov 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 commentaires
Image Analyst
le 27 Nov 2018
Modifié(e) : Image Analyst
le 27 Nov 2018
Your edges seem to be their own unique regions, but that's okay as long as they're inside the blob, not outside it.
This is what I have so far. Tell me why it's no good. And be specific, like the final blob at location (row, column) is not good for some reason.
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 = 18;
s2 = load('labelled.mat')
s1 = load('original.mat')
imagen_2d_aj = s1.imagen_2d_aj;
subplot(2, 2, 1);
imshow(imagen_2d_aj, []);
axis('on', 'image');
title('Original Image', 'fontSize', fontSize);
labeledImage = s2.mito_fill;
subplot(2, 2, 2);
imshow(labeledImage, []);
axis('on', 'image');
title('Labeled Image', 'fontSize', fontSize);
message = sprintf('Before filling, there are %d blobs.', max(labeledImage(:)));
fprintf('%s\n', message);
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'Outerposition', [0, 0.05, 1, 0.95]);
% Let's assign each blob a different color to visually show the user the distinct blobs.
coloredLabels = label2rgb (labeledImage, 'hsv', 'k', 'shuffle'); % pseudo random color labels
% coloredLabels is an RGB image. We could have applied a colormap instead (but only with R2014b and later)
subplot(2, 2, 3);
imshow(coloredLabels);
axis('on', 'image'); % Make sure image is not artificially stretched because of screen's aspect ratio.
title(message, 'FontSize', fontSize);
% Fill holes.
binaryImage = imfill(labeledImage > 0, 'holes');
% Relabel
[binaryImage, numberOfBlobs] = bwlabel(binaryImage);
caption = sprintf('After filling, there are %d blobs', numberOfBlobs);
fprintf('%s\n', caption);
% Let's assign each blob a different color to visually show the user the distinct blobs.
coloredLabels = label2rgb (labeledImage, 'hsv', 'k', 'shuffle'); % pseudo random color labels
% coloredLabels is an RGB image. We could have applied a colormap instead (but only with R2014b and later)
subplot(2, 2, 4);
imshow(coloredLabels);
axis('on', 'image'); % Make sure image is not artificially stretched because of screen's aspect ratio.
title(caption, 'FontSize', fontSize);

MARIA RODRIGUEZ SANZ
le 28 Nov 2018
Image Analyst
le 28 Nov 2018
Do you just want to remove nested blobs?
Raul Benitez
le 29 Nov 2018
Yes... That would be ok.
Thanks!
Image Analyst
le 29 Nov 2018
Then use bwboundaries() and look at all outputs. One of them has to do with whether or not a boundary is nested inside any others.
MARIA RODRIGUEZ SANZ
le 7 Déc 2018
MARIA RODRIGUEZ SANZ
le 7 Déc 2018
MARIA RODRIGUEZ SANZ
le 27 Nov 2018
0 votes
Image Analyst
le 7 Déc 2018
Modifié(e) : Image Analyst
le 7 Déc 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 commentaire
MARIA RODRIGUEZ SANZ
le 10 Déc 2018
Catégories
En savoir plus sur Image Processing Toolbox dans Centre d'aide et File Exchange
Produits
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!




