Automatic Detection and Quantification of Insects via Computer Vision

July 2015
NC Manoukis


We have used some simple computer vision (CV) techniques to quantify behavioral responses of tephritid fruit flies to lures. For a first experiment we examined the diurnal rhythmicity of Bactrocera cucurbitae response to cuelure. More recently, we have used the same technique to quantify Ceratitis capitata behavior on a Jackson trap:

Manoukis, NC and Jang, EB. 2013. The Diurnal Rhythmicity of Bactrocera cucurbitae (Diptera:Tephritidae) Attraction to Cuelure: Insights From an Interrupt-able Lure and Computer Vision. Annals of the Entomological Society of America 106(1):136–142, 2013.

Manoukis, NC. 2015. To Catch a Fly: Landing and Capture of Ceratitis capitata in a Jackson Trap With and Without an Insecticide. Submitted

In these pages I lay out the steps I used to generate data for the above-mentioned papers. The software I used for each task should be available generally, but there are no guarantees of this in the future; alternate programs may need to be used for some of the steps.


Preparing the image data and subtracting the background

Use mencoder to make and AVI file (uncompressed raw video) from your set of images using mencoder (I assume your input files are JPEGs here). Note that the dimensions need to be 720x480 to work with bsubtract.

mencoder mf://source_folder/*.jpg -mf type=jpg -ovc libdv -vf scale -zoom -xy 720 -vf crop=720:480 -o output1.avi

Next, use bsubtract to remove the background (use bsubtract with no arguments to get more infomration on options).

bsubtract output1.avi -cb -rgb -diff output1_diff.avi

Now we make images (PNG) from the subtracted video using FFMPEG:

ffmpeg -i output1_diff.avi -vcodec png output_directory/diff_frame%05d.png

Segmenting the images and counting flies

For this section we use ImageJ. I wrote a script to automate the process. You can get this here.


Below is a demonstration video that illustrates the method described above. The top left panel is the raw image (what was obtained from the camera), the top right is what the computer 'sees'. On the bottom half of the frame is the simple 2 minute-running average of the number of flies detected, with the blue line indicating the current time.


You can direct questions or comments to Nick Manoukis (nicholas.manoukis AT