grayscale to monochrome

Hi,
What this this command do? image.binary(thresholds(50, 255),invert=false)
Jim

Hi, binary turns an image black and white
What are you trying to do?

I am loading a saved file using image.Image—
I want to convert the image to monochrome.
I want to have the monochrome image show in the IDE frame buffer.
Jim

A grayscale image or a binary image?

Conversion from RGB565 to grayscale is not available right now. I’m working on increasing the firmware’s ability however. In particular, adding lots of stuff like for this for the next version. I’ll put this on my to-do queue.

Anyway, binary just produces a black and white image.

Alternatively, if you don’t mind speed issues. You have pixel level access of images. You can manually convert colors yourself. Just grab a pixel, use the RGB to yuv method to a yuv value, set the u and v parts to 0 and then convert back to RGB.

my image that I load in is already grayscale. I want to convert the grayscale to black and white.

Oh, then binary() is the method to use.

If you’d like for the camera to automatically pick the best threshold do:

value = img.get_histogram().get_threshold().value()
img.binary([(0, value)])

This uses otsu’s method to pick the most pleasing threshold.

how do i show it in the frame buffer so I can see it?

sensor.flush()

After everything.

Hello - I am trying this grayscale → binary method, but I am running into problems with a subsequent find_blobs call. (Please note that for testing, I am pointing the camera at a black screen with white spots on it)

img = sensor.snapshot()
print(img.get_histogram().get_threshold().value())
img.binary([(0, img.get_histogram().get_threshold().value())])
print(img.get_histogram().get_threshold().value())
blobs = img.find_blobs([(255, 255)])

The first print statement returns a value of, say, 135. The second print statement returns a value of 0, but I expect it to return 255 (white). The subsequent find_blobs does not find any white blobs.

I tried changing the second part of the binary tuple to the value + 1, with the same result.

Any thoughts?

Hi, how big are the white spots? find_blobs() filters by area and pixel count of blobs it sees. You need to adjust these threaholds if you want it to find small things.

Also, when the image is binarized get_threshold() just turns into returning if black (0) or white (255) has more pixels.

1 Like