Creating an automated CPP videos analyzer using ImageJ

Conditioned place preference (CPP) paradigm is used to test drug addiction in mice. The mouse is placed inside the apparatus, and time spent in compartments is measured. The design isn’t standard, it comes in many varieties. In my case, the box had three compartments — black and white compartments on the sides, and grey-ish one in-between.

Algorithm

Using ImageJ, the app (or plugin, if you will) first allows the user to highlight the black and grey compartments. This is because the mice used had white fur and there’s no reliable contrast between them and the white compartment. After highlighting the black and grey compartments, the user is asked to select an optimal threshold value that highlights the mouse from everything else. This threshold value will be applied to all frames. The app then starts analyzing the video. The app uses ffmpeg to extract all frames in the video, then process them one by one. First thing it does is cropping the frames to the area the user had highlighted in the beginning, then apply the threshold value. The app will check if the image is completely black (which means there is no mouse in the frame). If true, the app will skip this frame and move to the next one to save time and reduce false-positives. Else, which means there is a mouse, particle analyzer will analyze the 'mouse'. Of course there will be false-positives, so in order to eliminate the chance of error, the particle analyzer is initiated with area threshold. If the object is truly a mouse, the particle analyzer will find the centroid, then check if it’s in black, or grey compartment. The process is repeated for all frames, which are about 13,000 for a 15 minutes video at 15fps.

Download the code

You can get the code by clicking here. Once you downloaded the file, extract the included four files, run ImageJ, go to plugins > compile and run > UI.class then follow the instructions. Don’t forget to download and extract ffmpeg to the same folder

How accurate is it

I’ve checked it with manual counting, and the app is pretty accurate when configured correctly. After each analysis, there will be a folder called "debugging" which shows the results of the first 500 frames, you could check the accuracy from there.

License

WTFPL

How to set threshold

Good thresholding example

First you need to change the channel from red to all, then slide maximum to 0. Once you’ve done that, gradually increase minimum until you get something like the picture shown above. The mouse should appear featureless with contrasting background which doesn’t contain significant artifacts. The tail shouldn’t be visible. Note: it is imperative that you don’t press "Apply"; otherwise, the application will not be able to capture threshold values.

Bad thresholding example

The picture above shows a bad example of thresholding because the tail is visible, and minute artifacts can be seen at the lower edge. It’s for the best if you provided clean images.