ThresholdInRange.java 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. import java.awt.BorderLayout;
  2. import java.awt.Container;
  3. import java.awt.Image;
  4. import java.awt.event.WindowAdapter;
  5. import java.awt.event.WindowEvent;
  6. import java.util.List;
  7. import javax.swing.BoxLayout;
  8. import javax.swing.ImageIcon;
  9. import javax.swing.JFrame;
  10. import javax.swing.JLabel;
  11. import javax.swing.JPanel;
  12. import javax.swing.JSlider;
  13. import javax.swing.SwingWorker;
  14. import javax.swing.event.ChangeEvent;
  15. import javax.swing.event.ChangeListener;
  16. import org.opencv.core.Core;
  17. import org.opencv.core.Mat;
  18. import org.opencv.core.Scalar;
  19. import org.opencv.highgui.HighGui;
  20. import org.opencv.imgproc.Imgproc;
  21. import org.opencv.videoio.VideoCapture;
  22. public class ThresholdInRange {
  23. private static int MAX_VALUE = 255;
  24. private static int MAX_VALUE_H = 360/2;
  25. private static final String WINDOW_NAME = "Thresholding Operations using inRange demo";
  26. private static final String LOW_H_NAME = "Low H";
  27. private static final String LOW_S_NAME = "Low S";
  28. private static final String LOW_V_NAME = "Low V";
  29. private static final String HIGH_H_NAME = "High H";
  30. private static final String HIGH_S_NAME = "High S";
  31. private static final String HIGH_V_NAME = "High V";
  32. private JSlider sliderLowH;
  33. private JSlider sliderHighH;
  34. private JSlider sliderLowS;
  35. private JSlider sliderHighS;
  36. private JSlider sliderLowV;
  37. private JSlider sliderHighV;
  38. private VideoCapture cap;
  39. private Mat matFrame = new Mat();
  40. private JFrame frame;
  41. private JLabel imgCaptureLabel;
  42. private JLabel imgDetectionLabel;
  43. private CaptureTask captureTask;
  44. public ThresholdInRange(String[] args) {
  45. int cameraDevice = 0;
  46. if (args.length > 0) {
  47. cameraDevice = Integer.parseInt(args[0]);
  48. }
  49. //! [cap]
  50. cap = new VideoCapture(cameraDevice);
  51. //! [cap]
  52. if (!cap.isOpened()) {
  53. System.err.println("Cannot open camera: " + cameraDevice);
  54. System.exit(0);
  55. }
  56. if (!cap.read(matFrame)) {
  57. System.err.println("Cannot read camera stream.");
  58. System.exit(0);
  59. }
  60. //! [window]
  61. // Create and set up the window.
  62. frame = new JFrame(WINDOW_NAME);
  63. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  64. frame.addWindowListener(new WindowAdapter() {
  65. @Override
  66. public void windowClosing(WindowEvent windowEvent) {
  67. captureTask.cancel(true);
  68. }
  69. });
  70. // Set up the content pane.
  71. Image img = HighGui.toBufferedImage(matFrame);
  72. addComponentsToPane(frame.getContentPane(), img);
  73. // Use the content pane's default BorderLayout. No need for
  74. // setLayout(new BorderLayout());
  75. // Display the window.
  76. frame.pack();
  77. frame.setVisible(true);
  78. //! [window]
  79. captureTask = new CaptureTask();
  80. captureTask.execute();
  81. }
  82. //! [while]
  83. private class CaptureTask extends SwingWorker<Void, Mat> {
  84. @Override
  85. protected Void doInBackground() {
  86. Mat matFrame = new Mat();
  87. while (!isCancelled()) {
  88. if (!cap.read(matFrame)) {
  89. break;
  90. }
  91. publish(matFrame.clone());
  92. }
  93. return null;
  94. }
  95. @Override
  96. protected void process(List<Mat> frames) {
  97. Mat frame = frames.get(frames.size() - 1);
  98. Mat frameHSV = new Mat();
  99. Imgproc.cvtColor(frame, frameHSV, Imgproc.COLOR_BGR2HSV);
  100. Mat thresh = new Mat();
  101. Core.inRange(frameHSV, new Scalar(sliderLowH.getValue(), sliderLowS.getValue(), sliderLowV.getValue()),
  102. new Scalar(sliderHighH.getValue(), sliderHighS.getValue(), sliderHighV.getValue()), thresh);
  103. update(frame, thresh);
  104. }
  105. }
  106. //! [while]
  107. private void addComponentsToPane(Container pane, Image img) {
  108. if (!(pane.getLayout() instanceof BorderLayout)) {
  109. pane.add(new JLabel("Container doesn't use BorderLayout!"));
  110. return;
  111. }
  112. JPanel sliderPanel = new JPanel();
  113. sliderPanel.setLayout(new BoxLayout(sliderPanel, BoxLayout.PAGE_AXIS));
  114. //! [trackbar]
  115. sliderPanel.add(new JLabel(LOW_H_NAME));
  116. sliderLowH = new JSlider(0, MAX_VALUE_H, 0);
  117. sliderLowH.setMajorTickSpacing(50);
  118. sliderLowH.setMinorTickSpacing(10);
  119. sliderLowH.setPaintTicks(true);
  120. sliderLowH.setPaintLabels(true);
  121. sliderPanel.add(sliderLowH);
  122. sliderPanel.add(new JLabel(HIGH_H_NAME));
  123. sliderHighH = new JSlider(0, MAX_VALUE_H, MAX_VALUE_H);
  124. sliderHighH.setMajorTickSpacing(50);
  125. sliderHighH.setMinorTickSpacing(10);
  126. sliderHighH.setPaintTicks(true);
  127. sliderHighH.setPaintLabels(true);
  128. sliderPanel.add(sliderHighH);
  129. sliderPanel.add(new JLabel(LOW_S_NAME));
  130. sliderLowS = new JSlider(0, MAX_VALUE, 0);
  131. sliderLowS.setMajorTickSpacing(50);
  132. sliderLowS.setMinorTickSpacing(10);
  133. sliderLowS.setPaintTicks(true);
  134. sliderLowS.setPaintLabels(true);
  135. sliderPanel.add(sliderLowS);
  136. sliderPanel.add(new JLabel(HIGH_S_NAME));
  137. sliderHighS = new JSlider(0, MAX_VALUE, MAX_VALUE);
  138. sliderHighS.setMajorTickSpacing(50);
  139. sliderHighS.setMinorTickSpacing(10);
  140. sliderHighS.setPaintTicks(true);
  141. sliderHighS.setPaintLabels(true);
  142. sliderPanel.add(sliderHighS);
  143. sliderPanel.add(new JLabel(LOW_V_NAME));
  144. sliderLowV = new JSlider(0, MAX_VALUE, 0);
  145. sliderLowV.setMajorTickSpacing(50);
  146. sliderLowV.setMinorTickSpacing(10);
  147. sliderLowV.setPaintTicks(true);
  148. sliderLowV.setPaintLabels(true);
  149. sliderPanel.add(sliderLowV);
  150. sliderPanel.add(new JLabel(HIGH_V_NAME));
  151. sliderHighV = new JSlider(0, MAX_VALUE, MAX_VALUE);
  152. sliderHighV.setMajorTickSpacing(50);
  153. sliderHighV.setMinorTickSpacing(10);
  154. sliderHighV.setPaintTicks(true);
  155. sliderHighV.setPaintLabels(true);
  156. sliderPanel.add(sliderHighV);
  157. //! [trackbar]
  158. //! [low]
  159. sliderLowH.addChangeListener(new ChangeListener() {
  160. @Override
  161. public void stateChanged(ChangeEvent e) {
  162. JSlider source = (JSlider) e.getSource();
  163. int valH = Math.min(sliderHighH.getValue()-1, source.getValue());
  164. sliderLowH.setValue(valH);
  165. }
  166. });
  167. //! [low]
  168. //! [high]
  169. sliderHighH.addChangeListener(new ChangeListener() {
  170. @Override
  171. public void stateChanged(ChangeEvent e) {
  172. JSlider source = (JSlider) e.getSource();
  173. int valH = Math.max(source.getValue(), sliderLowH.getValue()+1);
  174. sliderHighH.setValue(valH);
  175. }
  176. });
  177. //! [high]
  178. sliderLowS.addChangeListener(new ChangeListener() {
  179. @Override
  180. public void stateChanged(ChangeEvent e) {
  181. JSlider source = (JSlider) e.getSource();
  182. int valS = Math.min(sliderHighS.getValue()-1, source.getValue());
  183. sliderLowS.setValue(valS);
  184. }
  185. });
  186. sliderHighS.addChangeListener(new ChangeListener() {
  187. @Override
  188. public void stateChanged(ChangeEvent e) {
  189. JSlider source = (JSlider) e.getSource();
  190. int valS = Math.max(source.getValue(), sliderLowS.getValue()+1);
  191. sliderHighS.setValue(valS);
  192. }
  193. });
  194. sliderLowV.addChangeListener(new ChangeListener() {
  195. @Override
  196. public void stateChanged(ChangeEvent e) {
  197. JSlider source = (JSlider) e.getSource();
  198. int valV = Math.min(sliderHighV.getValue()-1, source.getValue());
  199. sliderLowV.setValue(valV);
  200. }
  201. });
  202. sliderHighV.addChangeListener(new ChangeListener() {
  203. @Override
  204. public void stateChanged(ChangeEvent e) {
  205. JSlider source = (JSlider) e.getSource();
  206. int valV = Math.max(source.getValue(), sliderLowV.getValue()+1);
  207. sliderHighV.setValue(valV);
  208. }
  209. });
  210. pane.add(sliderPanel, BorderLayout.PAGE_START);
  211. JPanel framePanel = new JPanel();
  212. imgCaptureLabel = new JLabel(new ImageIcon(img));
  213. framePanel.add(imgCaptureLabel);
  214. imgDetectionLabel = new JLabel(new ImageIcon(img));
  215. framePanel.add(imgDetectionLabel);
  216. pane.add(framePanel, BorderLayout.CENTER);
  217. }
  218. private void update(Mat imgCapture, Mat imgThresh) {
  219. //! [show]
  220. imgCaptureLabel.setIcon(new ImageIcon(HighGui.toBufferedImage(imgCapture)));
  221. imgDetectionLabel.setIcon(new ImageIcon(HighGui.toBufferedImage(imgThresh)));
  222. frame.repaint();
  223. //! [show]
  224. }
  225. public static void main(String[] args) {
  226. // Load the native OpenCV library
  227. System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
  228. // Schedule a job for the event dispatch thread:
  229. // creating and showing this application's GUI.
  230. javax.swing.SwingUtilities.invokeLater(new Runnable() {
  231. @Override
  232. public void run() {
  233. new ThresholdInRange(args);
  234. }
  235. });
  236. }
  237. }