如何使用 PyWavelets 读取图像?

How to read an image with PyWavelets?

我需要使用 pyWavelet,即pywt 读取我的图像并为其制作小波,下面的示例仅用于加载相机图像,如何使用我的计算机路径中的另一个图像?

import pywt
import pywt.data

# Load image
original = pywt.data.camera()

我不确定您是否可以只使用 pywt 读取图像,但您可以使用 OpenCV 加载图像,然后将其转换为可用格式以用于 pywt

import cv2
import numpy as np
import pywt

image = cv2.imread('1.png')
image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

# Convert to float for more resolution for use with pywt
image = np.float32(image)
image /= 255

# ...
# Do your processing
# ...

# Convert back to uint8 OpenCV format
image *= 255
image = np.uint8(image)

cv2.imshow('image', image)
cv2.waitKey(0)

您可以尝试以下方法。

import numpy as np
import matplotlib.pyplot as plt
import pywt
import pywt.data
# Load image
original = pywt.data.camera()
# Wavelet transform of image, and plot approximation and details
titles = ['Approximation', ' Horizontal detail', 'Vertical detail', 'Diagonal detail']
coeffs2 = pywt.dwt2(original, 'bior1.3')
LL, (LH, HL, HH) = coeffs2
fig = plt.figure(figsize=(12, 3))
for i, a in enumerate([LL, LH, HL, HH]):
ax = fig.add_subplot(1, 4, i + 1)
ax.imshow(a, interpolation="nearest", cmap=plt.cm.gray)
ax.set_title(titles[i], fontsize=10)
ax.set_xticks([])
ax.set_yticks([])
fig.tight_layout()
plt.show()

参考:https://pywavelets.readthedocs.io/en/latest/

OpenCV 的替代品是 scikit-image。

import pywt
from skimage import io, color

data = io.imread(filename)

# Process your image
gray = color.rgb2gray(data)
coeffs = pywt.dwt2(gray, 'haar')

# Or... process each channel separately
r, g, b = [c.T for c in data.T]
cr = pywt.dwt2(r, 'haar')
cg = pywt.dwt2(r, 'haar')
cb = pywt.dwt2(r, 'haar')


# output: PIL, matplotlib, dump to file...

你可以使用 matplotlib 和 numpy:

from matplotlib.image import imread
import numpy as np
import pywt
   
A = imread("1.jpg")
original = np.mean(A, -1)
#rest of your codes

我使用pandas读取图像,因为我使用hm3.6数据集进行运动预测并应用小波变换作为预处理。

我的代码简单如下;

path = ".../your_path"
img = pd.read_csv(path + "h3.6m/dataset/S1/directions_1.txt") #read the image

#if you want to apply DWT you can continue with dataframe    
coeffs2 = dwt(image,  'bior1.3')
titles = ['Approximation', ' Horizontal detail',
              'Vertical detail', 'Diagonal detail']

LL, LH = coeffs2