用python屏蔽图像

问题描述 投票:0回答:1

我有一个二进制掩码和相同大小的图像。我想掩盖人类。我使用了np.multiply,但它给出的图像如下所示,但是,我希望使用人像而不是白色像素!:我绘制了蒙版和图像。它们是正常的,但是它们的乘法却不像我期望的那样。

enter image description here

import cv2
import os
import numpy as np
masks32x32 = "./DATA/resized_masks/"
images500x500 = "./DATA/resized_imgs/"

masks = os.listdir(masks32x32)
images = os.listdir(images500x500)

for inx, img in enumerate(images):
    img_array = cv2.imread(images500x500+img)
    mask_array = cv2.imread(masks32x32+img)
    mask120x120 = cv2.resize(mask_array,(500,500),interpolation=cv2.INTER_AREA)
    mask120x120 = np.divide(mask120x120, 255)

    masked_image=np.multiply(img_array, mask120x120)
    cv2.imshow("image",masked_image)
    cv2.waitKey(0)
    cv2.destroyAllWindows()
python crop mask
1个回答
0
投票

感谢Abitbol。我使用此链接解决了问题:OpenCV Error: Assertion failed ((mtype == CV_8U || mtype == CV_8S) && _mask.sameSize(*psrc1)) in cv::binary_op

我在这里张贴。也许对他们有帮助。我更正了以下代码。现在工作正常。

import cv2
import os
import numpy as np
masks32x32 = "./DATA/resized_masks/"
images500x500 = "./DATA/resized_imgs/"

masks = os.listdir(masks32x32)
images = os.listdir(images500x500)

for inx, img in enumerate(images):
    img_array = cv2.imread(images500x500+img)
    mask_array = cv2.imread(masks32x32+img)
    mask500x500 = cv2.resize(mask_array,(500,500),interpolation=cv2.INTER_AREA)
    print(mask500x500.shape)
    print(img_array.shape)

    mask500x500 = cv2.cvtColor(mask500x500, cv2.COLOR_BGR2GRAY)
    #mask500x500 = np.divide(mask500x500, 255)
    mask500x500 = mask500x500.astype(np.uint8)
    masked_image=cv2.bitwise_and(img_array, img_array, mask=mask500x500)
    #masked_image=np.multiply(img_array, mask120x120)
    cv2.imshow("image",masked_image)
    cv2.waitKey(0)
    cv2.destroyAllWindows()
© www.soinside.com 2019 - 2024. All rights reserved.