Tierpsy / tierpsy-tracker

Multi-Worm Behaviour Tracker. README here:
https://github.com/Tierpsy/tierpsy-tracker/blob/development/README.md
MIT License
19 stars 17 forks source link

Set Parameters crashes after enabling background subtraction #84

Open jskat opened 2 years ago

jskat commented 2 years ago

Hi @luigiferiani,

I installed Tierpsy from source yesterday on my Windows 11 laptop. I have a problem in the "Set Parameters" window in the GUI. To reproduce, I select a video file, then click "Background" and check "Subtract Background," setting background buffer size = 250 and gap between frames = 25 like in the tutorial; then if I click "Play" the window becomes mostly irresponsive to user input (for example, switching back to the "Mask" section can make the window stop responding).

Also, the window stops responding briefly (but then resumes working) if I click the "Update Background" button. It appears that this button applies the background subtraction (although I can't find any mention of it in the docs). Clicking "Play" after "Update Background" still makes the window nearly impossible to use.

Perhaps the issue is that running "Play" with background subtraction is computationally expensive, so the computer can only play the video at a rate of 1 frame per >3 seconds. This makes sense since sometimes I can see the frame count increasing.

Am I using background subtraction incorrectly?

As a side question, where can I find info about choosing appropriate parameter settings (background buffer size and gap between frames) for background subtraction?

luigiferiani commented 2 years ago

Hi @jskat,

you're doing everything right, it's just a matter of the background subtraction being too computationally expensive to be applied "on-the-fly" at any meaningful framerate.

Also, the window stops responding briefly (but then resumes working) if I click the "Update Background" button. It appears that this button applies the background subtraction (although I can't find any mention of it in the docs). Clicking "Play" after "Update Background" still makes the window nearly impossible to use.

Clicking on the Update Background should calculate the background and apply it to the current frame. Since calculating the background takes time, this is not done automatically when you switch to the background tab or every time the background parameters are updated, but you need to manually ok the process.

Tierpsy calculates the background by looping through the video and collecting N frames at an interval g. N is the background buffer size, and g the gap between frames - this is why calculating the background is slow, because Tierpsy is going through all (or at least a part of) the raw video. Once all these frames are collected, Tierpsy will take the brightest value over time of each pixel in the field of view to create the background (or the darkest, according to is_light_background).

Given that, I'd recommend parameters that allow you to collect frames from the whole video (their product should be close to the number of frames in the video). So for example if you have 9000 frames you could use a buffer size of 300 and a gap of 30. Not spanning the whole video increases the chances of having a worm in the background. E.g. if you have a worm that did not move for the first third of the video and then starts moving, and the background parameters are such that only frames from the first third of the video are taken, then you'll have a worm in the background :)

Since the background is effectively defined as "all the things that do not move", it's better not to use it if you care about stationary worms (e.g. if you dosed them with a compound that paralises them).

If you run the analysis with background subtraction you can look at the background in the viewer. You need to click on "Update Groups" and then select /background from the drop down menu that has also /mask and /full_data.

jskat commented 2 years ago

Thank you! I successfully used background subtraction to ignore some detritus in a video. I also looked at the background in the viewer.

So Tierpsy collects N equally-spaced frames and uses the maximum brightness pixels from these N frames to construct the background, then subtracts this background from the video at each frame before further processing?

Is the ratio between buffer size and gap supposed to be approximately 10:1?