Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

python remove background

import cv2
import numpy as np

# load image
img = cv2.imread('person.png')

# convert to graky
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

# threshold input image as mask
mask = cv2.threshold(gray, 250, 255, cv2.THRESH_BINARY)[1]

# negate mask
mask = 255 - mask

# apply morphology to remove isolated extraneous noise
# use borderconstant of black since foreground touches the edges
kernel = np.ones((3,3), np.uint8)
mask = cv2.morphologyEx(mask, cv2.MORPH_OPEN, kernel)
mask = cv2.morphologyEx(mask, cv2.MORPH_CLOSE, kernel)

# anti-alias the mask -- blur then stretch
# blur alpha channel
mask = cv2.GaussianBlur(mask, (0,0), sigmaX=2, sigmaY=2, borderType = cv2.BORDER_DEFAULT)

# linear stretch so that 127.5 goes to 0, but 255 stays 255
mask = (2*(mask.astype(np.float32))-255.0).clip(0,255).astype(np.uint8)

# put mask into alpha channel
result = img.copy()
result = cv2.cvtColor(result, cv2.COLOR_BGR2BGRA)
result[:, :, 3] = mask

# save resulting masked image
cv2.imwrite('person_transp_bckgrnd.png', result)

# display result, though it won't show transparency
cv2.imshow("INPUT", img)
cv2.imshow("GRAY", gray)
cv2.imshow("MASK", mask)
cv2.imshow("RESULT", result)
cv2.waitKey(0)
cv2.destroyAllWindows()
Comment

PREVIOUS NEXT
Code Example
Python :: python how to use input 
Python :: django template tag multiple arguments 
Python :: python falsy values 
Python :: how to use ggplot matplotlib 
Python :: python obtain data from pandas dataframe without index name 
Python :: localize timezone python 
Python :: new window selenium python 
Python :: how to add column to np array 
Python :: natural log and log base 10 in python 
Python :: how to count unique values in a column dataframe in python 
Python :: how do you see if a data type is an integer python 
Python :: django update model 
Python :: remove all integers from list python 
Python :: html to docx python 
Python :: django models using Value 
Python :: starting vscode on colab 
Python :: upload py file using flask 
Python :: pandas transpose 
Python :: boto signed url 
Python :: python string match ignore case 
Python :: how to use one with as statement to open two files python 
Python :: add to middle of list python 
Python :: print textbox value in tkinter 
Python :: how to say something python 
Python :: python random integer in range 
Python :: copy a dict in python 
Python :: show image with opencv2 
Python :: python pair two lists into a dictionary 
Python :: create alinked list inb pyhton 
Python :: console.log() python 
ADD CONTENT
Topic
Content
Source link
Name
4+6 =