Path: blob/master/samples/java/tutorial_code/ImgProc/threshold_inRange/ThresholdInRange.java
16354 views
import java.awt.BorderLayout;1import java.awt.Container;2import java.awt.Image;3import java.awt.event.WindowAdapter;4import java.awt.event.WindowEvent;5import java.util.List;67import javax.swing.BoxLayout;8import javax.swing.ImageIcon;9import javax.swing.JFrame;10import javax.swing.JLabel;11import javax.swing.JPanel;12import javax.swing.JSlider;13import javax.swing.SwingWorker;14import javax.swing.event.ChangeEvent;15import javax.swing.event.ChangeListener;1617import org.opencv.core.Core;18import org.opencv.core.Mat;19import org.opencv.core.Scalar;20import org.opencv.highgui.HighGui;21import org.opencv.imgproc.Imgproc;22import org.opencv.videoio.VideoCapture;2324public class ThresholdInRange {25private static int MAX_VALUE = 255;26private static int MAX_VALUE_H = 360/2;27private static final String WINDOW_NAME = "Thresholding Operations using inRange demo";28private static final String LOW_H_NAME = "Low H";29private static final String LOW_S_NAME = "Low S";30private static final String LOW_V_NAME = "Low V";31private static final String HIGH_H_NAME = "High H";32private static final String HIGH_S_NAME = "High S";33private static final String HIGH_V_NAME = "High V";34private JSlider sliderLowH;35private JSlider sliderHighH;36private JSlider sliderLowS;37private JSlider sliderHighS;38private JSlider sliderLowV;39private JSlider sliderHighV;40private VideoCapture cap;41private Mat matFrame = new Mat();42private JFrame frame;43private JLabel imgCaptureLabel;44private JLabel imgDetectionLabel;45private CaptureTask captureTask;4647public ThresholdInRange(String[] args) {48int cameraDevice = 0;49if (args.length > 0) {50cameraDevice = Integer.parseInt(args[0]);51}52//! [cap]53cap = new VideoCapture(cameraDevice);54//! [cap]55if (!cap.isOpened()) {56System.err.println("Cannot open camera: " + cameraDevice);57System.exit(0);58}59if (!cap.read(matFrame)) {60System.err.println("Cannot read camera stream.");61System.exit(0);62}6364//! [window]65// Create and set up the window.66frame = new JFrame(WINDOW_NAME);67frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);68frame.addWindowListener(new WindowAdapter() {69@Override70public void windowClosing(WindowEvent windowEvent) {71captureTask.cancel(true);72}73});74// Set up the content pane.75Image img = HighGui.toBufferedImage(matFrame);76addComponentsToPane(frame.getContentPane(), img);77// Use the content pane's default BorderLayout. No need for78// setLayout(new BorderLayout());79// Display the window.80frame.pack();81frame.setVisible(true);82//! [window]8384captureTask = new CaptureTask();85captureTask.execute();86}8788//! [while]89private class CaptureTask extends SwingWorker<Void, Mat> {90@Override91protected Void doInBackground() {92Mat matFrame = new Mat();9394while (!isCancelled()) {95if (!cap.read(matFrame)) {96break;97}98publish(matFrame.clone());99}100return null;101}102103@Override104protected void process(List<Mat> frames) {105Mat frame = frames.get(frames.size() - 1);106Mat frameHSV = new Mat();107Imgproc.cvtColor(frame, frameHSV, Imgproc.COLOR_BGR2HSV);108Mat thresh = new Mat();109Core.inRange(frameHSV, new Scalar(sliderLowH.getValue(), sliderLowS.getValue(), sliderLowV.getValue()),110new Scalar(sliderHighH.getValue(), sliderHighS.getValue(), sliderHighV.getValue()), thresh);111update(frame, thresh);112}113}114//! [while]115116private void addComponentsToPane(Container pane, Image img) {117if (!(pane.getLayout() instanceof BorderLayout)) {118pane.add(new JLabel("Container doesn't use BorderLayout!"));119return;120}121122JPanel sliderPanel = new JPanel();123sliderPanel.setLayout(new BoxLayout(sliderPanel, BoxLayout.PAGE_AXIS));124125//! [trackbar]126sliderPanel.add(new JLabel(LOW_H_NAME));127sliderLowH = new JSlider(0, MAX_VALUE_H, 0);128sliderLowH.setMajorTickSpacing(50);129sliderLowH.setMinorTickSpacing(10);130sliderLowH.setPaintTicks(true);131sliderLowH.setPaintLabels(true);132sliderPanel.add(sliderLowH);133134sliderPanel.add(new JLabel(HIGH_H_NAME));135sliderHighH = new JSlider(0, MAX_VALUE_H, MAX_VALUE_H);136sliderHighH.setMajorTickSpacing(50);137sliderHighH.setMinorTickSpacing(10);138sliderHighH.setPaintTicks(true);139sliderHighH.setPaintLabels(true);140sliderPanel.add(sliderHighH);141142sliderPanel.add(new JLabel(LOW_S_NAME));143sliderLowS = new JSlider(0, MAX_VALUE, 0);144sliderLowS.setMajorTickSpacing(50);145sliderLowS.setMinorTickSpacing(10);146sliderLowS.setPaintTicks(true);147sliderLowS.setPaintLabels(true);148sliderPanel.add(sliderLowS);149150sliderPanel.add(new JLabel(HIGH_S_NAME));151sliderHighS = new JSlider(0, MAX_VALUE, MAX_VALUE);152sliderHighS.setMajorTickSpacing(50);153sliderHighS.setMinorTickSpacing(10);154sliderHighS.setPaintTicks(true);155sliderHighS.setPaintLabels(true);156sliderPanel.add(sliderHighS);157158sliderPanel.add(new JLabel(LOW_V_NAME));159sliderLowV = new JSlider(0, MAX_VALUE, 0);160sliderLowV.setMajorTickSpacing(50);161sliderLowV.setMinorTickSpacing(10);162sliderLowV.setPaintTicks(true);163sliderLowV.setPaintLabels(true);164sliderPanel.add(sliderLowV);165166sliderPanel.add(new JLabel(HIGH_V_NAME));167sliderHighV = new JSlider(0, MAX_VALUE, MAX_VALUE);168sliderHighV.setMajorTickSpacing(50);169sliderHighV.setMinorTickSpacing(10);170sliderHighV.setPaintTicks(true);171sliderHighV.setPaintLabels(true);172sliderPanel.add(sliderHighV);173//! [trackbar]174175//! [low]176sliderLowH.addChangeListener(new ChangeListener() {177@Override178public void stateChanged(ChangeEvent e) {179JSlider source = (JSlider) e.getSource();180int valH = Math.min(sliderHighH.getValue()-1, source.getValue());181sliderLowH.setValue(valH);182}183});184//! [low]185//! [high]186sliderHighH.addChangeListener(new ChangeListener() {187@Override188public void stateChanged(ChangeEvent e) {189JSlider source = (JSlider) e.getSource();190int valH = Math.max(source.getValue(), sliderLowH.getValue()+1);191sliderHighH.setValue(valH);192}193});194//! [high]195sliderLowS.addChangeListener(new ChangeListener() {196@Override197public void stateChanged(ChangeEvent e) {198JSlider source = (JSlider) e.getSource();199int valS = Math.min(sliderHighS.getValue()-1, source.getValue());200sliderLowS.setValue(valS);201}202});203sliderHighS.addChangeListener(new ChangeListener() {204@Override205public void stateChanged(ChangeEvent e) {206JSlider source = (JSlider) e.getSource();207int valS = Math.max(source.getValue(), sliderLowS.getValue()+1);208sliderHighS.setValue(valS);209}210});211sliderLowV.addChangeListener(new ChangeListener() {212@Override213public void stateChanged(ChangeEvent e) {214JSlider source = (JSlider) e.getSource();215int valV = Math.min(sliderHighV.getValue()-1, source.getValue());216sliderLowV.setValue(valV);217}218});219sliderHighV.addChangeListener(new ChangeListener() {220@Override221public void stateChanged(ChangeEvent e) {222JSlider source = (JSlider) e.getSource();223int valV = Math.max(source.getValue(), sliderLowV.getValue()+1);224sliderHighV.setValue(valV);225}226});227228pane.add(sliderPanel, BorderLayout.PAGE_START);229JPanel framePanel = new JPanel();230imgCaptureLabel = new JLabel(new ImageIcon(img));231framePanel.add(imgCaptureLabel);232imgDetectionLabel = new JLabel(new ImageIcon(img));233framePanel.add(imgDetectionLabel);234pane.add(framePanel, BorderLayout.CENTER);235}236237private void update(Mat imgCapture, Mat imgThresh) {238//! [show]239imgCaptureLabel.setIcon(new ImageIcon(HighGui.toBufferedImage(imgCapture)));240imgDetectionLabel.setIcon(new ImageIcon(HighGui.toBufferedImage(imgThresh)));241frame.repaint();242//! [show]243}244245public static void main(String[] args) {246// Load the native OpenCV library247System.loadLibrary(Core.NATIVE_LIBRARY_NAME);248249// Schedule a job for the event dispatch thread:250// creating and showing this application's GUI.251javax.swing.SwingUtilities.invokeLater(new Runnable() {252@Override253public void run() {254new ThresholdInRange(args);255}256});257}258}259260261