How to remove white borders/edges around people in an image using python?


Micha Amir Cohen

I want to remove the white border between the black mask and the body image

Image input example:

enter image description here

Image output with thickness 1:

enter image description here

Image output with thickness 2:

enter image description here

I tried some games with blur and threshold found here, I also used this code to find and draw contours

    thickness = 3
    image = cv2.imread('../finetune/22.png')
    blank_mask = np.zeros(image.shape, dtype=np.uint8)
    gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

    cnts = cv2.findContours(gray, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
    cnts = cnts[0] if len(cnts) == 2 else cnts[1]

    cv2.drawContours(image, cnts, -1, (255,0,0), thickness)
    cv2.imshow('image', image)
    cv2.imwrite('../finetune/22-'+str(thickness)+'r.png',image)
    cv2.waitKey()

But the contours I found are the black mask edges, not the white lines I hit with the thickness, which works fine, but on each image the contours are different and the thickness is not equal throughout the image

What's the best precise way to remove it?

childish

Here are two ways:

method 1: cv2.erode()

You can use erosion is to erode away the boundaries of the white foreground object. Essentially the idea is to perform 2D convolution with a kernel. A kernel can be created using cv2.getStructuingElement() where you can pass the shape and size of the desired kernel to create. Typical kernels are cv2.MORPH_RECT, cv2.MORPH_ELLIPSE, or cv2.MORPH_CROSS. The kernel slides through the image where a pixel is considered a 1 if all the pixels under the kernel is 1 otherwise it is eroded to 0. The net effect is that all pixels on the boundaries will be discarded depending on the shape and size of the kernel. The thickness of the foreground decreases and is useful for removing small white noise or to detach objects. You can adjust the strength of the erosion with the number of iterations to perform.

kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (3,3))
erode = cv2.erode(image, kernel, iterations=1)

Method #1: Opening with cv2.morphologyEx()

The opposite of erosion is dilation, which enhances the image. Usually, the effect of dialing to "normalize" the shape manipulation is performed after corrosion. OpenCV combines these steps into one operation called morphological opening. Opening is just another name for eroding and then dilating, opening will usually give you smoother results than just eroding.

kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (3,3))
opening = cv2.morphologyEx(image, cv2.MORPH_OPEN, kernel)

result

enter image description here

You can experiment with the kernel shape and the number of iterations. To remove more noise, increase the kernel size and iterations, and to remove less noise, decrease the kernel size and iterations.

import cv2

image = cv2.imread('1.png')
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (3,3))
opening = cv2.morphologyEx(image, cv2.MORPH_OPEN, kernel, iterations=3)

cv2.imshow('opening', opening)
cv2.waitKey()

Related


How to remove white border around image in mobile view?

ZACK_G In my blog post, I uploaded some images and used CSS code to remove the white box borders that formed around the images on the desktop version. But I can't remove the white box border formed around the image in the mobile version. Suggest any suitable s

How to remove white border around image in mobile view?

ZACK_G In my blog post, I uploaded some images and used CSS code to remove the white box borders that formed around the images on the desktop version. But I can't remove the white box border formed around the image in the mobile version. Suggest any suitable s

How to remove white border around image in mobile view?

ZACK_G In my blog post, I uploaded some images and used CSS code to remove the white box borders that formed around the images on the desktop version. But I can't remove the white box border formed around the image in the mobile version. Suggest any suitable s

How to remove white border around image in mobile view?

ZACK_G In my blog post, I uploaded some images and used CSS code to remove the white box borders that formed around the images on the desktop version. But I can't remove the white box border formed around the image in the mobile version. Suggest any suitable s

How to remove white border around image in mobile view?

ZACK_G In my blog post, I uploaded some images and used CSS code to remove the white box borders that formed around the images on the desktop version. But I can't remove the white box border formed around the image in the mobile version. Suggest any suitable s

How to remove white border around image in mobile view?

ZACK_G In my blog post, I uploaded some images and used CSS code to remove the white box borders that formed around the images on the desktop version. But I can't remove the white box border formed around the image in the mobile version. Suggest any suitable s

Remove white space around image view

Kartus Khan I used image view in constraint layout and set height and width to zero and added height and width percentage. But when I add height and width percentage it adds spaces to the top and bottom of the image, how can I remove these spaces from the top

Remove white space around image view

Kartus Khan I used image view in constraint layout and set height and width to zero and added height and width percentage. But when I add height and width percentage it adds spaces to the top and bottom of the image, how can I remove these spaces from the top

How to remove white blur from image in python

Andy 31 I'm trying to remove the background from a product image, saving them as transparent pngs, but I can't figure out how and why there is a white line around the product, like a blur (see second image) Not sure what the effect really means . I also lost t

White pixels around iOS image using CIFilter

username I add a photo frame (image with transparent background) around an existing UIImage and save it all as one image. On the emulator everything seems to be working fine. However, on the device it adds some white pixels around certain areas of the frame im

How to remove white space around pictures in Android?

Manzotine Given an image with an alpha channel (transparency), I would like to remove any white space between the borders of the image and the actual image. This should be done in a background task or loading screen and within an acceptable runtime so as not t

How to remove white frame around photo in Tkinter?

something wrong When I upload an image in tkinter, a white box appears around the image. I searched a lot but didn't find any answer. White frame picture: Here is my code: from tkinter import * from PIL import ImageTk,Image root = Tk() root.geometry("1200x5

How to remove white frame around photo in Tkinter?

something wrong When I upload an image in tkinter, a white box appears around the image. I searched a lot but didn't find any answer. White frame picture: Here is my code: from tkinter import * from PIL import ImageTk,Image root = Tk() root.geometry("1200x5

How to remove white frame around photo in Tkinter?

something wrong When I upload an image in tkinter, a white box appears around the image. I searched a lot but didn't find any answer. White frame picture: Here is my code: from tkinter import * from PIL import ImageTk,Image root = Tk() root.geometry("1200x5

How to remove white space around pictures in Android?

Manzotine Given an image with an alpha channel (transparency), I would like to remove any white space between the borders of the image and the actual image. This should be done in a background task or loading screen and within an acceptable runtime so as not t

How to remove white margin around div tags?

its ADZZ So, I've been creating a very basic website header, nav, the main container and footer. I use markup viewporton my website . My problem occurs when I set widththe divs to 100%. Here is my code: #header { background-color: black; color: white; te

How to remove white frame around photo in Tkinter?

something wrong When I upload an image in tkinter, a white box appears around the image. I searched a lot but didn't find any answer. White frame picture: Here is my code: from tkinter import * from PIL import ImageTk,Image root = Tk() root.geometry("1200x5

How to remove white frame around photo in Tkinter?

has a problem When I upload an image in tkinter, a white box appears around the image. I searched a lot but didn't find any answer. White frame picture: Here is my code: from tkinter import * from PIL import ImageTk,Image root = Tk() root.geometry("1200x583

How to remove white frame around photo in Tkinter?

has a problem When I upload an image in tkinter, a white box appears around the image. I searched a lot but didn't find any answer. White frame picture: Here is my code: from tkinter import * from PIL import ImageTk,Image root = Tk() root.geometry("1200x583

How to remove white space around pictures in Android?

Manzotine Given an image with an alpha channel (transparency), I would like to remove any white space between the borders of the image and the actual image. This should be done in a background task or loading screen and within an acceptable runtime so as not t

How to remove white space around pictures in Android?

Manzotine Given an image with an alpha channel (transparency), I would like to remove any white space between the borders of the image and the actual image. This should be done in a background task or loading screen and within an acceptable runtime so as not t