Python unsharp mask

杀马特。学长 韩版系。学妹 提交于 2019-11-27 20:48:51

问题


I want to use unsharp mask on a 16 Bit Image. The Image has 640 x 480 Pixel and is saved in a numpy array. In the first Step i blur the Image withe a Gaussian filter (three different Methods). After this i create a Mask by subtract the blur Image form the Original. in The last step i add the Mask multiplied by wightfaktor to the Original Image. But it don´t really works.

Here is the Python code:

Gaussian1 = ndimage.filters.gaussian_filter(Image,sigma=10.0)
Gaussian2 = filters.gaussian_filter(Image,sigma=10.0)
Gaussian3 = cv2.GaussianBlur(Image,(9,9),sigmaX=10.0)

Mask1 = Image - Gaussian1
UnsharpImage = Image + (WightFaktor*Mask1)

May Someone help me?


回答1:


To get an unsharp image using OpenCV you need to use the addWeighted function as follows:

import cv2

image = cv2.imread("lenna.jpg")
gaussian_3 = cv2.GaussianBlur(image, (9,9), 10.0)
unsharp_image = cv2.addWeighted(image, 1.5, gaussian_3, -0.5, 0, image)
cv2.imwrite("lenna_unsharp.jpg", unsharp_image)

Giving the following kind of result:




回答2:


One could use scikit-image or PIL's unsharp mask implementation as well:

import numpy as np
import matplotlib.pylab as plt
from PIL import Image, ImageFilter
from skimage.io import imread
from skimage.filters import unsharp_mask
# with scikit-image
im = imread('images/lena.jpg')
im1 = np.copy(im).astype(np.float)
for i in range(3):
    im1[...,i] = unsharp_mask(im[...,i], radius=2, amount=2)
# with PIL
im = Image.open('images/lena.jpg')
im2 = im.filter(ImageFilter.UnsharpMask(radius=2, percent=150))
# plot
plt.figure(figsize=(20,7))
plt.subplot(131), plt.imshow(im), plt.axis('off'), plt.title('Original', size=20)
plt.subplot(132), plt.imshow(im1), plt.axis('off'), plt.title('Sharpened (skimage)', size=20)
plt.subplot(133), plt.imshow(im2), plt.axis('off'), plt.title('Sharpened (PIL)', size=20)
plt.show()

with the following output:

Also, adding some detailed stpes / comments on Martin Evans code with opencv-python:

import cv2

im = cv2.imread("images/lena.jpg")
im_blurred = cv2.GaussianBlur(im, (11,11), 10)
im1 = cv2.addWeighted(im, 1.0 + 3.0, im_blurred, -3.0, 0) # im1 = im + 3.0*(im - im_blurred)
plt.figure(figsize=(20,10))
plt.subplot(121),plt.imshow(cv2.cvtColor(im, cv2.COLOR_BGR2RGB)), plt.axis('off'), plt.title('Original Image', size=20)
plt.subplot(122),plt.imshow(cv2.cvtColor(im1, cv2.COLOR_BGR2RGB)), plt.axis('off'), plt.title('Sharpened Image', size=20)
plt.show()



来源:https://stackoverflow.com/questions/32454613/python-unsharp-mask

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!