Skip to content

Instantly share code, notes, and snippets.

View cheind's full-sized avatar

Christoph Heindl cheind

View GitHub Profile
@cheind
cheind / cv2ndc.py
Created September 28, 2023 11:10
OpenCV camera coordinates to OpenGL NDC matrix
import numpy as np
def cv_to_ndc(fx, fy, cx, cy, near, far, w, h):
"""Returns the 4x4 projection matrix that converts from OpenCV camera
coordinates to OpenGL NDC coordinates.
This takes into account that cameras in
- OpenCV has +z into scene, +y down the image
- OpenGL has -z into scene, +y up the image
@cheind
cheind / order_corners_cv2FindChessboardCornersSB.py
Last active September 17, 2024 08:06
Ensures a consistent ordering of corners returned by cv2.findChessboardCornersSB for asymmetrical chessboards.
"""
Ensures a consistent ordering of corners returned by cv2.findChessboardCornersSB
for asymmetrical chessboards.
For patterns with dims i, j where i > j and i is odd the script ensures the corner
order to start at a black square running along the positive i axis.
The detected corners are used to create a canonical chessboard view. This
view is analysed by looking at the 2x2 blocks associated with each corner.
A characteristic value is computed from each 2x2 block and if the series
@cheind
cheind / caprecap.py
Last active April 23, 2024 06:35
Capture-recapture method to estimate population size
# Estimate the population size using capture-recapture method
# Assumes: experiment twice, closed population
import numpy as np
import matplotlib.pyplot as plt
T = 1000 # true pop size
N1 = 50 # size of sample 1
N2 = 30 # size of sample 2
@cheind
cheind / platform_generate.py
Created May 5, 2022 07:53
cross platform pip-compile helper
import argparse
import platform
import sys
import subprocess
from pathlib import Path
def describe() -> dict:
meta = {
"python": "py" + ".".join(platform.python_version_tuple()[:2]),
@cheind
cheind / fractals.py
Last active July 24, 2023 13:01
Fractals from #Numberphile/Chaos Theory
import numpy as np
import matplotlib.pyplot as plt
def sierpinski_triangle():
"""Generates a Sierpinski triangle.
Given 3 anchor points and a trace point, the
next trace point is half-way between its current
location and a randomly chosen anchor.
@cheind
cheind / optional_property.h
Created December 13, 2011 19:29
C++ Policy Based Property Implementation
/**
* C++ property implementation
* Christoph Heindl 2011
* christoph.heindl@gmail.com
*/
#pragma once
#include <cheind/properties/property.h>
#include <cheind/properties/policy_optional_value.h>
@cheind
cheind / hmm_train_tf.py
Last active December 5, 2022 07:20
HMM training based on gradient descent (Tensorflow version)
__author__ = 'Christoph Heindl'
__copyright__ = 'Copyright 2017'
__license__ = 'BSD'
"""Trains a HMM based on gradient descent optimization.
The parameters (theta) of the model are transition and
emission probabilities, as well as the initial state probabilities.
Given a start solution, the negative log likelihood of data given the
@cheind
cheind / blend.py
Last active March 21, 2022 16:26
Blending of motion parabolas
import numpy as np
import matplotlib.pyplot as plt
import dataclasses
@dataclasses.dataclass
class MotionEstimate:
coeffs: np.ndarray # 3x1
t0: float
degree: int = dataclasses.field(init=False)
@cheind
cheind / complexity.py
Created November 10, 2021 08:41
(Batched) Sample Entropy in PyTorch for measuring complexities of time-series (see https://en.wikipedia.org/wiki/Sample_entropy)
import torch
def sample_entropy(
x: torch.Tensor, m: int = 2, r: float = None, stride: int = 1, subsample: int = 1
):
"""Returns the (batched) sample entropy of the given time series.
Sample entropy is a measure of complexity of sequences that can be related
to predictability. Sample entropy (SE) is defined as the negative logarithm of
the following ratio:
@cheind
cheind / optcost.py
Last active April 12, 2021 10:01
Linear Programming for Optimizing Funding Costs. See https://doi.org/10.5281/zenodo.4607219 for documentation.
from scipy.optimize import linprog
import numpy as np
import pandas as pd
def print_metrics(df):
print('Total staff costs', df.to_numpy().sum())
print('Management cost ratio')
print(df.MgtStaffCosts / df.to_numpy().sum())
print('Partner cost ratio')