使用 OpenCV 像素化 ROI 边界框并将其覆盖在原始图像上

Yos*_*gih 3 python opencv image-processing keras tensorflow

让我们直截了当。

我有私人项目在 open-cv 中使用边界框来阻止或像素化图像,类似于审查图像,灵感来自本文:

https://www.researchgate.net/publication/325746502_Seamless_Nudity_Censorship_an_Image-to-Image_Translation_Approach_based_on_Adversarial_Training

我已经找到了使用Keras对审查器区域进行分类的方法,但仍然不知道如何使用边界框将分类区域像素化,并将其叠加到原始图像上。任何帮助表示赞赏。

这是我想做的过程的示例:

在此处输入图片说明

nat*_*ncy 5

一个简单的方法是使用 Numpy 切片提取 ROI,像素化,然后将其粘贴回原始图像。我将使用如何在 Python 中使用 OpenCV 对图像进行像素化中找到的像素化技术. 这是一个简单的例子:


要提取的输入图像和 ROI

提取的投资回报率

像素化投资回报率

结果

代码

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()
Run Code Online (Sandbox Code Playgroud)

注意: ROI 边界框坐标是通过使用如何在没有 Guess & Check 的情况下获得 ROI Bounding Box Coordinates 中的脚本找到的。对于您的情况,我将假设您已经拥有x,y,w,h通过cv2.boundingRect.