如何使用Matlab进行基于对象的图像分析

338 阅读11分钟

使用Matlab进行基于对象的图像分析

基于对象的图像分析是根据图像像素的分类对图像进行处理,以获得基于图像中包含的物体的有用信息。例如,这种数据可以基于高度、物体边缘或物体边界。

Matlab为基于物体的图像分析提供了一个交互式环境,通过执行用于物体基础分析的函数或图像处理的内置应用程序。图像包含有不同区域的物体。这些物体有边界、形状和边缘。

Matlab允许使用图像分析器函数或区域道具对这些属性进行分析,以便从这些图像中获得数据。本文将讨论各种边缘检测方法、边界检测、图像对象的标记以及突出图像中的文本对象。

基于对象的图像分析非常有用,特别是在分析卫星地图、机器视觉、指纹识别以及根据图像中的对象特征获取信息方面。

前提条件

要学习本教程,您需要:

  • 安装[MATLAB]。
  • 对[MATLAB]基础知识的基本了解。

边缘检测

边缘检测是指识别图像中的点。这些点是图像具有极佳对比度的地方,它们是图像的定义点。边缘检测通过检测图像像素的亮度变化来工作。

边缘检测在图像分割和数据提取的比较、物体分离、计算机视觉和机器学习中非常有用。Matlab支持各种有助于边缘检测的功能。

边缘检测的方法

有几种方法用于图像的边缘检测。这些方法与主要函数edge() 。大多数边缘检测方法将在文章中使用相同的输入图像进行演示,然后对输出图像进行比较,找到更合适的边缘检测方法。

边缘检测的语法。

i=edge(grayscale image,'method')

所有这些方法都只接受灰度图像输入,因此,将导入的RGB图像转换为灰度图像是必不可少的。RGB图像到灰度图像的转换是通过函数rgb2gray()

所有图像处理的初始阶段是将图像导入到Matlab工作区。图像的导入是通过函数imread('imagefolderpath') 来完成的。

让我们看一个例子。

i = imread('print.PNG'); %importing the image
figure,imshow(i) %  Displaying the imported image

在上面的代码片断中, ,我们使用函数imread() ,导入图像。

import image

然后使用函数rgb2gray() ,将导入的RGB图像转换为灰度图像。这很重要,因为我们将要讨论的边缘检测方法只接受灰度图像作为输入图像。下面的代码显示了RGB图像到灰度的转换。

i2 = rgb2gray(i) %coverting the imported RGB image to grayscale image
figure,imshow(i2)% Displaying the grayscale image 

Grayscale version  of the imported image

我们将使用这个灰度图像i2 作为输入图像来演示以下边缘检测方法。

  • 边缘检测的Sobel方法。
  • 边缘检测的Prewitt方法。
  • 边缘检测的Canny方法。
  • 边缘检测的对数法。

边缘检测的索贝尔方法

在边缘检测的索贝尔方法中,边缘是由具有最高梯度的点决定的。

这种方法是用函数sobel() ,其语法如下所示。

i_edge = edge(i_gray,'sobel')

为了用索贝尔方法检测边缘,我们使用函数edge() 。然后我们指定灰度图像和用于边缘检测的方法作为参数。例如,在下面的代码片段中,我们的灰度图像是i2 ,然后方法是sobel

因此,当代码被执行时,分配给变量s 的输出图由使用索贝尔方法检测的物体边缘组成。

s = edge(i2,'sobel'); % edge detection using sobel method
figure,imshow(s) %displaying edges detected using sobel method

Edges detected using Sobel method

边缘检测的Prewitt方法

这是一种估计图像中边缘的大小和方向的合适方法。与索贝尔方法相比,它能提供更详细的输出。输出的图像可能会有噪音,这取决于存在的边缘数量和它们的接近程度。这个方法是用函数prewitt() ,其语法如下。

i_edge = edge(i_gray,'prewitt')

下面的代码展示了使用普雷维特方法的边缘检测。显示检测到的结果图像被标记为p ,灰度图像为i2 ,该方法被指定为prewitt

p = edge(i2,'prewitt'); %edge detection using prewitt method
figure, imshow(p) %displaying edges detected using prewitt method

Edges detected using Prewitt method

边缘检测的对数法

这种方法对图像进行平滑处理,然后执行拉普拉斯函数,从而得到一个双刃剑图像。该方法使用函数log() ,语法为。

  i_edge = edge(i_gray,'log')

例子。

l = edge(i2,'log'); %edge detection using log method
figure, imshow(l) %displaying edges detected using log method

上述代码使用log方法植入边缘检测,包含检测到的边缘的结果图像被分配一个变量l 。灰度输入图像被标记为i2 ,边缘检测方法被指定为log

Edge detected using log method

边缘检测的Canny方法

这种方法通过从图像中分离出噪声来检测边缘。这样做比较好,因为它不会干扰到图像中的边缘的特征。这种方法使用函数canny() ,其语法如下所示。

i_edges = edge(i_gray,'canny')

下面的代码片段展示了使用canny方法进行的边缘检测,输出图像被标记为c ,输入灰度图像为i2 ,边缘检测方法被指定为canny

c = edge(i2,'canny'); %edge detection using canny method
figure, imshow(c) %displaying edges detected using canny method

Edge detected using canny method

物体边界检测

边界是标志着一个物体或一个区域的界限的线条。图像中的边界可以通过对图像进行形态学操作或使用工具箱函数来检测。

使用形态学操作的边界检测

形态学操作主要涉及减去二值化图像中只有物体边界的部分。在形态学操作中,可以通过对获得边界的实体进行侵蚀或扩张来检测边界。

以下是使用形态学过程进行边界检测的方法。

  • 形态学侵蚀。
  • 形态学扩张。

通过形态学侵蚀获得边界

在这种方法中,二值化图像的一些像素被侵蚀。然后将被侵蚀的部分从包含主要物体的主图像中减去。剩下的部分就是物体的边界。图像的侵蚀是用函数imerode() ,并指定strel() 长度。下面的代码是对这种边界检测方法的一步步说明。

我们导入用于执行操作的输入图像。使用下面代码中所示的函数imread() ,导入图像。

i = imread(plate.PNG); %importing the image to workspace
figure,imshow(i) %displaying the imported image

Imported image

然后,我们使用函数rgb2gray() ,将导入的RGB图像转换为灰度图像,输出的灰度图像将被指定为i_gray ,如下代码所示。

i_gray = rgb2gray(i); % converting the imported image to grayscale image
figure,imshow(i_gray) %displaying the grayscale image

Grayscale version of the imported image

灰度图像i_gray ,使用一个阈值转换为黑白图像。对于我们的案例,我们将使用250的阈值。黑白图像将被标记为i2 。下面的代码片断演示了这个过程。

i2 = i_gray< 250; %making a black and white image from the grayscale image
figure, imshow (i2) %displaying the black and white image

Black and white image

然后,我们指定用于执行形态学侵蚀的桶的长度。我们将使用一个长度为80 的磁盘来侵蚀黑白图像i2 。侵蚀后的图像将被标记为i_eroded 。下面的代码显示了这个过程。

se1 = strel('disk',80); % specifying strel length for erosion process
i_eroded = imerode(i2,se1); % erosion of the image at a specified strel length
figure, imshow(i_eroded) % Displaying the eroded image

Eroded image

i2 然后通过从黑白图像i_eroded 中减去被侵蚀的部分来获得边界,如以下代码所示。

i_boundary = i2-i_eroded; %subtracting the eroded part of the image
figure , imshow(i_boundary) % displaying the boundary obtained

Boundary of the object by erosion method

通过扩张获得边界

扩张通过在图像的边界上增加一些像素来增加物体边界的大小。增加的像素数取决于被处理物体的大小和形状。

当原始图像的二值化版本从扩张后的图像中减去时,所得到的图像就是目标物体的边界。图像的扩张是使用函数imdilate() ,在二进制图像上有指定的strel() 长度。

输出的图像取决于原始图像的对比度和像素的清晰度。下面是这种边界检测方法中使用的步骤代码。

在下面的代码中所示的函数imread() ,输入的图像被导入。然后将导入的图像分配给一个变量i

i = imread('plate.PNG'); %importing an image
figure,imshow(i) %Displaying the imported image

Imported image

然后,我们使用函数rgb2gray() ,将导入的RGB图像i ,转换成灰度图像i2 ,如下图所示。

i2 = rgb2gray(i); %converting the imported RGB image to a grayscale image
figure,imshow(i2) %Displaying the grayscale image

Grayscale version of the imported image

灰度图像i2 ,然后使用下面代码中所示的函数imbinarize() ,将其转换为二进制图像bw1

bw1 = imbinarize(i2); %binarizing the grayscale image
figure,imshow(bw1) %Displaying the binarized image

Binary image

然后我们指定用于执行形态学扩张的柱状长度。然后用盘长5 ,对二进制图像进行扩张bw1 。扩张后的图像将被分配到i3 ,如下面的代码中所示。

se = strel('disk',5); % specifying strel length
i3 = imdilate(bw1,se); % dilation of the image at a specified strel length
figure, imshow(i3) %Displaying the dilated image

Dilated image

然后,通过将二进制图像bw1 与扩张后的图像i3 相减来获得边界,如以下代码所示。i_boundary 被分配为显示检测到的物体边界的图像的标签。

i_boundary = i3 - bw1; % subtracting some parts of the image
figure,imshow(i4) %Displaying the boundaries obtained 

Boubdary obtained by dilation method

突出图像中的文本

在含有不同文字信息的图像中,这些文字是图像中包含的对象。Matlab提供了突出显示文本图像中的指定单词的功能。

我们将通过在包含随机字母的图像中突出显示 "MATLAB "这个词来演示。locatetext() 函数用于定位要突出显示的文本。

在这个过程中使用了以下代码;我们需要使用imread() 函数导入包含不同单词的图像。如下面的代码片段所示,导入的图像被分配到变量i

i = imread('C:/Users/user/Pictures/TEXT.PNG'); % importing the image
figure,imshow(i) %Displaying the imported image

Imported immage

为了查看导入图像中检测到的字母i ,我们使用函数ocr() 。图像文本数据将被分配给ocrOutput ,如下面的代码片段所示。

ocrOutput = ocr(i) %text data

Image text data

在检测到的文本数据中,使用函数locatetext() ,完成单词的定位。对于我们的案例,条件是ignoreCase ,这意味着无论在什么情况下,指定的单词都会被看到并突出显示。

然后,我们将i2 赋予带有高亮字的图像。突出显示单词是使用函数insertshape() 。然后,我们将使用FilledRectangle 来突出显示指定的单词,如下面的代码所示。

text_location = locateText(ocrOutput, 'Matlab', 'IgnoreCase', true); %specifying text to located
i2 = insertShape(i, 'FilledRectangle', text_location); % highlighting the located text
figure,imshow(i2) %Displaying the highlighted text in the image

Highted text image

标注图像中的物体

含有图像的物体可以使用bwlabel() 函数进行检测和标记。这个函数是在一个二值化的图像中执行的。此外,人们可以通过使用函数label2rgb() ,将bwlabel() 的结果转换为RGB图像来查看标记的对象。

我们首先使用函数imread() 来导入图像。然后将导入的图像分配给变量i

i = imread('capture.PNG'); %importing the image
figure,imshow(i) %Displaying the imported image

Imported image

然后我们将导入的RGB图像i 转换为灰度图像i2 ,如下所示。

i2 = rgb2gray(i); % converting the imported image to grayscale
figure,imshow(i2)%Displaying the grayscale image

Grayscale image

灰度图像i2 ,然后转换为二值化图像i3 ,以便对物体进行标记,如下图所示。

i3 = imbinarize(i2); % binarizing the grayscale image
figure,imshow(i3) %Displaying the binarized image

Binary image

通过在二值化图像i3 ,执行函数bwlabel() ,给物体贴上标签。通过使用函数label2rgb() ,将分配给i3_labelbwlabel() 输出转换为分配给i4 的RGB图像,就可以查看标记的对象。在这个过程中使用了以下代码。

i3_label = bwlabel(i3) % labeling the objects in the binarized image
i4 = label2rgb(i3_label); % converting the labeled objects to RGB image
figure,imshow(i4) %Displaying the labeled objects

The labeled objects

结论

基于物体的图像分析有助于通过提取某些特征,如边界和边缘,将图像中包含的物体从图像的背景部分区分出来。基于物体的图像分析有广泛的应用范围。

它可以应用于以下领域。

  • 机器视觉。
  • 指纹匹配验证。
  • 医学诊断。
  • 地图分析。