PanoramaStitchingRotatingCamera.java 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. import java.util.ArrayList;
  2. import java.util.List;
  3. import org.opencv.core.*;
  4. import org.opencv.core.Range;
  5. import org.opencv.highgui.HighGui;
  6. import org.opencv.imgcodecs.Imgcodecs;
  7. import org.opencv.imgproc.Imgproc;
  8. class PanoramaStitchingRotatingCameraRun {
  9. void basicPanoramaStitching (String[] args) {
  10. String img1path = args[0], img2path = args[1];
  11. Mat img1 = new Mat(), img2 = new Mat();
  12. img1 = Imgcodecs.imread(img1path);
  13. img2 = Imgcodecs.imread(img2path);
  14. //! [camera-pose-from-Blender-at-location-1]
  15. Mat c1Mo = new Mat( 4, 4, CvType.CV_64FC1 );
  16. c1Mo.put(0 ,0 ,0.9659258723258972, 0.2588190734386444, 0.0, 1.5529145002365112,
  17. 0.08852133899927139, -0.3303661346435547, -0.9396926164627075, -0.10281121730804443,
  18. -0.24321036040782928, 0.9076734185218811, -0.342020183801651, 6.130080699920654,
  19. 0, 0, 0, 1 );
  20. //! [camera-pose-from-Blender-at-location-1]
  21. //! [camera-pose-from-Blender-at-location-2]
  22. Mat c2Mo = new Mat( 4, 4, CvType.CV_64FC1 );
  23. c2Mo.put(0, 0, 0.9659258723258972, -0.2588190734386444, 0.0, -1.5529145002365112,
  24. -0.08852133899927139, -0.3303661346435547, -0.9396926164627075, -0.10281121730804443,
  25. 0.24321036040782928, 0.9076734185218811, -0.342020183801651, 6.130080699920654,
  26. 0, 0, 0, 1);
  27. //! [camera-pose-from-Blender-at-location-2]
  28. //! [camera-intrinsics-from-Blender]
  29. Mat cameraMatrix = new Mat(3, 3, CvType.CV_64FC1);
  30. cameraMatrix.put(0, 0, 700.0, 0.0, 320.0, 0.0, 700.0, 240.0, 0, 0, 1 );
  31. //! [camera-intrinsics-from-Blender]
  32. //! [extract-rotation]
  33. Range rowRange = new Range(0,3);
  34. Range colRange = new Range(0,3);
  35. //! [extract-rotation]
  36. //! [compute-rotation-displacement]
  37. //c1Mo * oMc2
  38. Mat R1 = new Mat(c1Mo, rowRange, colRange);
  39. Mat R2 = new Mat(c2Mo, rowRange, colRange);
  40. Mat R_2to1 = new Mat();
  41. Core.gemm(R1, R2.t(), 1, new Mat(), 0, R_2to1 );
  42. //! [compute-rotation-displacement]
  43. //! [compute-homography]
  44. Mat tmp = new Mat(), H = new Mat();
  45. Core.gemm(cameraMatrix, R_2to1, 1, new Mat(), 0, tmp);
  46. Core.gemm(tmp, cameraMatrix.inv(), 1, new Mat(), 0, H);
  47. Scalar s = new Scalar(H.get(2, 2)[0]);
  48. Core.divide(H, s, H);
  49. System.out.println(H.dump());
  50. //! [compute-homography]
  51. //! [stitch]
  52. Mat img_stitch = new Mat();
  53. Imgproc.warpPerspective(img2, img_stitch, H, new Size(img2.cols()*2, img2.rows()) );
  54. Mat half = new Mat();
  55. half = new Mat(img_stitch, new Rect(0, 0, img1.cols(), img1.rows()));
  56. img1.copyTo(half);
  57. //! [stitch]
  58. Mat img_compare = new Mat();
  59. Mat img_space = Mat.zeros(new Size(50, img1.rows()), CvType.CV_8UC3);
  60. List<Mat>list = new ArrayList<>();
  61. list.add(img1);
  62. list.add(img_space);
  63. list.add(img2);
  64. Core.hconcat(list, img_compare);
  65. HighGui.imshow("Compare Images", img_compare);
  66. HighGui.imshow("Panorama Stitching", img_stitch);
  67. HighGui.waitKey(0);
  68. System.exit(0);
  69. }
  70. }
  71. public class PanoramaStitchingRotatingCamera {
  72. public static void main(String[] args) {
  73. System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
  74. new PanoramaStitchingRotatingCameraRun().basicPanoramaStitching(args);
  75. }
  76. }