将一张图像贴到另一张图像指定区域
作者:互联网
import cv2 import numpy as np def mergeImg(inputImg, maskImg, contourData, drawPosition): ''' :param inputImg: 输入的图像 :param maskImg: 输入的模板图像 :param contourData: 输入的模板中轮廓数据 numpy 形式如[(x1,y1),(x2,y2),...,] :param drawPosition: (x,y) 大图中要绘制模板的位置,以maskImg左上角为起始点 :return: outPutImg:输出融合后的图像 outContourData: 输出轮廓在inputImg的坐标数据 outRectData: 输出轮廓的矩形框在inputImg的坐标数据 ''' # 通道需要相等 if (inputImg.shape[2] != maskImg.shape[2]): print("inputImg shape != maskImg shape") return inputImg_h = inputImg.shape[0] inputImg_w = inputImg.shape[1] maskImg_h = maskImg.shape[0] maskImg_w = maskImg.shape[1] # inputImg图像尺寸不能小于maskImg if (inputImg_h < maskImg_h or inputImg_w < maskImg_w): print("inputImg size < maskImg size") return # 画图的位置不能超过原始图像 if (((drawPosition[0] + maskImg_w) > inputImg_w) or ((drawPosition[1] + maskImg_h) > inputImg_h)): print("drawPosition + maskImg > inputImg range") return outPutImg = inputImg.copy() input_roi = outPutImg[drawPosition[1]:drawPosition[1] + maskImg_h, drawPosition[0]:drawPosition[0] + maskImg_w] imgMask_array = np.zeros((maskImg_h, maskImg_w, maskImg.shape[2]), dtype=np.uint8) triangles_list = [contourData] cv2.fillPoly(imgMask_array, triangles_list, color=(1, 1, 1)) cv2.fillPoly(input_roi, triangles_list, color=(0, 0, 0)) # cv2.imshow('imgMask_array', imgMask_array) imgMask_array = imgMask_array * maskImg output_ori = input_roi + imgMask_array outPutImg[drawPosition[1]:drawPosition[1] + maskImg_h, drawPosition[0]:drawPosition[0] + maskImg_w] = output_ori return outPutImg if (__name__ == "__main__"): #原图 imgStr = r'img/1634081248.020272.jpg' #篡改后的车牌图像 imgMaskStr = r'plate.png' img = cv2.imread(imgStr) maskImg = cv2.imread(imgMaskStr) # w,h裁剪下来的原车牌size w=189 h=66 maskImg=cv2.resize(maskImg,(w,h)) print(maskImg.shape[1]) contourData = np.array([(0,0),(maskImg.shape[1],0),(maskImg.shape[1],maskImg.shape[0]),(0,maskImg.shape[0])]) #贴图的起始点,原车牌图像目标框的左上角点 x1=1668 y1=1360 outPutImg= mergeImg(img, maskImg, contourData, (x1,y1)) cv2.imwrite('result.png', outPutImg)
标签:inputImg,一张,cv2,贴到,shape,drawPosition,图像,maskImg,array 来源: https://www.cnblogs.com/h694879357/p/15942538.html