1. Reading and Displaying an Image with Alpha Channel
import cv2
import numpy as np
# Read the image with alpha channel
img = cv2.imread('Cat_Small.jpg', cv2.IMREAD_UNCHANGED)
print(img.shape)
# Display the image
cv2.imshow('Image with Alpha Channel', img)
cv2.waitKey(0)
cv2.destroyAllWindows()
2. Extracting Alpha Channel from an Image
# Extract alpha channel
alpha_channel = img[:, :, 2]
# Display the alpha channel
cv2.imshow('Alpha Channel', alpha_channel)
cv2.waitKey(0)
cv2.destroyAllWindows()
3. Creating a Transparent Background
# Create a transparent background
background = np.zeros_like(img, dtype=np.uint8)
# Display the transparent background
cv2.imshow('Transparent Background', background)
cv2.waitKey(0)
cv2.destroyAllWindows()
4. Overlaying an Image with Alpha Channel on Another Image
# Read the background image
background_img = cv2.imread('Land.jpg')
print(background_img.shape)
# Resize the background image to match the foreground image
background_img = cv2.resize(background_img, (img.shape[1], img.shape[0]))
# Overlay the images
result = cv2.addWeighted(img, 1, background_img, 1, 0)
# Display the result
cv2.imshow('Overlayed Image', result)
cv2.waitKey(0)
cv2.destroyAllWindows()
5. Changing the Transparency of an Image
# Change the transparency of the image
transparent_img = img.copy()
transparent_img[:, :, 2] = transparent_img[:, :, 2] // 2
# Display the transparent image
cv2.imshow('Transparent Image', transparent_img)
cv2.waitKey(0)
cv2.destroyAllWindows()
6. Creating a Circle with Alpha Channel
# Create a black image with alpha channel
circle_img = np.zeros((500, 500, 4), dtype=np.uint8)
# Draw a circle with alpha channel
cv2.circle(circle_img, (250, 250), 100, (0, 0, 255, 255), -1)
# Display the circle image
cv2.imshow('Circle with Alpha Channel', circle_img)
cv2.waitKey(0)
cv2.destroyAllWindows()
7. Extracting Foreground Using Alpha Channel
# Extract the foreground using the alpha channel
foreground = cv2.bitwise_and(img, img, mask=alpha_channel)
# Display the foreground
cv2.imshow('Foreground', foreground)
cv2.waitKey(0)
cv2.destroyAllWindows()
8. Removing Background Using Alpha Channel
# Invert the alpha channel
inverse_alpha = cv2.bitwise_not(alpha_channel)
# Remove the background using the inverted alpha channel
no_background = cv2.bitwise_and(img, img, mask=inverse_alpha)
# Display the image without background
cv2.imshow('No Background', no_background)
cv2.waitKey(0)
cv2.destroyAllWindows()
9. Adding a Border to an Image with Alpha Channel
# Add a red border to the image
border_img = cv2.copyMakeBorder(img, 10, 10, 10, 10, cv2.BORDER_CONSTANT, value=(0, 0, 255, 255))
# Display the image with border
cv2.imshow('Image with Border', border_img)
cv2.waitKey(0)
cv2.destroyAllWindows()
10. Rotating an Image with Alpha Channel
# Rotate the image by 45 degrees
rows, cols, _ = img.shape
M = cv2.getRotationMatrix2D((cols / 2, rows / 2), 45, 1)
rotated_img = cv2.warpAffine(img, M, (cols, rows), borderMode=cv2.BORDER_CONSTANT, borderValue=(0, 0, 0, 0))
# Display the rotated image
cv2.imshow('Rotated Image', rotated_img)
cv2.waitKey(0)
cv2.destroyAllWindows()
Bytes of Intelligence
Bytes Of IntelligenceExploring AI's mysteries in 'Bytes of Intelligence': Your Gateway to Understanding and Harnessing the Power of Artificial Intelligence.
You Might Also Like
- Bytes of Intelligence
- 0 Comments
- Bytes of Intelligence
- 0 Comments
- Bytes of Intelligence
- 0 Comments