如何使用 python 和 opencv 将两个图像相互减去?

How do I subtract two images from each other using python and opencv?

这里的objective就是把人脸原图减去嘴巴。 现在我知道如何使用 dlib 和面部标志来裁剪嘴巴了(这就是这个脚本所做的)。

问题是如何修改原始图像,使其完全相同,除了没有嘴巴?基本上我想把它剪掉。

predictor_path = "/home/victor/facial-landmarks/shape_predictor_68_face_landmarks.dat"
faces_folder_path = "/home/victor/TryImage/"
detector = dlib.get_frontal_face_detector()
predictor = dlib.shape_predictor(predictor_path)
i = 0
for f in glob.glob(os.path.join(faces_folder_path, "*.jpg")):
    print("Processing file: {}".format(f))
    img = cv2.imread(f)

    dets = detector(img, 1)
    print("Number of faces detected: {}".format(len(dets)))
    for k, d in enumerate(dets):
        print("Detection {}: Left: {} Top: {} Right: {} Bottom: {}".format(
            k, d.left(), d.top(), d.right(), d.bottom()))

    shape = predictor(img, d)
    i += 1

    xmouthpoints = [shape.part(x).x for x in range(48,68)]
    ymouthpoints = [shape.part(x).y for x in range(48,68)]
    maxx = max(xmouthpoints)
    minx = min(xmouthpoints)
    maxy = max(ymouthpoints)
    miny = min(ymouthpoints)

    pad = 10

    filename = os.path.splitext(os.path.basename(f))[0]

    crop_image = img[(miny-pad):(maxy+pad),(minx-pad):(maxx+pad)]
    img = cv2.subtract(img, crop_image)
    imshow(crop_image, 1)
    imshow(new_image, 1)

    cv2.imwrite(str(filename) + '.jpg',crop_image)

使用cv2.subtract会产生以下错误:error: OpenCV(3.4.1) /io/opencv/modules/core/src/arithm.cpp:659: error: (-209) The operation is neither 'array op array' (其中数组具有相同的大小和相同的通道数),也不是 'array op scalar',也不是函数 arithm_op

中的 'scalar op array'

我也想过使用图像减法,可以吗?

即使是黑色或白色的方框或圆圈而不是嘴巴,也是完美的。

注意:我会使用 GIMP 或 Photoshop,但我要处理数以千计的自拍照,所以这不是一个真正的选择。

谢谢:)

我正在使用的图片附在此处。

My selfie

如您的错误所述,OpenCV subtract 需要 2 个形状相同的图像。在你的情况下,你给了一个大图像和一个小图像,OpenCV 如何知道要减去大图像的哪一部分?

话又说回来,看起来你想去掉嘴巴,它的位置只有黑色像素。您可以通过以下方式获得相同的结果:

cv2.rectangle(img, ((minx-pad), (miny-pad)), ((maxx-pad), (maxy-pad)), (0,0,0), -1)

甚至直接使用 numpy:

img[(miny-pad):(maxy+pad),(minx-pad):(maxx+pad)] = 0

或者如果你真的想使用减法:

crop_image = img[(miny-pad):(maxy+pad),(minx-pad):(maxx+pad)]
img[(miny-pad):(maxy+pad),(minx-pad):(maxx+pad)] = cv2.subtract(img[(miny-pad):(maxy+pad),(minx-pad):(maxx+pad)], crop_image)

在最后一个中,您要告诉减法:要减去图像的哪一部分以及完成后将结果放在哪里。我会推荐选项 2,直接使用 numpy 方式。

如有疑问,请给我留言