3

Lets make it straightforward.

I have private project to block or pixelate image using boundary box in open-cv, something like censoring image, inspired from this paper:

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

I have found the way to classify the area of censor using Keras, but still don't know the way how to use the boundary box to pixelate the classified area, and overlay it to original image. Any help are appreciated.

This is the example of the process that I want to do:

enter image description here

nathancy
  • 42,661
  • 14
  • 115
  • 137
  • thank you for the response, however, i still not found the process to only pixelate ONLY the area that classified from bounding box and overlay it –  Jan 17 '20 at 02:47

1 Answers1

5

A simple method is to extract the ROI using Numpy slicing, pixelate, then paste it back into the original image. I will be using the pixelation technique found in how to pixelate image using OpenCV in Python?. Here's a simple example:


Input image and ROI to be extracted

Extracted ROI

Pixelated ROI

Result

Code

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()

Note: The ROI bounding box coordinates were found by using the script in how to get ROI Bounding Box Coordinates without Guess & Check. For your case, I will assume that you already have the x,y,w,h bounding box coordinates obtained by cv2.boundingRect.

nathancy
  • 42,661
  • 14
  • 115
  • 137
  • but i have another question, according to this question, what if i want to pixelate the image from the bounding box from my classifier-guessed (from Keras), not from pre-processed bound box? –  Jan 22 '20 at 05:07
  • I assumed you already had the bounding box coordinates, if you have the bounding box from Keras that means you have the coordinates – nathancy Jan 22 '20 at 20:43