如何将图像的 HH 子带传递给 greycomatrix 函数?

How to pass the HH subband of an image to greycomatrix function?

我正在使用 scikit-images 的函数 greycomatrix,我想将 haar 滤波后从小波变换获得的分量之一作为输入输入。

import pywt
import cv2
from skimage.feature import greycomatrix

original = cv2.imread('img.jpg', cv2.IMREAD_GRAYSCALE)
coeffs2 = pywt.dwt2(original, 'haar')
LL, (LH, HL, HH) = coeffs2
cv2.imshow('image', HH)
gCoMat = greycomatrix(HH, [2], [0], 256, symmetric=True, normed=True)

如果我将 HH 传递给 greycomatrix,我会收到此错误:

ValueError: Float images are not supported by greycomatrix. Convert the image to an unsigned integer type.

我尝试使用此代码转换图像:

from skimage import util

im = util.img_as_ubyte(HH)
im /= 32
gCoMat = greycomatrix(im, [2], [0], 256, symmetric=True, normed=True)

但是我得到了这个错误:

raise ValueError("Images of type float must be between -1 and 1.") ValueError: Images of type float must be between -1 and 1.

在将图像转换为 8 位无符号整数之前,您需要将强度重新调整到范围 [0, 1]

from skimage import util, exposure, data
import pywt
from skimage.feature import greycomatrix

original = data.camera()
LL, (LH, HL, HH) = pywt.dwt2(original, 'haar')
HH_scaled = exposure.rescale_intensity(HH, out_range=(0, 1))

bin_width = 32
im = util.img_as_ubyte(HH_scaled)
im_binned = im//bin_width

gCoMat = greycomatrix(im_binned, distances=[2], angles=[0], 
                      levels=256//bin_width, symmetric=True, normed=True)

注意,如果传递给graycomatrix的图像是binned,那么levels参数不应该是256,你必须将这个值除以相同的因子(32 在你的代码中)。同样重要的是要指出强度分箱必须通过整数除法(//)进行,否则im_binneddtype将是float。作为最后的警告,您必须将 as_gray=True 传递给函数 io.imread,否则在尝试计算灰度共生矩阵时会出现以下错误:

ValueError: The parameter image must be a 2-dimensional array