Take 2 images and combine them to form a single image & Take 2 images, crop some parts of both images, and swap them.

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:

  1. Image Processing: OpenCV provides a vast set of functions for image manipulation, including filtering, transformations, color space conversions, and more.
  2. Object Detection: It offers pre-trained models for detecting objects in images and videos. This can be used for tasks like face detection, object tracking, and more.
  3. Machine Learning: OpenCV has integration with machine learning libraries like scikit-learn and TensorFlow, allowing for the development of custom machine learning models.
  4. Video Analysis: OpenCV includes tools for working with video streams, allowing for tasks like video capture, video processing, and object tracking in real-time.
  5. Camera Calibration: OpenCV provides tools for camera calibration, which is essential for computer vision applications involving 3D reconstruction.
  6. Feature Detection and Matching: OpenCV includes algorithms for detecting and matching features in images, which is crucial for tasks like image registration.
  7. Image Segmentation: It offers tools for dividing an image into its constituent parts or objects.
  8. Geometric Transformations: OpenCV allows for various geometric operations on images, like resizing, rotation, and perspective transformations.
  9. Deep Learning Integration

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        

  1. Import the modules/Library computer vision library from 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

  • image1 = rainbow.png and image2 = nature.png two variables holding the image address

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.

  • With the module cvzone
  • Here, this code will read the image and
  • select the 2D dimensions (X and Y) and copy with the other image and vice versa
  • image1 = rainbow.png and image2 = nature.png two variables holding the image address
  • target_size :- define the Photo Re-size

# 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

要查看或添加评论,请登录

Prateek Mudgal??的更多文章

社区洞察

其他会员也浏览了