Saif Al-Khoja
Saif Al-Khoja

Reputation: 23

How to read an image with PyWavelets?

I need to use pyWavelet,i.e. pywt to read my image to make wavelets for it, the example below used to load camera image only, how to use another image from my computer path?

import pywt
import pywt.data

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

Upvotes: 2

Views: 4385

Answers (5)

rafasgj
rafasgj

Reputation: 29

An alternative to OpenCV is 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(g, 'haar')
cb = pywt.dwt2(b, 'haar')


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

Upvotes: 0

Kubra Altun
Kubra Altun

Reputation: 405

I have used pandas to read the image, because I used hm3.6 dataset for motion prediction and applied wavelet transform as pre-processing.

My code is simply as follows;

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

Upvotes: 0

a basuki
a basuki

Reputation: 1

You can use matplotlib and 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

Upvotes: 0

MAHESH DIVAKARAN
MAHESH DIVAKARAN

Reputation: 29

You can try the following.

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()

Reference:https://pywavelets.readthedocs.io/en/latest/

Upvotes: -1

nathancy
nathancy

Reputation: 46660

I'm not sure if you can read in an image just using pywt but you can load in a image using OpenCV and then convert it to a usable format for use with 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)

Upvotes: 1

Related Questions