Haar almashitirishini tasvirga qo’llash dasturi.
import numpy as np
import pywt
import cv2
def w2d(img, mode='haar', level=1):
imArray = cv2.imread(img)
# Ma'lumotlar turini o'zgartirish
# kulrang rangga aylantirish
imArray = cv2.cvtColor( imArray,cv2.COLOR_RGB2GRAY )
# floatga aylantiring
imArray = np.float32(imArray)
imArray /= 255;
# koeffitsientlarni hisoblash
coeffs=pywt.wavedec2(imArray, mode, level=level)
# Jarayon koeffitsientlari
coeffs_H=list(coeffs)
coeffs_H[0] *= 0;
# qayta qurish
imArray_H=pywt.waverec2(coeffs_H, mode);
imArray_H *= 255;
imArray_H = np.uint8(imArray_H)
# Natijani ko'rsatish
cv2.imshow('image',imArray_H)
cv2.waitKey(0)
cv2.destroyAllWindows()
w2d("test1.png",'db1',10)
Diskret veyvelet-almashtirish
Misol: Tasvirga veyvelet-almashtirishni qo’llash.
import numpy as np
import matplotlib.pyplot as plt
import cv2
import pywt
import pywt.data
# Load image
image = cv2.imread('test2.png')
image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# Convert to float for more resolution for use with pywt
image = np.float32(image)
image /= 255
# Wavelet transform of image, and plot approximation and details
titles = ['Approximation', ' Horizontal detail',
'Vertical detail', 'Diagonal detail']
coeffs2 = pywt.dwt2(image, '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()
Tezkor Haar almashtirishi
import numpy as np
from matplotlib import pyplot as plt
import pywt
import cv2
# Load image
img = cv2.imread('test1.png')
img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# Convert to float for more resolution for use with pywt
img = np.float32(img)
img /= 255
# Fully separable transform
fswavedecn_result = pywt.fswavedecn(img, 'db2', 'periodization', levels=4)
# Standard DWT
coefs = pywt.wavedec2(img, 'db2', 'periodization', level=4)
# convert DWT coefficients to a 2D array
mallat_array, mallat_slices = pywt.coeffs_to_array(coefs)
fig, (ax1, ax2) = plt.subplots(1, 2)
ax1.imshow(np.abs(mallat_array)**0.25,
cmap=plt.cm.gray,
interpolation='nearest')
ax1.set_axis_off()
ax1.set_title('Mallat decomposition\n(wavedec2)')
ax2.imshow(np.abs(fswavedecn_result.coeffs)**0.25,
cmap=plt.cm.gray,
interpolation='nearest')
ax2.set_axis_off()
ax2.set_title('Fully separable decomposition\n(fswt)')
plt.show()
Do'stlaringiz bilan baham: |