在MATLAB中有没有办法计算哪些离散图像区域被另一个区域包围或包围? [英] Is there a way in MATLAB to compute which discrete image regions enclose or are enclosed by another region?

查看:187
本文介绍了在MATLAB中有没有办法计算哪些离散图像区域被另一个区域包围或包围?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给出以下图片:





我想确定哪些彩色区域被其他彩色区域包围或包围。怎么计算这个?有没有办法创建一种显示此信息的树或表?



示例:所有红色像素都在黄色区域内。

解决方案

我知道没有可以执行此计算的内置函数,但是这里有一个关于如何获取所需信息的想法。 。



首先,您需要从上方拍摄RGB图像并将其转换为索引图像和颜色图。这是一种方法:

  img = double(imread('nested_regions.png'))./ 255; %加载RGB图像
map = unique(reshape(img,[],3),'rows'); %找到独特的颜色
labelImage = rgb2ind(img,map); %获取带标签(即索引)的图像
nColors = size(map,1);

接下来,您需要遍历每个标记区域,创建一个掩码,然后填写任何使用



例如,红色像素(标记为区域7)围绕标记区域3,4和5中的所有像素(分别为灰蓝色,紫色和石灰)。一些区域不形成封闭的轮廓,如6(浅紫色)和8(橙色)。区域1(绿色)实际上并未被区域2(蓝色)完全包含,因为一个伪像素或两个绿色区域在蓝色区域之外。



希望这能给你带来一些想法!


Given the following image:

I'd like to identify which colored regions are enclosed by or enclose which other colored regions. How might this be computed? Is there a way to create a sort of tree or table that shows this information?

Example: All the red pixels are within the yellow region.

解决方案

There's no built-in function I know of that can perform this calculation, but here's an idea for how you might get at the information you want...

First, you'll want to take your RGB image from above and turn it into an indexed image and a color map. Here's one way to do it:

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);

Next, you'll want to loop over each labeled region, create a mask, then fill any "holes" in that mask using 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 function:

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

After running the above, you will get the following:

 # | 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

For example, the red pixels (labeled as region 7) surround all the pixels in labeled regions 3, 4, and 5 (gray-blue, purple, and lime, respectively). Some regions don't form closed contours, like 6 (light purple) and 8 (orange). Region 1 (green) actually isn't fully contained by region 2 (blue) since a spurious pixel or two of green is outside the blue region.

Hope this gives you some ideas!

这篇关于在MATLAB中有没有办法计算哪些离散图像区域被另一个区域包围或包围?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆