Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to remove background of images in python

I have a dataset that contains full width human images I want to remove all the backgrounds in those Images and just leave the full width person,

my questions:

is there any python code that does that ?

and do I need to specify each time the coordinate of the person object?

enter image description here

like image 677
reham501 Avatar asked Jul 20 '20 18:07

reham501


People also ask

How do I remove the background of an image?

Select the picture that you want to remove the background from. On the toolbar, select Picture Format > Remove Background, or Format > Remove Background. If you don't see Remove Background, make sure you have selected a picture.

How do you separate an object from the background in Python?

BackgroundSubtractorMOG2() is used to remove the background from video frames. When it comes to images you can use the GrabCut Algorithm .

What is background removal in image processing?

Background removal is a digital image processing procedure that can be used to classify parts of an im- age in terms of unwanted and interest regions. Many applications of image processing and computer vi- sion require background removal before further anal- ysis and processing.

How do I remove the background of a logo?

Find the "Background Remover" in the tools menu on the left. Then, upload or drag and drop your logo from your computer. Fotor will remove background from logo automatically. Resize, rotate, and crop your logo, or add a new background if needed.


1 Answers

Here is one way using Python/OpenCV.

  • Read the input
  • Convert to gray
  • Threshold and invert as a mask
  • Optionally apply morphology to clean up any extraneous spots
  • Anti-alias the edges
  • Convert a copy of the input to BGRA and insert the mask as the alpha channel
  • Save the results

Input:

enter image description here

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()

Transparent result:

enter image description here

like image 118
fmw42 Avatar answered Oct 03 '22 07:10

fmw42