Python unsharp mask

前端 未结 2 1586
既然无缘
既然无缘 2020-12-14 11:31

I want to use unsharp mask on a 16 Bit Image.

The Image has 640 x 480 Pixels and is saved in a NumPy array.

I have done the following

相关标签:
2条回答
  • 2020-12-14 11:39

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

    0 讨论(0)
  • 2020-12-14 11:50

    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, (0, 0), 2.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:

    0 讨论(0)
提交回复
热议问题