【问题标题】:Getting Error: (-215:Assertion failed) When Using seamlessClone出现错误:(-215:断言失败)使用无缝克隆时
【发布时间】:2019-10-11 00:27:31
【问题描述】:

为了清楚起见,我是 Python 的初学者。我正在创建一个脚本,用文件夹中的一系列图像中的机器人替换眼睛。我被困在不断抛出此错误的无缝克隆功能上。

就写出整个脚本而言,代码是不完整的(到目前为止它只克隆了一只眼睛),但一切都应该正常工作。我已经在这个问题上停留了 6 个小时,我想我会在这里问。

我尝试检查我的文件名、路径、使用打印查看图像文件是否损坏、更改不同尺寸的图像文件、文件类型(png、jpg)等。

我还尝试将每个 numpy 数组(cv2_image,Eye)转换为 32 位数组,看看是否是问题所在,但没有成功。

# Import

from PIL import Image, ImageDraw, ImageFilter
from statistics import mean
import face_recognition
import cv2
import glob
import numpy as np


# Open Eye Images

Eye = cv2.imread('eye.jpg')

# Loop Through Images

for filename in glob.glob('images/*.jpg'):

    cv2_image = cv2.imread(filename)
    image = face_recognition.load_image_file(filename)
    face_landmarks_list = face_recognition.face_landmarks(image)
    for facemarks in face_landmarks_list:

        # Get Eye Data
        eyeLPoints = facemarks['left_eye']
        eyeRPoints = facemarks['right_eye']
        npEyeL = np.array(eyeLPoints)
        npEyeR = np.array(eyeRPoints)

        # Create Mask

        mask = np.zeros(cv2_image.shape, cv2_image.dtype)
        mask.fill(0)
        poly = np.array([eyeLPoints])
        cv2.fillPoly(mask, [poly], (255,255, 255))

        # Get Eye Image Centers 

        npEyeL = np.array(eyeLPoints)
        eyeLCenter = npEyeL.mean(axis=0).astype("int")
        x1 = (eyeLCenter[0])
        x2 = (eyeLCenter[1])
        print(x1, x2)

        # Get Head Rotation (No code yet)

        # Apply Seamless Clone To Main Image
        saveImage = cv2.seamlessClone(Eye, cv2_image, mask ,(x1, x2) , cv2.NORMAL_CLONE)

# Output and Save

cv2.imwrite('output/output.png', saveImage)

这是完整的错误:

Traceback (most recent call last):
  File "stack.py", line 45, in <module>
    saveImage = cv2.seamlessClone(Eye, cv2_image, mask ,(x1, x2) , cv2.NORMAL_CLONE)
cv2.error: OpenCV(4.1.1) /io/opencv/modules/core/src/matrix.cpp:466: error: (-215:Assertion failed) 0 <= roi.x && 0 <= roi.width && roi.x + roi.width <= m.cols && 0 <= roi.y && 0 <= roi.height && roi.y + roi.height <= m.rows in function 'Mat'

我期望的结果是眼睛图像被克隆到原始图像上,但是这个错误一直被抛出,阻止我完成脚本。如果对发生的事情有任何暗示,我觉得罪魁祸首是“眼睛”文件,但我可能是错的。任何帮助将不胜感激。

【问题讨论】:

    标签: python numpy opencv


    【解决方案1】:

    有两组关键点:

    • 目标图像中有一组眼睛的关键点:
    • 源图像中有一组眼睛的关键点:

    您使用了错误的一组关键点来为cv2.seamlessClone() 函数制作掩码。您应该使用源图像中的关键点。掩码必须是双通道图像,在您的代码中(以及其他问题)您使用的是三通道图像。

    这就是结果。你可以看到还应该有一个 resize 函数来匹配眼睛的大小:

    这是我使用的代码:

    import face_recognition
    import cv2
    import numpy as np
    
    # Open Eye Images
    eye = cv2.imread('eye.jpg')
    
    # Open Face image
    face = cv2.imread('face.jpeg')
    # Get Keypoints
    image = face_recognition.load_image_file('face.jpeg')
    face_landmarks_list = face_recognition.face_landmarks(image)
    for facemarks in face_landmarks_list:
        # Get Eye Data
        eyeLPoints = facemarks['left_eye']
        eyeRPoints = facemarks['right_eye']
        npEyeL = np.array(eyeLPoints)
    
    # These points define the contour of the eye in the EYE image
    poly_left = np.array([(51, 228), (100, 151), (233, 102), (338, 110), (426, 160), (373, 252), (246, 284), (134, 268)], np.int32)
    
    # Create a mask for the eye
    src_mask = np.zeros(face.shape, face.dtype)
    cv2.fillPoly(src_mask, [poly_left], (255, 255, 255))
    cv2.imwrite('src_mask.png', src_mask)
    # Find where the eye should go
    center, r = cv2.minEnclosingCircle(npEyeL)
    center = tuple(np.array(center, int))
    # Clone seamlessly.
    output = cv2.seamlessClone(eye, face, src_mask, center, cv2.NORMAL_CLONE)
    

    【讨论】:

    • 感谢您的快速回复!我不敢相信我忽略了这一点,因为最初我打算在眼睛周围创建一个蒙版,并将眼睛图像粘贴到源图像后面。我想我太沉迷于我的旧剧本了。不过我确实有一个问题。 “(在其他问题中)”您能否对此提供一些明确的说明?
    • 当然!很高兴我能帮上忙。存在三个问题(1)遮罩是三个通道而不是两个通道,(2)使用了错误的关键点集,以及(3)需要调整大小以使源眼睛的大小与目标眼睛匹配.
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-04
    • 2021-09-10
    • 2022-01-14
    • 1970-01-01
    • 2020-03-29
    • 2021-03-14
    相关资源
    最近更新 更多