基于连通分量标记的自动裁剪 (MATLAB实现)


在数字图像处理中,**基于连通分量标记的自动裁剪**是一种裁剪技术,它利用标记的连通分量来确定图像中的不同区域。然后,通过提取每个连通分量的周围区域来自动裁剪图像。

基于连通分量标记的自动裁剪的主要优势在于,它允许分离和提取图像中感兴趣的各个区域。

算法

执行基于连通分量标记的自动裁剪的分步过程如下所示

步骤 1 - 必要时处理图像。此步骤可能包括应用滤波器、对比度调整、RGB 到灰度转换等。

步骤 2 - 创建二值图像。这是通过使用阈值操作完成的。在二值图像中,图像中感兴趣的区域表示为前景。

步骤 3 - 对二值图像执行连通分量标记。

步骤 4 - 通过分析标记的连通分量来查找感兴趣的属性,例如面积、边界框等。在 MATLAB 中,此操作可以使用“regionprops()”函数执行。

步骤 5 - 根据特定条件选择要裁剪的所需连通分量。

步骤 6 - 使用 MATLAB 中的“imcrop()”函数对每个选定的分量执行裁剪。

步骤 7 - 使用 MATLAB 中的“imshow()”函数显示裁剪后的图像。

现在,让我们借助 MATLAB 程序来了解这种基于连通分量标记的图像自动裁剪。

示例

% MATLAB program to demonstrate auto cropping based on labeling the connected components
% Call imread() function to read the input image
img = imread('https://tutorialspoint.com/assets/questions/media/14304-1687425323.jpg');
% Convert the input image to grayscale
grayimg = rgb2gray(img);
% Create a binary image using a threshold
% Set the threshold value as per need
threshold = 0.35;
binaryimg = imbinarize(grayimg, threshold);
% Perform connected component labeling
labeledimg = bwlabel(binaryimg);
% Extract the properties of connected components
prop = regionprops(labeledimg, 'BoundingBox');
% Select and crop each connected component
for i = 1 : numel(prop)
   boundingbox = prop(i).BoundingBox;
   croppedimg = imcrop(img, boundingbox);
   % Display the cropped image
   imshow(croppedimg); title('Cropped Image');
end

结论

在这个 MATLAB 程序中,我们调用“imread”函数读取输入图像。然后,使用“rgb2gray”函数处理输入图像以将其转换为灰度图像。接下来,我们调用“imbinarize”函数使用阈值创建灰度图像的二值图像。然后,我们调用“bwlabel”函数执行连通分量标记。之后,我们使用“regionprops”函数查找连通分量的属性。在这个示例程序中,我们提取了分量的边界框。接下来,我们运行“for”循环来处理每个连通分量,并使用“imcrop”函数从原始图像中裁剪该区域。最后,我们调用“imshow”函数显示裁剪后的图像。

更新于:2023年7月18日

浏览量:76

启动您的职业生涯

完成课程获得认证

开始学习
广告
© . All rights reserved.