Python、OpenCV查找和覆盖/替换图像的部分
需要用其他图像查找并替换图像中的某些部分 原始图像如下所示。我想找到并替换:Python、OpenCV查找和覆盖/替换图像的部分,python,image,opencv,Python,Image,Opencv,需要用其他图像查找并替换图像中的某些部分 原始图像如下所示。我想找到并替换: blue squares to blue triangles orange squares to orange octagons 图像文件的位置如下所示: original.jpg, blue square.jpg, orange square.jpg, blue triangle.jpg, orange octagon.jpg 使用cv2,我可以找到目标图像的位置 import cv2 import num
blue squares to blue triangles
orange squares to orange octagons
图像文件的位置如下所示:
original.jpg, blue square.jpg, orange square.jpg, blue triangle.jpg, orange octagon.jpg
使用cv2,我可以找到目标图像的位置
import cv2
import numpy as np
img_original = cv2.imread("C:\\original.jpg")
img_to_replace_0 = cv2.imread("C:\\blue square.jpg")
img_to_replace_1 = cv2.imread("C:\\orange square.jpg")
img_overlay_0 = cv2.imread("C:\\blue triangle.jpg")
img_overlay_1 = cv2.imread("C:\\orange octagon.jpg")
res_0 = cv2.matchTemplate(img_original, img_to_replace_0, cv2.TM_CCOEFF_NORMED)
res_1 = cv2.matchTemplate(img_original, img_to_replace_1, cv2.TM_CCOEFF_NORMED)
threshold = 0.80
loc_0 = np.where (res_0 >= threshold)
loc_1 = np.where (res_1 >= threshold)
bl_0 = list(loc_0)
bl_1 = list(loc_1)
print bl_0
print bl_1
输出为:
[array([106, 294, 477]), array([17, 18, 21])]
[array([ 22, 210, 393]), array([16, 17, 20])]
继续前进的最佳方式是什么?CV2是这种情况下最好的工具吗?谢谢。是的,可以从那一点继续。您将获得每个轴的位置,第一个数组是
y
轴,第二个数组是x
轴。此位置是对象的左上角
这里可能存在的问题是模板的大小不同。。因此,它可能与原始图像中未出现的图像的其他部分重叠,我不知道这是否是一个问题。因此,如果这是一个问题,您可能必须确保它们都同样大,或者它们有足够的空间。这可以解决,例如将图像缩放为相同大小
无论如何,您可以实现如下替换:
import cv2
import numpy as np
# load the data
img = cv2.imread('scene.jpg')
blueTri = cv2.imread('blueTri.jpg')
blueSq = cv2.imread('blueSq.jpg')
bgColor = (255,255,255)
# find the matching rectangles
res = cv2.matchTemplate(img, blueSq, cv2.TM_CCOEFF_NORMED)
threshold = 0.98 # I used a higher threshold, because it was giving several "good" locations that are next to each other
loc = np.where (res >= threshold)
# process the positions
for i in range(len(loc[0])):
# it is given as (y,x)
pos = (loc[0][i], loc[1][i])
# delete the blue squares
img[pos[0]:pos[0]+blueSq.shape[0] , pos[1]:pos[1]+blueSq.shape[1] ] = bgColor
# put the new blue triangle
img[pos[0]:pos[0]+blueTri.shape[0] , pos[1]:pos[1]+blueTri.shape[1] ] = blueTri
cv2.imshow("Frame", img)
cv2.waitKey(0)
cv2.destroyAllWindows()
这是蓝色正方形到蓝色三角形的结果:
对于这两个更改,如您的示例所示:
如果两个模板的大小相等(即您要查找的图像和要替换的图像),则可以减少一个步骤并将for循环更改为:
# process the positions
for i in range(len(loc[0])):
# it is given as (y,x)
pos = (loc[0][i], loc[1][i])
# put the new blue triangle
img[pos[0]:pos[0]+blueTri.shape[0] , pos[1]:pos[1]+blueTri.shape[1] ] = blueTri
更新 要移动正在复制的形状,您可以移动整个ROI,无论如何,您已经删除了旧的ROI,这样就可以了。只是别忘了检查图像的限制。为此,您可以执行以下操作:
for i in range(len(loc[0])):
posToDelete = (loc[0][i], loc[1][i])
posToAdd = (loc[0][i] -10, loc[1][i]+15) # 10 pixels up and 15 to the right
posToAdd = (max(0, min(posToAdd[0],img.shape[0]-1 -blueTri.shape[0] )) , max(0, min(posToAdd[1],img.shape[1]-1-blueTri.shape[1]))) # sanity check to make sure it is inside the borders
img[posToDelete[0]:posToDelete[0]+blueSq.shape[0] , posToDelete[1]:posToDelete[1]+blueSq.shape[1] ] = bgColor
img[posToAdd[0]:posToAdd[0]+blueTri.shape[0] , posToAdd[1]:posToAdd[1]+blueTri.shape[1] ] = blueTri
谢谢你精彩的回答!解释是彻底的,并击中了正是关于较小的图像的大小点!出色的指导和对学习社区的巨大贡献@马克:不客气,如果你有任何问题,请随时提问:)如果它回答了你的问题,别忘了接受它祝你愉快!在将蓝色三角形粘贴到原始图像上时,如何将其稍微移动(左/右/上/下)?我试图在“.shape[1]”之后添加一个数字,但它不起作用。因此,基本上,它会在这个roi中粘贴这个图像,如果图像较小,它将只使用左上角。在您的例子中,只需移动上限,使其在可以粘贴的矩形中变大。例如,如果要将其向右移动,则必须通过更改两个限制来移动矩形。。。。看看我的最新答案太棒了!全世界的很多人都会从你的分享中受益!祝你有愉快的一天。