cp-vton-plus icon indicating copy to clipboard operation
cp-vton-plus copied to clipboard

binary mask for white cloth on white background

Open SarahH20 opened this issue 4 years ago • 6 comments

Hi there. I wanted to use your code to create a binary mask for a white cloth on a white background image; however, I only got an absolutely black mask image and I couldn't see the cloth at all. Could you advise how I can create a binary mask for a white cloth on a white background image? Thank you so much!

Screen Shot 2020-11-05 at 4 50 55 pm

SarahH20 avatar Nov 05 '20 05:11 SarahH20

This code is just heuristic. For more accuracy, you need to build a network for generating the mask. But for ease, you can simply change the thresholds to a lower value.

thaithanhtuan avatar Nov 05 '20 06:11 thaithanhtuan

Thanks @thaithanhtuan

SarahH20 avatar Nov 05 '20 08:11 SarahH20

import numpy as np
import cv2
from matplotlib import pyplot as plt


PATH_IMG ="5.jpg"
image = cv2.imread(PATH_IMG)
mask = np.zeros(image.shape[:2], dtype="uint8")
rect = (1, 1, mask.shape[1], mask.shape[0])
fgModel = np.zeros((1, 65), dtype="float")
bgModel = np.zeros((1, 65), dtype="float")
start = time.time()
(mask, bgModel, fgModel) = cv2.grabCut(image, mask, rect, bgModel,
                                       fgModel, iterCount=10, mode=cv2.GC_INIT_WITH_RECT)
outputMask = np.where((mask == cv2.GC_BGD) | (mask == cv2.GC_PR_BGD),0, 1)
outputMask = (outputMask * 255).astype("uint8")


plt.imshow(image)
plt.title('my picture')
plt.show()

image


plt.imshow(outputMask, cmap="gray")

image

josearangos avatar Nov 11 '20 21:11 josearangos

Thanks @josearangos for sharing your information. I just tried your code on a couple of random images and got very weird masking results. Do you know why is that? Attached are the cloth images and the cloth-masks generated using this code. Many thanks for your advice.

1/ cloth1 cloth1_mask

2/ cloth2 cloth2_mask

3/ cloth3 & its mask: this mask looks more similar to the cloth's shape but the mask's borderline has zigzac shape.

cloth3 cloth3_mask

SarahH20 avatar Nov 23 '20 01:11 SarahH20

For white cloth on a white background, I think you should try with a bigger threshold. For example, cv2.threshold(img, 250, 255, cv2.THRESH_BINARY_INV) Later, you could use the morphology operations depending on your needs.

minar09 avatar Dec 11 '20 13:12 minar09

Hey @SarahH20, https://github.com/minar09/cp-vton-plus/issues/36#issuecomment-725674701 @josearangos solution is easy and quick to use, but I tired pre-trained model of U2net and its working fine for me in almost all cases.

levindabhi avatar Jan 18 '21 20:01 levindabhi