锐化
使用unsharp mask的原理:
结果 = (a * 原图) + (b * 原图的高斯模糊),其中a >= 1 && b <= 0 && a + b == 1
用opencv的GaussianBlur
和addWeighted
即可
def someblur(src, blursize = 5):
# dst = cv2.blur(src, (blursize, blursize))
dst = cv2.GaussianBlur(src, (blursize, blursize), 1)
return dst
def sharpen(src):
blur = someblur(src, 5)
dst = cv2.addWeighted(src, 2, blur, -1, 0)
return dst
那么对上图做锐化操作:
img = cv2.imread('resource/messi.jpg')
imgsharp = sharpen(img)
反锐化
有时锐化的效果太过了,想要减弱一些:
结果 = (a * 原图) + (b * 原图的高斯模糊),其中 0 <= a <= 1 && 0 <= b <= 1 && a + b == 1
def desharpen(src):
blur = someblur(src, 5)
dst = cv2.addWeighted(src, 0.5, blur, 0.5, 0)
return dst
对上图的锐化结果进行操作:
imgdesharp = desharpen(imgsharp)
得到的图与最开始的原图很相似,但不可能完全一样,因为高斯模糊本身就不可逆。
以下为全部代码:
from cv2 import cv2
import numpy as np
def someblur(src, blursize = 5):
# dst = cv2.blur(src, (blursize, blursize))
dst = cv2.GaussianBlur(src, (blursize, blursize), 1)
return dst
def sharpen(src):
blur = someblur(src, 5)
dst = cv2.addWeighted(src, 2, blur, -1, 0)
return dst
def desharpen(src):
blur = someblur(src, 5)
dst = cv2.addWeighted(src, 0.5, blur, 0.5, 0)
return dst
img = cv2.imread('resource/messi.jpg')
# blur = someblur(img)
imgsharp = sharpen(img)
imgdesharp = desharpen(imgsharp)
cv2.imshow('origin', img)
# cv2.imshow('blur', imgblur)
cv2.imshow('sharp', imgsharp)
cv2.imshow('desharp', imgdesharp)