Opencv Shape Opacity-With Example Code

Opencv shape opacity is a function of “opencv” used for the execution of image processing and computer vision operations which helps to change the transparency or opacity of various image and shape. It can be used for a wide range of features, including object detection, face recognition, and tracking.

What is OpenCV Shape Opacity in Python?

In this post, we’ll look at how to use OpenCV shape opacity in Python to generate a semi-transparent shape. We occasionally require transparency in our products. Today, we’ll look at a simple method using Python and OpenCV. Here, we’ll discuss three different ways to shape, namely:

• Using Rectangular shape.
• Using Line.
• Using Circle.

Method 1: Using cv2.rectangle

To draw a rectangle in OpenCV Python.

```Syntax: cv2.rectangle(Image, start point, end point, colour, thickness)

Parameters:

image:  This is the picture on which the line should be drawn.

start point:  This identifies the line's beginning position. The coordinates are shown as pairs of two values, or tuples (X coordinate value, Y coordinate value).

end point:  These are the line's final coordinates. The coordinates are shown as pairs of two values, or tuples (X coordinate value, Y coordinate value).

color:  It is the colour of the line that will be drawn. We pass a tuple to BGR. as in (255, 0, 0) for the blue color.

thickness:  This term refers to the line's px thickness.

return value:  It provides an final image

```
```import cv2

image = cv2.imread('logo.jpg')
overlay = image.copy()

# Rectangle parameters
x, y, w, h = 10, 10, 300, 300

# A filled rectangle
cv2.rectangle(overlay, (x, y), (x+w, y+h), (0, 200, 0), -1)

cv2.imshow("some", overlay)
cv2.waitKey(0)

cv2.destroyAllWindows()```

Example 2: Showing transparent

For adding opacity, we are going to use the cv2.addWeighted() function. This helps us add two images with different alpha values.

```import cv2

image = cv2.imread('logo.jpg')
overlay = image.copy()

# Rectangle parameters
x, y, w, h = 10, 10, 300, 300

# A filled rectangle
cv2.rectangle(overlay, (x, y), (x+w, y+h), (0, 200, 0), -1)

alpha = 0.4 # Transparency factor.

# Following line overlays transparent rectangle
# over the image
image_new = cv2.addWeighted(overlay, alpha, image, 1 - alpha, 0)

cv2.imshow("some", image_new)
cv2.waitKey(0)

cv2.destroyAllWindows()```

Method-2 Using cv2_circle

```Syntax: cv2.circle(image, center coordinates, radius, colour, and thickness)

Parameters:

Image: It is the picture that a line needs to be drawn on.

center_coordinates: It is the circle's centre coordinates. The coordinates are shown as pairs of two values, or tuples (X coordinate value, Y coordinate value).

radius: It is the circle's radius.

color: It determines what colour the line will be. We pass a tuple to BGR. as in (255, 0, 0) for the colour blue.

thickness: It is the line's thickness in pixels.

Return Value: It gives back a picture.```

Example: In this case, we’ll use OpenCV Python to draw a circle. We will pass center coordinates and radius in this, where the center coordinate is shown as a tuple of two values each for the X-coordinate and the Y-coordinate.

```import cv2

image = cv2.imread('kotailogo.png')
overlay = image.copy()

# A filled circle
cv2.circle(overlay, (250, 250), 70, (15,75,50), 20)

alpha = 0.4 # Transparency factor.

# Following line overlays transparent rectangle
# over the image
image_new = cv2.addWeighted(overlay, alpha, image, 1 - alpha, 0)

cv2.imshow("some", image_new)
cv2.waitKey(0)

cv2.destroyAllWindows()```

Method 3:  Using cv2.line

Draw a line using Opencv Python,

```Syntax: cv2.line(image, start_point, end_point, color, thickness)

Parameters:

image: It is the image that a line needs to be drawn on.

start_point: It is the line's origin coordinates. The coordinates are shown as pairs of two values, or tuples (X coordinate value, Y coordinate value).

end_point: It is the line's final coordinates. The coordinates are shown as pairs of two values, or tuples (X coordinate value, Y coordinate value).

color: It determines what colour the line will be. We pass a tuple to BGR. as in (255, 0, 0) for the colour blue.

thickness: It is the line's thickness in pixels.

Return Value: It provides an image as the return value.```

Example: We will draw a line using OpenCV in Python. In this, we pass start_point and end_point which both are presented as a tuple of X-coordinate and Y- coordinate values

```import cv2

image = cv2.imread('kotailogo.png')
overlay = image.copy()

# A filled line
cv2.line(overlay, (0,0), (311,211), (0,355,0),40)

# Transparency factor.
alpha = 0.4

# Following line overlays transparent rectangle
# over the image
image_new = cv2.addWeighted(overlay, alpha, image, 1 - alpha, 0)

cv2.imshow("some", image_new)
cv2.waitKey(0)

cv2.destroyAllWindows()```

Image masking in OpenCV Python

This post will show you how to flip a mask that OpenCV built for a picture. Using the masking technique, you can draw attention to a certain object in the picture. It can be described as assigning specific pixels in a picture to a null value, such as 0 (black color), so that only the area of the image where the pixel value is not 0 is highlighted.

The process is essentially reversed when a mask is inverted, leaving all other pixels non-zero while the pixels in the highlighted area become 0. We transpose the value of each pixel’s bitwise not in order to do this.

```[ 0 1 0
0 0 0
0 1 0 ]```

As we apply a bitwise not operation on each value to invert this mask, 0 becomes 1 and the opposite:

```[ 1 0 1
1 1 1
1 0 1 ]```

Using OpenCV’s cv2.bitwise_not() function, which applies the bitwise_not operation to every pixel, we can invert a mask.

```Syntax: cv2.bitwise_not(masked_image)

Parameters:

masked_image: It is essential to invert the image.

Return Value: The inverted masked image is what is returned.```

Example 1: Reading the image comes first in this project. The “int” datatype’s unit matrix (5 x 5) is then constructed as the kernel. Since HSV is the only format that can be used for all operations, we now convert the image to that format. The image is then submitted to leveling, bending, and wave action processes in order to form the mask. An encoding, not the method in the CV2 package, can be used to inverse the mask after the pixel values have been swapped. The masking picture, which has been rotated, is finally displayed.

```# Python program to explain
# mask inversion on a b/w image.

# importing cv2 and numpy library
import cv2
import numpy as np

# Reading an image
img = cv2.imread('rgy.jpg')

# The kernel to be used for dilation purpose
kernel = np.ones((5, 5), np.uint8)

# converting the image to HSV format
hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)

# defining the lower and upper values of HSV,
# this will detect yellow colour
Lower_hsv = np.array([20, 70, 100])
Upper_hsv = np.array([30, 255, 255])

# creating the mask by eroding,morphing,
# dilating process
Mask = cv2.inRange(hsv, Lower_hsv, Upper_hsv)
Mask = cv2.erode(Mask, kernel, iterations=1)
Mask = cv2.morphologyEx(Mask, cv2.MORPH_OPEN, kernel)
Mask = cv2.dilate(Mask, kernel, iterations=1)

# Inverting the mask by
# performing bitwise-not operation
Mask = cv2.bitwise_not(Mask)

# Displaying the image
cv2.imshow('Mask', Mask)

# waits for user to press any key
# (this is necessary to avoid Python
# kernel form crashing)
cv2.waitKey(0)

# closing all open windows
cv2.destroyAllWindows()```

Example 2: This program is comparable to the one previously described. The only difference is that we directly mask the horse rather than first converting the image to black and white, and then using the bitwise operation to invert the mask we just made.

```# Python program to explain
# mask inversion on a RGB image.

# importing cv2 and numpy library
import cv2
import numpy as np

# Reading an image
img = cv2.imread('rgy.jpg')

# The kernel to be used for dilation
# purpose
kernel = np.ones((5, 5), np.uint8)

# converting the image to HSV format
hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)

# defining the lower and upper values
# of HSV, this will detect yellow color
Lower_hsv = np.array([20, 70, 100])
Upper_hsv = np.array([30, 255, 255])

# creating the mask
Mask = cv2.inRange(hsv, Lower_hsv, Upper_hsv)

# Inverting the mask
mask_yellow = cv2.bitwise_not(Mask)
Mask = cv2.bitwise_and(img, img, mask = mask_yellow)

# Displaying the image
cv2.imshow('Mask', Mask)

# waits for user to press any key
cv2.waitKey(0)

# closing all open windows
cv2.destroyAllWindows()```

Opencv Transparent overlay

In this article, we will discuss about how to create transparent overlays with Python and OpenCV.

We will first require two inputs for this program: a Background Image and an Overlay Image. Then, with all values set to 0, we’ll create a NumPy array with the same dimensions as the backdrop picture. Then, utilizing this array and the overlay, we’ll make a mask.

The cv2.addWeighted() method will now be used as the main element of our program to finish the overlay.

To create an output array, the cv2.addWeighted() process mainly obtains the average weighted sum of the two input arrays.

Below is the code:

```import cv2
import numpy as np

# Loading our images
# Background/Input image
background = cv2.imread('Assets/img.jpg')

# Overlay image
overlay_image = cv2.imread('Assets/img2.png')

# Resize the overlay image to match the bg image dimensions
overlay_image = cv2.resize(overlay_image, (1000, 1000))
h, w = overlay_image.shape[:2]

# Create a new np array
shapes = np.zeros_like(background, np.uint8)

# Put the overlay at the bottom-right corner
shapes[background.shape[0]-h:, background.shape[1]-w:] = overlay_image

# Change this into bool to use it as mask
mask = shapes.astype(bool)

# We'll create a loop to change the alpha
# value i.e transparency of the overlay
for alpha in np.arange(0, 1.1, 0.1)[::-1]:

# Create a copy of the image to work with
bg_img = background.copy()
# Create the overlay
bg_img[mask] = cv2.addWeighted(bg_img, 1 - alpha, shapes,
alpha, 0)[mask]

# print the alpha value on the image
cv2.putText(bg_img, f'Alpha: {round(alpha,1)}', (50, 200),
cv2.FONT_HERSHEY_PLAIN, 8, (200, 200, 200), 7)

# resize the image before displaying
bg_img = cv2.resize(bg_img, (630, 630))
cv2.imshow('Final Overlay', bg_img)
cv2.waitKey(0)```

Summary

In this article, we discussed about OpenCV shape opacity, Image masking, and openCV transparent overlay with an example code. I hope this article will help you all to understand the transparency process using OpenCV in Python.

THANKYOU. HAVE A GOOD DAY.