What is unsharp masking?

Digital image processing is a significant aspect of data science. It is used in image modification and enhancement to lead to a greater understanding of data.

One aspect of image enhancement is the reduction of image blur, which is how we distinguish between different objects in the image more clearly. The following is a blurry image.

widget

In unsharp masking, a blurred image is subtracted from the original image to get its edges only, and the result is added to the original image to get an enhanced version. For example, subtracting the Gaussian blurred version of the image from the original image gives the following output.

widget

Adding the subtracted image to the original image sharpens it by reducing the blur.

widget

Code example

In the example below, you will be prompted to upload an image file to the code widget, which the program will then output as a transformed image.

#import libraries
from PIL import Image
import numpy as np
import cv2
import math
#read image
img = cv2.imread('__ed_input.png')
#blur the image using gaussian blur
gaussian = cv2.GaussianBlur(img, (7, 7),0)
#store images as arrays
img=np.asarray(img)
gaussian=np.asarray(gaussian)
#subtract blurred image from original, then add to original
img_unsharp=img+(img-gaussian)
#output unsharp image
cv2.imwrite('output/saved_img.png', img_unsharp)
#convert array to image
img_unsharp=Image.fromarray(img_unsharp)
img_unsharp

Free Resources