Many times we need to use code to automatically process a part of the image , After processing, we have to splice it back , To avoid manual repetition
import cv2
def cv_show(neme, img):
# Adjust width and height ( Running again will only load your adjusted width and height )
# cv2.namedWindow(neme, cv2.WINDOW_NORMAL)
cv2.imshow(neme, img)
cv2.waitKey(0)
cv2.destroyAllWindows()
# Read images
img = cv2.imread("T.jpg")
# The first one Top left The lower right Coordinates
# 565 7
# 696 177
x1, y1, x2, y2 = 565, 7, 696, 177
# Screenshot y1 y2 x1 x2
image1 = img[y1:y2, x1:x2]
# Storage Screenshot Top left The lower right coordinate
with open("xy.txt", "w") as f:
f.write("{} {} {} {} ".format(x1, y1, x2, y2))
cv_show("1", image1)
cv2.imwrite("T0.jpg", image1)
print(" Image clipping complete ")
import cv2
import numpy as np
import os
def cv_show(neme, img):
# cv2.namedWindow(neme, cv2.WINDOW_NORMAL)
cv2.imshow(neme, img)
cv2.waitKey(0)
cv2.destroyAllWindows()
# Read the size of the original image
img = cv2.imread("T.jpg")
# Read screenshot
img1 = cv2.imread("T1.jpg")
# Create a big black picture
mage = np.zeros((img.shape[0], img.shape[1], 3), np.uint8)
# Read the coordinates of the screenshot
with open("xy.txt", "r") as f:
data = f.read()
# print(data)
# Default space division
x1, y1, x2, y2 = data.split()
# print(x1, y1, x2, y2)
# # Overlay the two images to be spliced onto the large image
# Screenshot sequence y1 y2 x1 x2
mage[:] = img
mage[int(y1):int(y2), int(x1):int(x2)] = img1
cv_show("s", mage)
# Save image
cv2.imwrite('T2.jpg', mage)
# Delete xy.txt
os.remove("xy.txt")
print(" Image mosaic is completed ")