MATLAB 计算灰度共生矩阵

在MATLAB中计算灰度共生矩阵

在数字图像处理中,灰度共生矩阵(GLCM)是一种用于表示数字图像中成对像素之间空间关系的统计方法。

灰度共生矩阵是一种表示图像中不同像素强度组合方式的方法。它主要用于指定图像的纹理属性,并提供有关图像空间区域内像素值的模式、变化和结构的信息。

什么是灰度共生矩阵

如上所述,灰度共生矩阵(GLCM)是一种统计方法,提供有关图像指定局部区域中像素强度之间关系的信息。

灰度共生矩阵基本上是一个表示图像中像素出现频率的方阵。这种技术在图像处理应用中非常有用,可以获取有关图像中的纹理和模式的信息。

如何在MATLAB中计算灰度共生矩阵

MATLAB提供了一个内置函数’geaycomatrix’,用于计算图像的灰度共生矩阵。

这个函数有以下三种语法形式:

glcm = graycomatrix(I) 
glcm = graycomatrix(I, Name, Value)
[glcm, SI] = graycomatrix(___)

我们将在本文的后续部分介绍所有这些语法。在此之前,让我们了解一下使用MATLAB计算灰度共生矩阵的逐步过程。

计算灰度共生矩阵的过程

下面解释了使用MATLAB从图像计算灰度共生矩阵的逐步过程:

步骤(1) - 读取输入图像并将其转换为灰度版本。

步骤(2) - 为计算灰度共生矩阵定义参数,如像素对之间的距离,角度等。

步骤(3) - 使用“graycomatrix”函数计算灰度共生矩阵。

该灰度共生矩阵可以用于分析图像的纹理和模式特性。

现在,让我们实际了解如何在MATLAB中从图像创建灰度共生矩阵。

使用默认参数计算灰度共生矩阵

在MATLAB中,我们可以使用“graycomatrix”函数的以下语法来使用默认参数从图像创建灰度共生矩阵(GLCM)。

glcm = graycomatrix(I);

其中,I是输入的灰度图像。

示例

现在让我们考虑一个示例程序,以演示在MATLAB编程中实现这种语法的方法。

% MATLAB program to create GLCM with default parameters
% Read the input image
img = imread('https://www.tutorialspoint.com/assets/questions/media/14304-1687425236.jpg');

% Convert the input image to gray scale image
I = rgb2gray(img);

% Compute GLCM with default parameters
glcm = graycomatrix(I);

% Display the GLCM
disp('The GLCM for the input image is:');
disp(glcm);

输出

The GLCM for the input image is:
33  129 41  14  1   0   0   0
135 2874    778 223 84  24  3   0
31  791 22294   3199    329 125 35  1
15  174 3236    28009   3252    394 152 16
3   106 311 3128    14310   2148    317 74
1   42  140 485 1775    12637   2085    200
0   6   59  186 513 1594    24010   2485
0   0   6   40  155 484 2289    136872

使用指定参数计算灰度共生矩阵

在MATLAB中,使用以下语法的’graycomatrix’函数来计算灰度共生矩阵。

glcm = graycomatrix(I, Name, Value, …);

在此语法中,我们使用名称-值对来指定参数。

示例

以下示例演示了在MATLAB编程中实际实现此语法。

% MATLAB program to create GLCM with specified parameters
% Read the input image
img = imread('https://www.tutorialspoint.com/assets/questions/media/14304-1687425236.jpg');

% Convert the input image to gray scale image
I = rgb2gray(img);

% Compute GLCM with default parameters
glcm = graycomatrix(I, 'Offset', [2 1], 'NumLevels', 7);

% Display the GLCM
disp('The GLCM for the input image is:');
disp(glcm);

输出

The GLCM for the input image is:
53  193 33  26  34  53  54
206 4087    1611    406 243 190 283
26  1607    32291   5636    1006    486 352
24  413 5511    20083   3230    844 604
29  236 1051    2957    11465   2442    768
49  201 555 808 1938    20689   3416
57  284 410 811 1073    2974    139777

计算灰度共生矩阵并返回缩放图像

在MATLAB中,使用以下的‘graycomatrix’函数的语法来计算灰度共生矩阵,并返回缩放图像。

[glcm, SI] = graycomatrix(___);

这里,变量’SI’是一个缩放图像,基本上是将输入图像归一化到范围[1,NumLevels]。

例子

让我们看一个例子,以理解在MATLAB编程中如何实现这种语法。

% MATLAB program to create GLCM returning scaled image
% Read the input image
img = imread('https://www.tutorialspoint.com/assets/questions/media/14304-1687425236.jpg');

% Convert the input image to gray scale image
I = rgb2gray(img);

% Compute GLCM with default parameters
[glcm, SI] = graycomatrix(I);

% Display the GLCM and scaled image
disp('The GLCM for the input image is:');
disp(glcm);
disp('The scaled image is:');
imshow(rescale(SI));

输出

The GLCM for the input image is:


The GLCM for the input image is:
33  129 41  14  1   0   0   0
135 2874    778 223 84  24  3   0
31  791 22294   3199    329 125 35  1
15  174 3236    28009   3252    394 152 16
3   106 311 3128    14310   2148    317 74
1   42  140 485 1775    12637   2085    200
0   6   59  186 513 1594    24010   2485
0   0   6   40  155 484 2289    136872

缩放后的图像是:

在MATLAB中计算灰度共生矩阵

结论

这就是关于在MATLAB中创建灰度共生矩阵(GLCM)的所有内容。对于此,MATLAB提供了一个内置函数‘graycomatrix’,可以从图像中计算共生矩阵,以分析图像的纹理特征。

在本教程中,我们解释了灰度共生矩阵的概念,在图像处理中的重要性以及实际示例,以了解如何使用MATLAB计算图像的GLCM。

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程