Strange behavior with to_grayscale

I want to capture an image, save it in color and then convert to grayscale for LBP computation.

My code essentially does this:

img = csi0.snapshot()
new_img = img.copy()
new_img.to_grayscale()
lbp = new_img.find_lbp()
img.save(...)

However, the image saved is in grayscale. I have tried all different kinds of things. Passing copy=True to to_grayscale doesn’t seem to have any effect (in this case I don’t call img.copy() but call img.to_grayscale(copy=True) instead. I still get a grayscale image saved. If I pass copy_to_fb to to_grayscale I get a corrupted image out of the framebuffer.

Am I doing this correctly?

In the real code, snapshot() happens in a function and the returned image is acted upon later. Once some processing happens, I save everything. Do I need to save the image before calling to_grayscale, even though I supposedly deep-copied it and am acting on the copy?

David

Yeah… it should have been deep copied.

import sensor
import time

sensor.reset()  # Reset and initialize the sensor.
sensor.set_pixformat(sensor.RGB565)  # Set pixel format to RGB565 (or GRAYSCALE)
sensor.set_framesize(sensor.VGA)  # Set frame size to QVGA (320x240)
sensor.skip_frames(time=2000)  # Wait for settings take effect.
clock = time.clock()  # Create a clock object to track the FPS.

img = sensor.snapshot()
new_img = img.to_grayscale(copy=True)
lbp = new_img.find_lbp([0, 0, 640, 480])
img.save("img.jpg")

This works for me with firmware v4.8.1? I get a color image.

This is odd. I ran your code and sure enough, I get a color image. But when I run my larger program it’s grayscale. I guess I will have to double-check the code.

Oh wow, this broke it:

import sensor
import time

from utils import lbp

sensor.reset()  # Reset and initialize the sensor.
sensor.set_pixformat(sensor.RGB565)  # Set pixel format to RGB565 (or GRAYSCALE)
sensor.set_framesize(sensor.VGA)  # Set frame size to QVGA (320x240)
sensor.skip_frames(time=2000)  # Wait for settings take effect.
clock = time.clock()  # Create a clock object to track the FPS.

img = sensor.snapshot()
lbp = lbp.compute_lbp(img, [0, 0, 640, 480])
img.save("img.jpg")

utils is a little library of utilities, one of which is compute_lbp. There are various interdependencies in the library. I tried creating a test_utils library containing only lbp.py but it passed with that. I wonder if something is getting confused by all the cross imports within the library.

If there’s a bug in the importer or something, that could explain the other strange memory errors I’m seeing.

Hmm. Now I can’t reproduce it. It may have been user error. Will keep investigating.

And…now my original program works perfectly! It’s almost as if your your code and me trying to reproduce the problem reset something in the camera and now it works.

I mentioned a post-processing step in another post. That’s the one where things were always crashing and bricking the board. Is it at all possible that in those repeated brickings, something “stuck” and kept things in a non-working state, even after resetting the camera in the IDE, unplugging it, etc.? And somehow that thing got knocked loose and things now work?

Seems strange, but this whole experience has been odd. I suspect there is indeed a bug somewhere deep but it’s one of those that’s going to really hard to track down…

Yeah, these things can be hard to track down. Here to help.