Skip to content

Instantly share code, notes, and snippets.

@Hermann-SW
Last active March 17, 2023 16:25
Embed
What would you like to do?
tool for playing with Raspberry Pi Global Shutter Camera crop values
#!/bin/bash
if [[ $# -lt 4 ]]; then echo Format: "$0" width height framerate ms [us]; exit; fi
if [[ $# -gt 4 ]]; then SHTR="--shutter"; else SHTR=""; fi
for((m=1; m<=5; ++m))
do
if media-ctl -d "/dev/media$m" --set-v4l2 "'imx296 10-001a':0 [fmt:SBGGR10_1X10/${1}x$2 crop:($(( (1440 - $1) / 2 )),$(( (1088 - $2) / 2 )))/${1}x$2]" >/dev/null; then echo -e "/dev/media$m\n"; break; fi
done
libcamera-hello --list-cameras ;echo
rm -f /dev/shm/tst.pts
libcamera-vid --width "$1" --height "$2" --denoise cdn_off --framerate "$3" --save-pts /dev/shm/tst.pts -t "$4" "$SHTR" "$5" -o /dev/shm/tst.h264 -n ;echo
rm -f tstamps.csv && ptsanalyze /dev/shm/tst.pts
@Hermann-SW
Copy link
Author

Hermann-SW commented Mar 16, 2023

Related forum posting:
https://forums.raspberrypi.com/viewtopic.php?p=2090640#p2090640

Script is "shellcheck" clean.

Needs ptsanalyze tool installed somewhere on $PATH:
https://github.com/Hermann-SW/userland/blob/master/tools/ptsanalyze

  • determines the /dev/mediaX device GS camera is connected to, in order to configure crop
  • does "--list-cameras" to confirm crop setting was successful
  • captures video with "libcamera-vid"
  • does frame delay and skip analysis with "ptsanalyze"

Usage:

pi@raspberrypi4B2:~ $ ./GScrop 
Format: ./GScrop width height framerate ms
pi@raspberrypi4B2:~ $ 

Example capturing with 532fps requested, 128x96 crop, for 3000ms — with proof of success and no frame skips:

pi@raspberrypi4B2:~ $ ./GScrop 128 96 532 3000
/dev/media3

Available cameras
-----------------
0 : imx296 [1456x1088] (/base/soc/i2c0mux/i2c@1/imx296@1a)
    Modes: 'SRGGB10_CSI2P' : 1456x1088 [535.91 fps - (0, 0)/128x96 crop]

[9:21:43.834461391] [120326]  INFO Camera camera_manager.cpp:299 libcamera v0.0.4+22-923f5d70
[9:21:43.875920693] [120327]  INFO RPI raspberrypi.cpp:1476 Registered camera /base/soc/i2c0mux/i2c@1/imx296@1a to Unicam device /dev/media3 and ISP device /dev/media0
Mode selection:
    SRGGB10_CSI2P 1456x1088 - Score: 3581.84
[9:21:43.879168888] [120326]  INFO Camera camera.cpp:1028 configuring streams: (0) 128x96-YUV420 (1) 1456x1088-SBGGR10_CSI2P
[9:21:43.879389255] [120327]  INFO RPI raspberrypi.cpp:851 Sensor: /base/soc/i2c0mux/i2c@1/imx296@1a - Selected sensor format: 128x96-SBGGR10_1X10 - Selected unicam format: 128x96-pBAA
Halting: reached timeout of 3000 milliseconds.

creating tstamps.csv
1591 frames were captured
majority framerate 535fps
  frame delta time[us] distribution
      1 1859
      2 1861
      4 1862
     13 1863
     28 1864
     90 1865
    485 1866
    751 1867
    156 1868
     38 1869
     11 1870
      6 1871
      2 1872
      1 1874
> after skip frame indices (middle column)
0 frame skips (0%)
average framerate 536fps
pi@raspberrypi4B2:~ $ 

Slowed down 10× of captured 536fps video (analog clock second hand):

@Hermann-SW
Copy link
Author

Hermann-SW commented Mar 17, 2023

Updated GScrop allows to pass optional shutter time (in microseconds) after video length (in milliseconds), to be used for libcamera-vid video capturing. Also now crop is centered in sensor area (see "...(656, 496)/128x96 crop]" further below):

pi@raspberrypi4B2:~ $ ./GScrop 
Format: ./GScrop width height framerate ms [us]
pi@raspberrypi4B2:~ $ 
pi@raspberrypi4B2:~ $ shellcheck GScrop 
pi@raspberrypi4B2:~ $ 

Sample execution with 29us shutter time, for capturing fast rotating (22,500rpm) RC airplane propeller.
https://forums.raspberrypi.com/viewtopic.php?p=2091003#p2091003
22500/60/536 = 0.70 rotations between successive frames (propeller rotates clockwise, follow one side central reflective marker).
1fps animated .gif shows 20 successive frames from recorded video:

pi@raspberrypi4B2:~ $ ./GScrop 128 96 534 2000 29
/dev/media3

Available cameras
-----------------
0 : imx296 [1456x1088] (/base/soc/i2c0mux/i2c@1/imx296@1a)
    Modes: 'SRGGB10_CSI2P' : 1456x1088 [535.91 fps - (656, 496)/128x96 crop]

[28:22:05.521087746] [277565]  INFO Camera camera_manager.cpp:299 libcamera v0.0.4+22-923f5d70
[28:22:05.562791538] [277566]  INFO RPI raspberrypi.cpp:1476 Registered camera /base/soc/i2c0mux/i2c@1/imx296@1a to Unicam device /dev/media3 and ISP device /dev/media0
Mode selection:
    SRGGB10_CSI2P 1456x1088 - Score: 3581.84
[28:22:05.565814553] [277565]  INFO Camera camera.cpp:1028 configuring streams: (0) 128x96-YUV420 (1) 1456x1088-SBGGR10_CSI2P
[28:22:05.566060087] [277566]  INFO RPI raspberrypi.cpp:851 Sensor: /base/soc/i2c0mux/i2c@1/imx296@1a - Selected sensor format: 128x96-SBGGR10_1X10 - Selected unicam format: 128x96-pBAA
Halting: reached timeout of 2000 milliseconds.

creating tstamps.csv
1054 frames were captured
majority framerate 535fps
  frame delta time[us] distribution
      1 1861
      2 1862
      5 1864
     17 1865
    358 1866
    618 1867
     40 1868
      4 1869
      3 1870
      1 1871
      1 1872
      1 1873
> after skip frame indices (middle column)
0 frame skips (0%)
average framerate 536fps
pi@raspberrypi4B2:~ $ 

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment