Combine blocks of different images and produce a new image

后端 未结 2 1070
小蘑菇
小蘑菇 2021-01-23 15:55

I have six photographs. I changed them into block structure. Consider an image of size 200x200. 1. I converted into blocks of 10x10 so now I have 400 blocks in total each of siz

2条回答
  •  梦毁少年i
    2021-01-23 16:40

    I consider you wonder to know how to merge two or more images. In python, when you load an image using opencv, it is stored in numpy arrays. So it is easy using numpy. Below is an example to merge two images. Firstly, load two images:

    import cv2
    import numpy as np
    
    img1 = cv2.imread('pic1.png')
    img2 = cv2.imread('pic2.png')
    
    cv2.imshow('img1', img1)
    cv2.imshow('img2', img2)
    

    the two images are like:

    Then to merge those two imgs:

    # get the height and width of those pictures
    h1, w1 = img1.shape[:2]
    h2, w2 = img2.shape[:2]
    
    # define the height and width of the merged pictures
    h, w = max(h1, h2), w1 + w2
    img = np.zeros((h, w, 3), np.uint8)
    
    # paste each img to the right place
    img[0:h1, 0:w1] = img1
    img[0:h2, w1:] = img2
    
    cv2.imshow('img', img)
    cv2.waitKey(0)
    

    the result would be like:

提交回复
热议问题