出现错误:(-215:断言失败)使用SeamlessClone时

时间:2019-10-10 15:10:15

标签: python numpy opencv

为清楚起见,我是Python的初学者。我正在创建一个脚本,该脚本用文件夹中一系列图像中的机械手代替眼睛。我被困在SeamlessClone函数上,该函数不断抛出此错误。

就编写整个脚本而言,代码是不完整的(到目前为止,它只克隆了一只眼睛),但是一切都应该正常工作。我在这上面呆了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'

我期望的结果是将眼睛图像克隆到原始图像上,但是此错误不断抛出,从而使我无法完成脚本。如果对发生的事情有任何提示,我认为罪魁祸首是“ Eye”文件,但我可能是错的。任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

有两组关键点:

  • 目标图像中的眼睛有一组关键点: keypoints dst image
  • 源图像中有一组用于眼睛的关键点: keypoints source image

您使用了错误的关键点集来制作cv2.seamlessClone()函数的掩码。您应该使用源图像中的关键点。遮罩必须是两个通道图像,在您的代码中(除其他问题外)您正在使用三个通道图像。

这是结果。您可以看到还应该有一个调整大小功能以匹配眼睛的大小:

output

这是我使用的代码:

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)