p[i][j] = glcm[i][j] / np.sum(glcm[i][j])
时间: 2024-01-04 09:04:40 浏览: 21
This line of code normalizes the values in a gray-level co-occurrence matrix (GLCM) by dividing each element by the sum of all elements in the matrix. GLCM is a statistical method used to analyze the spatial relationship between pairs of pixels in an image. Each element in the matrix represents the frequency of occurrence of a pair of pixel values at a given distance and direction within an image. Normalization is necessary to ensure that the sum of all elements in the matrix is equal to one, which makes it easier to compare GLCMs of different images. The resulting matrix, p, represents the probability distribution of pairs of pixel values in the image.
相关问题
Python求灰度共生矩阵的对比度、相关性、能量、均匀性、熵最大概率、对角线和、对角线差(Sum of Squares: Difference) 自相关、聚类度、聚类倾斜度、反转度、和方差、惯性的代码
以下是Python求解灰度共生矩阵的各个特征值的代码:
```python
import numpy as np
from skimage.feature import greycomatrix, greycoprops
# 生成灰度共生矩阵
def generate_glcm(image, distance, angle):
# 将图像转换为灰度图像
img_gray = np.round(255 * (image / np.max(image)))
img_gray = img_gray.astype('uint8')
# 生成灰度共生矩阵
glcm = greycomatrix(img_gray, [distance], [angle], levels=256, symmetric=True, normed=True)
return glcm
# 计算灰度共生矩阵的对比度
def calc_contrast(glcm):
contrast = greycoprops(glcm, prop='contrast')[0, 0]
return contrast
# 计算灰度共生矩阵的相关性
def calc_correlation(glcm):
correlation = greycoprops(glcm, prop='correlation')[0, 0]
return correlation
# 计算灰度共生矩阵的能量
def calc_energy(glcm):
energy = greycoprops(glcm, prop='energy')[0, 0]
return energy
# 计算灰度共生矩阵的均匀性
def calc_homogeneity(glcm):
homogeneity = greycoprops(glcm, prop='homogeneity')[0, 0]
return homogeneity
# 计算灰度共生矩阵的熵
def calc_entropy(glcm):
entropy = -np.sum(glcm * np.log2(glcm + (glcm == 0)))
return entropy
# 计算灰度共生矩阵的最大概率
def calc_max_probability(glcm):
max_probability = np.max(glcm)
return max_probability
# 计算灰度共生矩阵的对角线和
def calc_sum_of_squares(glcm):
sum_of_squares = np.sum(glcm ** 2)
return sum_of_squares
# 计算灰度共生矩阵的对角线差
def calc_difference(glcm):
difference = np.sum(np.abs(np.arange(glcm.shape[0]) - np.arange(glcm.shape[1])) ** 2 * glcm)
return difference
# 计算灰度共生矩阵的自相关
def calc_autocorrelation(glcm):
x, y = np.meshgrid(np.arange(glcm.shape[0]), np.arange(glcm.shape[1]))
mean_x = np.sum(x * glcm)
mean_y = np.sum(y * glcm)
std_x = np.sqrt(np.sum((x - mean_x) ** 2 * glcm))
std_y = np.sqrt(np.sum((y - mean_y) ** 2 * glcm))
autocorrelation = np.sum((x - mean_x) * (y - mean_y) * glcm) / (std_x * std_y)
return autocorrelation
# 计算灰度共生矩阵的聚类度
def calc_cluster_prominence(glcm):
x, y = np.meshgrid(np.arange(glcm.shape[0]), np.arange(glcm.shape[1]))
mean_x = np.sum(x * glcm)
mean_y = np.sum(y * glcm)
std_x = np.sqrt(np.sum((x - mean_x) ** 2 * glcm))
std_y = np.sqrt(np.sum((y - mean_y) ** 2 * glcm))
cluster_prominence = np.sum((x + y - mean_x - mean_y) ** 4 * glcm) / (std_x ** 4 * std_y ** 4)
return cluster_prominence
# 计算灰度共生矩阵的聚类倾斜度
def calc_cluster_shade(glcm):
x, y = np.meshgrid(np.arange(glcm.shape[0]), np.arange(glcm.shape[1]))
mean_x = np.sum(x * glcm)
mean_y = np.sum(y * glcm)
std_x = np.sqrt(np.sum((x - mean_x) ** 2 * glcm))
std_y = np.sqrt(np.sum((y - mean_y) ** 2 * glcm))
cluster_shade = np.sum((x + y - mean_x - mean_y) ** 3 * glcm) / (std_x ** 3 * std_y ** 3)
return cluster_shade
# 计算灰度共生矩阵的反转度
def calc_inverse_difference(glcm):
inverse_difference = np.sum(glcm / (1 + np.abs(np.arange(glcm.shape[0]) - np.arange(glcm.shape[1]))))
return inverse_difference
# 计算灰度共生矩阵的和方差
def calc_sum_average(glcm):
sum_average = np.sum(np.arange(2, 2 * glcm.shape[0] + 1) * np.sum(glcm, axis=0))
return sum_average
# 计算灰度共生矩阵的惯性
def calc_inertia(glcm):
x, y = np.meshgrid(np.arange(glcm.shape[0]), np.arange(glcm.shape[1]))
inertia = np.sum((x - y) ** 2 * glcm)
return inertia
```
注意:以上代码中用到了`skimage`库的`greycomatrix`和`greycoprops`函数,需要提前安装。可以通过以下命令进行安装:
```python
pip install scikit-image
```
用python写一段批量读取谋文件夹下所有图片的图像纹理特征的代码,方法用灰度共生矩阵,不用greycomatrix, greycoprops,提取的特征包括对比度(contrast)、均匀性(homogeneity)、能量(energy)、相关性(correlation),并将结果保存到csv文件中
好的,以下是Python代码实现:
```python
import cv2
import os
import numpy as np
import pandas as pd
# 定义保存特征的列表
contrast_list = []
homogeneity_list = []
energy_list = []
correlation_list = []
# 定义灰度共生矩阵函数
def glcm_feature(img_gray):
glcm = np.zeros((256, 256), dtype=np.uint8)
for i in range(img_gray.shape[0]-1):
for j in range(img_gray.shape[1]-1):
glcm[img_gray[i][j]][img_gray[i+1][j]] += 1
glcm = glcm + glcm.T
glcm = glcm.astype(np.float)
glcm = glcm / np.sum(glcm)
contrast = np.sum(np.square(glcm)) - np.square(np.sum(glcm))
homogeneity = np.sum(glcm / (1+np.abs(np.subtract.outer(range(256), range(256)))))
energy = np.sqrt(np.sum(np.square(glcm)))
correlation = np.sum(np.multiply(np.outer(range(256), range(256)), glcm)) - np.multiply(np.mean(range(256)), np.mean(range(256)))
correlation = correlation / (np.sqrt(np.var(range(256))) * np.sqrt(np.var(range(256))))
return contrast, homogeneity, energy, correlation
# 定义遍历文件夹的函数
def traverse_folder(folder_path):
for root, dirs, files in os.walk(folder_path):
for file in files:
if file.endswith('.jpg') or file.endswith('.png'):
img_path = os.path.join(root, file)
img_gray = cv2.imread(img_path, cv2.IMREAD_GRAYSCALE)
contrast, homogeneity, energy, correlation = glcm_feature(img_gray)
contrast_list.append(contrast)
homogeneity_list.append(homogeneity)
energy_list.append(energy)
correlation_list.append(correlation)
# 调用遍历文件夹函数
folder_path = 'your_folder_path'
traverse_folder(folder_path)
# 将特征保存到csv文件中
df = pd.DataFrame({'Contrast': contrast_list, 'Homogeneity': homogeneity_list, 'Energy': energy_list, 'Correlation': correlation_list})
df.to_csv('glcm_features.csv', index=False)
```
其中,`glcm_feature()`函数用于计算灰度共生矩阵特征,`traverse_folder()`函数用于遍历文件夹并计算每张图片的特征,最后将特征保存到csv文件中。你只需要将`your_folder_path`替换成你自己的图片文件夹路径即可。