获取错误:(215:断言失败)使用seamlesscon时

2024-03-28 13:38:27 发布

您现在位置:Python中文网/ 问答频道 /正文

为清楚起见,我是Python的初学者。我正在创建一个脚本,用一个机器人从文件夹中的一系列图像中替换眼睛。我被一直抛出这个错误的seamlessClone函数卡住了。你知道吗

就编写整个脚本而言,代码是不完整的(到目前为止它只克隆了一只眼睛),但一切都应该正常。我已经在这件事上耽搁了6个小时了,我想我应该在这里问一下。你知道吗

我试过检查我的文件名,路径,查看图像文件是否被print损坏,更改不同尺寸的图像文件,文件类型(png,jpg)等等。你知道吗

我也尝试过将每个numpy数组(cv2\u图像,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'

我期望的结果是眼睛图像被克隆到原始图像上,但是这个错误一直被抛出,阻止我完成脚本。如果有任何关于发生了什么的暗示,我觉得罪魁祸首是“眼睛”档案,但我可能错了。任何帮助都将不胜感激。你知道吗


Tags: in图像imageimportnpmaskarraycv2
1条回答
网友
1楼 · 发布于 2024-03-28 13:38:27

有两组关键点:

  • 目标图像中有一组眼睛的关键点: 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)

相关问题 更多 >