Ric*_*oul 5 python opencv image-processing computer-vision pillow
我目前正在使用 opencv (CV2) 和 Python Pillow 图像库来尝试拍摄任意手机的图像并用新图像替换屏幕。我已经到了可以拍摄图像并识别手机屏幕并获得角落的所有坐标的地步,但是我很难用新图像替换图像中的那个区域。
我到目前为止的代码:
import cv2
from PIL import Image
image = cv2.imread('mockup.png')
edged_image = cv2.Canny(image, 30, 200)
(contours, _) = cv2.findContours(edged_image.copy(), cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
contours = sorted(contours, key = cv2.contourArea, reverse = True)[:10]
screenCnt = None
for contour in contours:
peri = cv2.arcLength(contour, True)
approx = cv2.approxPolyDP(contour, 0.02 * peri, True)
# if our approximated contour has four points, then
# we can assume that we have found our screen
if len(approx) == 4:
screenCnt = approx
break
cv2.drawContours(image, [screenCnt], -1, (0, 255, 0), 3)
cv2.imshow("Screen Location", image)
cv2.waitKey(0)
Run Code Online (Sandbox Code Playgroud)
我还可以使用这行代码获取屏幕角的坐标:
screenCoords = [x[0].tolist() for x in screenCnt]
// [[398, 139], [245, 258], [474, 487], [628, 358]]
Run Code Online (Sandbox Code Playgroud)
但是,我一生都无法弄清楚如何拍摄新图像并将其缩放到我找到的坐标空间的形状并将图像叠加在上面。
我的猜测是,我可以使用 Pillow 中的图像变换来执行此操作,该函数是我从这个 stackoverflow 问题改编的:
def find_transform_coefficients(pa, pb):
"""Return the coefficients required for a transform from start_points to end_points.
args:
start_points -> Tuple of 4 values for start coordinates
end_points --> Tuple of 4 values for end coordinates
"""
matrix = []
for p1, p2 in zip(pa, pb):
matrix.append([p1[0], p1[1], 1, 0, 0, 0, -p2[0]*p1[0], -p2[0]*p1[1]])
matrix.append([0, 0, 0, p1[0], p1[1], 1, -p2[1]*p1[0], -p2[1]*p1[1]])
A = numpy.matrix(matrix, dtype=numpy.float)
B = numpy.array(pb).reshape(8)
res = numpy.dot(numpy.linalg.inv(A.T * A) * A.T, B)
return numpy.array(res).reshape(8)
Run Code Online (Sandbox Code Playgroud)
但是,我有点不知所措,我无法正确了解细节。有人能给我一些帮助吗?
编辑
好的,现在我正在使用 getPerspectiveTransform 和 warpPerspective 函数,我有以下附加代码:
screenCoords = numpy.asarray(
[numpy.asarray(x[0], dtype=numpy.float32) for x in screenCnt],
dtype=numpy.float32
)
overlay_image = cv2.imread('123.png')
overlay_height, overlay_width = image.shape[:2]
input_coordinates = numpy.asarray(
[
numpy.asarray([0, 0], dtype=numpy.float32),
numpy.asarray([overlay_width, 0], dtype=numpy.float32),
numpy.asarray([overlay_width, overlay_height], dtype=numpy.float32),
numpy.asarray([0, overlay_height], dtype=numpy.float32)
],
dtype=numpy.float32,
)
transformation_matrix = cv2.getPerspectiveTransform(
numpy.asarray(input_coordinates),
numpy.asarray(screenCoords),
)
warped_image = cv2.warpPerspective(
overlay_image,
transformation_matrix,
(background_width, background_height),
)
cv2.imshow("Overlay image", warped_image)
cv2.waitKey(0)
Run Code Online (Sandbox Code Playgroud)
图像正在正确旋转和倾斜(我认为),但它与屏幕的大小不同。它的“较短”:
如果我使用垂直非常高的不同图像,我最终会得到太“长”的东西:
我是否需要应用额外的转换来缩放图像?不确定这里发生了什么,我认为透视变换会使图像自动缩放到提供的坐标。
我下载了你的图像数据并在我的本地机器上重现了问题以找到解决方案。也已下载lenna.png以适合手机屏幕。
import cv2
import numpy as np
# Template image of iPhone
img1 = cv2.imread("/Users/anmoluppal/Downloads/46F1U.jpg")
# Sample image to be used for fitting into white cavity
img2 = cv2.imread("/Users/anmoluppal/Downloads/Lenna.png")
rows,cols,ch = img1.shape
# Hard coded the 3 corner points of white cavity labelled with green rect.
pts1 = np.float32([[201, 561], [455, 279], [742, 985]])
# Hard coded the same points on the reference image to be fitted.
pts2 = np.float32([[0, 0], [512, 0], [0, 512]])
# Getting affine transformation form sample image to template.
M = cv2.getAffineTransform(pts2,pts1)
# Applying the transformation, mind the (cols,rows) passed, these define the final dimensions of output after Transformation.
dst = cv2.warpAffine(img2,M,(cols,rows))
# Just for Debugging the output.
final = cv2.addWeighted(dst, 0.5, img1, 0.5, 1)
cv2.imwrite("./garbage.png", final)
Run Code Online (Sandbox Code Playgroud)
您可以通过以下方式将新图像(旋转到手机屏幕方向)叠加到原始图像上
import cv2
A_img = cv2.imread("new_image.png")
B_img = cv2.imread("larger_image.jpg")
x_offset=y_offset=50
B_img[y_offset:y_offset+A_img.shape[0], x_offset:x_offset+A_img.shape[1]] = A_img
Run Code Online (Sandbox Code Playgroud)
如果需要,您可以使用 Alpha 通道适当旋转新图像。
正如您在下面的评论中提到的(在透视变换标题下),新图像需要进行透视变换(扭曲)。请查看下面的链接,了解透视变换如何将扭曲的图像修复为直的(您想要相反的效果)。
http://docs.opencv.org/master/da/d6e/tutorial_py_geometric_transformations.html#gsc.tab=0
Run Code Online (Sandbox Code Playgroud)
您基本上需要在原始空间和扭曲空间中提供 4 个点(pts1 和 pts2)进行转换。
我认为您也许可以使用要插入的原始图像的四个角(pts1),并且轮廓的角(pts2)应该可以工作。