Background removing is one from the problems in image processing which seem to be pretty easy but gives headache instead. OpenCV library has section dedicated to background removal in videos. BackgroundSubtractorMOG , BackgroundSubtractorMOG2, BackgroundSubtractorGMG are the functions that appears in the library. They remove background from each frame, the mask is made which allow to separate object of interest. Form one side these functions are not perfect. From the other side we oftenly have only two frames, on one of them is only background, and the other frame is the same background but with the object of interests. We can use functions mentioned above or we can go with something lower level which maight be more precise in some cases.
If given frames are prepared by precision computer image functions meaning that background any shifts and each pixel of the background on one image is equivalent to the other image pixel, also value of these pixels has to be identical. In this case we can use compare function and this is how we can do it:
To make a mask, firstly process image in cvtColor by making it one layer image, and than use compare node with code setting set on CMP_NE. In that step the output image is a small trail with some noise around, which is byproduct of the image compression. Small differences of value of the pixels may depend from the type of compression and its settings. These noise artifacts are removed by erode function. Mask which we achieved has to be process by bitwise function and as effect we can get image without background. These approach is autonomous because it doesn’t require change in settings for other images.
For background removing we can use another different method which is based on the function watershed. Method is very precise but it required a key regions of the object which we want to separate from the background. But it will be covered in the next article.