From 88e6df69b4434e2d3a6ce690e85723056f2a444e Mon Sep 17 00:00:00 2001 From: Samuel Audet Date: Thu, 15 Nov 2018 23:48:52 +0900 Subject: [PATCH] * Upgrade dependencies for OpenCV 4.0.0-rc --- CHANGELOG.md | 2 +- platform/pom.xml | 4 +- pom.xml | 4 +- samples/Demo.java | 3 +- .../org/bytedeco/javacv/CameraDevice.java | 20 +- .../bytedeco/javacv/GeometricCalibrator.java | 72 +++++-- .../bytedeco/javacv/IPCameraFrameGrabber.java | 8 +- .../bytedeco/javacv/JavaCvErrorCallback.java | 3 + .../java/org/bytedeco/javacv/MarkedPlane.java | 2 +- .../bytedeco/javacv/ProCamTransformer.java | 4 +- .../org/bytedeco/javacv/ProjectiveDevice.java | 177 +++++++++--------- .../javacv/ProjectiveTransformer.java | 2 +- .../org/bytedeco/javacv/ProjectorDevice.java | 20 +- 13 files changed, 183 insertions(+), 138 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a5136dcb..dc69036a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,7 +2,7 @@ * Add support for multiple inputs to `FFmpegFrameFilter` ([issue #955](https://github.com/bytedeco/javacv/issues/955)) * Fix fps in output of `FFmpegFrameRecorder` by setting deprecated `AVStream.codec.time_base` ([issue #1069](https://github.com/bytedeco/javacv/issues/1069)) * Fix memory leak in `FFmpegFrameRecorder` on `writePacket()` ([issue #1068](https://github.com/bytedeco/javacv/issues/1068)) - * Upgrade dependencies for FFmpeg 4.0.3 and Tesseract 4.0.0 + * Upgrade dependencies for OpenCV 4.0.0-rc, FFmpeg 4.1, and Tesseract 4.0.0 ### October 15, 2018 version 1.4.3 * Add `imageScalingFlags` property to `FrameGrabber` and `FrameRecorder`, with `SWS_BILINEAR` as default for FFmpeg ([issue #845](https://github.com/bytedeco/javacv/issues/845)) diff --git a/platform/pom.xml b/platform/pom.xml index 4e022109..a4d9ba47 100644 --- a/platform/pom.xml +++ b/platform/pom.xml @@ -28,12 +28,12 @@ org.bytedeco.javacpp-presets opencv-platform - 3.4.3-${javacpp.version} + 4.0.0-rc-${javacpp.version} org.bytedeco.javacpp-presets ffmpeg-platform - 4.0.3-${javacpp.version} + 4.1-${javacpp.version} org.bytedeco.javacpp-presets diff --git a/pom.xml b/pom.xml index 4ec48ea9..979217c9 100644 --- a/pom.xml +++ b/pom.xml @@ -74,12 +74,12 @@ org.bytedeco.javacpp-presets opencv - 3.4.3-${javacpp.version} + 4.0.0-rc-${javacpp.version} org.bytedeco.javacpp-presets ffmpeg - 4.0.3-${javacpp.version} + 4.1-${javacpp.version} org.bytedeco.javacpp-presets diff --git a/samples/Demo.java b/samples/Demo.java index 6374fb27..4c21b845 100644 --- a/samples/Demo.java +++ b/samples/Demo.java @@ -113,8 +113,7 @@ public static void main(String[] args) throws Exception { // Let's try to detect some faces! but we need a grayscale image... cvtColor(grabbedImage, grayImage, CV_BGR2GRAY); RectVector faces = new RectVector(); - classifier.detectMultiScale(grayImage, faces, - 1.1, 3, CV_HAAR_FIND_BIGGEST_OBJECT | CV_HAAR_DO_ROUGH_SEARCH, null, null); + classifier.detectMultiScale(grayImage, faces); long total = faces.size(); for (long i = 0; i < total; i++) { Rect r = faces.get(i); diff --git a/src/main/java/org/bytedeco/javacv/CameraDevice.java b/src/main/java/org/bytedeco/javacv/CameraDevice.java index 7ca01dd7..98ca2ed4 100644 --- a/src/main/java/org/bytedeco/javacv/CameraDevice.java +++ b/src/main/java/org/bytedeco/javacv/CameraDevice.java @@ -45,7 +45,7 @@ public CameraDevice(String name, String filename) throws Exception { settings.setImageWidth(imageWidth); settings.setImageHeight(imageHeight); } - public CameraDevice(String name, CvFileStorage fs) throws Exception { + public CameraDevice(String name, FileStorage fs) throws Exception { super(name, fs); settings.setImageWidth(imageWidth); settings.setImageHeight(imageHeight); @@ -514,21 +514,21 @@ public FrameGrabber createFrameGrabber() throws FrameGrabber.Exception { } public static CameraDevice[] read(String filename) throws Exception { - CvFileStorage fs = CvFileStorage.open(filename, null, CV_STORAGE_READ); + FileStorage fs = new FileStorage(filename, FileStorage.READ); CameraDevice[] devices = read(fs); fs.release(); return devices; } - public static CameraDevice[] read(CvFileStorage fs) throws Exception { - CvFileNode node = cvGetFileNodeByName(fs, null, "Cameras"); - CvSeq seq = node.data_seq(); - int count = seq.total(); + public static CameraDevice[] read(FileStorage fs) throws Exception { + FileNode node = fs.get("Cameras"); + FileNodeIterator seq = node.begin(); + int count = (int)seq.remaining(); CameraDevice[] devices = new CameraDevice[count]; - for (int i = 0; i < count; i++) { - Pointer p = cvGetSeqElem(seq, i); - if (p == null) continue; - String name = cvReadString(new CvFileNode(p), (String)null); + for (int i = 0; i < count; i++, seq.increment()) { + FileNode n = seq.access(); + if (n.empty()) continue; + String name = n.asBytePointer().getString(); devices[i] = new CameraDevice(name, fs); } return devices; diff --git a/src/main/java/org/bytedeco/javacv/GeometricCalibrator.java b/src/main/java/org/bytedeco/javacv/GeometricCalibrator.java index e21218fe..65a19d84 100644 --- a/src/main/java/org/bytedeco/javacv/GeometricCalibrator.java +++ b/src/main/java/org/bytedeco/javacv/GeometricCalibrator.java @@ -209,6 +209,38 @@ public int getImageCount() { return allObjectMarkers.size(); } + private Point3fVectorVector getObjectPoints(CvMat points, CvMat counts) { + FloatBuffer pointsBuf = points.getFloatBuffer(); + IntBuffer countsBuf = counts.getIntBuffer(); + int n = counts.length(); + Point3fVectorVector vectors = new Point3fVectorVector(n); + for (int i = 0; i < n; i++) { + int m = countsBuf.get(); + Point3fVector vector = new Point3fVector(m); + for (int j = 0; j < m; j++) { + vector.put(j, new Point3f(pointsBuf.get(), pointsBuf.get(), pointsBuf.get())); + } + vectors.put(i, vector); + } + return vectors; + } + + private Point2fVectorVector getImagePoints(CvMat points, CvMat counts) { + FloatBuffer pointsBuf = points.getFloatBuffer(); + IntBuffer countsBuf = counts.getIntBuffer(); + int n = counts.length(); + Point2fVectorVector vectors = new Point2fVectorVector(n); + for (int i = 0; i < n; i++) { + int m = countsBuf.get(); + Point2fVector vector = new Point2fVector(m); + for (int j = 0; j < m; j++) { + vector.put(j, new Point2f(pointsBuf.get(), pointsBuf.get())); + } + vectors.put(i, vector); + } + return vectors; + } + private CvMat[] getPoints(boolean useCenters) { // fill up pointCounts, objectPoints and imagePoints, with data from // srcMarkers and dstMarkers @@ -294,8 +326,8 @@ public static double[] computeReprojectionError(CvMat object_points, cvGetRows(rot_vects, rot_vect, i, i+1, 1); cvGetRows(trans_vects, trans_vect, i, i+1, 1); - cvProjectPoints2(object_points_i, rot_vect, trans_vect, - camera_matrix, dist_coeffs, image_points2_i); + projectPoints(cvarrToMat(object_points_i), cvarrToMat(rot_vect), cvarrToMat(trans_vect), + cvarrToMat(camera_matrix), cvarrToMat(dist_coeffs), cvarrToMat(image_points2_i)); err = cvNorm(image_points_i, image_points2_i); err *= err; if (per_view_errors != null) @@ -347,11 +379,21 @@ public double[] calibrate(boolean useCenters) { cvGetCols(d.extrParams, transVects, 3, 6); CvMat[] points = getPoints(useCenters); - cvCalibrateCamera2(points[0], points[1], points[2], - cvSize(d.imageWidth, d.imageHeight), - d.cameraMatrix, d.distortionCoeffs, - rotVects, transVects, dsettings.flags, - cvTermCriteria(CV_TERMCRIT_ITER | CV_TERMCRIT_EPS, 30, JavaCV.DBL_EPSILON)); + MatVector rvecs = new MatVector(); + MatVector tvecs = new MatVector(); + Mat distortionCoeffs = new Mat(); + calibrateCamera(getObjectPoints(points[0], points[2]), getImagePoints(points[1], points[2]), + new Size(d.imageWidth, d.imageHeight), + cvarrToMat(d.cameraMatrix), distortionCoeffs, + rvecs, tvecs, dsettings.flags, + new TermCriteria(CV_TERMCRIT_ITER | CV_TERMCRIT_EPS, 30, JavaCV.DBL_EPSILON)); + int n = (int)rvecs.size(); + CvMat row = new CvMat(); + for (int i = 0; i < n; i++) { + cvTranspose(cvMat(rvecs.get(i)), cvGetRow(rotVects, row, i)); + cvTranspose(cvMat(tvecs.get(i)), cvGetRow(transVects, row, i)); + } + d.distortionCoeffs = cvMat(distortionCoeffs).clone(); if (cvCheckArr(d.cameraMatrix, CV_CHECK_QUIET, 0, 0) != 0 && cvCheckArr(d.distortionCoeffs, CV_CHECK_QUIET, 0, 0) != 0 && @@ -383,12 +425,12 @@ public static double[] computeStereoError(CvMat imagePoints1, CvMat imagePoints2 CvMat L1 = CvMat.create(1, N, CV_32F, 3); CvMat L2 = CvMat.create(1, N, CV_32F, 3); //Always work in undistorted space - cvUndistortPoints(imagePoints1, imagePoints1, M1, D1, null, M1); - cvUndistortPoints(imagePoints2, imagePoints2, M2, D2, null, M2); + undistortPoints(cvarrToMat(imagePoints1), cvarrToMat(imagePoints1), cvarrToMat(M1), cvarrToMat(D1), null, cvarrToMat(M1)); + undistortPoints(cvarrToMat(imagePoints2), cvarrToMat(imagePoints2), cvarrToMat(M2), cvarrToMat(D2), null, cvarrToMat(M2)); //imagePoints1.put(d1.undistort(imagePoints1.get())); //imagePoints2.put(d2.undistort(imagePoints2.get())); - cvComputeCorrespondEpilines(imagePoints1, 1, F, L1); - cvComputeCorrespondEpilines(imagePoints2, 2, F, L2); + computeCorrespondEpilines(cvarrToMat(imagePoints1), 1, cvarrToMat(F), cvarrToMat(L1)); + computeCorrespondEpilines(cvarrToMat(imagePoints2), 2, cvarrToMat(F), cvarrToMat(L2)); double avgErr = 0, maxErr = 0; CvMat p1 = imagePoints1, p2 = imagePoints2; for(int i = 0; i < N; i++ ) { @@ -488,10 +530,10 @@ public double[] calibrateStereo(boolean useCenters, GeometricCalibrator peer) { dp.E = CvMat.create(3, 3); dp.F = CvMat.create(3, 3); - cvStereoCalibrate(objectPointsMat, imagePoints1Mat, imagePoints2Mat, pointCountsMat, - d.cameraMatrix, d.distortionCoeffs, dp.cameraMatrix, dp.distortionCoeffs, - cvSize(d.imageWidth, d.imageHeight), dp.R, dp.T, dp.E, dp.F, dpsettings.flags, - cvTermCriteria(CV_TERMCRIT_ITER+CV_TERMCRIT_EPS,100,1e-6)); + stereoCalibrate(getObjectPoints(objectPointsMat, pointCountsMat), getImagePoints(imagePoints1Mat, pointCountsMat), getImagePoints(imagePoints2Mat, pointCountsMat), + cvarrToMat(d.cameraMatrix), cvarrToMat(d.distortionCoeffs), cvarrToMat(dp.cameraMatrix), cvarrToMat(dp.distortionCoeffs), + new Size(d.imageWidth, d.imageHeight), cvarrToMat(dp.R), cvarrToMat(dp.T), cvarrToMat(dp.E), cvarrToMat(dp.F), dpsettings.flags, + new TermCriteria(CV_TERMCRIT_ITER+CV_TERMCRIT_EPS,100,1e-6)); // compute and return epipolar error... d.avgEpipolarErr = 0.0; diff --git a/src/main/java/org/bytedeco/javacv/IPCameraFrameGrabber.java b/src/main/java/org/bytedeco/javacv/IPCameraFrameGrabber.java index c7c29489..64a6dd90 100644 --- a/src/main/java/org/bytedeco/javacv/IPCameraFrameGrabber.java +++ b/src/main/java/org/bytedeco/javacv/IPCameraFrameGrabber.java @@ -68,7 +68,7 @@ public static void tryLoad() throws Exception { private final int readTimeout; private DataInputStream input; private byte[] pixelBuffer = new byte[1024]; - private IplImage decoded = null; + private Mat decoded = null; /** * @param url The URL to create the camera connection with. @@ -160,9 +160,9 @@ public void trigger() throws Exception { public Frame grab() throws Exception { try { final byte[] b = readImage(); - final CvMat mat = cvMat(1, b.length, CV_8UC1, new BytePointer(b)); + final Mat mat = new Mat(1, b.length, CV_8UC1, new BytePointer(b)); releaseDecoded(); - return converter.convert(decoded = cvDecodeImage(mat)); + return converter.convert(decoded = imdecode(mat, IMREAD_COLOR)); } catch (IOException e) { throw new Exception(e.getMessage(), e); } @@ -180,7 +180,7 @@ public BufferedImage grabBufferedImage() throws IOException { */ private void releaseDecoded() { if (decoded != null) { - cvReleaseImage(decoded); + decoded.release(); decoded = null; } } diff --git a/src/main/java/org/bytedeco/javacv/JavaCvErrorCallback.java b/src/main/java/org/bytedeco/javacv/JavaCvErrorCallback.java index c401e500..21a86084 100644 --- a/src/main/java/org/bytedeco/javacv/JavaCvErrorCallback.java +++ b/src/main/java/org/bytedeco/javacv/JavaCvErrorCallback.java @@ -38,6 +38,8 @@ */ public class JavaCvErrorCallback extends CvErrorCallback { + static JavaCvErrorCallback instance; + public JavaCvErrorCallback() { this(false); } @@ -48,6 +50,7 @@ public JavaCvErrorCallback(boolean showDialog, Component parent) { this(showDialog, parent, 0); } public JavaCvErrorCallback(boolean showDialog, Component parent, int rc) { + instance = this; this.parent = parent; this.showDialog = showDialog; this.rc = rc; diff --git a/src/main/java/org/bytedeco/javacv/MarkedPlane.java b/src/main/java/org/bytedeco/javacv/MarkedPlane.java index c375a70e..86b38c6c 100644 --- a/src/main/java/org/bytedeco/javacv/MarkedPlane.java +++ b/src/main/java/org/bytedeco/javacv/MarkedPlane.java @@ -200,7 +200,7 @@ public double getTotalWarp(Marker[] imagedMarkers, CvMat totalWarp, boolean useC if (numPoints == 4) { JavaCV.getPerspectiveTransform(srcPts.get(), dstPts.get(), totalWarp); } else { - cvFindHomography(srcPts, dstPts, totalWarp); + cvCopy(cvMat(findHomography(cvarrToMat(srcPts), cvarrToMat(dstPts))), totalWarp); } // compute transformed source<->dest RMSE diff --git a/src/main/java/org/bytedeco/javacv/ProCamTransformer.java b/src/main/java/org/bytedeco/javacv/ProCamTransformer.java index e1906580..5ff24e58 100644 --- a/src/main/java/org/bytedeco/javacv/ProCamTransformer.java +++ b/src/main/java/org/bytedeco/javacv/ProCamTransformer.java @@ -473,7 +473,7 @@ private double[] setSubspaceInternal(double ... p) { } double[] dst = new double[8+3]; t.put(p[0], p[1], p[2]); - cvRodrigues2(t, R, null); + Rodrigues(cvarrToMat(t), cvarrToMat(R), null); t.put(p[3], p[4], p[5]); // compute new H @@ -514,7 +514,7 @@ private double[] getSubspaceInternal() { cvMatMul(surfaceTransformer.getInvK2(), H, H); JavaCV.HtoRt(H, R, t); - cvRodrigues2(R, n, null); + Rodrigues(cvarrToMat(R), cvarrToMat(n), null); double[] p = { n.get(0), n.get(1), n.get(2), t.get(0), t.get(1), t.get(2) }; return p; diff --git a/src/main/java/org/bytedeco/javacv/ProjectiveDevice.java b/src/main/java/org/bytedeco/javacv/ProjectiveDevice.java index f5662964..e227d542 100644 --- a/src/main/java/org/bytedeco/javacv/ProjectiveDevice.java +++ b/src/main/java/org/bytedeco/javacv/ProjectiveDevice.java @@ -25,7 +25,7 @@ import java.io.File; import java.nio.FloatBuffer; import java.util.Arrays; -import org.bytedeco.javacpp.Pointer; +import org.bytedeco.javacpp.opencv_core; import static org.bytedeco.javacpp.opencv_calib3d.*; import static org.bytedeco.javacpp.opencv_core.*; @@ -49,7 +49,7 @@ public ProjectiveDevice(String name, String filename) throws Exception { this(name); readParameters(filename); } - public ProjectiveDevice(String name, CvFileStorage fs) throws Exception { + public ProjectiveDevice(String name, FileStorage fs) throws Exception { this(name); readParameters(fs); } @@ -509,7 +509,10 @@ private void initUndistortMaps() { undistortMaps1[p] = IplImage.create(imageWidth, imageHeight, IPL_DEPTH_32F, 1); undistortMaps2[p] = IplImage.create(imageWidth, imageHeight, IPL_DEPTH_32F, 1); } - cvInitUndistortMap(cameraMatrix, distortionCoeffs, undistortMaps1[p], undistortMaps2[p]); + Mat A = cvarrToMat(cameraMatrix); + Mat m1 = cvarrToMat(undistortMaps1[p]); + Mat m2 = cvarrToMat(undistortMaps2[p]); + initUndistortRectifyMap(A, cvarrToMat(distortionCoeffs), new Mat(), A, m1.size(), m1.type(), m1, m2); if (mapsPyramidLevel > 0) { IplImage map1 = undistortMaps1[p]; IplImage map2 = undistortMaps2[p]; @@ -686,7 +689,7 @@ public CvMat getFrontoParallelH(double[] roipts, CvMat n, CvMat H) { double[] dir = JavaCV.unitize(-s*n.get(1), s*n.get(0)); double theta = Math.acos(s*n.get(2)/JavaCV.norm(n.get())); t.put(theta*dir[0], theta*dir[1], 0.0); - cvRodrigues2(t, H, null); + Rodrigues(cvarrToMat(t), cvarrToMat(H), null); // and from z-axis to device axis cvMatMul(R, H, H); @@ -729,12 +732,13 @@ public CvMat getRectifyingHomography(ProjectiveDevice peer, CvMat H) { CvMat R1 = R13x3.get(); CvMat P1 = P13x4.get(); CvMat R2 = R23x3.get(); CvMat P2 = P23x4.get(); - CvSize imageSize = cvSize((peer.imageWidth + imageWidth )/2, + Size imageSize = new Size((peer.imageWidth + imageWidth )/2, (peer.imageHeight + imageHeight)/2); // ? - cvStereoRectify(peer.cameraMatrix, cameraMatrix, - peer.distortionCoeffs, distortionCoeffs, - imageSize, relativeR, relativeT, - R1, R2, P1, P2, null, 0, -1, CvSize.ZERO, null, null); + stereoRectify(cvarrToMat(peer.cameraMatrix), cvarrToMat(cameraMatrix), + cvarrToMat(peer.distortionCoeffs), cvarrToMat(distortionCoeffs), + imageSize, cvarrToMat(relativeR), cvarrToMat(relativeT), + cvarrToMat(R1), cvarrToMat(R2), cvarrToMat(P1), cvarrToMat(P2), + new Mat(), 0, -1, new Size(), null, null); cvMatMul(cameraMatrix, R2, R2); cvInvert(cameraMatrix, R1); cvMatMul(R2, R1, H); @@ -748,7 +752,7 @@ public static class Exception extends java.lang.Exception { } public static ProjectiveDevice[] read(String filename) throws Exception { - CvFileStorage fs = CvFileStorage.open(filename, null, CV_STORAGE_READ); + FileStorage fs = new FileStorage(filename, FileStorage.READ); CameraDevice [] cameraDevices = CameraDevice .read(fs); ProjectorDevice [] projectorDevices = ProjectorDevice.read(fs); ProjectiveDevice[] devices = new ProjectiveDevice[cameraDevices.length+projectorDevices.length]; @@ -778,24 +782,23 @@ public static void write(String filename, ProjectiveDevice[] ... devices) { write(filename, allDevices); } public static void write(String filename, ProjectiveDevice ... devices) { - CvFileStorage fs = CvFileStorage.open(filename, null, CV_STORAGE_WRITE); - CvAttrList a = cvAttrList(); + FileStorage fs = new FileStorage(filename, FileStorage.WRITE); - cvStartWriteStruct(fs, "Cameras", CV_NODE_SEQ, null, a); + shiftLeft(shiftLeft(fs, "Cameras"), "["); for (ProjectiveDevice d : devices) { if (d instanceof CameraDevice) { - cvWriteString(fs, null, d.getSettings().getName(), 0); + opencv_core.write(fs, d.getSettings().getName()); } } - cvEndWriteStruct(fs); + shiftLeft(fs, "]"); - cvStartWriteStruct(fs, "Projectors", CV_NODE_SEQ, null, a); + shiftLeft(shiftLeft(fs, "Projectors"), "["); for (ProjectiveDevice d : devices) { if (d instanceof ProjectorDevice) { - cvWriteString(fs, null, d.getSettings().getName(), 0); + opencv_core.write(fs, d.getSettings().getName()); } } - cvEndWriteStruct(fs); + shiftLeft(fs, "]"); for (ProjectiveDevice d : devices) { d.writeParameters(fs); @@ -807,108 +810,106 @@ public void writeParameters(File file) { writeParameters(file.getAbsolutePath()); } public void writeParameters(String filename) { - CvFileStorage fs = CvFileStorage.open(filename, null, CV_STORAGE_WRITE); + FileStorage fs = new FileStorage(filename, FileStorage.WRITE); writeParameters(fs); fs.release(); } - public void writeParameters(CvFileStorage fs) { - CvAttrList a = cvAttrList(); + public void writeParameters(FileStorage fs) { + shiftLeft(shiftLeft(fs, getSettings().getName()), "{"); - cvStartWriteStruct(fs, getSettings().getName(), CV_NODE_MAP, null, a); - - cvWriteInt(fs, "imageWidth", imageWidth); - cvWriteInt(fs, "imageHeight", imageHeight); - cvWriteReal(fs, "responseGamma", getSettings().getResponseGamma()); -// cvWriteReal(fs, "initAspectRatio", settings.initAspectRatio); -// cvWriteInt(fs, "flags", getSettings().flags); + opencv_core.write(fs, "imageWidth", imageWidth); + opencv_core.write(fs, "imageHeight", imageHeight); + opencv_core.write(fs, "responseGamma", getSettings().getResponseGamma()); +// opencv_core.write(fs, "initAspectRatio", settings.initAspectRatio); +// opencv_core.write(fs, "flags", getSettings().flags); if (cameraMatrix != null) - cvWrite(fs, "cameraMatrix", cameraMatrix, a); + opencv_core.write(fs, "cameraMatrix", cvarrToMat(cameraMatrix)); if (distortionCoeffs != null) - cvWrite(fs, "distortionCoeffs", distortionCoeffs, a); + opencv_core.write(fs, "distortionCoeffs", cvarrToMat(distortionCoeffs)); if (extrParams != null) - cvWrite(fs, "extrParams", extrParams, a); + opencv_core.write(fs, "extrParams", cvarrToMat(extrParams)); if (reprojErrs != null) - cvWrite(fs, "reprojErrs", reprojErrs, a); - cvWriteReal(fs, "avgReprojErr", avgReprojErr); - cvWriteReal(fs, "maxReprojErr", maxReprojErr); -// cvWriteReal(fs, "nominalDistance", nominalDistance); + opencv_core.write(fs, "reprojErrs", cvarrToMat(reprojErrs)); + opencv_core.write(fs, "avgReprojErr", avgReprojErr); + opencv_core.write(fs, "maxReprojErr", maxReprojErr); +// opencv_core.write(fs, "nominalDistance", nominalDistance); if (R != null) - cvWrite(fs, "R", R, a); + opencv_core.write(fs, "R", cvarrToMat(R)); if (T != null) - cvWrite(fs, "T", T, a); + opencv_core.write(fs, "T", cvarrToMat(T)); if (E != null) - cvWrite(fs, "E", E, a); + opencv_core.write(fs, "E", cvarrToMat(E)); if (F != null) - cvWrite(fs, "F", F, a); - cvWriteReal(fs, "avgEpipolarErr", avgEpipolarErr); - cvWriteReal(fs, "maxEpipolarErr", maxEpipolarErr); + opencv_core.write(fs, "F", cvarrToMat(F)); + opencv_core.write(fs, "avgEpipolarErr", avgEpipolarErr); + opencv_core.write(fs, "maxEpipolarErr", maxEpipolarErr); - cvWriteString(fs, "colorOrder", colorOrder, 0); + opencv_core.write(fs, "colorOrder", colorOrder); if (colorMixingMatrix != null) - cvWrite(fs, "colorMixingMatrix", colorMixingMatrix, a); + opencv_core.write(fs, "colorMixingMatrix", cvarrToMat(colorMixingMatrix)); if (additiveLight != null) - cvWrite(fs, "additiveLight", additiveLight, a); - cvWriteReal(fs, "avgColorErr", avgColorErr); - cvWriteReal(fs, "colorR2", colorR2); + opencv_core.write(fs, "additiveLight", cvarrToMat(additiveLight)); + opencv_core.write(fs, "avgColorErr", avgColorErr); + opencv_core.write(fs, "colorR2", colorR2); - cvEndWriteStruct(fs); + shiftLeft(fs, "}"); } public void readParameters(File file) throws Exception { readParameters(file.getAbsolutePath()); } public void readParameters(String filename) throws Exception { - CvFileStorage fs = CvFileStorage.open(filename, null, CV_STORAGE_READ); + FileStorage fs = new FileStorage(filename, FileStorage.READ); readParameters(fs); fs.release(); } - public void readParameters(CvFileStorage fs) throws Exception { + public void readParameters(FileStorage fs) throws Exception { if (fs == null) { - throw new Exception("Error: CvFileStorage is null, cannot read parameters for device " + + throw new Exception("Error: FileStorage is null, cannot read parameters for device " + getSettings().getName() + ". Is the parametersFile correct?"); } - CvAttrList a = cvAttrList(); - - CvFileNode fn = cvGetFileNodeByName(fs, null, getSettings().getName()); + FileNode fn = fs.get(getSettings().getName()); if (fn == null) { - throw new Exception("Error: CvFileNode is null, cannot read parameters for device " + + throw new Exception("Error: FileNode is null, cannot read parameters for device " + getSettings().getName() + ". Is the name correct?"); } - imageWidth = cvReadIntByName(fs, fn, "imageWidth", imageWidth); - imageHeight = cvReadIntByName(fs, fn, "imageHeight", imageHeight); - getSettings().setResponseGamma(cvReadRealByName(fs, fn, "gamma", getSettings().getResponseGamma())); -// getSettings().initAspectRatio = cvReadRealByName(fs, fn, "initAspectRatio", getSettings().initAspectRatio); -// getSettings().flags = cvReadIntByName(fs, fn, "flags", getSettings().flags); - Pointer p = cvReadByName(fs, fn, "cameraMatrix", a); - cameraMatrix = p == null ? null : new CvMat(p); - p = cvReadByName(fs, fn, "distortionCoeffs", a); - distortionCoeffs = p == null ? null : new CvMat(p); - p = cvReadByName(fs, fn, "extrParams", a); - extrParams = p == null ? null : new CvMat(p); - p = cvReadByName(fs, fn, "reprojErrs", a); - reprojErrs = p == null ? null : new CvMat(p); - avgReprojErr = cvReadRealByName(fs, fn, "avgReprojErr", avgReprojErr); - maxReprojErr = cvReadRealByName(fs, fn, "maxReprojErr", maxReprojErr); -// nominalDistance = cvReadRealByName(fs, fn, "nominalDistance", nominalDistance); - p = cvReadByName(fs, fn, "R", a); - R = p == null ? null : new CvMat(p); - p = cvReadByName(fs, fn, "T", a); - T = p == null ? null : new CvMat(p); - p = cvReadByName(fs, fn, "E", a); - E = p == null ? null : new CvMat(p); - p = cvReadByName(fs, fn, "F", a); - F = p == null ? null : new CvMat(p); - avgEpipolarErr = cvReadRealByName(fs, fn, "avgEpipolarErr", avgEpipolarErr); - maxEpipolarErr = cvReadRealByName(fs, fn, "maxEpipolarErr", maxEpipolarErr); - - colorOrder = cvReadStringByName(fs, fn, "colorOrder", colorOrder); - p = cvReadByName(fs, fn, "colorMixingMatrix", a); - colorMixingMatrix = p == null ? null : new CvMat(p); - p = cvReadByName(fs, fn, "additiveLight", a); - additiveLight = p == null ? null : new CvMat(p); - avgColorErr = cvReadRealByName(fs, fn, "avgColorErr", avgColorErr); - colorR2 = cvReadRealByName(fs, fn, "colorR2", colorR2); + FileNode n; + if ((n = fn.get("imageWidth")).isInt()) imageWidth = n.asInt(); + if ((n = fn.get("imageHeight")).isInt()) imageHeight = n.asInt(); + if ((n = fn.get("gamma")).isReal()) getSettings().setResponseGamma(n.asDouble()); +// if ((n = fn.get("initAspectRatio")).isReal()) getSettings().setInitAspectRatio(n.asDouble()); +// if ((n = fn.get("flags")).isInt()) getSettings().setFlags(n.asInt()); + Mat m = new Mat(); + opencv_core.read(fn.get("cameraMatrix"), m); + cameraMatrix = m.empty() ? null : cvMat(m).clone(); + opencv_core.read(fn.get("distortionCoeffs"), m); + distortionCoeffs = m.empty() ? null : cvMat(m).clone(); + opencv_core.read(fn.get("extrParams"), m); + extrParams = m.empty() ? null : cvMat(m).clone(); + opencv_core.read(fn.get("reprojErrs"), m); + reprojErrs = m.empty() ? null : cvMat(m).clone(); + if ((n = fn.get("avgReprojErr")).isReal()) avgReprojErr = n.asDouble(); + if ((n = fn.get("maxReprojErr")).isReal()) maxReprojErr = n.asDouble(); +// if ((n = fn.get("nominalDistance")).isReal()) nominalDistance = n.asDouble(); + opencv_core.read(fn.get("R"), m); + R = m.empty() ? null : cvMat(m).clone(); + opencv_core.read(fn.get("T"), m); + T = m.empty() ? null : cvMat(m).clone(); + opencv_core.read(fn.get("E"), m); + E = m.empty() ? null : cvMat(m).clone(); + opencv_core.read(fn.get("F"), m); + F = m.empty() ? null : cvMat(m).clone(); + if ((n = fn.get("avgEpipolarErr")).isReal()) avgEpipolarErr = n.asDouble(); + if ((n = fn.get("maxEpipolarErr")).isReal()) maxEpipolarErr = n.asDouble(); + + if ((n = fn.get("colorOrder")).isString()) colorOrder = n.asBytePointer().getString(); + opencv_core.read(fn.get("colorMixingMatrix"), m); + colorMixingMatrix = m.empty() ? null : cvMat(m).clone(); + opencv_core.read(fn.get("additiveLight"), m); + additiveLight = m.empty() ? null : cvMat(m).clone(); + if ((n = fn.get("avgColorErr")).isReal()) avgColorErr = n.asDouble(); + if ((n = fn.get("colorR2")).isReal()) colorR2 = n.asDouble(); } @Override public String toString() { diff --git a/src/main/java/org/bytedeco/javacv/ProjectiveTransformer.java b/src/main/java/org/bytedeco/javacv/ProjectiveTransformer.java index 5e6da42e..ca71a84d 100644 --- a/src/main/java/org/bytedeco/javacv/ProjectiveTransformer.java +++ b/src/main/java/org/bytedeco/javacv/ProjectiveTransformer.java @@ -489,7 +489,7 @@ protected void update() { t2 = CvMat.create(3, 1); } t2.put(0, projectiveParameters, 0, 3); - cvRodrigues2(t2, R2, null); + Rodrigues(cvarrToMat(t2), cvarrToMat(R2), null); t2.put(0, projectiveParameters, 3, 3); // H = R-tn^T diff --git a/src/main/java/org/bytedeco/javacv/ProjectorDevice.java b/src/main/java/org/bytedeco/javacv/ProjectorDevice.java index 72ac3b35..0b0ec4cf 100644 --- a/src/main/java/org/bytedeco/javacv/ProjectorDevice.java +++ b/src/main/java/org/bytedeco/javacv/ProjectorDevice.java @@ -43,7 +43,7 @@ public ProjectorDevice(String name, String filename) throws Exception { settings.setImageWidth(imageWidth); settings.setImageHeight(imageHeight); } - public ProjectorDevice(String name, CvFileStorage fs) throws Exception { + public ProjectorDevice(String name, FileStorage fs) throws Exception { super(name, fs); settings.setImageWidth(imageWidth); settings.setImageHeight(imageHeight); @@ -370,21 +370,21 @@ public double getAttenuation(double x, double y, CvMat n, double d) { } public static ProjectorDevice[] read(String filename) throws Exception { - CvFileStorage fs = CvFileStorage.open(filename, null, CV_STORAGE_READ); + FileStorage fs = new FileStorage(filename, FileStorage.READ); ProjectorDevice[] devices = read(fs); fs.release(); return devices; } - public static ProjectorDevice[] read(CvFileStorage fs) throws Exception { - CvFileNode node = cvGetFileNodeByName(fs, null, "Projectors"); - CvSeq seq = node.data_seq(); - int count = seq.total(); + public static ProjectorDevice[] read(FileStorage fs) throws Exception { + FileNode node = fs.get("Projectors"); + FileNodeIterator seq = node.begin(); + int count = (int)seq.remaining(); ProjectorDevice[] devices = new ProjectorDevice[count]; - for (int i = 0; i < count; i++) { - Pointer p = cvGetSeqElem(seq, i); - if (p == null) continue; - String name = cvReadString(new CvFileNode(p), (String)null); + for (int i = 0; i < count; i++, seq.increment()) { + FileNode n = seq.access(); + if (n.empty()) continue; + String name = n.asBytePointer().getString(); devices[i] = new ProjectorDevice(name, fs); } return devices;