小编Ric*_*oul的帖子

如何使用 Python 用新图像替换图像中的轮廓(矩形)?

我目前正在使用 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 = …
Run Code Online (Sandbox Code Playgroud)

python opencv image-processing computer-vision pillow

5
推荐指数
2
解决办法
4279
查看次数