Search in sources :

Example 1 with CameraModel

use of boofcv.struct.calib.CameraModel in project BoofCV by lessthanoptimal.

the class CameraCalibration method handleDirectory.

protected void handleDirectory() {
    final CalibrateMonoPlanar calibrationAlg = new CalibrateMonoPlanar(detector.getLayout());
    final CalibratedPlanarPanel gui;
    ProcessThread monitor = null;
    switch(modeType) {
        case PINHOLE:
            calibrationAlg.configurePinhole(zeroSkew, numRadial, tangential);
            break;
        case UNIVERSAL:
            calibrationAlg.configureUniversalOmni(zeroSkew, numRadial, tangential);
            break;
        default:
            throw new RuntimeException("Unknown model type: " + modeType);
    }
    if (visualize) {
        switch(modeType) {
            case PINHOLE:
                gui = new MonoPlanarPanel();
                break;
            case UNIVERSAL:
                gui = new FisheyePlanarPanel();
                break;
            default:
                throw new RuntimeException("Unknown model type: " + modeType);
        }
        monitor = new ProcessThread(gui);
        monitor.start();
    } else {
        gui = null;
    }
    File directory = new File(inputDirectory);
    if (!directory.exists()) {
        System.err.println("Input directory doesn't exist!");
        System.err.println("  " + inputDirectory);
        System.exit(0);
    }
    List<File> files = Arrays.asList(directory.listFiles());
    BoofMiscOps.sortFilesByName(files);
    if (files.isEmpty()) {
        System.err.println("No image files found!");
        System.err.println(inputDirectory);
        System.exit(0);
    }
    if (visualize) {
        monitor.setMessage(0, "Loading images");
    }
    final List<File> imagesSuccess = new ArrayList<>();
    final List<File> imagesFailed = new ArrayList<>();
    boolean first = true;
    for (File f : files) {
        if (f.isDirectory() || f.isHidden())
            continue;
        final BufferedImage buffered = UtilImageIO.loadImage(f.getPath());
        if (buffered == null)
            continue;
        GrayF32 image = ConvertBufferedImage.convertFrom(buffered, (GrayF32) null);
        if (visualize) {
            monitor.setMessage(0, f.getName());
            if (first) {
                first = false;
                // should do this more intelligently based on image resolution
                int width = Math.min(1000, image.getWidth());
                int height = Math.min(width * image.height / image.width, image.getHeight());
                gui.mainView.setPreferredSize(new Dimension(width, height));
                gui.showImageProcessed(buffered);
                ShowImages.showWindow(gui, "Monocular Calibration", true);
            } else {
                BoofSwingUtil.invokeNowOrLater(new Runnable() {

                    @Override
                    public void run() {
                        gui.showImageProcessed(buffered);
                    }
                });
            }
        }
        if (!detector.process(image)) {
            imagesFailed.add(f);
            System.err.println("Failed to detect target in " + f.getName());
        } else {
            calibrationAlg.addImage(detector.getDetectedPoints());
            imagesSuccess.add(f);
        }
    }
    if (visualize) {
        monitor.setMessage(1, "Computing intrinsics");
    }
    // process and compute intrinsic parameters
    try {
        final CameraModel intrinsic = calibrationAlg.process();
        if (visualize) {
            monitor.stopThread();
            if (imagesFailed.size() > 0) {
                JOptionPane.showMessageDialog(gui, "Failed to detect in " + imagesFailed.size() + " images");
            }
            SwingUtilities.invokeLater(new Runnable() {

                public void run() {
                    gui.setImages(imagesSuccess);
                    gui.setImagesFailed(imagesFailed);
                    gui.setObservations(calibrationAlg.getObservations());
                    gui.setResults(calibrationAlg.getErrors());
                    gui.setCalibration(calibrationAlg.getZhangParam());
                    gui.setCorrection(intrinsic);
                    gui.repaint();
                }
            });
        }
        calibrationAlg.printStatistics();
        System.out.println();
        System.out.println("--- " + modeType + " Parameters ---");
        System.out.println();
        switch(modeType) {
            case PINHOLE:
                {
                    CameraPinholeRadial m = (CameraPinholeRadial) intrinsic;
                    switch(formatType) {
                        case BOOFCV:
                            CalibrationIO.save(m, outputFileName);
                            break;
                        case OPENCV:
                            UtilOpenCV.save(m, outputFileName);
                            break;
                    }
                    m.print();
                }
                break;
            case UNIVERSAL:
                {
                    CameraUniversalOmni m = (CameraUniversalOmni) intrinsic;
                    CalibrationIO.save(m, outputFileName);
                    m.print();
                }
                break;
            default:
                throw new RuntimeException("Unknown model type. " + modeType);
        }
        System.out.println();
        System.out.println("Save file format " + formatType);
        System.out.println();
    } catch (RuntimeException e) {
        if (visualize)
            BoofSwingUtil.warningDialog(gui, e);
        e.printStackTrace();
        System.exit(1);
    }
}
Also used : CameraModel(boofcv.struct.calib.CameraModel) ArrayList(java.util.ArrayList) FisheyePlanarPanel(boofcv.gui.calibration.FisheyePlanarPanel) BufferedImage(java.awt.image.BufferedImage) ConvertBufferedImage(boofcv.io.image.ConvertBufferedImage) CalibratedPlanarPanel(boofcv.gui.calibration.CalibratedPlanarPanel) GrayF32(boofcv.struct.image.GrayF32) CameraPinholeRadial(boofcv.struct.calib.CameraPinholeRadial) CameraUniversalOmni(boofcv.struct.calib.CameraUniversalOmni) CalibrateMonoPlanar(boofcv.abst.geo.calibration.CalibrateMonoPlanar) File(java.io.File) MonoPlanarPanel(boofcv.gui.calibration.MonoPlanarPanel)

Aggregations

CalibrateMonoPlanar (boofcv.abst.geo.calibration.CalibrateMonoPlanar)1 CalibratedPlanarPanel (boofcv.gui.calibration.CalibratedPlanarPanel)1 FisheyePlanarPanel (boofcv.gui.calibration.FisheyePlanarPanel)1 MonoPlanarPanel (boofcv.gui.calibration.MonoPlanarPanel)1 ConvertBufferedImage (boofcv.io.image.ConvertBufferedImage)1 CameraModel (boofcv.struct.calib.CameraModel)1 CameraPinholeRadial (boofcv.struct.calib.CameraPinholeRadial)1 CameraUniversalOmni (boofcv.struct.calib.CameraUniversalOmni)1 GrayF32 (boofcv.struct.image.GrayF32)1 BufferedImage (java.awt.image.BufferedImage)1 File (java.io.File)1 ArrayList (java.util.ArrayList)1