I have scanned images of various products packagings as following
These are in different sizes. I need to remove white background on right and under. I have tried several solutions but only following code is giving me the best results.
import cv2
import numpy as np
# Load image, convert to grayscale, Gaussian blur, Otsu's threshold
image = cv2.imread("img.jpg")
original = image.copy()
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
blur = cv2.GaussianBlur(gray, (3,3), 0)
thresh = cv2.threshold(blur, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)[1]
# Obtain bounding rectangle and extract ROI
x,y,w,h = cv2.boundingRect(thresh)
cv2.rectangle(image, (x, y), (x + w, y + h), (36,255,12), 2)
ROI = original[y:y+h, x:x+w]
# Add alpha channel
b,g,r = cv2.split(ROI)
alpha = np.ones(b.shape, dtype=b.dtype) * 50
ROI = cv2.merge([b,g,r,alpha])
cv2.imwrite('thresh.jpg', thresh)
cv2.imwrite('image.jpg', image)
cv2.imwrite('ROI.jpg', ROI)
But still it is not cropping out whole white background as it can be seen in following image it only selects a part of it and then crops it. Please can some help me with how can I properly remove white background? Thank you.