-
Notifications
You must be signed in to change notification settings - Fork 157
/
Copy pathEx2CalibrateCamera.scala
55 lines (42 loc) · 1.43 KB
/
Ex2CalibrateCamera.scala
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
/*
* Copyright (c) 2011-2019 Jarek Sacha. All Rights Reserved.
*
* Author's e-mail: jpsacha at gmail.com
*/
package opencv_cookbook.chapter10
import java.io.File
import opencv_cookbook.OpenCVUtils._
import org.bytedeco.javacpp.indexer.DoubleRawIndexer
import org.bytedeco.opencv.opencv_core.Size
/**
* The main example for section "Calibrating a camera" in Chapter 9, page 221.
*/
object Ex2CalibrateCamera extends App {
// Generate file list
val fileList = for (i <- 1 to 20) yield new File("data/chessboards/chessboard%02d.jpg".format(i))
// Create calibrator object
val cameraCalibrator = new CameraCalibrator()
// Add the corners from the chessboard
println("Adding chessboard points from images...")
val boardSize = new Size(6, 4)
cameraCalibrator.addChessboardPoints(fileList, boardSize)
// Load image for that will be undistorted
val image = loadAndShowOrExit(fileList(6))
// Calibrate camera
println("Calibrating...")
cameraCalibrator.calibrate(image.size())
// Undistort
println("Undistorting...")
val undistorted = cameraCalibrator.remap(image)
// Display camera matrix
val m = cameraCalibrator.cameraMatrix
val mIndx = m.createIndexer().asInstanceOf[DoubleRawIndexer]
println("Camera intrinsic: " + m.rows + "x" + m.cols)
for (i <- 0 until 3) {
for (j <- 0 until 3) {
print("%7.2f ".format(mIndx.get(i, j)))
}
println("")
}
show(undistorted, "Undistorted image.")
}