r3dapple
r3dapple

Reputation: 441

Gstreamer How do I interpret the output of video/x-raw,format=RGB?

So I have been working on a project of mine where I want to make a drone fly autonomously. The drone creates a WiFi which I connect to from my PC. The videostream comes via a UDP connection and is in raw h264. I use gstreamer to display the stream:

gst-launch-1.0 udpsrc port=5555 ! h264parse ! avdec_h264 ! videoconvert ! autovideosink sync=false

This works great for viewing the stream. But what I need is a 3D RGB array of each image of the stream. So I was trying to get the decoded h264 out of the gstreamer pipeline by converting it into RGB.

gst-launch-1.0 udpsrc port=5555 ! h264parse ! avdec_h264 ! decodebin ! videoconvert ! video/x-raw,format=RGB ! tcpclientsink host=NETCAT_TCPLISTENER port=8888
nc -lnvp 8888 > rgb.txt

But I am having a hard time figuring out what the dump means. The wiki doesn't really help me either. https://gstreamer.freedesktop.org/documentation/design/mediatype-video-raw.html

   +--+--+--+ +--+--+--+
   |R0|G0|B0| |R1|G1|B1| ...
   +--+--+--+ +--+--+--+

    Component 0: R
      depth:           8
      pstride:         3
      offset:          0

    Component 1: G
      depth:           8
      pstride:         3
      offset:          1

    Component 2: B
      depth            8
      pstride:         3
      offset:          2

    Image
      default rstride: RU4 (width * 3)
      default size:    rstride (image) * height

Here is a snippet of a sample hexdump:

hexdump rgb.txt
.
.
.
0000100 232d 2b4c 4c21 212b 2b4c 4b21 202a 2a4b
0000110 4b20 202a 2a4b 4b20 202a 2a4a 4922 2129
0000120 2a45 4523 232a 2a42 4122 2129 2941 4021
0000130 2028 263e 3e1e 1e26 253d 3d1d 1d25 243c
0000140 3b1c 1b23 233b 3a1b 1a22 2137 3719 1921
0000150 2032 3217 1720 202f 2f16 1620 202f 2f16
0000160 1620 1f2e 2e15 151f 1e2d 2d14 141e 1d2c
0000170 2c13 131d 1d2c 2c13 131d 1d27 2714 141d
0000180 1b20 2014 141b 1c1c 1c13 131c 1c1c 1c13
0000190 131c 1b1b 1b12 121b 1a1a 1a11 111a 1919
00001a0 1910 1019 1919 1910 1019 181a 1a10 1018
00001b0 171c 1c10 1017 171c 1c10 1017 171c 1c10
00001c0 1017 171c 1c10 1017 171c 1c10 1017 171c
00001d0 1c10 1017 171c 1c10 1017 171c 1c10 1017
00001e0 191b 1b11 1119 1a1a 1a11 111a 1a1a 1a11
00001f0 111a 1a1a 1a11 111a 1a1a 1a11 111a 1a1a
0000200 1a11 111a 1a1a 1b11 121b 1b1b 1c12 131c
0000210 1d1d 1f14 161f 1f1f 2016 1720 2020 2017
0000220 1720 2020 2017 1720 2020 2017 1720 2020
0000230 2017 1720 2020 2017 1720 2020 2017 1720
0000240 1f1f 1f16 161f 1f1f 1f16 161f 1f1f 1f16
0000250 161f 1f1f 1f16 161f 1f1f 1f16 161f 1f1f
0000260 1f16 161f 1f1f 1f16 161f 1f1f 1f16 161f
0000270 2020 2017 1720 2020 2017 1720 2020 2017
0000280 1720 2020 2017 1720 2020 2017 1720 2020
0000290 2017 1720 2020 2017 1720 2020 2017 1720
00002a0 1f1f 1f16 161f 1f1f 1f16 161f 1f1f 1f16
00002b0 161f 1f1f 1f16 161f 1f1f 1f16 161f 1f1f
00002c0 1f16 161f 1f1f 1f16 161f 1f1f 1f16 161f
00002d0 1f1f 1f16 161f 1f1f 1f16 161f 1f1f 1f16
00002e0 161f 1f1f 1f16 161f 1f1f 1f16 161f 1f1f
00002f0 1f16 161f 1f1f 1f16 161f 1f1f 1f16 161f
0000300 1d1d 1d14 141d 1d1d 1d14 141d 1d1d 1d14
0000310 141d 1d1d 1d14 141d 1d1d 1d14 141d 1d1d
0000320 1d14 141d 1d1d 1d14 141d 1d1d 1d14 141d
0000330 1f1f 1f16 161f 1f1f 1f16 161f 1f1f 1f16
0000340 161f 1f1f 1f16 161f 1f1f 1f16 161f 1f1f
0000350 1f16 161f 1f1f 1f16 161f 1f1f 1f16 161f
0000360 1f1f 1f16 161f 1f1f 1f16 161f 1f1f 1f16
0000370 161f 1f1f 1f16 161f 1f1f 1f16 161f 1f1f
0000380 1f16 161f 1f1f 1f16 161f 1f1f 1f16 161f
0000390 1f1f 1f16 161f 1f1f 1f16 161f 1f1f 1f16
00003a0 161f 1f1f 1f16 161f 1f1f 1f16 161f 1f1f
00003b0 1f16 161f 1f1f 1f16 161f 1f1f 1f16 161f
00003c0 1f1f 1f16 161f 1f1f 1f16 161f 1f1f 1f16
00003d0 161f 1f1f 1f16 161f 1f1f 1f16 161f 1f1f
00003e0 1f16 161f 1f1f 1f16 161f 1f1f 1f16 161f
00003f0 1d1d 1d14 141d 1d1d 1d14 141d 1d1d 1d14
0000400 141d 1d1d 1f14 161f 1f1f 1f16 161f 1f1f
0000410 2016 1720 2020 2017 1720 2020 2017 1720
0000420 2121 2118 1821 2121 2118 1821 2121 2118
0000430 1821 2121 2118 1821 2121 2118 1821 2121
0000440 1517 ff13 1517 ff13 1517 ff13 1517 ff13
    .
    .
    .

So my question is: How do I interpret the RGB dump of the video/x-raw,format=RGB function? (My goal: In order to write a parser which returns me a 3D RGB array of each frame of the stream.)

Upvotes: 2

Views: 3136

Answers (1)

r3dapple
r3dapple

Reputation: 441

As @MarkSetchell proposed I have to take 2 letters/digits per pixel. Also hexdump on Linux formated the hex values in an undesired way.

The python script I came up with for rendering the Raw RGB output of gstreamer is:

import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import binascii

with open('rgb.txt', 'rb') as f:
    hexdata = binascii.hexlify(f.read())

hexdata = hexdata.decode()
n = 2
hexlist = [hexdata[i:i+n] for i in range(0, len(hexdata), n)]

height = 720
width = 1280
depth = 3
array = np.zeros((height,width,depth), dtype=float)

counter = 0
for y in range(0,height):
    for x in range(0,width):
        for z in range(0,depth):
            array[y][x][z] = float(int(hexlist[counter], 16))
            counter = counter + 1



test_image = image.array_to_img(array)
imgplot = plt.imshow(test_image)

Upvotes: 1

Related Questions