minar09 / cp-vton-plus

Official implementation for "CP-VTON+: Clothing Shape and Texture Preserving Image-Based Virtual Try-On", CVPRW 2020
https://minar09.github.io/cpvtonplus/
MIT License
340 stars 120 forks source link

binary mask for white cloth on white background #36

Open SarahH20 opened 3 years ago

SarahH20 commented 3 years ago

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
thaithanhtuan commented 3 years ago

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.

SarahH20 commented 3 years ago

Thanks @thaithanhtuan

josearangos commented 3 years ago
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

SarahH20 commented 3 years ago

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

minar09 commented 3 years ago

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.

levindabhi commented 3 years ago

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.