How to Remove Background of Images in Python?
Python is one of the interesting languages. Its capability and variety of use cases make it a handy programming language to master. Python is a great way to enhance your skills and research, and there are countless opportunities for learning.
remove bg from pic using pthon
on Jan 12, 2021
import cv2
import numpy as np
#== Parameters =======================================================================
BLUR = 21
CANNY_THRESH_1 = 10
CANNY_THRESH_2 = 200
MASK_DILATE_ITER = 10
MASK_ERODE_ITER = 10
MASK_COLOR = (0.0,0.0,1.0) # In BGR format
#== Processing =======================================================================
#-- Read image -----------------------------------------------------------------------
img = cv2.imread('C:/Temp/person.jpg')
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
#-- Edge detection -------------------------------------------------------------------
edges = cv2.Canny(gray, CANNY_THRESH_1, CANNY_THRESH_2)
edges = cv2.dilate(edges, None)
edges = cv2.erode(edges, None)
#-- Find contours in edges, sort by area ---------------------------------------------
contour_info = []
_, contours, _ = cv2.findContours(edges, cv2.RETR_LIST, cv2.CHAIN_APPROX_NONE)
# Previously, for a previous version of cv2, this line was:
# contours, _ = cv2.findContours(edges, cv2.RETR_LIST, cv2.CHAIN_APPROX_NONE)
# Thanks to notes from commenters, I've updated the code but left this note
for c in contours:
contour_info.append((
c,
cv2.isContourConvex(c),
cv2.contourArea(c),
))
contour_info = sorted(contour_info, key=lambda c: c[2], reverse=True)
max_contour = contour_info[0]
#-- Create empty mask, draw filled polygon on it corresponding to largest contour ----
# Mask is black, polygon is white
mask = np.zeros(edges.shape)
cv2.fillConvexPoly(mask, max_contour[0], (255))
#-- Smooth mask, then blur it --------------------------------------------------------
mask = cv2.dilate(mask, None, iterations=MASK_DILATE_ITER)
mask = cv2.erode(mask, None, iterations=MASK_ERODE_ITER)
mask = cv2.GaussianBlur(mask, (BLUR, BLUR), 0)
mask_stack = np.dstack([mask]*3) # Create 3-channel alpha mask
#-- Blend masked img into MASK_COLOR background --------------------------------------
mask_stack = mask_stack.astype('float32') / 255.0 # Use float matrices,
img = img.astype('float32') / 255.0 # for easy blending
masked = (mask_stack * img) + ((1-mask_stack) * MASK_COLOR) # Blend
masked = (masked * 255).astype('uint8') # Convert back to 8-bit
cv2.imshow('img', masked) # Display
cv2.waitKey()
#cv2.imwrite('C:/Temp/person-masked.jpg', masked) # Save
Source: stackoverflow.com
0
This book focuses on the various features of Python that can help make your life easy if you want to optimize your experience using this powerful language.
Python answers related to "remove bg from pic using pthon"
remove bg from pic using pthon how to get the url of the current page in selenium python linux uninstall python pycharm remove not in use imports remove element from list remove last element from dictionary python linux uninstall python pythone remove list pythone remove list remove charachter from string remove charachter from string remove charachter from string uninstall python ubuntu how to remove letters from string pdb remove breakpoint remove element from list remove element from list remove element from list how to remove letters from string how to remove brackets from list in python text file vba make word from bytes remove elements from dictionary python remove elements from dictionary python pythone remove list pythone remove list drop na how to remove last 2 elements from list in python how to remove last 2 elements from list in python how to remove empty elements in a list python python list remove at index
View All Python queries
Python queries related to "remove bg from pic using pthon"
remove bg from pic using pthon delete database entry using name django bind keyboard enter key to a function using tkinter how to make html files open in chrome using python how to reverse a list in python using for loop launch a script from python using threading write data to using pickle compute confusion matrix using python transpose of a matrix using numpy dictionary comprehension using while copying elements from another dictionary in python how to input elements in list in python using for loop pycharm remove not in use imports python remove empty list remove element from list remove nans and infs python remove last element from dictionary python remove scientific notation python matplotlib python list remove at index python string remove whitespace and newlines remove zeroes from beginning and end how to remove letters from string pdb remove breakpoint remove repetitive characters from the specified column of a given DataFrame pythone remove list remove charachter from string how to remove empty elements in a list python how to remove last 2 elements from list in python remove elements from dictionary python how to remove brackets from list in python text file
Browse Other Code Languages
Abap ActionScript Assembly BASIC C C# C++ Clojure Cobol CSS Dart Delphi Elixir Erlang F# Fortran Go Groovy Haskell Html Java Javascript Julia Kotlin Lisp Lua Matlab Objective-C Pascal Perl PHP PostScript Prolog Python R Ruby Rust Scala Scheme Shell/Bash Smalltalk SQL Swift TypeScript VBA WebAssembly Whatever