Last active
June 21, 2018 11:52
-
-
Save hsuRush/2471f389e378fb45c6bc0de1240ff726 to your computer and use it in GitHub Desktop.
Load opencv matrix in python ! reference from https://gist.github.com/autosquid/66acc22b3798b36aea0a
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
reference from https://gist.github.com/autosquid/66acc22b3798b36aea0a | |
example yaml file: | |
%YAML:1.0 | |
--- | |
camera_matrix: !!opencv-matrix | |
rows: 3 | |
cols: 3 | |
dt: d | |
data: [ 4.8175329437169847e+02, 0., 3.0642034206715834e+02, 0., | |
4.8165811490603426e+02, 2.4021175432811785e+02, 0., 0., 1. ] | |
distortion_coefficients: !!opencv-matrix | |
rows: 1 | |
cols: 5 | |
dt: d | |
data: [ 5.0298513119414620e-02, -8.1903434648371942e-01, | |
1.4316138220892072e-03, -1.3935294585502944e-02, | |
1.0838184439911540e+00 ] | |
""" | |
import yaml # " $pip install pyyaml " if you haven't | |
import numpy as np | |
def _opencv_matrix(loader, node): # for constructor | |
mapping = loader.construct_mapping(node, deep = True) | |
mat = np.array(mapping["data"]) | |
mat = mat.copy() | |
mat.resize(mapping["rows"], mapping["cols"]) | |
return mat | |
def load_opencv_yaml(file_name): | |
skiplines = 2 | |
with open(file_name) as fin: | |
for i in range(skiplines): | |
_ = fin.readline() | |
yaml.add_constructor(u"tag:yaml.org,2002:opencv-matrix", _opencv_matrix) | |
result = yaml.load(fin) | |
return result | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment