二维码去模糊

import cv2

from skimage import io, color, restoration

import numpy as np

def matlab_style_gauss2D(shape=(3,3),sigma=0.5):
“”"
2D gaussian mask - should give the same result as MATLAB’s
fspecial(‘gaussian’,[shape],[sigma])
“”"
m,n = [(ss-1.)/2. for ss in shape]
y,x = np.ogrid[-m:m+1,-n:n+1]
h = np.exp( -(xx + yy) / (2.sigmasigma) )
h[ h < np.finfo(h.dtype).eps*h.max() ] = 0
sumh = h.sum()
if sumh != 0:
h /= sumh
return h

def process(filename):
psf = matlab_style_gauss2D((5,5),1)
print(psf)
psf_v2 = np.ones((5, 5)) / 25
print(psf_v2)

img = io.imread(filename)
img = color.rgb2gray(img)

print(img.std())
img += 0.1 * img.std() * np.random.standard_normal(img.shape)

deconvolved_img = restoration.wiener(img, psf, 0.1)
io.imsave('midel.jpg', deconvolved_img)

print('fin.')

image=cv2.imread('midel.jpg',0)
#  高斯模糊
guassblur=cv2.GaussianBlur(image,(3,3),0)
cv2.imwrite("guassblur.jpg",guassblur)
#图像锐化
kernel = np.array([[0, -1, 0], [-1, 5, -1], [0, -1, 0]], np.float32) #定义一个核
shape = cv2.filter2D(guassblur, -1, kernel=kernel)
cv2.imwrite("shape.jpg",shape)
#直方图均衡化
imgEqu1G = cv2.equalizeHist(shape)

cv2.imwrite('imgEqu1G.jpg',imgEqu1G)
#二值化
ret, binary = cv2.threshold(imgEqu1G, 110, 255, cv2.THRESH_BINARY)
cv2.imwrite('binary.jpg',binary)

import math
def psnr2(img1, img2):
mse = np.mean( (img1/255. - img2/255.) ** 2 )
if mse < 1.0e-10:
return 100
PIXEL_MAX = 1
return 20 * math.log10(PIXEL_MAX / math.sqrt(mse))

if name==‘main’:
filename = “2.jpg”
process(filename)
target_filename=‘binary.jpg’
target=cv2.imread(target_filename)
img=cv2.imread(filename)
print(psnr2(target,img))

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值