计算图像的灰度共生矩阵


针对一张20*20的图像块,编程计算该图像块的灰度共生矩阵(d=1,θ=0°),
并将原始图像块和共生矩阵的数值显示出来。


首先查看一下计算原理:
ppt
乍一看我还以为需要四个参数计算。
看一下百科:
baidu
这样看是很清楚的,指定了a和b实际上就是指定了方向和距离。
比如 PPT里的例子,0° 1就是a=1,b=0,135° 1就是a=-1,b=-1
代码如下:

import cv2 as cv
import numpy as np
import matplotlib.pyplot as plt

class ComatrixCaler(object):
    def __init__(self,mat,a=1,b=0) -> None:
        self.mat = mat
        self.a = a
        self.b = b
        self.countMatrix = None
        self.probaMatrix = None
        self.l = None
        self.calComatrix()

    @staticmethod
    def getRange(a,b,r,c):
        opa,opb = int(a/abs(a)) if a!=0 else 1,int(b/abs(b)) if b!=0 else 1
        lb_r = 0 if opb>=0 else r-1
        rb_r = r-b if opb>=0 else 0+b+1
        lb_c = 0 if opa>=0 else c-1
        rb_c = c-a if opa>=0 else 0+a+1
        return range(lb_r,rb_r,opb),range(lb_c,rb_c,opa)
    
    def calComatrix(self):
        self.l = list(set(self.mat.flatten().tolist()))
        self.l.sort()
        counts = self.l.__len__()
        m = {k:v for k,v in zip(self.l,range(counts))}
        countMat = np.zeros([counts,counts])
        r,c = self.mat.shape
        borders = self.getRange(self.a,self.b,r,c)
        for i in borders[0]:
            for j in borders[1]:
                countMat[m[self.mat[i,j]],m[self.mat[i+self.b,j+self.a]]]+=1
                countMat[m[self.mat[r-i-1,c-j-1]],m[self.mat[r-1-i-self.b,c-1-j-self.a]]]+=1
        self.countMatrix = countMat.astype('uint8')
        self.probaMatrix = countMat/self.countMatrix.sum()
    def setargs(self,a,b):
        self.a = a
        self.b = b
        self.calComatrix()

egm = np.array([[0,0,1,1],[0,0,1,1],[0,2,2,2],[2,2,3,3]])

# 这里复现了一下PPT里的例子
eg = ComatrixCaler(egm)
print(eg.countMatrix,eg.probaMatrix)
eg.setargs(-1,-1)
print(eg.countMatrix,eg.probaMatrix)

理论上灰度共生矩阵应该是概率分布矩阵,这里的countMatrix是计数矩阵,probaMatrix是概率矩阵
显然的是,灰度共生矩阵的大小取决于图像中灰度值的种类,所以建议orange = orange - np.mod(orange,25)来减少灰度值种类,共生矩阵图像也更明显

我自己的例子:

orange = cv.imread('./pics/orange.jpg',flags=cv.IMREAD_GRAYSCALE).astype(np.uint8)
orange = orange - np.mod(orange,25)
# orange = cv.resize(orange,(20,20))
print(list(set(orange.flatten().tolist())).__len__())
orange_com = ComatrixCaler(orange)
orange_float_gray = orange_com.probaMatrix

fig,axs = plt.subplots(1,3)
axs[0].imshow(orange,cmap='gray')
axs[1].imshow(orange_float_gray,cmap='gray')
axs[2].imshow(orange_com.countMatrix,cmap='gray')

效果:
outimg

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
灰度共生矩阵(Gray-Level Co-occurrence Matrix,GLCM)是用于描述图像纹理特征的一种方法。GLCM可以反映出图像中灰度级别之间的空间关系,从而描述出图像的纹理特征。下面是一个简单的Python程序,用于计算灰度共生矩阵: ```python import cv2 import numpy as np # 读入图像并转换为灰度图像 img = cv2.imread("test.jpg") gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) # 定义灰度共生矩阵的参数 d = 1 theta = [0, np.pi/4, np.pi/2, 3*np.pi/4] # 计算灰度共生矩阵 glcm = np.zeros((256, 256, len(theta))) for i in range(gray.shape[0]-d): for j in range(gray.shape[1]-d): for k in range(len(theta)): x = gray[i,j] y = gray[i+d*np.sin(theta[k]), j+d*np.cos(theta[k])] glcm[x,y,k] += 1 # 将灰度共生矩阵归一化 glcm /= np.sum(glcm) # 打印灰度共生矩阵 print(glcm) ``` 在上面的程序中,首先读入了一张测试图像,并将其转换为灰度图像。然后定义了灰度共生矩阵的参数,包括距离和方向。接着使用三重循环遍历图像中的每个像素,并计算出该像素与其相邻像素之间的灰度共生矩阵。最后将灰度共生矩阵归一化,并打印出来。 需要注意的是,上面的程序中计算灰度共生矩阵时只考虑了4个方向,如果需要考虑更多的方向,可以增加theta的取值。此外,在实际应用中,还需要根据具体的任务选择合适的灰度共生矩阵特征,如能量、对比度、相关性等。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值