Skip to content

Commit

Permalink
add func InitUndistortRectifyMap (hybridgroup#405)
Browse files Browse the repository at this point in the history
* add func InitUndistortRectifyMap
  • Loading branch information
linyongzuo authored and deadprogram committed Mar 7, 2019
1 parent 7e49e3e commit e95a3bc
Show file tree
Hide file tree
Showing 9 changed files with 118 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ count.out
/stage
.vscode/
/build
.idea/
17 changes: 17 additions & 0 deletions calib3d.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,20 @@ void Fisheye_UndistortImageWithParams(Mat distorted, Mat undistorted, Mat k, Mat
cv::fisheye::undistortImage(*distorted, *undistorted, *k, *d, *knew, sz);
}

void InitUndistortRectifyMap(Mat cameraMatrix,Mat distCoeffs,Mat r,Mat newCameraMatrix,Size size,int m1type,Mat map1,Mat map2) {
cv::Size sz(size.width, size.height);
cv::initUndistortRectifyMap(*cameraMatrix,*distCoeffs,*r,*newCameraMatrix,sz,m1type,*map1,*map2);
}

Mat GetOptimalNewCameraMatrixWithParams(Mat cameraMatrix,Mat distCoeffs,Size size,double alpha,Size newImgSize,Rect* validPixROI,bool centerPrincipalPoint) {
cv::Size sz(size.width, size.height);
cv::Size newSize(newImgSize.width, newImgSize.height);
cv::Rect rect(validPixROI->x,validPixROI->y,validPixROI->width,validPixROI->height);
cv::Mat* mat = new cv::Mat(cv::getOptimalNewCameraMatrix(*cameraMatrix,*distCoeffs,sz,alpha,newSize,&rect,centerPrincipalPoint));
validPixROI->x = rect.x;
validPixROI->y = rect.y;
validPixROI->width = rect.width;
validPixROI->height = rect.height;
return mat;
}

31 changes: 31 additions & 0 deletions calib3d.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,34 @@ func FisheyeUndistortImageWithParams(distorted Mat, undistorted *Mat, k, d, knew
}
C.Fisheye_UndistortImageWithParams(distorted.Ptr(), undistorted.Ptr(), k.Ptr(), d.Ptr(), knew.Ptr(), sz)
}

// InitUndistortRectifyMap computes the joint undistortion and rectification transformation and represents the result in the form of maps for remap
//
// For further details, please see:
// https://docs.opencv.org/master/d9/d0c/group__calib3d.html#ga7dfb72c9cf9780a347fbe3d1c47e5d5a
//
func InitUndistortRectifyMap(cameraMatrix Mat, distCoeffs Mat, r Mat, newCameraMatrix Mat, size image.Point, m1type int, map1 Mat, map2 Mat) {
sz := C.struct_Size{
width: C.int(size.X),
height: C.int(size.Y),
}
C.InitUndistortRectifyMap(cameraMatrix.Ptr(), distCoeffs.Ptr(), r.Ptr(), newCameraMatrix.Ptr(), sz, C.int(m1type), map1.Ptr(), map2.Ptr())
}

// GetOptimalNewCameraMatrixWithParams computes and returns the optimal new camera matrix based on the free scaling parameter.
//
// For further details, please see:
// https://docs.opencv.org/master/d9/d0c/group__calib3d.html#ga7a6c4e032c97f03ba747966e6ad862b1
//
func GetOptimalNewCameraMatrixWithParams(cameraMatrix Mat, distCoeffs Mat, imageSize image.Point, alpha float64, newImgSize image.Point, centerPrincipalPoint bool) (Mat, image.Rectangle) {
sz := C.struct_Size{
width: C.int(imageSize.X),
height: C.int(imageSize.Y),
}
newSize := C.struct_Size{
width: C.int(newImgSize.X),
height: C.int(newImgSize.Y),
}
rt := C.struct_Rect{}
return newMat(C.GetOptimalNewCameraMatrixWithParams(cameraMatrix.Ptr(), distCoeffs.Ptr(), sz, C.double(alpha), newSize, &rt, C.bool(centerPrincipalPoint))), toRect(rt)
}
3 changes: 3 additions & 0 deletions calib3d.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ extern "C" {
void Fisheye_UndistortImage(Mat distorted, Mat undistorted, Mat k, Mat d);
void Fisheye_UndistortImageWithParams(Mat distorted, Mat undistorted, Mat k, Mat d, Mat knew, Size size);

void InitUndistortRectifyMap(Mat cameraMatrix,Mat distCoeffs,Mat r,Mat newCameraMatrix,Size size,int m1type,Mat map1,Mat map2);
Mat GetOptimalNewCameraMatrixWithParams(Mat cameraMatrix,Mat distCoeffs,Size size,double alpha,Size newImgSize,Rect* validPixROI,bool centerPrincipalPoint);

#ifdef __cplusplus
}
#endif
Expand Down
61 changes: 61 additions & 0 deletions calib3d_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package gocv

import (
"fmt"
"image"
"image/color"
"testing"
)

Expand Down Expand Up @@ -99,3 +101,62 @@ func TestFisheyeUndistorImageWithParams(t *testing.T) {
}
// IMWrite("images/fisheye_sample-up.jpg", dest)
}

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

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

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

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

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

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

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

d.SetDoubleAt(0, 0, -3.65584802e-01)
d.SetDoubleAt(0, 1, 1.41555815e-01)
d.SetDoubleAt(0, 2, -2.62985819e-03)
d.SetDoubleAt(0, 3, 2.05841873e-04)
d.SetDoubleAt(0, 4, -2.35021914e-02)
//FisheyeUndistortImage(img, &dest, k, d)
//img.Reshape()
newC, roi := GetOptimalNewCameraMatrixWithParams(k, d, image.Point{X: img.Cols(), Y: img.Rows()}, (float64)(1), image.Point{X: img.Cols(), Y: img.Rows()}, false)
if newC.Empty() {
t.Error("final image is empty")
return
}
fmt.Printf("roi:%+v\n", roi)
defer newC.Close()
r := NewMat()
defer r.Close()
mapx := NewMat()
defer mapx.Close()
mapy := NewMat()
defer mapy.Close()
//dest := NewMat()
InitUndistortRectifyMap(k, d, r, newC, image.Point{X: img.Cols(), Y: img.Rows()}, 5, mapx, mapy)

Remap(img, &dest, &mapx, &mapy, InterpolationDefault, BorderConstant, color.RGBA{0, 0, 0, 0})
flg := IMWrite("images/distortion-correct.jpg", dest)
if !flg {
t.Error("IMWrite failed")
}
}
4 changes: 4 additions & 0 deletions core.go
Original file line number Diff line number Diff line change
Expand Up @@ -1749,6 +1749,10 @@ func toRectangles(ret C.Rects) []image.Rectangle {
return rects
}

func toRect(rect C.Rect) image.Rectangle {
return image.Rect(int(rect.x), int(rect.y), int(rect.x+rect.width), int(rect.y+rect.height))
}

func toCPoints(points []image.Point) C.struct_Points {
cPointSlice := make([]C.struct_Point, len(points))
for i, point := range points {
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module gocv.io/x/gocv
Binary file added images/distortion-correct.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/distortion.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit e95a3bc

Please sign in to comment.