alok9871
Posts: 2
Joined: Thu Jul 10, 2014 8:58 am

Achieving real time grayscale image data

Thu Jul 10, 2014 9:11 am

Hi Friends

I have just started to work with the Raspberry Pi and the Pi Camera Module. I can operate the camera module using the Python based RaspiCam and Picamera APIs.

My Motive is to find a way to record the 8 bit grayscale intensity levels of the image in a buffer. I need this data in real-time for further processing. I tried using the above two APIs, but I couldnt find a way to do this.

Can you please suggest any suitable way to achieve this. Many Thanks.

Regards
Alok

jamesh
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 27461
Joined: Sat Jul 30, 2011 7:41 pm

Re: Achieving real time grayscale image data

Thu Jul 10, 2014 9:40 am

Checkout out the C demo code raspistillYUV. This outputs a YUV image to file, and the Y of the YUV is greyscale.

But do you want video? You could make the same changes to produce video frames of YUV, but performance will be much slower due to the larger amount sof data being transferred from GPU to the ARM.
Principal Software Engineer at Raspberry Pi (Trading) Ltd.
Contrary to popular belief, humorous signatures are allowed.
I've been saying "Mucho" to my Spanish friend a lot more lately. It means a lot to him.

6by9
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 9943
Joined: Wed Dec 04, 2013 11:27 am
Location: ZZ9 Plural Z Alpha, aka just outside Cambridge.

Re: Achieving real time grayscale image data

Thu Jul 10, 2014 10:02 am

Adding to James' post, for the I420 format which RaspiStillYUV uses, Y (luma) always comes first as width x height 8 bit values.
You then get the two chroma planes as width/2 x height/2 8 bit values.

So on a VGA capture, you'll get a 460800 byte buffer, where the first 307200 bytes are the luma as a 640x480 8bit/pixel greyscale image. Just ignore everything after that.
Software Engineer at Raspberry Pi Trading. Views expressed are still personal views.
I'm not interested in doing contracts for bespoke functionality - please don't ask.

mikerr
Posts: 2826
Joined: Thu Jan 12, 2012 12:46 pm
Location: UK
Contact: Website

Re: Achieving real time grayscale image data

Thu Jul 10, 2014 10:15 am

PIython PiCamera can just get the Y:

Code: Select all

camera.capture(stream, 'yuv')
# Rewind the stream for reading
stream.seek(0)
# Calculate the actual image size in the stream 
fwidth = (width + 31) // 32 * 32
fheight = (height + 15) // 16 * 16
# Load the Y (luminance) data from the stream
Y = np.fromfile(stream, dtype=np.uint8, count=fwidth*fheight).\
        reshape((fheight, fwidth))
http://picamera.readthedocs.org/en/latest/recipes2.html
Android app - Raspi Card Imager - download and image SD cards - No PC required !

alok9871
Posts: 2
Joined: Thu Jul 10, 2014 8:58 am

Re: Achieving real time grayscale image data

Wed Aug 06, 2014 3:36 am

Thanks James, 6by9 and mikerr for the solution. It worked. Although I don't get the desired speed, but I'm very close.

I am having an issue in using the stream, as I am not very good with python. However I will post it as a separate question, so that a beginner like me can find the answer quickly.

@James : Thanks ..No I do not need video, I just need very rapid capture of image data in numerical format.

User avatar
gagle
Posts: 82
Joined: Fri Feb 14, 2014 6:54 pm
Contact: Website

Re: Achieving real time grayscale image data

Wed Aug 06, 2014 8:41 am

You can try my lib omxcam: https://github.com/gagle/raspberrypi-om ... /yuv/yuv.c

PD: it's unstable, api changes can occur.

6by9
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 9943
Joined: Wed Dec 04, 2013 11:27 am
Location: ZZ9 Plural Z Alpha, aka just outside Cambridge.

Re: Achieving real time grayscale image data

Wed Aug 06, 2014 8:59 am

alok9871 wrote:Thanks James, 6by9 and mikerr for the solution. It worked. Although I don't get the desired speed, but I'm very close.

I am having an issue in using the stream, as I am not very good with python. However I will post it as a separate question, so that a beginner like me can find the answer quickly.

@James : Thanks ..No I do not need video, I just need very rapid capture of image data in numerical format.
What speed do you require? If you've requested a particular framerate then the GPU will be attempting to deliver that rate. Whether your application can keep up is another matter.
Software Engineer at Raspberry Pi Trading. Views expressed are still personal views.
I'm not interested in doing contracts for bespoke functionality - please don't ask.

Return to “Camera board”