cp-vton-plus
cp-vton-plus copied to clipboard
binary mask for white cloth on white background
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!

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.
Thanks @thaithanhtuan
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()
plt.imshow(outputMask, cmap="gray")
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/
2/
3/ cloth3 & its mask: this mask looks more similar to the cloth's shape but the mask's borderline has zigzac shape.
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.
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.