二维码去模糊

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

你可能感兴趣的:(计算机视觉,python,矩阵)