Robin's Blog

Previously Unpublicised Code: RPiNDVI

Another instalment in my Previously Unpublicised Code series…this time RPiNDVI, my code for displaying live NDVI images from the Raspberry Pi NoIR camera.

It isn’t perfect, and it isn’t finished – but it does the job as a proof-of-concept. If you point the camera out of your window you should see high NDVI values (white) over vegetation, and low NDVI values (black) over various other things (particularly the sky!).

This is the point at which I would like to include a screenshot of the program running…but unfortunately I can’t actually find my Raspberry Pi to run it! (I guess that’s the problem with small computers…).

I can’t say the codeĀ is exceptionally exciting – it’s only about 100 lines – but it might be useful to someone. It demonstrates how to do real-time (or near-real-time) processing of video from the Raspberry Pi camera using OpenCV, and also has a few handy functions for doing contrast stretching of imagery and combining multiple images on a single display.

As always, the code is available at Github, along with a list of requirements – so have fun!

If you found this post useful, please consider buying me a coffee.
This post originally appeared on Robin's Blog.

Categorised as: Previously Unpublicised Code, Programming, Python


  1. Mr.Jiang says:

    Glad to see your blog.I’m very interested in this and
    I’m admire you very much.Alse ,i’ve download this codes from GitHub,however i see the follow notes after running this python code.I need your help if you can give me some useful suggestions for this problem.My device is RPI 3B and RPI NoIR camera.
    Thank you much!

    pi@raspberrypi:~ $ python
    /usr/lib/python2.7/dist-packages/picamera/ PiCameraResolutionRounded: frame size rounded up from 532×400 to 544×400
    width, height, fwidth, fheight))) RuntimeWarning: divide by zero encountered in double_scalars
    out *= ((out_min – out_max) / (in_min – in_max)) RuntimeWarning: invalid value encountered in multiply
    out *= ((out_min – out_max) / (in_min – in_max))

    (image:3036): Gtk-WARNING **: cannot open display:
    pi@raspberrypi:~ $

  2. Robin Wilson says:

    Hi – glad you like this script. You can ignore the RuntimeWarnings that are shown – the actual problem is the Gtk-WARNING error saying that it can’t open a display. This will be because you’re running this outside of X Windows (the Linux graphical environment). The script requires access to the graphical environment to be able to display the live video from the camera – so this script can’t be used from a pure command-line interface (eg. over SSH). Hope that helps!

  3. Leo says:

    Hi, it’s very good what you do, I’m on a research project, if you can send me your email, I’ll thank you.

Leave a Reply

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