Enumerating birds in a video

Justin Pearson
2014-10-25

Abstract

We explore computational techniques for enumerating birds in a video file. The video was filmed in November 2011 in Simi Valley, California, and features thousands of crows on their daily evening commute from the city to a large green open space. We determine that the 8-minute video captures roughly 1200 crows.

index_1.gif

index_2.gif

index_3.gif

Since the crows are exiting the frame through the top and left sides, the main idea is to count the crows by counting when they leave the top or left side of the frame.

Footage

Here’s our footage:

index_4.gif

index_5.gif

index_6.gif

File name short_clip.avi
File Size (bytes) 12939120
BitDepth 8
ColorSpace RGB
Duration 3.46667
FrameCount 104
FrameRate 30.
ImageSize {720,480}
VideoEncoding DV/DVCPRO - NTSC
{ImageList,100} index_7.gif

index_8.gif

index_9.gif

index_10.gif

index_11.gif

Idea 1: View video as 3D Image

If we mask out the crows and stack the frames up to form a 3D image, each crow will trace out a long tube-like shape. Counting the tubes tells us the number of crows in the whole video.

index_12.gif

index_13.gif

index_14.gif

index_15.gif

Here’s a good threshold value:

index_16.gif

Make & Export 3D image

This takes a long time, so I locked these cells.

index_17.gif

index_18.gif

index_19.gif

index_20.gif

Show 3D image

index_21.gif

index_22.gif

Idea 2: Count when crows leave the frame

Import the frames as raw pixel values instead of Image[] objects.

index_23.gif

index_24.gif

index_25.gif

index_26.gif

index_27.gif

index_28.gif

index_29.gif

index_30.gif

The fact that the lefthand side has some dark trees will make things difficult; let’s just crop them out.

index_31.gif

index_32.gif

index_33.gif

index_34.gif

index_35.gif

Take vertical column of pixels from each frame

index_36.gif

index_37.gif

index_38.gif

index_39.gif

Take horizontal slice of pixels from each frame

index_40.gif

index_41.gif

index_42.gif

index_43.gif

Quick check: Show a video of crows leaving the frame

You can see the crows getting “stuck” when they hit the upper and left edges.

index_44.gif

index_45.gif

index_46.gif

Looks like ~15 leave the RHS and 6 leave the top.

The idea will be to have MMA count the number of crows that leave the left & top sides.

Crows exiting top of frame

Here’s the top few rows of a 15-minute video of crows. The whole file was too big to import.

index_47.gif

File name horiz_720x24_quicktime_h264.mov
File Size (bytes) 29436035
BitDepth 8
ColorSpace RGB
Duration 479.479
FrameCount 14370
FrameRate 29.97
ImageSize {720,24}
VideoEncoding H.264
{ImageList,100} index_48.gif

index_49.gif

index_50.gif

Import video in chunks of 300 frames; extract the top row of pixels from each frame; parallelize this for speed.

index_51.gif

index_52.gif

index_53.gif

index_54.gif

index_55.gif

index_56.gif

index_57.gif

index_58.gif

index_59.gif

index_60.gif

index_61.gif

index_62.gif

index_63.gif

index_64.gif

index_65.gif

index_66.gif

index_67.gif

index_68.gif

index_69.gif

index_70.gif

Detection via GradientFilter

index_71.gif

index_72.gif

index_73.gif

index_74.gif

index_75.gif

It’s hard to see in this pic, but if you zoom in, black is background and white is crows.

index_76.gif

Find a good threshold pixel value to differentiate between “crow” and “background”.

index_77.gif

index_78.gif

index_79.gif

So it looks like a good dividing line between black background and white crows is around 0.07.

index_80.gif

index_81.gif

Looks pretty good. Let’s highlight them:

index_82.gif

index_83.gif

index_84.gif

Here’s a zoom-in of the left side of the image:

index_85.gif

index_86.gif

Hard to see, but the crows are highlighted slightly.

index_87.gif

So, how many crows left the top of the frame during this 8-minute video?

index_88.gif

index_89.gif

Crows exiting left side of frame

Do essentially the same thing, but for the video that’s the left side of the frame.

A long file. Import in pieces.

index_90.gif

File name vert_24x350_quicktime_h264.mov
File Size (bytes) 18630731
BitDepth 8
ColorSpace RGB
Duration 479.479
FrameCount 14370
FrameRate 29.97
ImageSize {24,350}
VideoEncoding H.264
{ImageList,100} index_91.gif

Preview

index_92.gif

index_93.gif

index_94.gif

Aside: Consider just assembling these skinny frames instead of only their leftmost cols.

The easiest thing to do would be to just squish all the frames together.

index_95.gif

index_96.gif

Too dotty.

Import in pieces.

index_97.gif

index_98.gif

index_99.gif

index_100.gif

index_101.gif

index_102.gif

index_103.gif

index_104.gif

index_105.gif

index_106.gif

index_107.gif

index_108.gif

index_109.gif

index_110.gif

index_111.gif

index_112.gif

A very long image:

index_113.gif

index_114.gif

index_115.gif

index_116.gif

Left side of the image:

index_117.gif

index_118.gif

index_119.gif

Detection via GradientFilter

index_120.gif

index_121.gif

index_122.gif

Black is background and white is crows.

index_123.gif

index_124.gif

index_125.gif

index_126.gif

So it looks like a good dividing line between black background and white crows is around 0.07.

index_127.gif

index_128.gif

Looks pretty good. Let’s highlight them:

index_129.gif

index_130.gif

index_131.gif

index_132.gif

So, how many crows departed out the left side of the frame during the 8-minute video?

index_133.gif

index_134.gif

Conclusion

index_135.gif

index_136.gif

About 1400 crows flew through the video over the course of 8 minutes: about 3 crows a second. Wow!

Created with the Wolfram Language