Threshold.java 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. import java.awt.BorderLayout;
  2. import java.awt.Container;
  3. import java.awt.Image;
  4. import javax.swing.BoxLayout;
  5. import javax.swing.ImageIcon;
  6. import javax.swing.JFrame;
  7. import javax.swing.JLabel;
  8. import javax.swing.JPanel;
  9. import javax.swing.JSlider;
  10. import javax.swing.event.ChangeEvent;
  11. import javax.swing.event.ChangeListener;
  12. import org.opencv.core.Core;
  13. import org.opencv.core.Mat;
  14. import org.opencv.highgui.HighGui;
  15. import org.opencv.imgcodecs.Imgcodecs;
  16. import org.opencv.imgproc.Imgproc;
  17. public class Threshold {
  18. private static int MAX_VALUE = 255;
  19. private static int MAX_TYPE = 4;
  20. private static int MAX_BINARY_VALUE = 255;
  21. private static final String WINDOW_NAME = "Threshold Demo";
  22. private static final String TRACKBAR_TYPE = "<html><body>Type: <br> 0: Binary <br> "
  23. + "1: Binary Inverted <br> 2: Truncate <br> "
  24. + "3: To Zero <br> 4: To Zero Inverted</body></html>";
  25. private static final String TRACKBAR_VALUE = "Value";
  26. private int thresholdValue = 0;
  27. private int thresholdType = 3;
  28. private Mat src;
  29. private Mat srcGray = new Mat();
  30. private Mat dst = new Mat();
  31. private JFrame frame;
  32. private JLabel imgLabel;
  33. public Threshold(String[] args) {
  34. //! [load]
  35. String imagePath = "../data/stuff.jpg";
  36. if (args.length > 0) {
  37. imagePath = args[0];
  38. }
  39. // Load an image
  40. src = Imgcodecs.imread(imagePath);
  41. if (src.empty()) {
  42. System.out.println("Empty image: " + imagePath);
  43. System.exit(0);
  44. }
  45. // Convert the image to Gray
  46. Imgproc.cvtColor(src, srcGray, Imgproc.COLOR_BGR2GRAY);
  47. //! [load]
  48. //! [window]
  49. // Create and set up the window.
  50. frame = new JFrame(WINDOW_NAME);
  51. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  52. // Set up the content pane.
  53. Image img = HighGui.toBufferedImage(srcGray);
  54. addComponentsToPane(frame.getContentPane(), img);
  55. // Use the content pane's default BorderLayout. No need for
  56. // setLayout(new BorderLayout());
  57. // Display the window.
  58. frame.pack();
  59. frame.setVisible(true);
  60. //! [window]
  61. }
  62. private void addComponentsToPane(Container pane, Image img) {
  63. if (!(pane.getLayout() instanceof BorderLayout)) {
  64. pane.add(new JLabel("Container doesn't use BorderLayout!"));
  65. return;
  66. }
  67. JPanel sliderPanel = new JPanel();
  68. sliderPanel.setLayout(new BoxLayout(sliderPanel, BoxLayout.PAGE_AXIS));
  69. //! [trackbar]
  70. sliderPanel.add(new JLabel(TRACKBAR_TYPE));
  71. // Create Trackbar to choose type of Threshold
  72. JSlider sliderThreshType = new JSlider(0, MAX_TYPE, thresholdType);
  73. sliderThreshType.setMajorTickSpacing(1);
  74. sliderThreshType.setMinorTickSpacing(1);
  75. sliderThreshType.setPaintTicks(true);
  76. sliderThreshType.setPaintLabels(true);
  77. sliderPanel.add(sliderThreshType);
  78. sliderPanel.add(new JLabel(TRACKBAR_VALUE));
  79. // Create Trackbar to choose Threshold value
  80. JSlider sliderThreshValue = new JSlider(0, MAX_VALUE, 0);
  81. sliderThreshValue.setMajorTickSpacing(50);
  82. sliderThreshValue.setMinorTickSpacing(10);
  83. sliderThreshValue.setPaintTicks(true);
  84. sliderThreshValue.setPaintLabels(true);
  85. sliderPanel.add(sliderThreshValue);
  86. //! [trackbar]
  87. //! [on_trackbar]
  88. sliderThreshType.addChangeListener(new ChangeListener() {
  89. @Override
  90. public void stateChanged(ChangeEvent e) {
  91. JSlider source = (JSlider) e.getSource();
  92. thresholdType = source.getValue();
  93. update();
  94. }
  95. });
  96. sliderThreshValue.addChangeListener(new ChangeListener() {
  97. @Override
  98. public void stateChanged(ChangeEvent e) {
  99. JSlider source = (JSlider) e.getSource();
  100. thresholdValue = source.getValue();
  101. update();
  102. }
  103. });
  104. //! [on_trackbar]
  105. pane.add(sliderPanel, BorderLayout.PAGE_START);
  106. imgLabel = new JLabel(new ImageIcon(img));
  107. pane.add(imgLabel, BorderLayout.CENTER);
  108. }
  109. //! [Threshold_Demo]
  110. private void update() {
  111. Imgproc.threshold(srcGray, dst, thresholdValue, MAX_BINARY_VALUE, thresholdType);
  112. Image img = HighGui.toBufferedImage(dst);
  113. imgLabel.setIcon(new ImageIcon(img));
  114. frame.repaint();
  115. }
  116. //! [Threshold_Demo]
  117. public static void main(String[] args) {
  118. // Load the native OpenCV library
  119. System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
  120. // Schedule a job for the event dispatch thread:
  121. // creating and showing this application's GUI.
  122. javax.swing.SwingUtilities.invokeLater(new Runnable() {
  123. @Override
  124. public void run() {
  125. new Threshold(args);
  126. }
  127. });
  128. }
  129. }