opencv - Removing black background and make transparent from grabcut output in python open cv -


i have been trying remove black background grabcut output using python opencv.

import numpy np import cv2  img = cv2.imread(r'myfile_1.png') mask = np.zeros(img.shape[:2],np.uint8)  bgdmodel = np.zeros((1,65),np.float64) fgdmodel = np.zeros((1,65),np.float64)  rect = (1,1,665,344) 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]  cv2.imshow('img',img) cv2.imwrite('img.png',img) cv2.waitkey(0) cv2.destroyallwindows() 

above code had written save grabcut output. please suggest, how can remove black background , make transparent?

enter image description here

enter image description here

i have achieved using following snippet.

import cv2 file_name = "grab.png"  src = cv2.imread(file_name, 1) tmp = cv2.cvtcolor(src, cv2.color_bgr2gray) _,alpha = cv2.threshold(tmp,0,255,cv2.thresh_binary) b, g, r = cv2.split(src) rgba = [b,g,r, alpha] dst = cv2.merge(rgba,4) cv2.imwrite("test.png", dst) 

enter image description here


Comments

Popular posts from this blog

asynchronous - C# WinSCP .NET assembly: How to upload multiple files asynchronously -

aws api gateway - SerializationException in posting new Records via Dynamodb Proxy Service in API -

asp.net - Problems sending emails from forum -