import sys
import cv2
import numpy as np
def algdwt_haar(img):
h,w,c = img.shape
if c > 1:
img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
src = img * 1.0
if h % 2 == 1:
src = np.vstack((src, src[h-1]))
if w % 2 == 1:
src = np.hstack((src, src[:,w-1]))
weight = 1.0 / np.sqrt(2)
lowf = (src[:,0::2] + src[:,1::2]) * weight
highf = (src[:,0::2] - src[:,1::2]) * weight
buf = np.hstack((lowf, highf))
lowf = (buf[0::2] + buf[1::2]) * weight
highf = (buf[0::2] - buf[1::2]) * weight
res = np.vstack((lowf, highf))
return (res, src.shape[0] - img.shape[0], src.shape[1] - img.shape[1])
if __name__ == "__main__":
img = cv2.imread(sys.argv[1])
h,w,c = img.shape
if h > 401:
w = np.int32(w * 401.0/h)
img = cv2.resize(img, (h,w))
result, dh,dw = algdwt_haar(img)
result = np.abs(result)
mat255 = np.ones(result.shape, float) * 255;
img = np.minimum(result,mat255)
img = np.uint8(img)
cv2.imshow('dwt', img)
cv2.waitKey(-1)
haar dwt in python
最新推荐文章于 2024-04-24 17:02:08 发布