程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

The simplest splicing of OpenCV Python images

編輯:Python

Use scenarios : Fix the upper left according to 、 The lower right 、 Standard rectangular screenshot of coordinate points , Do what you want with the screenshot , Splice it back .

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

The original picture is as follows :

After screenshot :

Post screenshot processing : The little pink stars were painted by myself

Finally, splice it back

Implementation code :

Screenshot section :

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 ")

Mosaic screenshot :

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 ")

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved