我在Python中使用OpenCV。我使用以下代码选择主图像的一部分并将其复制到子图像中:
boundingBoxRotatedRect = cv2.minAreaRect(boxPoints)
if boundingBoxRotatedRect[2] < -45:
boundingBoxRotatedRect = (boundingBoxRotatedRect[0], (boundingBoxRotatedRect[1][1],boundingBoxRotatedRect[1][0]), boundingBoxRotatedRect[2] + 90)
M = cv2.getRotationMatrix2D(boundingBoxRotatedRect[0], boundingBoxRotatedRect[2], 1.0)
size = np.int0(boundingBoxRotatedRect[1])
size = (size[0],size[1])
dst = cv2.warpAffine(mainImage, M, (mainImage.shape[1], mainImage.shape[0]))
subImage = cv2.getRectSubPix(dst, size, boundingBoxRotatedRect[0])
其中boxPoints是4个点的数组,构成了从主图像中裁剪的区域周围的边界框,boundingBoxRotatedRect是表示为旋转的rect对象的相同框,M是旋转矩阵,size是宽度/边界框的高度,mainImage是被裁剪的主图像,subImage最终是从主图像裁剪的最终图像。下面链接的图片进一步解释了正在发生的事情。
我的问题是:如果我使用OpenCV绘图函数来编辑subImage,我怎样才能将那些相同的图形放回到mainImage的相应像素上?例如(如果在我提供的解释图像中使用图像形状)我在subImage上绘制一个直立的笑脸,如何将其转换为mainImage上正确位置的正确对齐笑脸?
答案 0 :(得分:2)
找到解决方案。单应!
将上述代码替换为:
#If bottomLeft = true, boxPoints[0] corresponds to btm-lft corner of subImage.
#If bottomLeft = false, boxPoints[0] corresponds to btm-right corner of subImage.
bottomLeft = True
boundingBoxRotatedRect = cv2.minAreaRect(boxPoints)
if boundingBoxRotatedRect[2] < -45:
boundingBoxRotatedRect = (boundingBoxRotatedRect[0], (boundingBoxRotatedRect[1][1],boundingBoxRotatedRect[1][0]), boundingBoxRotatedRect[2] + 90)
bottomLeft = False
M = cv2.getRotationMatrix2D(boundingBoxRotatedRect[0], boundingBoxRotatedRect[2], 1.0)
size = np.int0(boundingBoxRotatedRect[1])
size = (size[0],size[1])
dst = cv2.warpAffine(mainImage, M, (mainImage.shape[1], mainImage.shape[0]))
subImage = cv2.getRectSubPix(dst, size, boundingBoxRotatedRect[0])
#Get homography matrix
if bottomLeft:
pts_src = np.array([[0, size[0] - 1], [0, 0], [size[1] - 1, 0],[size[1] - 1, size[0] - 1]])
else:
pts_src = np.array([[size[1] - 1, size[0] - 1], [0, size[0] - 1], [0, 0], [size[1] - 1, 0]])
pts_dst = boxPoints
h, status = cv2.findHomography(pts_src, pts_dst)
## BELOW, REPLACE "[x, y], [X2, Y2], ...]" WITH LIST OF POINTS FROM SUBIMAGE TO BE TRANSLATED TO MAINIMAGE
a = np.array([[x, y], [X2, Y2], ...] dtype='float32')
a = np.array([a])
pointsOut = cv2.perspectiveTransform(a, h)
pointsOut = pointsOut[0]
#Then use the points in pointsOut to draw whatever you want!