Message Boards Message Boards


Image extraction work

Posted 12 days ago
6 Replies
1 Total Likes


Hope everyone is doing good and I really need some help with regards to automate the image extraction work.

We usually receive team/group images and the majority of the groups are shot on a natural background. We create the path and do the extraction on images.

The normal time of doing the extraction is around 30 minutes for 15-18 people, which we want to reduce.

Kindly suggest a way to automate or reduce time for such processing.

Thank you.

6 Replies
Posted 12 days ago

Extracting content from images is very dependent on the nature of the image. Can you provide some sample images?

Thanks for your reply and usually we receive single person and team images and I have attached one example for both.

So team images, I understand time consuming and but atleast if I get 80-90% then rest I will do it manually.




Posted 11 days ago

Take a look at this post on MSE. I tried it on your images. I had to make them smaller, otherwise Mathematica requires too much memory.

(* Copied from the post *)
net = NetModel["Ademxapp Model A1 Trained on ADE20K Data"];

netevaluate[img_, device_: "CPU"] := 
 Block[{resized, encData, dec, mean, var, prob}, 
  resized = ImageResize[img, {504}];
  encData = Normal@NetExtract[net, "Input"];
  dec = NetExtract[net, "Output"];
  {mean, var} = Lookup[encData, {"MeanImage", "VarianceImage"}];
  prob = NetReplacePart[
     net, {"Input" -> 
       NetEncoder[{"Image", ImageDimensions@resized, 
         "MeanImage" -> mean, "VarianceImage" -> var}], 
      "Output" -> Automatic}][resized, TargetDevice -> device];
  prob = ArrayResample[prob, Append[Reverse@ImageDimensions@img, 150]];

getPeople[i_] := Map[ReplaceAll[{13 -> 1, _ -> 0}], netevaluate[i], {2}] // Image

(* Try it on the sample images *)
smile = Import["~/Downloads/Smiling Face.jpg"];
smallSmile = ImageResize[smile, 1000];
smileMask = getPeople[smallSmile];

ImageMultiply[smileMask, smallSmile]

enter image description here

(* Same for the other image *)

enter image description here

It did not do so well on the second image. One thing you could do is to save the mask. Import the image and mask in Photoshop and manually adjust the mask. That should take less effort compared to starting from scratch.

This is an interesting problem. I cannot offer a satisfying solution, but maybe it is helpful though. The striking feature of your sample images is that the focus lies on the foreground, while the background - which is to be removed - is unsharp. This is the basis of my approach. So I first tried something using Fouriertransform, but then I discovered that RidgeFilter is doing the job. (For this demo I am working with downsized images. When working on bigger images, probably some parameters need to be adjusted.)

img0 = Import[

img1c = ImageResize[img0, 1000];
img1g = ColorConvert[img1c, "Grayscale"];
img1gs = Sharpen[img1g];
rfImg = DeleteSmallComponents[Binarize[RidgeFilter[img1gs], .02], 500]

enter image description here

From this - using MorphologicalComponents - it easy to create a "background mask":

morphComp = MorphologicalComponents[ColorNegate@Dilation[rfImg, 1]];
bgNumber = First[Commonest@Flatten[morphComp]];
bgMask = Erosion[ImageApply[If[# == bgNumber, 0, 1] &, Image[morphComp]], 2]

enter image description here

The result is an image with transparent background ...

resultImg = RemoveBackground[bgMask img1c]

enter image description here

... so an arbitrary background can be chosen:

testImg = ExampleData[{"TestImage", "House2"}];
ImageCompose[ImageResize[testImg, ImageDimensions[resultImg]], resultImg]

enter image description here

In case of the first image things do not work that well. But in particular small and critical parts (e.g. the hairs in the wind) can perfectly be separated (using a blue background here as an example):

ImageApply[If[# == {0, 0, 0}, {0, 0, 1}, #] &, rfImg img1c]

enter image description here

Hope that helps a bit, regards -- Henrik


Thanks for your reply and I am sorry to ask this silly question but I am new to this forum and really dont know where can I execute these commands in order to get the required results.

I believe you are talking about opencv. Please guide. Thanks.

Oh, no! No, we are not talking about OpenCV here! This forum is exclusively about Mathematica ( and other Wolfram technologies!

Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
or Discard

Group Abstract Group Abstract