Creating an automated CPP analyzer

Conditioned place preference (CPP) is not a new term for pharmacologists. It's a device used to test drug addiction in mice. The mouse is placed inside the box and time spent in compartments is taken. The box isn't standard, it can come in many shapes, sizes, and colors. In my case, the box had three compartments — black and white compartments on the sides, and grey-ish one in between.
There has been some journal articles about automated methods for measuring the time, unfortunately they don't work.


I decided to use ImageJ, since no one has got the time to code the app from scratch (standing on the shoulders of giants). The app (or plugin, if you will) first lets the user highlight the black and grey compartments, since the mice were white and there's no reliable contrast between the mice and 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 on all frames. Once that's done, the app will start 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.
If no, 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 change 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. The process is repeated for all frames, which are about 13,000 for a 15 minutes video at 15fps.

How to get it

The app is available here . Once you downloaded the four files, run ImageJ and go to plugins> compile and run> UI.class then follow the instructions. Don't forget to download FFmpeg first

How accurate is it

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


Source code is available under GPL. Link-backs are welcome

Minimum system requirements

It should work on Windows, Linux, and Mac (via ImageJ and Java). 16GB RAM (free 10GB) are required. For optimal speeds, set ImageJ priority to High from task manager.

How to set threshold

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

The image above shows bad example of thresholding because the tail is visible, and minutes artifacts can be seen at the lower edge. Despite the app's capabilities to exclude artifacts, it's for the best if you provide clean images.

Based on HTML5 UP