OpenCV BodyPix掩模

OpenCV BodyPix掩模,opencv,bodypix,Opencv,Bodypix,我试图在图像上画一个遮罩,遮罩是body pix(在NodeJS中)处理的结果。出于性能原因,我想使用OpenCV来绘制遮罩,而不是htmlcanva const segmentation = await net.segmentPersonParts(img, { flipHorizontal: false, internalResolution: 'medium', segmentationThreshold: 0.7 });

我试图在图像上画一个遮罩,遮罩是body pix(在NodeJS中)处理的结果。出于性能原因,我想使用OpenCV来绘制遮罩,而不是htmlcanva

    const segmentation = await net.segmentPersonParts(img, {
        flipHorizontal: false,
        internalResolution: 'medium',
        segmentationThreshold: 0.7
    });

    //Mask into opencv Mat
    const segmentationMask = new cv.Mat(segmentation.data, segmentation.height, segmentation.width, cv.CV_8UC4);
    const mask = segmentationMask.cvtColor(cv.COLOR_BGRA2BGR);
    //Application of mask
    const result = mat.bitwiseAnd(mask);
    cv.imwrite('mask.jpg', mask);
    cv.imwrite('result.jpg', result);
这项工作非常完美,并实现了在检测到的人身上绘制黑色面具(语义分割)的预期结果。但是
SegmentPersonParts
SegmentPerson
方法慢得多,我希望使用最后一种方法。问题是,面具不起作用。执行以下操作时:

    const segmentation = await net.segmentPerson(img, {
        flipHorizontal: false,
        internalResolution: 'medium',
        segmentationThreshold: 0.7
    });

    //Mask into opencv Mat
    const segmentationMask = new cv.Mat(segmentation.data, segmentation.height, segmentation.width, cv.CV_8UC4);
    const mask = segmentationMask.cvtColor(cv.COLOR_BGRA2BGR);
    //Application of mask
    const result = mat.bitwiseAnd(mask);
    cv.imwrite('mask.jpg', mask);
    cv.imwrite('result.jpg', result);

结果只是一个黑色图像,因为遮罩没有正确构建。如何解决这个问题?

我有一个脚本,可以使用OpenCV Python在图像中实现这一点:

import cv2

def pixelate(image):
    # Get input size
    height, width, _ = image.shape

    # Desired "pixelated" size
    h, w = (16, 16)

    # Resize image to "pixelated" size
    temp = cv2.resize(image, (w, h), interpolation=cv2.INTER_LINEAR)

    # Initialize output image
    return cv2.resize(temp, (width, height), interpolation=cv2.INTER_NEAREST)

# Load image
image = cv2.imread('1.png')

# ROI bounding box coordinates
x,y,w,h = 122,98,283,240

# Extract ROI
ROI = image[y:y+h, x:x+w]

# Pixelate ROI
pixelated_ROI = pixelate(ROI)

# Paste pixelated ROI back into original image
image[y:y+h, x:x+w] = pixelated_ROI

cv2.imshow('pixelated_ROI', pixelated_ROI)
cv2.imshow('image', image)
cv2.waitKey()
您需要获取
边界框
坐标,并在
ROI
上使用它