This is a follow on to a previously posted question about removing black backgrounds from .png images (Remove Background from Image - Python). The script I used in that Q&A works well but I still get minor inconsistencies in removing the black background exclusively while preserving everything inside the image contours. I tried a different script using the OpenCV grabcut algorithm, but I still get the same issue. I want to be able to remove the black background from the images without removing pixels inside image contours, here is the script I am using along with example input/output images upon running the script.
import numpy as np
import cv2
img = cv2.imread("C:\\users\\mdl518\\Desktop\\overhead_image.png") # Input 8-bit 3-channel image
mask = np.zeros(img.shape[:2], np.uint8) # img.shape[:2] = first, second values of img.shape
bgdModel = np.zeros((1,65), np.float64) # temporary array for background
fgdModel = np.zeros((1,65), np.float64) # temporary array for foreground
rect = (1,0,img.shape[1],img.shape[0]) # specify w,h beyond bounds of img.shape
# Grabcut algorithm to extract foreground (5 iterations) within region of interest
cv2.grabCut(img, mask, rect, bgdModel, fgdModel, 5, cv2.GC_INIT_WITH_RECT)
# Split RGB channels and create new alpha channel
r_channel, g_channel, b_channel = cv2.split(img)
a_channel = np.where((mask==2)|(mask==0), 0, 255).astype('uint8')
img_RGBA = cv2.merge((r_channel, g_channel, b_channel, a_channel)) # add alpha channel to image
cv2.imwrite(".//output_image.png", img_RGBA) ## write new image to file
Input Image:
Output Image (using script above):
Output Image (using "contour" method):
I am attempting to tweak my 'rect' bounds and the mask settings between 0 and 255, but I still cannot rectify this issue - The current settings seem to preserve the image the most without removing additional pixels, but it still does not preserve the whole image. Any assistance is most appreciated!