Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python unsharp mask

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:

  • blurred the image with a Gaussian filter (three different methods)
  • then, created a mask by subtracting the blur Image form the original
  • finally, added the mask multiplied by WightFaktor to the original Image

But it doesn´t really work.

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)
like image 301
omni Avatar asked Sep 08 '15 09:09

omni


People also ask

How does Unsharp Mask work?

The Unsharp Mask increases the image contrast along the edges of objects in a photo. The effect doesn't actually detect edges, but it can identify pixel values that differ from their neighboring pixels by a certain amount.

What is the difference between sharpen and Unsharp Mask?

The Sharpening Tool is like using a hammer to sharpen. There is no fine control. The Unsharp Mask Tool give fine control. It finds the edges of the different tones and increases contrast to make the image appear sharper.

What is unsharp masking and high boost filtering?

When k= 1 this is known as Unsharp masking. For k>1 we call this as high-boost filtering because we are boosting the high-frequency components by giving more weight to the masked (edge) image. We can also write the above two equations into one as the weighted average of the original and the blurred image.


1 Answers

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

import cv2

image = cv2.imread("example.jpg")
gaussian_3 = cv2.GaussianBlur(image, (0, 0), 2.0)
unsharp_image = cv2.addWeighted(image, 2.0, gaussian_3, -1.0, 0)
cv2.imwrite("example_unsharp.jpg", unsharp_image)

Giving the following kind of result:

mandrill unsharp example

addWeighted() is used here as follows:

dst = cv2.addWeighted(src1, alpha, src2, beta, gamma)

Giving you the following transformation:

dst = src1*alpha + src2*beta + gamma

The strength of the effect can be altered by adjusting the alpha and beta weightings, for example: 1.5 and -0.5.

like image 126
Martin Evans Avatar answered Sep 17 '22 15:09

Martin Evans