Take 2 images and combine them to form a single image & Take 2 images, crop some parts of both images, and swap them.
Computer Vision is the sense organ to the Computer which See the World through “CAMERA”
One of the Library of Python make this ease is OpenCV
OpenCV is a powerful and versatile library used in a wide range of applications, including robotics, computer vision research, augmented reality, image and video processing, and more. It is particularly popular in the fields of computer vision and artificial intelligence.
‘cv2’ is an abbreviation for OpenCV, which stands for Open Source Computer Vision Library. It's an open-source library for computer vision and image processing tasks. OpenCV provides a wide range of tools and functions that allow developers to work with images and videos in various ways, including tasks like object detection, image recognition, feature extraction, and more.
Features of openCV:
Let us go to the task of this blog practically with python code,
Take 2 images and combine them to form a single image
To install the required library open Command Line
pip install opencv-python
import cv2
import cvzone
2. View the photo Saved on the Local system
#image 1
photo1 = cv2.imread("nature.png")
cv2.imshow("Photo",photo1)
cv2.waitKey(5000) #5seconds auto close
cv2.destroyAllWindows()
#image 2
photo2 = cv2.imread("rainbow.png")
cv2.imshow("Photo",photo2)
cv2.waitKey(5000) #5seconds auto close
cv2.destroyAllWindows()
Image 1 :
Image 2:
领英推荐
Shape or Dimension of the image
3. Let us Create a Function that manages the merge or combining of two photos
Here, merge_img(a,b,c) is the function with 3 arguments.
# Function to combine two images
def merge_img(image1,image2,target_size):
# Load the images
image1 = cv2.imread(image1)
image2 = cv2.imread(image2)
# image1 = cv2.resize(image1, (image2.shape[1], image2.shape[0]))
image1_resized = cv2.resize(image1, target_size)
image2_resized = cv2.resize(image2, target_size)
# Combine images
combined_image = cv2.hconcat([image1_resized, image2_resized])
return combined_image
4. Now Will Call the function with the two image variables
if __name__ == "__main__":
p1 = "rainbow.png"
p2 = "nature.png"
#for the output screen window (width - height)
targetSize = (375, 350)
#store output value
combined_image = merge_img(p1,p2,targetSize)
cv2.imshow("Combined Image", combined_image)
cv2.waitKey(0)
cv2.destroyAllWindows()
Output:
Both the images have been merged together as Single Photo
Take 2 images, crop some parts of both images and swap them.
# Load the images
image1 = cv2.imread('nature.png')
image2 = cv2.imread('rainbow.png')
# Define the regions to be cropped and swapped
# Format: (startY:endY, startX:endX)
target_size = (500, 500)
image2 = cv2.resize(image2,(700 ,700))
# Swap the cropped regions
image1[100:300, 100:300] = image2[100:300, 100:300]
image2[300:600, 300:600] = image1[300:600, 300:600]
# Display the modified images
image1 = cv2.resize(image1, target_size)
image2 = cv2.resize(image2, target_size)
cv2.imshow('Image 1 with swapped region', image1)
cv2.imshow('Image 2 with swapped region', image2)
cv2.waitKey(0)
cv2.destroyAllWindows()
Output:
Two independent Window dispaying the swapped images of one other of some portion of the 2D image.
Thank you