the task is to remove the blue background in the picture, how can I implement that?
question from:https://stackoverflow.com/questions/66059653/remove-blue-background-color-in-python-opencvthe task is to remove the blue background in the picture, how can I implement that?
question from:https://stackoverflow.com/questions/66059653/remove-blue-background-color-in-python-opencvYou should listen @fmw42, as he suggested, you could perform color segmentation to get the binary mask. Then using binary mask, you can remove the background.
cv2.inRange
to obtain a binary maskcv2.bitwise_and
. Arithmetic operation and is highly useful for defining roi in hsv colored images.Code:
import cv2
import numpy as np
# Load the image
img = cv2.imread("iSXsL.jpg")
# Convert to HSV color-space
hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
# Perform color-segmentation to get the binary mask
lwr = np.array([0, 0, 0])
upr = np.array([179, 255, 146])
msk = cv2.inRange(hsv, lwr, upr)
# Extracting the rod using binary-mask
krn = cv2.getStructuringElement(cv2.MORPH_RECT, (5, 3))
dlt = cv2.dilate(msk, krn, iterations=5)
res = 255 - cv2.bitwise_and(dlt, msk)
# Display
cv2.imshow("res", res)
cv2.waitKey(0)