Skip to content

Commit

Permalink
Add Calibrate for Fisheye model(WIP)
Browse files Browse the repository at this point in the history
  • Loading branch information
kylycht committed Jun 7, 2018
1 parent 5b9af9c commit 1e487d8
Show file tree
Hide file tree
Showing 4 changed files with 249 additions and 0 deletions.
19 changes: 19 additions & 0 deletions calib3d.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#include "calib3d.h"

double Fisheye_Calibrate(Mat objectPoints, Mat imagePoints, Size size, Mat k, Mat d, Mat rvecs, Mat tvecs) {
cv::Size sz(size.width, size.height);
return cv::fisheye::calibrate(*objectPoints, *imagePoints, sz, *k, *d, *rvecs, *tvecs);
}

void Fisheye_UndistortPoints(Mat distorted, Mat undistorted, Mat k, Mat d) {
cv::fisheye::undistortPoints(*distorted, *undistorted, *k, *d);
}

void Fisheye_UndistortImage(Mat distorted, Mat undistorted, Mat k, Mat d) {
cv::fisheye::undistortImage(*distorted, *undistorted, *k, *d);
}

void Fisheye_UndistortImageWithKNewMat(Mat distorted, Mat undistorted, Mat k, Mat d, Mat knew) {
cv::fisheye::undistortImage(*distorted, *undistorted, *k, *d, *knew);
}

81 changes: 81 additions & 0 deletions calib3d.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package gocv

/*
#include <stdlib.h>
#include "calib3d.h"
*/
import "C"

import (
"image"
)

// Calib is a wrapper around OpenCV's "Camera Calibration and 3D Reconstruction" of
// Fisheye Camera model
//
// For more details, please see:
// https://docs.opencv.org/trunk/db/d58/group__calib3d__fisheye.html

// CalibFlag value for calibration
type CalibFlag int32

const (
// CalibUseIntrinsicGuess indicates that cameraMatrix contains valid initial values
// of fx, fy, cx, cy that are optimized further. Otherwise, (cx, cy) is initially
// set to the image center ( imageSize is used), and focal distances are computed
// in a least-squares fashion.
CalibUseIntrinsicGuess CalibFlag = 1 << iota

// CalibRecomputeExtrinsic indicates that extrinsic will be recomputed after each
// iteration of intrinsic optimization.
CalibRecomputeExtrinsic

// CalibCheckCond indicates that the functions will check validity of condition number
CalibCheckCond

// CalibFixSkew indicates that skew coefficient (alpha) is set to zero and stay zero
CalibFixSkew

// CalibFixK1 indicates that selected distortion coefficients are set to zeros and stay zero
CalibFixK1

// CalibFixK2 indicates that selected distortion coefficients are set to zeros and stay zero
CalibFixK2

// CalibFixK3 indicates that selected distortion coefficients are set to zeros and stay zero
CalibFixK3

// CalibFixK4 indicates that selected distortion coefficients are set to zeros and stay zero
CalibFixK4

// CalibFixIntrinsic indicates that fix K1, K2? and D1, D2? so that only R, T matrices are estimated
CalibFixIntrinsic

// CalibFixPrincipalPoint indicates that the principal point is not changed during the global optimization.
// It stays at the center or at a different location specified when CalibUseIntrinsicGuess is set too.
CalibFixPrincipalPoint
)

// FisheyeCalibrate performs camera calibaration.
func FisheyeCalibrate(objectPoints, imagePoints, k, d, rvecs, tvecs Mat, size image.Point) float64 {
sz := C.struct_Size{
width: C.int(size.X),
height: C.int(size.Y),
}
return float64(C.Fisheye_Calibrate(objectPoints.Ptr(), imagePoints.Ptr(), sz, k.Ptr(), d.Ptr(), rvecs.Ptr(), tvecs.Ptr()))
}

// FisheyeUndistortPoints undistorts 2D points using fisheye model
func FisheyeUndistortPoints(distorted, undistorted, k, d Mat) {
C.Fisheye_UndistortPoints(distorted.Ptr(), undistorted.Ptr(), k.Ptr(), d.Ptr())
}

// FisheyeUndistortImage transforms an image to compensate for fisheye lens distortion
func FisheyeUndistortImage(distorted, undistorted, k, d Mat) {
C.Fisheye_UndistortImage(distorted.Ptr(), undistorted.Ptr(), k.Ptr(), d.Ptr())
}

// FisheyeUndistortImageWithKNewMat transforms an image to compensate for fisheye lens distortion with Knew matrix
func FisheyeUndistortImageWithKNewMat(distorted, undistorted, k, d, knew Mat) {
C.Fisheye_UndistortImageWithKNewMat(distorted.Ptr(), undistorted.Ptr(), k.Ptr(), d.Ptr(), knew.Ptr())
}
24 changes: 24 additions & 0 deletions calib3d.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#ifndef _OPENCV3_CALIB_H_
#define _OPENCV3_CALIB_H_

#ifdef __cplusplus
#include <opencv2/opencv.hpp>
#include <opencv2/calib3d.hpp>


extern "C" {
#endif

#include "core.h"

//Calib
double Fisheye_Calibrate(Mat objectPoints, Mat imagePoints, Size size, Mat k, Mat d, Mat rvecs, Mat tvecs);
void Fisheye_UndistortPoints(Mat distorted, Mat undistorted, Mat k, Mat d);
void Fisheye_UndistortImage(Mat distorted, Mat undistorted, Mat k, Mat d);
void Fisheye_UndistortImageWithKNewMat(Mat distorted, Mat undistorted, Mat k, Mat d, Mat knew);

#ifdef __cplusplus
}
#endif

#endif //_OPENCV3_CALIB_H
125 changes: 125 additions & 0 deletions calib3d_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
package gocv

import (
"image/jpeg"
"os"
"testing"
)

func TestFisheyeCalibrate(t *testing.T) {

}

func TestFisheyeUndistorImage(t *testing.T) {
img := IMRead("images/fisheyelens.jpg", IMReadUnchanged)
if img.Empty() {
t.Error("Invalid read of Mat in BilateralFilter test")
}
defer img.Close()

dest := NewMat()
defer dest.Close()

k := NewMatWithSize(3, 3, MatTypeCV64F)
defer k.Close()

k.SetDoubleAt(0, 0, 689.21)
k.SetDoubleAt(0, 1, 0)
k.SetDoubleAt(0, 2, 1295.56)

k.SetDoubleAt(1, 0, 0)
k.SetDoubleAt(1, 1, 690.48)
k.SetDoubleAt(1, 2, 942.17)

k.SetDoubleAt(2, 0, 0)
k.SetDoubleAt(2, 1, 0)
k.SetDoubleAt(2, 2, 1)

d := NewMatWithSize(1, 4, MatTypeCV64F)
defer d.Close()

d.SetDoubleAt(0, 0, 0)
d.SetDoubleAt(0, 1, 0)
d.SetDoubleAt(0, 2, 0)
d.SetDoubleAt(0, 3, 0)

FisheyeUndistortImage(img, dest, k, d)

finalImg, err := dest.ToImage()
if err != nil {
t.Error(err)
}

f, err := os.Create("images/fisheye_undistorted.jpg")
if err != nil {
t.Error(err)
}

defer f.Close()

if err := jpeg.Encode(f, finalImg, nil); err != nil {
t.Error(err)
}

}

func TestFisheyeUndistorImageWithKNewMat(t *testing.T) {
img := IMRead("images/fisheyelens.png", IMReadUnchanged)
if img.Empty() {
t.Error("Invalid read of Mat in BilateralFilter test")
}
defer img.Close()

dest := NewMat()
defer dest.Close()

k := NewMatWithSize(3, 3, MatTypeCV64F)
defer k.Close()

k.SetDoubleAt(0, 0, 689.21)
k.SetDoubleAt(0, 1, 0)
k.SetDoubleAt(0, 2, 1295.56)

k.SetDoubleAt(1, 0, 0)
k.SetDoubleAt(1, 1, 690.48)
k.SetDoubleAt(1, 2, 942.17)

k.SetDoubleAt(2, 0, 0)
k.SetDoubleAt(2, 1, 0)
k.SetDoubleAt(2, 2, 1)

d := NewMatWithSize(1, 4, MatTypeCV64F)
defer d.Close()

d.SetDoubleAt(0, 0, 0)
d.SetDoubleAt(0, 1, 0)
d.SetDoubleAt(0, 2, 0)
d.SetDoubleAt(0, 3, 0)

knew := NewMat()
defer knew.Close()

k.CopyTo(knew)

knew.SetDoubleAt(0, 0, 0.4*k.GetDoubleAt(0, 0))
knew.SetDoubleAt(1, 1, 0.4*k.GetDoubleAt(1, 1))

FisheyeUndistortImageWithKNewMat(img, dest, k, d, knew)

finalImg, err := dest.ToImage()
if err != nil {
t.Error(err)
}

f, err := os.Create("images/fisheye_undistortedknewMat.jpg")
if err != nil {
t.Error(err)
}

defer f.Close()

if err := jpeg.Encode(f, finalImg, nil); err != nil {
t.Error(err)
}

}

0 comments on commit 1e487d8

Please sign in to comment.