Capturing monochromatic images
Posted: Wed Apr 01, 2020 12:54 pm
I am looking to capture monochromatic images for my project and am wondering of the best way to achieve this.
At the moment I am capturing RGB output from the camera into a numpy array and then getting a grayscale image by performing a dot-product of the RGB matrix with an RGB to grayscale conversion. Something like:
However I am now thinking that a more efficient way to do this would be to grab the Y channel (luma) component of a YUV output.
I want my output to be a 2D matrix so that I work it forward more easily. E.g. for a 1280 x 480 image I would like a 1280 x 480 matrix where each element is between 0 and 255.
I have tried to achieve this in two separate ways.
First outputting the Y data from the YUV data into an array of the same resolution:
Unfortunately the image produced from this is not as I would expect, clearly something has gone very wrong. Image attached.
Secondly I tried to use the PiYUVArray method.
The trouble with this is that the shape of the array outputted is (720, 1280, 3). How can I reduce this to a (720, 1280) matrix for just the Y component?
At the moment I am capturing RGB output from the camera into a numpy array and then getting a grayscale image by performing a dot-product of the RGB matrix with an RGB to grayscale conversion. Something like:
Code: Select all
def rgb2grey(rgb):
return np.dot(rgb[...,:3], [0.1140, 0.5870, 0.2989]).astype(int)
I want my output to be a 2D matrix so that I work it forward more easily. E.g. for a 1280 x 480 image I would like a 1280 x 480 matrix where each element is between 0 and 255.
I have tried to achieve this in two separate ways.
First outputting the Y data from the YUV data into an array of the same resolution:
Code: Select all
import time
import picamera
import picamera.array
import numpy as np
with picamera.PiCamera() as camera:
camera.resolution = (100, 100)
time.sleep(2)
y_data = np.empty((112, 128), dtype=np.uint8)
try:
camera.capture(y_data, 'yuv')
except IOError:
pass
y_data = y_data[:100, :100]
# y_data now contains the Y-plane only
Secondly I tried to use the PiYUVArray method.
Code: Select all
import time
import picamera
import picamera.array
with picamera.PiCamera() as camera:
with picamera.array.PiYUVArray(camera) as stream:
camera.resolution = (1280, 720)
camera.start_preview()
time.sleep(2)
camera.capture(stream, 'yuv')
print(stream.array.shape)