[英]Moment of image into center of blank image (Python, OpenCV)
这是我的问题。 我尝试复制此图片(20x20): 放入由代码(28x28空白画布)创建且位置精确的新图片。 我想做的是将源图像的紫罗兰色点设置为新的(画布)图像的中心。 这是我要做的代码:
import cv2
import numpy as np
import os
# Read images : src image will be cloned into dst
im = cv2.imread(os.path.expanduser('~\\Desktop\\cube.png'))
obj = cv2.imread(os.path.expanduser('~\\Desktop\\testCV.png'))
# Create an all white mask
mask = 255 * np.ones(obj.shape, obj.dtype)
# The location of the center of the src in the dst
center = (int(10), int(13))
# Seamlessly clone src into dst and put the results in output
normal_clone = cv2.seamlessClone(obj, im, mask, center, cv2.NORMAL_CLONE)
# Write results
cv2.imwrite(os.path.expanduser('~\\Desktop\\fin.png'), normal_clone)
您怎么看是不完美的,右侧部分有些白色,导致我出现一些问题,我知道问题是“蒙版”,我尝试对其进行修改,但是当我更改1项时,代码不起作用。 您知道做相同想法的其他方法吗,或者也许我只需要对此进行修改。
谢谢
这不是我的代码,我发现了另一个问题,尝试了一下,效果很好。 这是代码:
import cv2
import os
import numpy as np
def findCenter(img):
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
th, threshed = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY_INV | cv2.THRESH_OTSU)
_, cnts, hierarchy = cv2.findContours(threshed, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
M = cv2.moments(cnts[0])
cX = int(M["m10"] / M["m00"])
cY = int(M["m01"] / M["m00"])
return (cX, cY)
img1 = cv2.imread("Path of the image you want to copy")
img2 = cv2.imread("Path of the image you want to use like a backgroud")
pt1 = findCenter(img1)
pt2 = findCenter(img2)
## (2) Calc offset
dx = (pt1[0] - pt2[0])
dy = (pt1[1] - pt2[1])
h, w = img2.shape[:2]
dst = img1.copy()
dst[dy:dy + h, dx:dx + w] = img2
cv2.imwrite(path + roi, dst)
这是原始答案: 匹配两个图像的中心(OpenCV,Python)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.