RGB channel extraction

Do:

img.to_rgb565(rgb_channel=0)

This will extract the red channel only. If you want the image to go to grayscale do:

img.to_grayscale(rgb_channel=0)

Then the image will be the red channel but in grayscale.

Hi, I’m trying to use the example you’ve shown to show only the Red channel, but It doesn’t work, it seems like it ignores any value I put on the rgb_channel. do you have any idea why?

Using Micropyton v0.4.0-52-g3b8c18b.

import sensor
import image
import lcd

lcd.init()

sensor.reset()
sensor.set_pixformat(sensor.RGB565)
sensor.set_framesize(sensor.QVGA)
sensor.set_hmirror(0)
sensor.set_vflip(1)

sensor.run(1)

while True:
img=sensor.snapshot()
img.to_grayscale(True, rgb_channel=0)
lcd.display(img)

Thanks,
AYahoo

Hi, it’s because you are passing the first arg as copy=True which makes a copy of the image. Then you never assign that copy and it gets thrown away. You need to not set the copy argument to True.

Thanks for the replay, I’ve tried using:


img=sensor.snapshot()
img2 = img.to_grayscale(True, rgb_channel=0)
lcd.display(img2)

And tried img.to_grayscale(False, rgb_channel=0)

But still nothing.

[import sensor
import image
import lcd

lcd.init()

sensor.reset()
sensor.set_pixformat(sensor.RGB565)
sensor.set_framesize(sensor.QVGA)
sensor.set_hmirror(0)
sensor.set_vflip(1)

while True:
img=sensor.snapshot()
img = img.to_grayscale(rgb_channel=0)
lcd.display(img)](import%20sensor%0Aimport%20image%0Aimport%20lcd%0A%0Alcd.init%28%29%0A%0Asensor.reset%28%29%0Asensor.set_pixformat%28sensor.RGB565%29%0Asensor.set_framesize%28sensor.QVGA%29%0Asensor.set_hmirror%280%29%0Asensor.set_vflip%281%29%0A%0Awhile%20True%3A%0A%20%20%20%20img=sensor.snapshot%28%29%0A%20%20%20%20img%20=%20img.to_grayscale%28rgb_channel=0%29%0A%20%20%20%20lcd.display%28img%29)

This works. That said, I see you have sensor.run(1) in your code. You’re not using an OpenMV Cam are you…

No, I’m using Sipeed Maixduino I thought it is compatible with the OpenMV.

AYahoo

I have no idea what they do with our code base.

Hi, img.to_grayscale(rgb_channel=0/1/2) works fine up to QVGA resolution, but at VGA resolution I get an error, image is not mutable. I suppose you compress it to save space, as it is 0.6 MB raw.
Would it be feasible to have img = sensor.snapshot(rgb_channel=0/1/2) return a grayscale image?

That could be done. It’s not a lot of code. That said, what’s the use case for not just capturing a grayscale image?

It is a red laser on a dark background. Grayscale works, but I hope to get better contrast.
Or does it mean only 5 bits of red? Than probably grayscale with the global shutter camera would be a better option.

Yes, it’s 5 bits of red.