denose with dwt

import sys
import cv2
import numpy as np
import pywt

def apply_thresh(coef, thresh, mode):
	if mode == 'hard':
		tmp = np.abs(coef)
		tmp = tmp >= thresh
		coef = coef * tmp
	else: #soft
		tmp = np.abs(coef)
		tmp = tmp >= thresh
		coef = coef * tmp
		coef = np.sign(coef) * (np.abs(coef) - thresh)
	return coef

def get_thresh_uplimit(coef):
	c = 13 #noise estimation
	s = coef.shape[0] * coef.shape[1]
	return c * np.sqrt(2 * np.log(s))

def get_thresh_MAD(coef): #mean absolute difference
	c = 2.0
	coef = np.abs(coef)
	return c * np.std(np.reshape(coef, coef.shape[0]*coef.shape[1]))
	
def denoise_dwt(img):
	if img.ndim != 2:
		img = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
	img = np.float32(img)
	h,w = img.shape
	if h < 32 or w < 32:
		return np.zeros(img.shape,np.uint8)
	get_thresh = get_thresh_uplimit
	N = 3
	coef = pywt.wavedec2(img, 'db1', level = N)
	for k in range(2,N+1):
		cH, cV, cD = coef[k]
		t = get_thresh(cH)
		cH = apply_thresh(cH, t, 'soft')
		t = get_thresh(cV)
		cV = apply_thresh(cV, t, 'soft')
		t = get_thresh(cD)
		cD = apply_thresh(cD, t, 'soft')
		coef[k] = (cH, cV, cD)
	cleanimg = pywt.waverec2(coef, 'db1')
	cleanimg = np.uint8(cleanimg)
	return cleanimg

def imnoise(img):
	if img.ndim != 2:
		img = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
	noiseimg = np.float32(img) + np.random.normal(0, 50, img.shape)
	m0 = np.zeros(img.shape, float)
	m1 = np.ones(img.shape,float) * 255
	noiseimg = np.minimum(noiseimg, m1)
	noiseimg = np.maximum(noiseimg, m0)
	noiseimg = np.uint8(noiseimg)
	return noiseimg

if __name__=='__main__':
	img = cv2.imread(sys.argv[1])
	cv2.imshow('src', img)
	cv2.waitKey(-1)
	noiseimg = imnoise(img)
	cv2.imshow('noise', noiseimg)
	cv2.waitKey(-1)
	cleanimg = denoise_dwt(noiseimg)
	cv2.imshow('clean', cleanimg)
	cv2.waitKey(-1)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值