Search code examples
opencvpaintbackground-subtraction

Paint detected objects OpenCV?


Background subtraction. MOG and MOG2 turned out to be unhelpful because they assume the first frame is the background.

So I did frame by frame subtraction. like this
enter image description here

My problem is to now paint the only the detected object white.
Btw, I did try out The inbuilt FindContours() method & obtained thousands of contours in the image.


Solution

  • for findContours() you may are mislead. The method assumes a binarized image as input, if it is not quite binary, it treats non-zero pixel as 1, regardless which color or grayscale the are. findContours

    So your image is nearly binarized and you observe black and white regions. The black regions are treated as background and the not-black ones (non-zero) are treated as foreground pixels respektivly regions. findContours() does nothing more or less than "marking" coherent foreground pixels (yes the regions). So you get a List of vectors (a vector of points for each detected region).

    For detecting the whole bus as object, you may want to lookup on: convexHull This is (if I recall correctly) a list of vertrices too, that describes a region where all (previously found) regions are inside. So you may need to substract outliers first (like the piece of street or shadow on the bottom of your image).

    also interesting: convexityDefects and: approxPolyDP