Ask Your Question

Sebastián Ramírez's profile - activity

2020-02-20 12:55:09 -0600 received badge  Famous Question (source)
2018-08-29 09:48:50 -0600 received badge  Notable Question (source)
2018-02-19 00:51:31 -0600 received badge  Popular Question (source)
2017-04-26 08:58:38 -0600 received badge  Notable Question (source)
2016-02-29 14:40:07 -0600 received badge  Popular Question (source)
2015-05-08 11:53:07 -0600 received badge  Nice Answer (source)
2014-11-10 14:20:52 -0600 answered a question OpenCV Python cv2.BackgroundSubtractor parameters

I found that (since some version of OpenCV) when I call the method apply from the BackgroundSubtractorMOG object, it is using a default learningRate parameter set at 0.0, so the BackgroundSubtractor is not "learning" the new frames, it just sticks to the first frame used.

To avoid it, I have to call the method apply with an explicit learningRate parameter, then it works as it worked before.

So after starting the code:

bg1 = cv2.BackgroundSubtractorMOG()
cap = cv2.VideoCapture('video.mp4')
frame = cap.read()[1]

...instead of doing:

fg1 = bg1.apply(frame)

I should do:

fg1 = bg1.apply(frame, learningRate=0.001)
2014-11-05 07:12:12 -0600 received badge  Teacher (source)
2014-11-04 13:43:20 -0600 received badge  Self-Learner (source)
2014-11-04 13:43:20 -0600 received badge  Necromancer (source)
2014-11-04 13:34:15 -0600 answered a question Build opencv_contrib sample executables on Linux

I solved it. It was a silly mistake.

I was setting up OPENCV_EXTRA_MODULES_PATH as: cmake -D OPENCV_EXTRA_MODULES_PATH=../opencv_contrib/modules ..

But my working directory was "opencv/build/" (I was in a directory "build" inside of opencv and the "opencv_contrib" was in the same directory as "opencv"), so that variable should have been: cmake -D OPENCV_EXTRA_MODULES_PATH=../../opencv_contrib/modules ..

2014-11-04 13:32:24 -0600 asked a question OpenCV Python cv2.BackgroundSubtractor parameters

Summary

It seems like changing the parametes of BackgroundSubtractorMOG doesn't affect the results of the algorithm.

I'm usign Python: 2.7.6 |Anaconda 2.1.0 (64-bit)
OpenCV: '2.4.10'
OS: Windows 7 x64

The default parameters for the algorithm are:

history=200, nmixtures=5, backgroundRatio=0.7

So, creating the background subtractor with different parameters should give different results. But I always get the same result (foreground mask) with default parameters and with custom parameters.


Reproducing the problem

First, Create two background subtractor objects with different parameters:

bg1 = cv2.BackgroundSubtractorMOG()
bg2 = cv2.BackgroundSubtractorMOG(history=3, nmixtures=5, backgroundRatio=0.0001)

Create 2 "VideoCaptrue" objects:

cap = cv2.VideoCapture('video.mp4')
cap2 = cv2.VideoCapture('different_video.mp4')

Test the results:

# Get a frame from the first capturing object:
frame = cap.read()[1]

# Get the foreground mask from both background subtractors:
fg1 = bg1.apply(frame)
fg2 = bg2.apply(frame)

# Show both results and the difference between them:
cv2.imshow('Window name', np.hstack((fg1, fg2, fg1 - fg2)))
cv2.waitKey(30)

After running that block of code for some frames, the window shows results from the first background subtractor, results from the second and the difference of the two.

Because both masks are the same, the result of their difference (the third pane) is a full black frame:

2 BackgroundSubtractorMOG and difference

Then, suddenly change the source of the video (the second VideoCapture object):

# Get a frame from the second capturing object:
frame = cap2.read()[1]

# Get the foreground mask from both background subtractors:
fg1 = bg1.apply(frame)
fg2 = bg2.apply(frame)

# Show both results and the difference between them:
cv2.imshow('Window name', np.hstack((fg1, fg2, fg1 - fg2)))
cv2.waitKey(30)

And after running that block of code for some frames you get:

2 BackgroundSubtractorMOG and difference with new video

The two foreground masks look the same, and that's why the third pane is a full black frame.

But after running the last block of code for more than 3 frames the results of the second pane should become blacker again (because of the history=3 in the creation of the bg2 object). But both background subtractors keep getting the same results (as shown by the third full black pane).


Alternatives

I have also tried modifying their parameters with:

bg2.setInt('history')

But I get the same results.

And I always get the paremeters I did "set" (e.g.):

>>>bg2.getInt('history')
3

Is there anything I'm missing?

2014-09-29 10:16:22 -0600 received badge  Supporter (source)
2014-09-23 18:42:58 -0600 asked a question Build opencv_contrib sample executables on Linux

System: VM Ubuntu 14.04 x64

Summary:
I'm building OpenCV 3.0.0-dev with the contrib modules from https://github.com/Itseez/opencv_contrib but I can't get the executables from opencv_contrib.


I was trying to follow this tutorial about the tracking API: http://docs.opencv.org/trunk/modules/tracking/doc/tracking.html

Somewhere it says: To see how API works, try tracker demo: https://github.com/lenlen/opencv/blob/tracking_api/samples/cpp/tracker.cpp

But that's a forked old repository (by user lenlen), not the official repository. In the recent versions (with the new repos) it seems like that file is not in that location anymore but in: https://github.com/Itseez/opencv_contrib/blob/master/modules/tracking/samples/tracker.cpp

The problem: I'm being able to build OpenCV without errors, but I can't see how to build that executable file from opencv_contrib, or where is it getting located.

I've tried:

  • Setting up OPENCV_EXTRA_MODULES_PATH with: cmake -D OPENCV_EXTRA_MODULES_PATH=../opencv_contrib/modules ..
  • Setting up INSTALL_C_EXAMPLES with: cmake -D INSTALL_C_EXAMPLES=ON ..
  • Entering in opencv_contrib, creating and entering into a "build" directory and from there executing: "cmake .." ...but that gives errors (no CMakeLists.txt).
  • Entering in "opencv_contrib/modules/tracking/", creating and entering into a "build" directory and executing "cmake .." (because there's a CMakeLists.txt inside of the tracking directory). But that also gives errors.
  • Entering in "opencv_contrib/modules/tracking/samples/" and executing "g++ tracker.cpp", but that also gives errors.

Thanks in advance!