Someone on the OpenCV Google Plus page suggested that I look at the Lucas-Kanade feature tracking algorithm, rather than trying to analyse all of the pixels at once like I was doing.
This looks quite promising. First you have to decide which features in the image to use - corners are good for tracking. OpenCV has a neat cv.GoodFeaturesToTrack function which makes suggestions - you give it a couple of parameters, including a 'quality' parameter to help it choose. This gives a list of (x,y) coordinates of the good features to track. Note that this means 'good' mathematically, not necessarily the limbs of the test subject....
Once you have some features to track, OpenCV again provides a cv.CalcOpticalFlowPyrLK, where you give it the list of features, the previous image and a new image, and it calculates the locations of the features in the new image.
I have then gone into the fourier analysis that I have been trying for the other types of seizure detection. This time I calculate the speed of each feature over a couple of seconds, and record this as a time series, then calculate the fourier transform to give the frequency spectrum of the motion. If there is oscillation above a threshold amplitude in a given frequency band for a specified time we raise an alarm as a possible seizure.
The code is functioning, but is a fair way off being operational yet. The code for this is in my OpenSeizureDetector github repository (https://github.com/jones139/OpenSeizureDetector).
The current issues are:
- I really want to track motion of limbs, but there is no guarantee that cv.GoodFeaturesToTrack will detect these as good features - I can make this more likely by attaching reflective tape, which glows under IR illumination from the night vision camera...if I can persuade Benjamin to wear it.
- There is something wrong with the frequency calculation still - I can understand a factor of two, but it seems a bit more than that.
- If the motion is too quick, it looses the point, so I have to set it to re-initialise using GoodFeaturesToTrack periodically.
- An Example of it working with my daughter doing Benjamin-like behaviour is shown below. Red circles are drawn around points if a possible seizure is detected.
- This does not look too good - lots of points detected, and even the reflective strips on the wrists and ankles get lost. It seems to work better in darkness though, where I get something like the second video, where there are only a few points, and most of those are on my high-vis reflective strips.
- It does give some nice debugging graphs of the speed measurements and the frequency spectra though.
So, still a bit of work to do.....