2

It is possible to use the BackgroundSubtractorMOG (OpenCV library) in still images?

I want to get only the foreground image from one single still image (captured from camera) using OpenCV library.

The still image look like this:

The original still image

I want to remove the background to get only the cars.

The similar question have been asked such as here, but in my case, the background is not uniform.

Thank you for answering my question.

Community
  • 1
  • 1
joshua14
  • 135
  • 2
  • 13
  • They already answer your question in the comments of the linked question, `BackgroundSubtractorMOG` is for detect **moving** objects in a timeline of continuous frames, in your problem you only have one image, is not possible for this algorithm to detect background without a reference. – Alex S. Diaz May 04 '16 at 16:32
  • So, is there any technique/algorithm to get the foreground objects in one single image -- in my case: cars? – joshua14 May 04 '16 at 16:48
  • It's difficult if you only have one image. You could use [Cascade Classification](http://docs.opencv.org/2.4/modules/objdetect/doc/cascade_classification.html) to detect your cars, but you will need to train it with a bunch of samples. – Alex S. Diaz May 04 '16 at 17:24
  • 1
    Although it would be an awesome thing, there is no way, how could a computer program possibly decide, what is background and what is foreground from only one single image. However subtraction functions, like `cvSub`, could provide you what you need, if you will provide one "empty" image (road without cars) and one to be subtracted from (with cars) ... Try to describe more what and mainly why you need it. There could be another possible approach – j.kaspar May 04 '16 at 17:38
  • @AlexSifuentes: If I have the continuous frames (still image) of the road, and using the `BackgroundSubtractorMOG`, it will give result the foreground only?? – joshua14 May 04 '16 at 17:56
  • @j.kaspar: using background subtraction function will easy to get the cars, but in my case, the "empty" image is not provided. There's why I'm confused how to get only the cars :( – joshua14 May 04 '16 at 17:56
  • 1
    @joshua14 understood. And what are your intentions? You have a set of different pictures of cars, and need to extract all the cars from them? Or you have set of pictures from the same place, in different time, ie. with different cars, and you need to extract the cars? Or even - you have only one image and want to extract the cars? If so, do you really need to do it programatically at all? – j.kaspar May 04 '16 at 18:07
  • @j.kaspar: my goal is to counting how many cars in the sequence of images in the same place, in different time -- like CCTV monitoring. And yes, i need to do it programatically.. – joshua14 May 04 '16 at 18:16
  • @joshua14 in that case, I would definitely implement a haar classifier, as Alex Sifuentes suggested. It will be a lot of work with positive samples, but it could be pretty reliable – j.kaspar May 04 '16 at 18:34
  • @j.kaspar: thanks for your suggestion. By the way, is there any method/way to genereate the background model ("empty" image) from my sequence of image containing cars?? If i can get that background model, I can use for background subtraction function... – joshua14 May 05 '16 at 02:20
  • @joshua14 I had the same idea, but didn't recommend it, because if the images are from a different time, it means that there will be different lighting situation, which means, that the substraction will not be perfect, or even may not work at all... You could give it a try, but easiest way is not try to "generate a clean background" programatically, but create it in some photo editing software. Then try to subtract it... – j.kaspar May 05 '16 at 07:32
  • @j.kaspar: sorry for late reply. Hmm, but I can't use the photo editing software. it's prohibited because this is a project for my essay in college. it must all done programatically by my application.. – joshua14 May 05 '16 at 13:36

1 Answers1

0

Since you are not allowed to use photo editing software, I highly recommend not to bother yourself with background subtraction, combined with following counting of cars. Even if you were 100% successfull in the subtraction, which is highly unlikely, you will then have problems counting the cars. Or you could fail to correctly count them at all, because some of them can overlap each other etc.

Instead, use the haar classifier, that will give you the detection (regardless of lighting situation), and the count in one step. There are pretrained classifiers available on the internet as well.

See these links:

http://mark-kay.net/2014/06/24/detecting-vehicles-cctv-image/ https://abhishek4273.com/2014/03/16/traincascade-and-car-detection-using-opencv/ https://github.com/andrewssobral/vehicle_detection_haarcascades

j.kaspar
  • 751
  • 1
  • 11
  • 29
  • thank you so much for your advice and recommendation.. :) but unfortunately I must use the background subtraction method in this project.. it's ok if the counting will not correctly count the cars.. – joshua14 May 07 '16 at 07:06