使用MATLAB进行图像边界提取
在图像中,边界是指将对象与图像背景或图像内两个不同区域分隔开的元素。边界提供了有关图像中存在的对象形状和结构的信息。
边界主要分为以下两种类型:
内边界
它是图像中对象的边界,将其与图像背景隔开。内边界基本上是对象的轮廓,并提供有关对象形状的信息。
因此,通过提取对象的内边界,我们可以识别和分析图像中对象的形状、大小和位置。
在MATLAB中,我们可以通过计算原始图像和腐蚀图像之间的差异来提取图像的内边界,即:
Inner Boundary = Original Image – Eroded Image
其中,腐蚀图像是通过腐蚀收缩原始图像获得的图像。
外边界
包围图像最外围区域或图像中特定感兴趣区域的边界称为外边界。外边界指定图像中多个对象或整个图像本身的轮廓。
因此,外边界主要用于执行各种图像处理任务,例如裁剪、更改或移除背景等。
在MATLAB中,可以通过计算膨胀图像和原始图像之间的差异来获得图像的外边界,即:
Outer Boundary = Dilated Image – Original Image
其中,膨胀图像是通过膨胀获得的原始图像的扩展版本。
在本文中,我们将学习如何使用MATLAB代码提取图像的边界。以下MATLAB程序演示了图像内边界和外边界的提取。
示例
% MATLAB program to extract inner boundary of an image
% Read the input image
img = imread('https://tutorialspoint.com/electrical_machines/images/electrical_machines_logo.jpg');
% Convert the input image to grayscale
gray_img = rgb2gray(img);
% Specify the structuring element to perform erosion of image
se = strel('disk', 2);
% Perform erosion of the grayscale image
eroded_img = imerode(gray_img, se);
% Extract the inner boundary by subtracting the eroded image from gray image
inner_boundary = gray_img - eroded_img;
% Display the original image and the inner boundary
subplot(1, 2, 1); imshow(img); title('Original Image');
subplot(1, 2, 2); imshow(inner_boundary); title('Inner Boundary');
输出
解释
此MATLAB代码演示了图像的内边界提取。在这个MATLAB程序中,我们使用“imread”函数读取输入图像,然后使用“rgb2gray”函数将其转换为灰度图像。
之后,我们创建一个结构元素“se”来执行图像的腐蚀,为此我们使用“strel”函数。在这种情况下,我们创建的结构元素具有半径为2的圆盘形状。接下来,我们使用“imrode”函数执行图像的腐蚀。由于腐蚀,图像内的区域会收缩以消除外边界。
然后,我们通过从原始图像“gray_img”中减去腐蚀图像“eroded_img”来提取内边界,并将结果存储在“inner_boundary”变量中。
最后,我们使用“imshow”函数显示原始图像和内边界。
示例
% MATLAB program to extract outer boundary of an image
% Read the input image
img = imread('https://tutorialspoint.com/electrical_machines/images/electrical_machines_logo.jpg');
% Convert the RGB image to grayscale
gray_img = rgb2gray(img);
% Specify a structuring element to perform dilation of the image
se = strel('disk', 2);
% Perform dilation on the image to expand it
dilated_img = imdilate(gray_img, se);
% Extract the outer boundary by computing the difference between dilated image and original image
outer_boundary = dilated_img - gray_img;
% Display the original image and the outer boundary
subplot(1, 2, 1); imshow(img); title('Original Image');
subplot(1, 2, 2); imshow(outer_boundary); title('Outer Boundary');
输出
解释
在这个MATLAB程序中,我们提取了图像的外边界。为此,我们首先使用“imread”函数读取图像。然后,我们使用“rgb2gray”函数将输入图像转换为灰度图像。接下来,我们定义一个结构元素来对图像执行膨胀。之后,我们使用“imdilate”函数对图像执行膨胀。
接下来,我们通过计算膨胀图像“dilated_img”和原始图像“gray_img”之间的差异来提取外边界,并将结果存储在“outer_boundary”变量中。
最后,我们调用“imshow”函数来显示原始图像和外边界。
结论
这就是我们如何使用MATLAB编程轻松执行图像边界提取的方法。MATLAB允许我们提取图像的内边界和外边界。要提取内边界,则执行图像的腐蚀。而要提取外边界,则执行图像的膨胀。
数据结构
网络
关系数据库管理系统(RDBMS)
操作系统
Java
iOS
HTML
CSS
Android
Python
C语言编程
C++
C#
MongoDB
MySQL
Javascript
PHP