Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to use mask to remove the background in python

I want to remove the background by using the mask image. Now, I have already get the mask image.I try to let the value of the original image's background become 0 where the value of mask is 0. But the result is very bad. How can I solve this problem. Thank you

from skimage import io
import numpy as np
img = io.imread("GT06.jpg")
mask = io.imread("GT03.png")
mask2 = np.where((mask==0),0,1).astype('uint8')
img = img*mask2[:,:,np.newaxis]
io.imshow(img)
io.show()

GT06.jpg

enter image description here

GT03.png

enter image description here

This results in: enter image description here

I want to get the foreground like this:

enter image description here

like image 256
user3960019 Avatar asked Jan 09 '16 07:01

user3960019


2 Answers

The problem is that your mask isn't pure black and white, i.e. all 0 or 255 changing you mask two generation to:

mask2 = np.where((mask<200),0,1).astype('uint8')

results in:

Remasked

You could either play with the mask or the threshold number - I used 200.

like image 168
Steve Barnes Avatar answered Sep 21 '22 18:09

Steve Barnes


In Python you could use OpenCV. Here are instructions to install OpenCV in Python if you don't have it in your system. I think you could do the same with other libraries, the procedure will be the same, the trick is to invert the mask and apply it to some background, you will have your masked image and a masked background, then you combine both.

The image1 is your image masked with the original mask, image2 is the background image masked with the inverted mask, and image3 is the combined image. Important. image1, image2 and image3 must be of the same size and type. The mask must be grayscale. foreground and background masked then combined

import cv2
import numpy as np

# opencv loads the image in BGR, convert it to RGB
img = cv2.cvtColor(cv2.imread('E:\\FOTOS\\opencv\\iT5q1.png'),
                   cv2.COLOR_BGR2RGB)

# load mask and make sure is black&white
_, mask = cv2.threshold(cv2.imread('E:\\FOTOS\\opencv\\SH9jL.png', 0),
                        0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)

# load background (could be an image too)
bk = np.full(img.shape, 255, dtype=np.uint8)  # white bk, same size and type of image
bk = cv2.rectangle(bk, (0, 0), (int(img.shape[1] / 2), int(img.shape[0] / 2)), 0, -1)  # rectangles
bk = cv2.rectangle(bk, (int(img.shape[1] / 2), int(img.shape[0] / 2)), (img.shape[1], img.shape[0]), 0, -1)

# get masked foreground
fg_masked = cv2.bitwise_and(img, img, mask=mask)

# get masked background, mask must be inverted 
mask = cv2.bitwise_not(mask)
bk_masked = cv2.bitwise_and(bk, bk, mask=mask)

# combine masked foreground and masked background 
final = cv2.bitwise_or(fg_masked, bk_masked)
mask = cv2.bitwise_not(mask)  # revert mask to original
like image 40
lmiguelmh Avatar answered Sep 22 '22 18:09

lmiguelmh