# How can I separate the dress from the background?

img=Import["https://i.stack.imgur.com/NjyIK.jpg"]


The dress is closely similar with the color of the background stair rail.

How can I binarize the image to get the dress mask or human mask?

Any methods of Binarize is acceptable, like RegionBinarize, training an semantic segment model

I've tried several Binarize* functions, and EdgeDetect functions, and confirmed that there are some challenges in the right-down part of the dress.

Background explaination: Why to segment? For example, consider there are many dress model pictures, we can cluster them by dress, and remove background we could get better features, for example textures and shapes.

Maybe someone knows more about traditional segment methods and has various experiences. Thanks for @rhermans 's comment.

RemoveBackground[Import["https://i.stack.imgur.com/NjyIK.jpg‌​"], {"Foreground", {RGBColor[ 0.9157048167754652, 0.8904987221508752, 0.8709273742640429], 0.062}}]

ImagePartition is a good idea with better region effect in Watershed.

WatershedComponents[,mask]//Colorize


• Do you want the left forearm and hand in front of the dress removed as well? – m_goldberg Jul 13 '17 at 7:38
• Also, are you concerned only with this particular image or are you looking for general method that would on other images with a similar problem? – m_goldberg Jul 13 '17 at 7:41
• I think one way would be to define a Mask manually (related How to retouch (smart fill) photo image?). Or you can try splitting your image into segments for better contrast img = Import["https://i.stack.imgur.com/NjyIK.jpg"];Grid[Partition[EdgeDetect[#] & /@ (ImagePartition[img, 50]~Flatten~1), 4], Spacings -> {0, 0}] – Sumit Jul 13 '17 at 9:05
• Do you need a fully automated procedure or can you afford doing steps by hand? I think you should edit your question and explain better what you need. As it stands, the question is too broad. – rhermans Jul 13 '17 at 10:25
• Why do you want to remove the dress? – bill s Jul 13 '17 at 13:43

My take at this—not perfect by any means.

The main feature I would exploit is that the area you want to segment is spatially well defined, so you can combine the color information with the position one.

Let's first define a white mask:

white = Binarize[ColorDistance[i, Darker[White, .1]], {0, .1}]


and a white stripe inside the image boundary:

border = ImagePad[ImagePad[0 i, -60], 60, 1];


We can now combine them in a backgroundMask and a foregroundMask

backgroundMask = Erosion[border + ColorNegate[white], 3];
foregroundMask = Erosion[ColorNegate[border], BoxMatrix[{60, 30}]] * white;



and use them in GrowCutComponents to assign the remaining pixels.

mask = Image[
];


The result is not perfect—no spoilers here—but almost all the extra white has been removed

SetAlphaChannel[i, Blur[Erosion[mask, 2], 1]]


You can tweak the code to make the boxes a little more asymmetric and improve the segmentation area, but—as others have said—there's a limit to where automatic (non semantic) segmentation can take you.

• MaxDetect[img, .2] will give your white – yode Jul 13 '17 at 19:09
• @yode only for pure white though—GrayLevel[1]. – Batracos Jul 13 '17 at 19:12
• see my answer, the Neural Network is very useful – HyperGroups Aug 9 '18 at 10:49

Cool result of NetModel of 11.3.

You can download the models like Ademxapp Model A1 Trained on PASCAL VOC2012 and MS-COCO Data from Wolfram Netmodel Reposity

Open the Notebook, then import the sample image, and run the code.

img=Import["https://i.stack.imgur.com/NjyIK.jpg"]
result[img]


• Using an image segmentation network is a pretty good idea; I don't see what the problem is with this answer. However, it would be better to just give the complete code that downloads the network from within a notebook. I.e., resource = "Ademxapp Model A1 Trained on PASCAL VOC2012 and MS-COCO Data"; ResourceObject[resource]; net = NetModel[resource]; net[img]. Furthermore, it seems like this network doesn't get you all the way to the final answer, though it's definitely useful as a first step in defining a mask for further segmentation analysis. – Sjoerd Smit Aug 14 '18 at 13:07