Tag Archives: cv2.addWeighted()

Unsharp Masking and Highboost filtering

In this blog, we will learn how we can sharpen an image or perform edge enhancement using a smoothing filter. Let’s see how this is done

  • First, we blur the image. We know by smoothing an image we suppress most of the high-frequency components.
  • Then, we subtract this smoothed image from the original image(the resulting difference is known as a mask). Thus, the output image will have most of the high-frequency components that are blocked by the smoothing filter.
  • Adding this mask back to the original will enhance the high-frequency components.

Because we are using a blurred or unsharp image to create a mask this technique is known as Unsharp Masking.

Thus, unsharp masking first produces a mask m(x,y) as

where, f(x,y) is the original image and fb(x,y) is the blurred version of the original image.

Then this mask is added back to the original image which results in enhancing the high-frequency components.

where k specifies what portion of the mask to be added. 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.

Note: Instead of subtracting the blurred image from the original, we can directly use a negative Laplacian filter to obtain the mask.

If the image contains noise, this method will not produce satisfactory results, like most of the other sharpening filters.

Let’s see how to do this using OpenCV-Python

OpenCV-Python

Since in the last equation we described unsharp masking as the weighted average of the original and the input image, we will simply use OpenCV cv2.addWeighted() function.

The output is shown below

Hope you enjoy reading.

If you have any doubt/suggestion please feel free to ask and I will do my best to help or improve myself. Good-bye until next time.

Image Overlays using Bitwise Operations OpenCV-Python

In the previous blog, we learned how to overlay an image to another image using OpenCV cv2.addWeighted() function. But this approach is limited to rectangular ROI. In this blog, we will learn how to overlay non-rectangular ROI to another image.

Task:

Put the TheAILearner text image(shown in the left) above an image (Right one).

Because the TheAILearner text is non-rectangular, we will be using OpenCV cv2.bitwise_and(img1, img2, mask) where the mask is an 8-bit single channel array, that specifies elements of the output array to be changed.

For Bitwise_and you need to know the following two rules

  • Black + Any Color = Black
  • White + Any Color = That Color

Now, let’s see step by step how to do this

  • First load the two images
  • Select the region in the image where you want to put the logo. Here, I am putting this in the top left corner.
  • Now, we will create a mask. You can create a mask by a number of ways but here we will be using thresholding for this as shown in the code below. We will also create an inverse mask. Depending on the image you need to change the thresholding function parameters.

The mask and mask_inv looks like this

  • Now black out the area of logo in the roi created above using the bitwise_and as shown in the code below

This looks like this

  • Now, we will extract the logo region (with colors) from the logo image using the following code

The output looks like this

  • Now, we will simply add the above two images because black has intensity 0 so adding this doesn’t change anything and outputs the same color. This is done using the following code

The final output looks like this

So, using these simple bitwise operations we can overlay an image to another. Be careful while creating the mask as it entirely depends on the image. According to the image you need to make adjustments to the thresholding function parameters.

Hope you enjoy reading.

If you have any doubt/suggestion please feel free to ask and I will do my best to help or improve myself. Good-bye until next time.