sensor.snapshot() array manipulation

I am using the standard why of getting a frame from the camera

 img = sensor.snapshot()

. The sensor is configured to be RGB565

  1. I would like to manipulate the img array to select one of the colour channels, the usual way would be to do img[:,:,1] however I get a TypeError saying that image indices must be integers and not tuples.

  2. Once I have the array I would like to be able to perform a column wise mean that is to collapse the 2D array to a 1D array.

I cannot see a why of doing either with MicroPython?


Hi, the image array is not a numpy array. The purpose of the python API is to stich together image processing operations. You should avoid pixel wise access. That said you can use a get_pixel() and set_pixel() method to do things but you should look through our API for a method that does something similar to what you need and use it. Note that you can index the image like as if it were a 1D C array. But, this is mostly not that great to use.

Anyway, keep in mind you are using a Microcontroller with 512 KB of RAM. Memory usage in most desktop applications is extremely wasteful and copy intensive. So that’s why we had a different API to deal with these limits.

Anyway, if you are looking to get color channel averages please use the get_stats() method or get_histogram(). These methods compute the mean of the color channels very quickly and output the results in LAB values. If you need RGB output we have conversation methods to convert LAB to RGB. The methods take an ROI so if you want to do things column by column you can call the methods on each column one by one.

Every method in our library takes an ROI value [x, y, w, h] which allow you to target operations on particular areas.

Thank you for the suggestions.

What I am trying to do is extract a 1D profile along the image for one of the colour channels.


Yeah, just do then:

for i in range(img.width()):
    rect = (i, 0, 1, img.height()) # x y w h
    stats = img.get_stats(roi = rect)