calib

package
v1.5.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 21, 2026 License: BSD-3-Clause Imports: 9 Imported by: 0

Documentation

Overview

Package calib converts between camera-frame 3D coordinates (millimetres) and image pixel coordinates using pinhole intrinsics.

The math mirrors the vendor SDK's stereoConvetPoint3dToDepth / stereoConvetDepthToPoint3d (libStereoCamera.so, Huaray/Dahua 5000 series): pure pinhole projection without distortion, rescaled from the calibration resolution to the actual output resolution by the width ratio only:

scale = intrinsicImgWidth / imgW
u     = (x*K[fx]/z + K[cx]) / scale
v     = (y*K[fy]/z + K[cy]) / scale

Points are expected in the target camera's own frame: origin on the optical axis, X right, Y down, Z forward along the optical axis (millimetres).

Index

Constants

View Source
const (
	FX = 0
	FY = 4
	CX = 2
	CY = 5
)

Indices into the row-major 3x3 intrinsic matrix { fx, 0, cx, 0, fy, cy, 0, 0, 1 }.

Variables

View Source
var ErrNoCalib = errors.New("calib: camera calibration bank is empty")

ErrNoCalib is returned when the camera reports an empty calibration bank (device shipped without on-camera calibration).

Functions

func ReadCalibTypes

func ReadCalibTypes(p RegisterPort) ([]string, error)

ReadCalibTypes returns the calibration type names the camera reports in bank 0x20000 (semicolon-separated, e.g. "calibration_pd").

Types

type CamCalib

type CamCalib struct {
	// IntrinsicImgWidth/Height is the resolution the intrinsics were
	// calibrated at. Projections onto other resolutions are rescaled by the
	// width ratio, matching the vendor SDK.
	IntrinsicImgWidth  int
	IntrinsicImgHeight int

	// K is the row-major 3x3 intrinsic matrix { fx, 0, cx, 0, fy, cy, 0, 0, 1 }.
	K [9]float64
}

CamCalib holds the pinhole intrinsics of one image type.

func (CamCalib) DeprojectPixel

func (c CamCalib) DeprojectPixel(u, v, z float64, imgW, imgH int) (x, y float64)

DeprojectPixel back-projects a pixel plus depth (mm) to a camera-frame point. It is the exact inverse of ProjectPoint3D.

func (CamCalib) ProjectPoint3D

func (c CamCalib) ProjectPoint3D(x, y, z float64, imgW, imgH int) (u, v float64)

ProjectPoint3D projects a camera-frame point (mm) to pixel coordinates on an image of the given size. It returns NaN pixels for z <= 0 or imgW <= 0.

type RegisterPort

type RegisterPort interface {
	ReadReg(addr uint32) (uint32, error)
	WriteReg(addr, value uint32) error
	ReadMem(addr uint32, n int) ([]byte, error)
}

RegisterPort is the register-level transport needed to read camera memory banks. *gvcp.GVCP implements it.

type StereoCalib

type StereoCalib struct {
	LeftImgW, LeftImgH        int
	LeftK                     [9]float64  // leftCamIntrinsic @0x008
	LeftDistortion            [12]float64 // leftCamDistortion @0x050
	RightImgW, RightImgH      int
	RightK                    [9]float64  // rightCamIntrinsic @0x0B8
	RightDistortion           [12]float64 // rightCamDistortion @0x100
	LeftToRightExtrinsic      [16]float64 // @0x160
	LeftRectifyR              [9]float64  // @0x1E0
	RightRectifyR             [9]float64  // @0x228
	LeftP                     [12]float64 // @0x270
	RightP                    [12]float64 // @0x2D0
	Q                         [16]float64 // disparity-to-depth matrix @0x330
	LeftValidRoi              [4]int32    // @0x3B0
	RightValidRoi             [4]int32    // @0x3C0 (never serialized by the vendor JSON)
	StereoRmsError            float64     // @0x3D0
	AveEpipolarError          float64     // @0x3D8
	ColorImgW, ColorImgH      int
	ColorK                    [9]float64  // colorCamIntrinsic @0x3E8
	ColorDistortion           [12]float64 // colorCamDistortion @0x430
	RectLeftToColorExtrinsic  [16]float64 // @0x490
	RectRightToColorExtrinsic [16]float64 // @0x510
	LeftToColorRmsError       float64     // @0x590
	RightToColorRmsError      float64     // @0x598
	ColorRmsError             float64     // @0x5A0
}

StereoCalib mirrors the vendor MvSstereoCalibrateResult POD (1600 bytes, little-endian) stored in camera memory bank 0x20001. Field offsets were recovered from CalibFile::struct2Json and cross-checked against a real DS5131MG30CE calibration export.

func ReadStereoCalib

func ReadStereoCalib(p RegisterPort) (*StereoCalib, error)

ReadStereoCalib downloads the stereo/color calibration from the camera's memory bank 0x20001 over GVCP, verifying the vendor CRC32.

func (*StereoCalib) Color

func (s *StereoCalib) Color() (CamCalib, error)

Color returns the color-camera intrinsics.

func (*StereoCalib) Left

func (s *StereoCalib) Left() (CamCalib, error)

Left returns the raw left (IR) camera intrinsics.

func (*StereoCalib) RectifiedLeft

func (s *StereoCalib) RectifiedLeft() (CamCalib, error)

RectifiedLeft returns the rectified-left projection P as cam calib. Volume results are computed on rectified images, so pack centres are most often in this frame.

type VendorCalibJSON

type VendorCalibJSON struct {
	CameraCalib struct {
		ColorCamImgWidth  int       `json:"colorCamImgWidth"`
		ColorCamImgHeight int       `json:"colorCamImgHeight"`
		ColorCamIntrinsic []float64 `json:"colorCamIntrinsic"`
		LeftCamImgWidth   int       `json:"leftCamImgWidth"`
		LeftCamImgHeight  int       `json:"leftCamImgHeight"`
		LeftCamIntrinsic  []float64 `json:"leftCamIntrinsic"`
		LeftP             []float64 `json:"leftP"`
		WorkDistance      float64   `json:"WorkDistance"`
	} `json:"CameraCalib"`
}

VendorCalibJSON mirrors the calibration export written by the vendor tools ("IPC4.94 Camera Calibration.json" style): a top-level CameraCalib object with per-camera intrinsics and resolutions.

func LoadVendorFile

func LoadVendorFile(path string) (VendorCalibJSON, error)

LoadVendorFile reads a vendor calibration export from path.

func (VendorCalibJSON) Color

func (v VendorCalibJSON) Color() (CamCalib, error)

Color returns the color-camera intrinsics from a vendor calibration export.

func (VendorCalibJSON) Left

func (v VendorCalibJSON) Left() (CamCalib, error)

Left returns the raw left (IR) camera intrinsics from a vendor export.

func (VendorCalibJSON) RectifiedLeft

func (v VendorCalibJSON) RectifiedLeft() (CamCalib, error)

RectifiedLeft returns the rectified-left projection P as cam calib. Volume results are computed on rectified images, so pack centres are most often in this frame; P has the form { fx', 0, cx', 0, fy', cy', ... }.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL