summaryrefslogtreecommitdiffhomepage
path: root/opentrack/opencv-calibration.hpp
blob: 71f30850933eb53e36c0a8ee03eb12e1415daedc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
/* Copyright (c) 2015, Stanislaw Halik <sthalik@misaki.pl>

 * Permission to use, copy, modify, and/or distribute this
 * software for any purpose with or without fee is hereby granted,
 * provided that the above copyright notice and this permission
 * notice appear in all copies.
 */

#pragma once
#include <QCoreApplication>
#include <QString>
#include <QByteArray>
#include <string>
#include <opencv2/core.hpp>

template<typename = void>
bool get_camera_calibration(const QString& camera_name, cv::Mat& intrinsics, cv::Mat& distortion, int w, int h)
{
    QString pathname_ = QCoreApplication::applicationDirPath() + "/camera/" + camera_name + ".yml";
    std::string pathname = pathname_.toStdString();
    cv::FileStorage fs(pathname, cv::FileStorage::READ);
    if (!fs.isOpened())
        return false;
    cv::Mat intrinsics_, distortion_;
    fs["camera_matrix"] >> intrinsics_;
    fs["distortion_coefficients"] >> distortion_;
    intrinsics_.at<double>(0, 0) *= w / 640.;
    intrinsics_.at<double>(2, 0) *= w / 640.;
    intrinsics_.at<double>(1, 1) *= h / 480.;
    intrinsics_.at<double>(2, 1) *= h / 480.;
    intrinsics = intrinsics_;
    distortion = distortion_;
    return true;
}