Pyramids.java 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. import org.opencv.core.*;
  2. import org.opencv.highgui.HighGui;
  3. import org.opencv.imgcodecs.Imgcodecs;
  4. import org.opencv.imgproc.Imgproc;
  5. class PyramidsRun {
  6. String window_name = "Pyramids Demo";
  7. public void run(String[] args) {
  8. /// General instructions
  9. System.out.println("\n" +
  10. " Zoom In-Out demo \n" +
  11. "------------------ \n" +
  12. " * [i] -> Zoom [i]n \n" +
  13. " * [o] -> Zoom [o]ut \n" +
  14. " * [ESC] -> Close program \n");
  15. //! [load]
  16. String filename = ((args.length > 0) ? args[0] : "../data/chicky_512.png");
  17. // Load the image
  18. Mat src = Imgcodecs.imread(filename);
  19. // Check if image is loaded fine
  20. if( src.empty() ) {
  21. System.out.println("Error opening image!");
  22. System.out.println("Program Arguments: [image_name -- default ../data/chicky_512.png] \n");
  23. System.exit(-1);
  24. }
  25. //! [load]
  26. //! [loop]
  27. while (true){
  28. //! [show_image]
  29. HighGui.imshow( window_name, src );
  30. //! [show_image]
  31. char c = (char) HighGui.waitKey(0);
  32. c = Character.toLowerCase(c);
  33. if( c == 27 ){
  34. break;
  35. //![pyrup]
  36. }else if( c == 'i'){
  37. Imgproc.pyrUp( src, src, new Size( src.cols()*2, src.rows()*2 ) );
  38. System.out.println( "** Zoom In: Image x 2" );
  39. //![pyrup]
  40. //![pyrdown]
  41. }else if( c == 'o'){
  42. Imgproc.pyrDown( src, src, new Size( src.cols()/2, src.rows()/2 ) );
  43. System.out.println( "** Zoom Out: Image / 2" );
  44. //![pyrdown]
  45. }
  46. }
  47. //! [loop]
  48. System.exit(0);
  49. }
  50. }
  51. public class Pyramids {
  52. public static void main(String[] args) {
  53. // Load the native library.
  54. System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
  55. new PyramidsRun().run(args);
  56. }
  57. }