User avatar
HermannSW
Posts: 4959
Joined: Fri Jul 22, 2016 9:09 pm
Location: Eberbach, Germany
Contact: Website Twitter YouTube

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
https://github.com/Hermann-SW/memrun
https://stamm-wilbrandt.de/2wheel_balancing_robot
https://stamm-wilbrandt.de/en#raspcatbot
https://github.com/Hermann-SW/Raspberry_v1_camera_global_external_shutter
https://stamm-wilbrandt.de/en/Raspberry_camera.html

jamesh
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 30438
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 Ltd.
Working in the Applications Team.

User avatar
HermannSW
Posts: 4959
Joined: Fri Jul 22, 2016 9:09 pm
Location: Eberbach, Germany
Contact: Website Twitter YouTube

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)
https://github.com/Hermann-SW/memrun
https://stamm-wilbrandt.de/2wheel_balancing_robot
https://stamm-wilbrandt.de/en#raspcatbot
https://github.com/Hermann-SW/Raspberry_v1_camera_global_external_shutter
https://stamm-wilbrandt.de/en/Raspberry_camera.html

User avatar
HermannSW
Posts: 4959
Joined: Fri Jul 22, 2016 9:09 pm
Location: Eberbach, Germany
Contact: Website Twitter YouTube

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
https://github.com/Hermann-SW/memrun
https://stamm-wilbrandt.de/2wheel_balancing_robot
https://stamm-wilbrandt.de/en#raspcatbot
https://github.com/Hermann-SW/Raspberry_v1_camera_global_external_shutter
https://stamm-wilbrandt.de/en/Raspberry_camera.html

User avatar
HermannSW
Posts: 4959
Joined: Fri Jul 22, 2016 9:09 pm
Location: Eberbach, Germany
Contact: Website Twitter YouTube

Re: Question on yuv I420 to grey conversion

Tue Aug 20, 2019 5:53 pm

Looked into "Raspberry Pi Camera Module" documentation yesterday:
https://www.raspberrypi.org/documentati ... /camera.md

If interested only in the Y channel of YUV image, then why transmit U and V at all?
This can be achieved by "raspividyuv" option "--luma / -y".
And by "raspivid" option "--raw-format, -rf" of "grey" in case "--raw, -r" option specifies output file name.

yex script from last posting can be simplified in this case to:

Code: Select all

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

Processing YUV in gstreamer pipeline via:

Code: Select all

... ! "video/x-raw, format=I420, ..." ! ...

becomes simply this for Y only processing:

Code: Select all

... ! "video/x-raw, format=gray8, ..." ! ...
https://github.com/Hermann-SW/memrun
https://stamm-wilbrandt.de/2wheel_balancing_robot
https://stamm-wilbrandt.de/en#raspcatbot
https://github.com/Hermann-SW/Raspberry_v1_camera_global_external_shutter
https://stamm-wilbrandt.de/en/Raspberry_camera.html

User avatar
HermannSW
Posts: 4959
Joined: Fri Jul 22, 2016 9:09 pm
Location: Eberbach, Germany
Contact: Website Twitter YouTube

Re: Question on yuv I420 to grey conversion

Tue Oct 22, 2019 9:49 pm

TIL that for keeping YUV frame format, conversion to grey is done by setting U and V value to 128 for all frame pixels.

While the "YUV to RGB" formula might be oversimplified as 6by9 stated, you can see that setting U=V=128 results in equal R, G and B values, which is grey:
https://www.fourcc.org/fccyvrgb.php

Code: Select all

B = 1.164(Y - 16)                   + 2.018(U - 128)
G = 1.164(Y - 16) - 0.813(V - 128) - 0.391(U - 128)
R = 1.164(Y - 16) + 1.596(V - 128)
https://github.com/Hermann-SW/memrun
https://stamm-wilbrandt.de/2wheel_balancing_robot
https://stamm-wilbrandt.de/en#raspcatbot
https://github.com/Hermann-SW/Raspberry_v1_camera_global_external_shutter
https://stamm-wilbrandt.de/en/Raspberry_camera.html

Return to “Camera board”