Remove image region inside another region
3 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
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
0 Kommentare
Antworten (3)
Image Analyst
am 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 Kommentare
Image Analyst
am 7 Dez. 2018
Bearbeitet: Image Analyst
am 7 Dez. 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');
Siehe auch
Kategorien
Mehr zu Red finden Sie in Help Center und File Exchange
Produkte
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!