MATLAB 中有没有一种方法可以计算哪些离散图像区域包围或被另一个区域包围?
Is there a way in MATLAB to compute which discrete image regions enclose or are enclosed by another region?
给定下图:
我想确定哪些彩色区域被其他彩色区域包围或包围。这可能如何计算?有没有办法创建一种显示此信息的树或 table?
示例:所有红色像素都在黄色区域内。
据我所知,没有内置函数可以执行此计算,但这里有一个关于如何获取所需信息的想法...
首先,您需要从上方获取 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
. If the filled regions contain label values that the rest of the image doesn't, then those regions are completely contained by the region you filled. The code below does this using the setdiff
函数填充该蒙版中的任何 "holes":
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
例如,红色像素(标记为区域 7)围绕标记区域 3、4 和 5(分别为灰蓝色、紫色和石灰)中的所有像素。有些区域不形成闭合轮廓,如 6(浅紫色)和 8(橙色)。区域 1(绿色)实际上并未完全包含在区域 2(蓝色)中,因为一个或两个绿色的虚假像素位于蓝色区域之外。
希望这能给你一些想法!
给定下图:
我想确定哪些彩色区域被其他彩色区域包围或包围。这可能如何计算?有没有办法创建一种显示此信息的树或 table?
示例:所有红色像素都在黄色区域内。
据我所知,没有内置函数可以执行此计算,但这里有一个关于如何获取所需信息的想法...
首先,您需要从上方获取 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
. If the filled regions contain label values that the rest of the image doesn't, then those regions are completely contained by the region you filled. The code below does this using the setdiff
函数填充该蒙版中的任何 "holes":
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
例如,红色像素(标记为区域 7)围绕标记区域 3、4 和 5(分别为灰蓝色、紫色和石灰)中的所有像素。有些区域不形成闭合轮廓,如 6(浅紫色)和 8(橙色)。区域 1(绿色)实际上并未完全包含在区域 2(蓝色)中,因为一个或两个绿色的虚假像素位于蓝色区域之外。
希望这能给你一些想法!