Numpy == goodness! (deinterlacing video in Numpy)

A few posts back I was trying to get Linux to record and play video at the same time.

I gave up on that, but got it working under Windows with Python; I’ll post the source for that here at some point.

A big part of the solution was OpenCV, PyGame and Numpy.

I’m hardly the first to say it, but I’m excited – Numpy is goodness!

My (stupid) video capture device grabs both interlaced fields of SDTV and composes them into a single frame. So instead of getting clean 720×240 at 60 Hz (sampling every other line, interlaced), you get 720×480 at 30 Hz with horrible herringbone interlace artifacts if there is any movement in the scene.

The artifacts were really annoying, so I found a way to get Numpy to copy all the even numbered lines on top of the odd numbered lines to get rid of the interlace artifacts:

img[1::2] = img[::2]

That’s it – one line of code. And it’s fast (machine speed)! My laptop does it easily in real time. And I learned enough to do it after just 20 minutes reading the Numpy tutorial.

Then, I decided I could do better – instead of just doubling the even lines, I could interpolate between them to produce the odd-numbered lines as the average of the even-numbered lines (above and below):

img[1:-1:2] = img[0:-2:2]/2 + img[2::2]/2

It works great! Numpy == goodness!

 


PS: Yes, I know I’m still throwing away half the information (the odd numbered lines); if you know a better way, please post a comment. Also, I’d like to record audio too, but OpenCV doesn’t seem to support that – if I get that working, I’ll post here.

2 thoughts on “Numpy == goodness! (deinterlacing video in Numpy)

  1. I’ll bet APL could not only do it in one line, but do it with fewer characters.

  2. I know I am necrobumping, but this post was one of the first google hits on my request.
    I’d like to answer the question stated in the post: in order to avoid throwing away half of the information you can just repeat the process twise: once for odd-numbered lines and once for even-numbered lines. So you will get 2 deinterlaced frames from 1 original interweaved frame. Then you can display them one by one – display one frame immediately and another one after 1/60s delay. That way you will get a 60FPS fluid video.

Leave a Reply

Your email address will not be published. Required fields are marked *