HermannSW
Posts: 1203
Joined: Fri Jul 22, 2016 9:09 pm

Question on yuv I420 to grey conversion

Wed May 22, 2019 11:48 am

In YUV I420 format the full size Y plane is followed by half width/half height U and V planes, leading to 8+4+4 bit per pixel:
https://www.fourcc.org/pixel-format/yuv-i420/

The Y plane is called achromatic plane.
What is disadvantage/advantage of taking just the Y plane and use as grey8 image?

I started with a v1 camera mode 2 raspistill 5MP image.
Then I converted it to yuv i420:

Code: Select all

$ ffmpeg -i test.5MP.jpg -s 2592x1944 -pix_fmt yuv420p test.5MP.yuv

Finally I created a grey8 .pgm binary file from that:

Code: Select all

$ echo -e "P5\n2592 1944\n255" > test.5MP.pgm
$ head --bytes 5038848 test.5MP.yuv >> test.5MP.pgm
$

The grey image looks fine for me ... (.pgm does not display in browser, can be viewed in eog or gimp):
https://stamm-wilbrandt.de/en/forum/test.5MP.pgm
https://stamm-wilbrandt.de/en/forum/test.5MP.yuv
https://stamm-wilbrandt.de/en/forum/test.5MP.jpg
Image
bookmark list: https://stamm-wilbrandt.de/en/Raspberry_camera.html

https://github.com/Hermann-SW/Raspberry_v1_camera_global_external_shutter
https://github.com/Hermann-SW/fork-raspiraw
https://github.com/Hermann-SW/userland
https://twitter.com/HermannSW

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

Re: Question on yuv I420 to grey conversion

Wed May 22, 2019 12:13 pm

What is disadvantage/advantage of taking just the Y plane and use as grey8 image?
None, it is a greyscale image - effectively the pixel intensity. U and V provide the colour information.

It's done all the time when you just want greyscale.
Principal Software Engineer at Raspberry Pi (Trading) Ltd.
Contrary to popular belief, humorous signatures are allowed. Here's an example...
"My grief counseller just died, luckily, he was so good, I didn't care."

HermannSW
Posts: 1203
Joined: Fri Jul 22, 2016 9:09 pm

Re: Question on yuv I420 to grey conversion

Wed May 22, 2019 4:20 pm

> None,
>
That surprises me, but your explanation makes sense.

That is cool, since using raspividyuv one can just don't look at U and V and work on width*height Y 8bit grey8 information in

Code: Select all

static void callback(MMAL_PORT_T *port, MMAL_BUFFER_HEADER_T *buffer)
without having to deal with raw10 Bayer conversion!
(for feature extraction and eg. robot control)
bookmark list: https://stamm-wilbrandt.de/en/Raspberry_camera.html

https://github.com/Hermann-SW/Raspberry_v1_camera_global_external_shutter
https://github.com/Hermann-SW/fork-raspiraw
https://github.com/Hermann-SW/userland
https://twitter.com/HermannSW

HermannSW
Posts: 1203
Joined: Fri Jul 22, 2016 9:09 pm

Re: Question on yuv I420 to grey conversion

Tue Jun 11, 2019 9:07 pm

Today I wanted to do grey extraction from raspividyuv video and not from ffmpeg generated .yuv as before.
raspividyuv captures multiple of 16 vertical lines, for mode 1 (1920x1080) that is 1920x1088.
This script extracts that grey image from last frame Y as .pgm binary file:

Code: Select all

$ cat yex
#!/bin/bash
echo -e "P5\n1920 1080\n255" > yex.pgm
tail --bytes $((1920*544*3)) $1 | head --bytes $((1920*1080)) >> yex.pgm
$

This is a sample application:

Code: Select all

$ raspividyuv -md 1 -p 10,10,960,540 -t 7100 -fps 1 -pts tst.pts -o tst.yuv
$ ./yex tst.yuv 
$

I used "pnmtopng yex.pgm > yex.pgm.png" to convert to image that can be displayed in browser -- a nice grey image:
Image
bookmark list: https://stamm-wilbrandt.de/en/Raspberry_camera.html

https://github.com/Hermann-SW/Raspberry_v1_camera_global_external_shutter
https://github.com/Hermann-SW/fork-raspiraw
https://github.com/Hermann-SW/userland
https://twitter.com/HermannSW

Return to “Camera board”