RemapDemo.java 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. import org.opencv.core.Core;
  2. import org.opencv.core.CvType;
  3. import org.opencv.core.Mat;
  4. import org.opencv.highgui.HighGui;
  5. import org.opencv.imgcodecs.Imgcodecs;
  6. import org.opencv.imgproc.Imgproc;
  7. class Remap {
  8. private Mat mapX = new Mat();
  9. private Mat mapY = new Mat();
  10. private Mat dst = new Mat();
  11. private int ind = 0;
  12. //! [Update]
  13. private void updateMap() {
  14. float buffX[] = new float[(int) (mapX.total() * mapX.channels())];
  15. mapX.get(0, 0, buffX);
  16. float buffY[] = new float[(int) (mapY.total() * mapY.channels())];
  17. mapY.get(0, 0, buffY);
  18. for (int i = 0; i < mapX.rows(); i++) {
  19. for (int j = 0; j < mapX.cols(); j++) {
  20. switch (ind) {
  21. case 0:
  22. if( j > mapX.cols()*0.25 && j < mapX.cols()*0.75 && i > mapX.rows()*0.25 && i < mapX.rows()*0.75 ) {
  23. buffX[i*mapX.cols() + j] = 2*( j - mapX.cols()*0.25f ) + 0.5f;
  24. buffY[i*mapY.cols() + j] = 2*( i - mapX.rows()*0.25f ) + 0.5f;
  25. } else {
  26. buffX[i*mapX.cols() + j] = 0;
  27. buffY[i*mapY.cols() + j] = 0;
  28. }
  29. break;
  30. case 1:
  31. buffX[i*mapX.cols() + j] = j;
  32. buffY[i*mapY.cols() + j] = mapY.rows() - i;
  33. break;
  34. case 2:
  35. buffX[i*mapX.cols() + j] = mapY.cols() - j;
  36. buffY[i*mapY.cols() + j] = i;
  37. break;
  38. case 3:
  39. buffX[i*mapX.cols() + j] = mapY.cols() - j;
  40. buffY[i*mapY.cols() + j] = mapY.rows() - i;
  41. break;
  42. default:
  43. break;
  44. }
  45. }
  46. }
  47. mapX.put(0, 0, buffX);
  48. mapY.put(0, 0, buffY);
  49. ind = (ind+1) % 4;
  50. }
  51. //! [Update]
  52. public void run(String[] args) {
  53. String filename = args.length > 0 ? args[0] : "../data/chicky_512.png";
  54. //! [Load]
  55. Mat src = Imgcodecs.imread(filename, Imgcodecs.IMREAD_COLOR);
  56. if (src.empty()) {
  57. System.err.println("Cannot read image: " + filename);
  58. System.exit(0);
  59. }
  60. //! [Load]
  61. //! [Create]
  62. mapX = new Mat(src.size(), CvType.CV_32F);
  63. mapY = new Mat(src.size(), CvType.CV_32F);
  64. //! [Create]
  65. //! [Window]
  66. final String winname = "Remap demo";
  67. HighGui.namedWindow(winname, HighGui.WINDOW_AUTOSIZE);
  68. //! [Window]
  69. //! [Loop]
  70. for (;;) {
  71. updateMap();
  72. Imgproc.remap(src, dst, mapX, mapY, Imgproc.INTER_LINEAR);
  73. HighGui.imshow(winname, dst);
  74. if (HighGui.waitKey(1000) == 27) {
  75. break;
  76. }
  77. }
  78. //! [Loop]
  79. System.exit(0);
  80. }
  81. }
  82. public class RemapDemo {
  83. public static void main(String[] args) {
  84. // Load the native OpenCV library
  85. System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
  86. new Remap().run(args);
  87. }
  88. }