
RatTracker
Conditioned place preference, or CPP, is a classic behavioral experiment used to study motivation. By measuring how much time an animal spends in an area associated with a particular stimulus, researchers can infer its preferences. On paper it sounds simple; in practice it’s painstaking. A single study can contain hundreds of trials, and someone has to watch every frame, stopwatch in hand, hoping not to blink.
That was the starting point for RatTracker. The idea was modest: let a camera and a few lines of computer vision do the watching, and let software keep time. No fancy tricks, just careful zone detection, stable tracking, and a clean readout of how long each animal spent in each compartment.

CPP arenas with three distinct zones per box.
Each video could include several boxes. Every box had three zones: a colored corridor, a black compartment, and a checkered compartment. The rat could only pass between the side compartments through the corridor, so the boundaries mattered. If the software misread a line on the floor, the timing would be wrong. Reliability began with understanding the setup.

The workflow evolved into a small, dependable pipeline. First, build a stable view of the scene by extracting the background across the video. Then, use color and pattern to segment the zones. After that, find the rat, whether it’s moving or still, and decide, frame by frame, which zone it occupies. Finally, convert frames to seconds. What once required hours of manual scoring now finished in the time it took the video to play.

Background extraction made everything else easier. Sampling every tenth frame kept things fast; a median image captured the static scene; a light Gaussian blur tamed sensor noise. With a good background, both the zones and the animal stood out more clearly.

Zones were identified by their visual signatures. Color cues isolated the corridor and black region; the checkered pattern required a bit more care. Subtracting the background emphasized structure, and a few rounds of opening and closing smoothed the masks into usable shapes. Convex hulls finished the job, turning noisy edges into clean boundaries.

Finding the rat required two perspectives. Frame-to-frame differences were excellent when the animal moved; comparisons to the background exposed it even when it paused. Weighted together, the two images formed a reliable composite. A small sequence—erode, threshold, dilate, produced a crisp mask without overcomplicating things.




With zones defined and the rat located, the final step was simply counting time. For each frame, assign the animal to the zone with the strongest signal, but keep the assignment sensible: ignore tiny, noisy blips; forbid impossible jumps across walls; prefer continuity when the evidence is weak. At the end: three numbers that matter, seconds in corridor, seconds in black, seconds in checkered.

RatTracker didn’t try to be clever. It tried to be dependable. A few well-chosen primitives, background modeling, color and pattern cues, gentle morphology, were enough to turn a labor-intensive task into something reproducible and fast. It was a small reminder that thoughtful engineering can give researchers back their time, reduce fatigue and inconsistency, and let attention move from counting seconds to asking better questions.