Home > Net >  How to fill the background of a cropped image
How to fill the background of a cropped image

Time:10-01

At the moment I have an image that I crop in OpenCV with the given x,y coordinates. I'm trying to detect the white pixels on said image and display them. The code works fine but on some frames of the example video I'm using to make screenshots, the crop background includes elements with white in it aswell.

I have the following code:

import cv2

img = cv2.imread('Image_crop.jpg')
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
ret,gray = cv2.threshold(gray, 150,255,0)
gray2 = gray.copy()

cv2.imshow('IMG',gray2)
cv2.waitKey(0)
cv2.destroyAllWindows()

Crop output:

crop output

White pixel output:

white pixel output

Wanted output:

wanted output

Is there a way to fill the background of the cropped image or get the wanted output?

CodePudding user response:

Well, it's not as easy as it looks. The simple solution could be using cv2.grabCut ( Sample tutorial ) , but it is not going to get you perfect results.

Instead of cropping the image at first, give the boundary to cv2.grabCut, mask the background and crop the image afterward.

img = cv2.imread('image.jpg') 
mask = np.zeros(img.shape[:2], np.uint8)

bgdModel = np.zeros((1, 65), np.float64)
fgdModel = np.zeros((1, 65), np.float64)

rect = (x, y, width, height) # boundary of interest
cv2.grabCut(img, mask, rect, bgdModel, fgdModel, 5, cv2.GC_INIT_WITH_RECT)
mask2 = np.where((mask == 2) | (mask == 0), 0, 1).astype('uint8')
img = img * mask2[:, :, np.newaxis]

# crop the image and ...

If the input is video, you could increase your performance using object tracking algorithms.

  • Related