/** * Construct the demo * * @param window * @param capture * @throws Exception */ public VideoColourSIFT(final JComponent window, final VideoCapture capture) throws Exception { final int width = capture.getWidth(); final int height = capture.getHeight(); this.capture = capture; this.polygonListener = new PolygonDrawingListener(); GridBagConstraints gbc = new GridBagConstraints(); final JLabel label = new JLabel( "<html><body><p>Hold an object in front of the camera, and press space. Select<br/>" + "the outline of the object by clicking points on the frozen video<br/>" + "image, and press C when you're done. Press space to start the video<br/>" + "again, and the object should be tracked.</p></body></html>"); gbc.gridx = 0; gbc.gridy = 0; gbc.gridwidth = 2; gbc.insets = new Insets(8, 8, 8, 8); window.add(label, gbc); this.vidPanel = new JPanel(new GridBagLayout()); this.vidPanel.setBorder(BorderFactory.createTitledBorder("Live Video")); this.videoFrame = VideoDisplay.createVideoDisplay(capture, this.vidPanel); gbc = new GridBagConstraints(); gbc.gridy = 1; gbc.gridx = 0; gbc.gridwidth = 1; window.add(this.vidPanel, gbc); this.modelPanel = new JPanel(new GridBagLayout()); this.modelPanel.setBorder(BorderFactory.createTitledBorder("Model")); this.modelFrame = new ImageComponent(true, false); this.modelFrame.setSize(width, height); this.modelFrame.setPreferredSize(new Dimension(width, height)); this.modelPanel.add(this.modelFrame); gbc = new GridBagConstraints(); gbc.anchor = GridBagConstraints.PAGE_START; gbc.gridy = 1; gbc.gridx = 1; window.add(this.modelPanel, gbc); this.matchPanel = new JPanel(new GridBagLayout()); this.matchPanel.setBorder(BorderFactory.createTitledBorder("Matches")); this.matchFrame = new ImageComponent(true, false); this.matchFrame.setSize(width * 2, height); this.matchFrame.setPreferredSize(new Dimension(width * 2, height)); this.matchPanel.add(this.matchFrame); gbc = new GridBagConstraints(); gbc.anchor = GridBagConstraints.PAGE_END; gbc.gridy = 2; gbc.gridx = 0; gbc.gridwidth = 2; window.add(this.matchPanel, gbc); this.videoFrame.getScreen().addMouseListener(this.polygonListener); this.videoFrame.addVideoListener(this); this.engine = new DoGColourSIFTEngine(); this.engine.getOptions().setDoubleInitialImage(false); }
/** * Construct with the given dimensions * * @param width the width * @param height the height * @throws VideoCaptureException */ public VideoCaptureComponent(int width, int height, String devName) throws VideoCaptureException { super(BoxLayout.Y_AXIS); this.setOpaque(false); this.width = width; this.height = height; final List<Device> devices = VideoCapture.getVideoDevices(); Video<MBFImage> vc = null; if (devices == null || devices.size() == 0) { currentDevice = null; final MBFImage[] frames = {new MBFImage(width, height, ColourSpace.RGB)}; frames[0].fill(RGBColour.RED); vc = new ArrayBackedVideo<MBFImage>(frames); } else { for (final Device d : devices) { if (d.getNameStr().contains(devName)) { currentDevice = d; break; } } if (currentDevice == null) currentDevice = devices.get(0); vc = new VideoCapture(width, height, currentDevice); } final JPanel videoDisplayPanel = new JPanel(); videoDisplayPanel.setMinimumSize(new Dimension(width, height)); videoDisplayPanel.setOpaque(false); display = VideoDisplay.createVideoDisplay(vc, videoDisplayPanel); add(videoDisplayPanel); final JPanel sourcesPanel = new JPanel(); sourcesPanel.setOpaque(false); sources = new JComboBox<String>(); sources.setOpaque(false); if (devices == null || devices.size() == 0) { sources.addItem("No cameras found"); sources.setEnabled(false); } else { for (final Device s : devices) sources.addItem(s.getNameStr()); } sources.setSelectedItem(currentDevice.getNameStr()); sources.addItemListener(this); sourcesPanel.add(sources); add(sourcesPanel); }
public static void main(String[] args) throws VideoCaptureException { if (args.length == 0) { int i = 0; System.out.println("Usage: VideoTest device [width [height [rate]]]"); for (final Device d : VideoCapture.getVideoDevices()) { System.out.println(i + "\t" + d.getNameStr()); i++; } return; } final Device dev = VideoCapture.getVideoDevices().get(Integer.parseInt(args[0])); final int width = args.length > 2 ? Integer.parseInt(args[1]) : 320; final int height = args.length > 2 ? Integer.parseInt(args[2]) : 240; final double rate = args.length > 2 ? Double.parseDouble(args[3]) : 30; final VideoCapture c = new VideoCapture(width, height, rate, dev); VideoDisplay.createVideoDisplay(c); }
@Override public void itemStateChanged(ItemEvent event) { if (event.getStateChange() == ItemEvent.SELECTED) { final String item = (String) event.getItem(); final Device d = VideoCapture.getVideoDevices().get(sources.getSelectedIndex()); if (d.getNameStr().equals(item) && !currentDevice.equals(d)) { try { currentDevice = d; display.setMode(Mode.STOP); display.getVideo().close(); display.changeVideo(new VideoCapture(width, height, currentDevice)); display.setMode(Mode.PLAY); } catch (final VideoCaptureException e) { e.printStackTrace(); } } } }