gocv icon indicating copy to clipboard operation
gocv copied to clipboard

undistort function does not work correctly

Open AlexMcGopher opened this issue 1 year ago • 0 comments

Description

I tried to reproduce the undistort test from gocv/calib3d_test.go / function TestUndistort with the original Image distortion.jpg and unchanged code from the repository, but I didn't got the example working.

Steps to Reproduce

  1. Copy gocv/calib3d_test.go / function TestUndistort to a new golang file test-distortion.go
  2. Copy testimage distortion.jpg to same path like go-file
  3. go run test-distortion.go

Your Environment

  • Operating System and version: Linux ubuntu 18
  • OpenCV version used:
  • How did you install OpenCV? build from source
  • GoCV version used: v0.31.0
  • Go version: go version go1.19 linux/arm64
  • Did you run the env.sh or env.cmd script before trying to go run or go build? No

Code of example programm: test-distortion.go (same as gocv/calib3d_test.go / function TestUndistort)

`package main

import ( "fmt" "gocv.io/x/gocv" )

func main() {

img := gocv.IMRead("distortion.jpg", gocv.IMReadUnchanged)
if img.Empty() {
	fmt.Println("#### error open file #############")
	//t.Error("Invalid read of Mat test")
	return
}
defer img.Close()

dest := img.Clone()
defer dest.Close()

k := gocv.NewMatWithSize(3, 3, gocv.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 := gocv.NewMatWithSize(1, 4, gocv.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 := gocv.NewMat()
defer knew.Close()

k.CopyTo(&knew)

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

gocv.Undistort(img, &dest, k, d, knew)

if dest.Empty() {
	fmt.Println("#### final image is empty #############")
	//t.Error("final image is empty")
	return
}
gocv.IMWrite("distortion_up.jpg", dest)

} `

output image:

distortion_up

AlexMcGopher avatar Aug 19 '22 09:08 AlexMcGopher