Techno Blender
Digitally Yours.

Blend Images and Create Watermark with OpenCV

0 38


Kaptai Lake in Bangladesh (Image By Author)

Motivation

In the modern world, there are thousands of tools by which we can easily perform edit, resize, alter, add different effects, etc. operations on an image. But we hardly care about how it works in the backend. This article will discuss one of the important image processing techniques called blending and pasting images. This knowledge is essential both for image processing and computer vision. Though the techniques are simple, it is one of the core basics of computer vision.

If you are a beginner in image processing and computer vision, this article might be helpful for you.

[N.B. This article is part of my computer vision series. You may also read my previous articles on NumPy and OpenCV basic and Color representation as well.]

What is image blending and pasting?

According to the oxford dictionary, ‘blend’ means “a mixture of different substances or other things.” The word is also used for similar meanings in image processing and computer vision. It’s a technique of combining two or more images to create a new one. The output image holds the elements of the input images.

Image Blending (Image By Author)

Blending is possible when the image size is the same or different. Both of the methods will be discussed in the implementation section.

Pasting an image means copying the pixels of an image to another image.

When blending is important?

We often need the process of combining two or more images. Generally, we can do it with image editing software. In computer vision, we need to develop an automated image-blending process. In that case, manual editing is not possible. So, hands-on knowledge is required in this case.

Step by Step Implementation with OpenCV

We can blend image when —

  1. Blended image size is same
  2. Image size is different

Let’s import the necessary libraries.

It’s time to read the images with OpenCV. cv2.imread() function helps us to read the images.

Visualize the first image.

Before visualization, I converted the color channel from BGR to RGB because OpenCV read the image in BGR format. On the other hand, matplotlib works in RGB format. So, I have converted the color channel with c2.COLOR_BGR2RGB.

Now, visualize the second image.

We will combine these two images to create a watermark on the first image.

In the case of blending the images with the same size, the shape of the images must be equal. Let’s find the shape of our images.

The shape of the two images is different. Now, we need to reshape the images to reform these images.

We have transformed the two images into equal sizes successfully.

We are at the final step to blend the images.

In image blending, we use a mathematical formula to combine the pixel values of two or more images to create a new image. The formula used for blending is:

Here, image_1 and image_2 are the two images that we want to blend. Alpha and beta are blending weights that determine the contribution of each image to the final output. Gamma is a scalar value added to create noise in the output image.

The alpha, beta, and gamma values can be adjusted to control the appearance of the output image. For example, increasing the alpha value will increase the contribution of image_1 to the output, resulting in a final image that looks more likely to image_1. Similarly, increasing the beta value will increase the contribution of image_2 to the output, resulting in a final image that looks more likely to image_2.

The following piece of code can do the job.

We have used the cv2.addWeighted() function to blend the images using the blending formula. We set the values of alpha 0.4 and beta 0.6, which means that the second image contributes more than the first image to the final output. We set the value of gamma to 0, which means that no scalar value is added to the result.

Finally, we display the output image using the plt.imshow() function. We have found that image two is more visible than image one. As we have used, the beta value is more than the alpha value.

We have successfully blended the images with equal sizes.

Most often, we need to blend different size images. There is no straightforward way to do this. We need to follow some tricks and processes to do so. In the next section, I will show the implementation.

  • Image blending with different sizes

After importing the libraries and images, we will resize the two images for the simplicity of the implementation.

Create a Region of Interest (ROI)

A region of interest (ROI) is part of an image you want to select and process separately from the rest of the image. ROIs can be defined using coordinates or shapes like rectangles or polygons. For instance, in face detection in an image, an ROI can be selected around the expected location of faces, leading to improved accuracy and efficiency of the algorithm. The following graphical representation shows how it works.

Region of Interest (Image By Author)

In the above figure, we want to cut a small white portion of width and height (100,300) from the large image (400, 700). We have done the same thing for our task.

We have first selected the portion of the large image, which is equal to the shape of the small images, to the bottom right corner.

These lines of code define x_off and y_off as the horizontal and vertical offsets for creating ROI from the image. The first image has a width of 2400 pixels and a height of 2000 pixels; the second image has a width and height of 1400 pixels. We want to create ROI in the bottom right corner of the first image as we want to paste the second image onto the bottom-right corner of the first image.

The above image is the extracted region where we will paste the second image. It’s time to paste the second image (copyright) on top of the extracted region.

Create a Mask Image to Put Watermark

Image masking is the technique of selecting the important part of an image hiding rest. For our case, we just want to extract the letters of the following copyright image.

Image By Author

It’s an image with three color channels. Let’s convert it to a grayscale image; otherwise, it will be difficult for us to operate further operations.

Here, we see the text ‘Capture By Zubair,’ which is black. We need to keep this portion unchanged and change the other portion.

We need to create a mask to separate the foreground and background of an image. In blending images, we need to create a mask that represents the shape of the foreground object so that we can isolate it and blend it with the background image.

Let’s inverse the image pixels to transform white into black and black into white.

Now, we will perform a bitwise OR operation on the inversed masked image to extract the main color of the masked region.

[N.B. If you want to know more about bitwise OR operation of OpenCV, read the article. ]

We have successfully extracted the color of the copyright image into the masked region. Again, we will run a bitwise OR operation on the extracted ROI image.

Let’s join the full background image to the above image.

We have just replaced the background image pixels with the masked ROI image. Finally, we formulated our final image with a watermark.

Conclusion

I have tried my best to represent the process of blending in the easiest possible way. This knowledge is one of the fundamentals of computer vision.


Kaptai Lake in Bangladesh (Image By Author)

Motivation

In the modern world, there are thousands of tools by which we can easily perform edit, resize, alter, add different effects, etc. operations on an image. But we hardly care about how it works in the backend. This article will discuss one of the important image processing techniques called blending and pasting images. This knowledge is essential both for image processing and computer vision. Though the techniques are simple, it is one of the core basics of computer vision.

If you are a beginner in image processing and computer vision, this article might be helpful for you.

[N.B. This article is part of my computer vision series. You may also read my previous articles on NumPy and OpenCV basic and Color representation as well.]

What is image blending and pasting?

According to the oxford dictionary, ‘blend’ means “a mixture of different substances or other things.” The word is also used for similar meanings in image processing and computer vision. It’s a technique of combining two or more images to create a new one. The output image holds the elements of the input images.

Image Blending (Image By Author)

Blending is possible when the image size is the same or different. Both of the methods will be discussed in the implementation section.

Pasting an image means copying the pixels of an image to another image.

When blending is important?

We often need the process of combining two or more images. Generally, we can do it with image editing software. In computer vision, we need to develop an automated image-blending process. In that case, manual editing is not possible. So, hands-on knowledge is required in this case.

Step by Step Implementation with OpenCV

We can blend image when —

  1. Blended image size is same
  2. Image size is different

Let’s import the necessary libraries.

It’s time to read the images with OpenCV. cv2.imread() function helps us to read the images.

Visualize the first image.

Before visualization, I converted the color channel from BGR to RGB because OpenCV read the image in BGR format. On the other hand, matplotlib works in RGB format. So, I have converted the color channel with c2.COLOR_BGR2RGB.

Now, visualize the second image.

We will combine these two images to create a watermark on the first image.

In the case of blending the images with the same size, the shape of the images must be equal. Let’s find the shape of our images.

The shape of the two images is different. Now, we need to reshape the images to reform these images.

We have transformed the two images into equal sizes successfully.

We are at the final step to blend the images.

In image blending, we use a mathematical formula to combine the pixel values of two or more images to create a new image. The formula used for blending is:

Here, image_1 and image_2 are the two images that we want to blend. Alpha and beta are blending weights that determine the contribution of each image to the final output. Gamma is a scalar value added to create noise in the output image.

The alpha, beta, and gamma values can be adjusted to control the appearance of the output image. For example, increasing the alpha value will increase the contribution of image_1 to the output, resulting in a final image that looks more likely to image_1. Similarly, increasing the beta value will increase the contribution of image_2 to the output, resulting in a final image that looks more likely to image_2.

The following piece of code can do the job.

We have used the cv2.addWeighted() function to blend the images using the blending formula. We set the values of alpha 0.4 and beta 0.6, which means that the second image contributes more than the first image to the final output. We set the value of gamma to 0, which means that no scalar value is added to the result.

Finally, we display the output image using the plt.imshow() function. We have found that image two is more visible than image one. As we have used, the beta value is more than the alpha value.

We have successfully blended the images with equal sizes.

Most often, we need to blend different size images. There is no straightforward way to do this. We need to follow some tricks and processes to do so. In the next section, I will show the implementation.

  • Image blending with different sizes

After importing the libraries and images, we will resize the two images for the simplicity of the implementation.

Create a Region of Interest (ROI)

A region of interest (ROI) is part of an image you want to select and process separately from the rest of the image. ROIs can be defined using coordinates or shapes like rectangles or polygons. For instance, in face detection in an image, an ROI can be selected around the expected location of faces, leading to improved accuracy and efficiency of the algorithm. The following graphical representation shows how it works.

Region of Interest (Image By Author)

In the above figure, we want to cut a small white portion of width and height (100,300) from the large image (400, 700). We have done the same thing for our task.

We have first selected the portion of the large image, which is equal to the shape of the small images, to the bottom right corner.

These lines of code define x_off and y_off as the horizontal and vertical offsets for creating ROI from the image. The first image has a width of 2400 pixels and a height of 2000 pixels; the second image has a width and height of 1400 pixels. We want to create ROI in the bottom right corner of the first image as we want to paste the second image onto the bottom-right corner of the first image.

The above image is the extracted region where we will paste the second image. It’s time to paste the second image (copyright) on top of the extracted region.

Create a Mask Image to Put Watermark

Image masking is the technique of selecting the important part of an image hiding rest. For our case, we just want to extract the letters of the following copyright image.

Image By Author

It’s an image with three color channels. Let’s convert it to a grayscale image; otherwise, it will be difficult for us to operate further operations.

Here, we see the text ‘Capture By Zubair,’ which is black. We need to keep this portion unchanged and change the other portion.

We need to create a mask to separate the foreground and background of an image. In blending images, we need to create a mask that represents the shape of the foreground object so that we can isolate it and blend it with the background image.

Let’s inverse the image pixels to transform white into black and black into white.

Now, we will perform a bitwise OR operation on the inversed masked image to extract the main color of the masked region.

[N.B. If you want to know more about bitwise OR operation of OpenCV, read the article. ]

We have successfully extracted the color of the copyright image into the masked region. Again, we will run a bitwise OR operation on the extracted ROI image.

Let’s join the full background image to the above image.

We have just replaced the background image pixels with the masked ROI image. Finally, we formulated our final image with a watermark.

Conclusion

I have tried my best to represent the process of blending in the easiest possible way. This knowledge is one of the fundamentals of computer vision.

FOLLOW US ON GOOGLE NEWS

Read original article here

Denial of responsibility! Techno Blender is an automatic aggregator of the all world’s media. In each content, the hyperlink to the primary source is specified. All trademarks belong to their rightful owners, all materials to their authors. If you are the owner of the content and do not want us to publish your materials, please contact us by email – [email protected]. The content will be deleted within 24 hours.
Leave a comment