Is there a way in MATLAB to calculate which areas of a discrete image are enclosed or enclosed in another region?

Given the following image:

Regions

I would like to determine which color areas are enclosed in other color areas or enclose in them. How can this be calculated? Is there a way to create some kind of tree or table displaying this information?

Example: all red pixels are within the yellow area.

+4
source share
1 answer

There is no built-in function that I know that can perform this calculation, but there is an idea here how you can get the information you need ...

RGB . :

img = double(imread('nested_regions.png'))./255;  % Load the RGB image
map = unique(reshape(img, [], 3), 'rows');        % Find the unique colors
labelImage = rgb2ind(img, map);                   % Get a labeled (i.e. indexed) image
nColors = size(map, 1);

, , "" , imfill. , , , . setdiff:

contains = cell(nColors, 1);              % Storage for the contained region labels
str=' # | contains\n---+------------\n';  % String for displaying output

for iColor = 1:nColors

  maskImage = (labelImage == iColor-1);      % Mask of the current region
  filledImage = imfill(maskImage, 'holes');  % Mask with holes filled
  holeImage = (filledImage & ~maskImage);    % Mask of the filled holes
  contains{iColor} = setdiff(unique(labelImage(holeImage)), ...
                             unique(labelImage(~holeImage))).';  %.'
  str = [str ' ' num2str(iColor-1) ' | ' num2str(contains{iColor}) '\n'];

end

imshow(labelImage, map, 'InitialMagnification', 60);  % Display image
colorbar();                                           %   with a colorbar
fprintf(str);  % Create some formatted text output

:

 # | contains
---+------------
 0 | 1  2  3  4  5  6  7  8  9
 1 | 3  4  5  7  9
 2 | 3  4  5  7  9
 3 | 
 4 | 3
 5 | 3  4
 6 | 
 7 | 3  4  5
 8 | 
 9 | 3  4  5  7

enter image description here

, ( 7) 3, 4 5 (-, ). , 6 (-) 8 (). 1 () 2 (), .

, !

+4

All Articles