Thanks Your time, I am a newbie in opencv and I want to extract foreground from my image by making the background white. So far, I have been able to convert the image background to black, how can I make it white..??
import numpy as np
import cv2 as cv
import cv2
import glob
import os
image_data_path = os.path.abspath('../8470p/corn_weed_datasets/bluegrass')
gt_data_path = os.path.abspath('../8470p/GT data')
image_out_path = os.path.abspath('../8470p/corn_weed_datasets/corn1')
curr_image = 0
for image_name in sorted(os.listdir(image_data_path)):
curr_image += 1
image_path = os.path.join(image_data_path, image_name)
img = cv2.imread(image_path)
hsv = cv.cvtColor(img, cv.COLOR_BGR2HSV)
l_b = np.array([23,26,12])
# the array is the our range of lower blue colors
u_b = np.array([277,255,277])
# the array is the our range of upper blue colors
mask = cv.inRange(hsv, l_b, u_b)
# threshold d hsv image to get only d blue color
res = cv2.bitwise_and(img,img, mask = mask)
Image = cv2.cvtColor(res, cv2.COLOR_RGB2BGR)
cv.imshow('res',Image)
k = cv.waitKey(0) & 0xFF
if k ==27:
break
for bb,file in enumerate (glob.glob(image_data_path)):
cv2.imwrite('../8470p/corn_weed_datasets/corn1/bluegrass{}.png'.format(bb), Image)
# cv2.imwrite(os.path.join(image_out_path,Image ))
cv.destroyAllWindows()



