# Distinguishing different cars in picture

I recently posted a question. And I got a great answer from Mr. Carl Lange

Image Segmentation

His solution worked like a charm. However, I got another request which is more generic in nature. The request is to be able to pick out the foreground car and eliminate background cars. The test picture input picture is this

And the result I am currently getting is this:

But it should be more like this:

Any ideas would be greatly appreciated. Thanks.

• In other words, the background cars should be filtered out. – Alberto Artasanchez Apr 25 '18 at 21:23

There's an undocumented function called ImageContents that will give you bounding boxes for various objects in your image. (This likely is some nice wrappers over the YOLO V2 network which you could use directly.)

First we set up a quick getCarMask function from the previous answer:

getCarMask[img_] :=
Map[# /. {21 -> 1, _ -> 0} &, netevaluate[img], {2}]


If you know that the car you care about is going to be the biggest car, do something like:

imask=SetAlphaChannel[img,mask]


ImageContents[imask][

From here you could, for instance, attempt to re-run the original answer's method on this smaller section, or do some other processing to get a better answer - doing EdgeDetect,ImageSaliencyFilter, and GrowCutComponents might be a good place to start. Hope that helps, sorry it's not a drop-in answer - hopefully a decent starting point though.
I stress that you could probably get the same/similar result without using the undocumented ImageContents by using the YOLO V2 network.