如何使用“regionprops”显示多个连接组件中的一个
How to display a single connected component out of many using `regionprops`
在MATLAB中使用函数regionprops
时,有一个选项可以提取每个连通分量的二值图像。二值图像的大小减小到连通分量的大小。我不想减少二进制图像的大小。我希望二值图像的大小保持其原始大小,同时仅在原始图像大小的相应位置显示所选的连接组件。如何提取原始图像大小的连通分量?
只需创建一个与原始图像大小相同的空白图像,而不是按 blob 提取图像,而是参考每个 blob 的原始图像提取实际像素位置,然后通过设置这些位置来填充空白图像到这个空白图像中的二进制 true
。使用 regionprops
中的 PixelIdxList
属性获取所需组件的列主要位置,然后使用它们将这些相同位置的输出图像设置为 true
.
假设您的 regionprops
结构存储在 S
中并且您想提取第 k
个组件并且原始图像存储在 A
中,请执行以下:
% Allocate blank image
out = false(size(A, 1), size(A, 2));
% Run regionprops
S = regionprops(A, 'PixelIdxList');
% Determine which object to extract
k = ...; % Fill in ID here
% Obtain the indices
idx = S(k).PixelIdxList;
% Create the mask to be the same size as the original image
out(idx) = true;
imshow(out); % Show the final mask
如果您有多个对象并且想要为每个对象分别创建图像原始大小的蒙版,您可以使用 for
循环为您完成此操作:
% Run regionprops
S = regionprops(A, 'PixelIdxList');
% For each blob...
for k = 1 : numel(S)
out = false(size(A, 1), size(A, 2)); % Allocate blank image
% Extract out the kth object's indices
idx = S(k).PixelIdxList;
% Create the mask
out(idx) = true;
% Do your processing with out ...
% ...
end
在MATLAB中使用函数regionprops
时,有一个选项可以提取每个连通分量的二值图像。二值图像的大小减小到连通分量的大小。我不想减少二进制图像的大小。我希望二值图像的大小保持其原始大小,同时仅在原始图像大小的相应位置显示所选的连接组件。如何提取原始图像大小的连通分量?
只需创建一个与原始图像大小相同的空白图像,而不是按 blob 提取图像,而是参考每个 blob 的原始图像提取实际像素位置,然后通过设置这些位置来填充空白图像到这个空白图像中的二进制 true
。使用 regionprops
中的 PixelIdxList
属性获取所需组件的列主要位置,然后使用它们将这些相同位置的输出图像设置为 true
.
假设您的 regionprops
结构存储在 S
中并且您想提取第 k
个组件并且原始图像存储在 A
中,请执行以下:
% Allocate blank image
out = false(size(A, 1), size(A, 2));
% Run regionprops
S = regionprops(A, 'PixelIdxList');
% Determine which object to extract
k = ...; % Fill in ID here
% Obtain the indices
idx = S(k).PixelIdxList;
% Create the mask to be the same size as the original image
out(idx) = true;
imshow(out); % Show the final mask
如果您有多个对象并且想要为每个对象分别创建图像原始大小的蒙版,您可以使用 for
循环为您完成此操作:
% Run regionprops
S = regionprops(A, 'PixelIdxList');
% For each blob...
for k = 1 : numel(S)
out = false(size(A, 1), size(A, 2)); % Allocate blank image
% Extract out the kth object's indices
idx = S(k).PixelIdxList;
% Create the mask
out(idx) = true;
% Do your processing with out ...
% ...
end