Search in sources :

Example 1 with AdjustmentType

use of boofcv.alg.distort.AdjustmentType in project BoofCV by lessthanoptimal.

the class BatchRemoveLensDistortion method main.

public static void main(String[] args) {
    String inputPath, regex, pathIntrinsic, outputDir;
    AdjustmentType adjustmentType = AdjustmentType.FULL_VIEW;
    boolean rename = false;
    if (args.length >= 4) {
        int numFlags = args.length - 4;
        for (int i = 0; i < numFlags; i++) {
            if (args[i].compareToIgnoreCase("-rename") == 0) {
                rename = true;
            } else if (args[i].compareToIgnoreCase("-EXPAND") == 0) {
                adjustmentType = AdjustmentType.EXPAND;
            } else if (args[i].compareToIgnoreCase("-FULL_VIEW") == 0) {
                adjustmentType = AdjustmentType.FULL_VIEW;
            } else {
                System.err.println("Unknown flag " + args[i]);
            }
        }
        inputPath = args[numFlags];
        regex = args[numFlags + 1];
        pathIntrinsic = args[numFlags + 2];
        outputDir = args[numFlags + 3];
    } else {
        printHelpAndExit(args);
        System.exit(0);
        return;
    }
    System.out.println("AdjustmentType = " + adjustmentType);
    System.out.println("rename         = " + rename);
    System.out.println("input path     = " + inputPath);
    System.out.println("name regex     = " + regex);
    System.out.println("output dir     = " + outputDir);
    File fileOutputDir = new File(outputDir);
    if (!fileOutputDir.exists()) {
        if (!fileOutputDir.mkdirs()) {
            throw new RuntimeException("Output directory did not exist and failed to create it");
        } else {
            System.out.println("  created output directory");
        }
    }
    CameraPinholeRadial param = CalibrationIO.load(pathIntrinsic);
    CameraPinholeRadial paramAdj = new CameraPinholeRadial();
    List<File> files = Arrays.asList(UtilIO.findMatches(new File(inputPath), regex));
    Collections.sort(files);
    System.out.println("Found a total of " + files.size() + " matching files");
    Planar<GrayF32> distoredImg = new Planar<>(GrayF32.class, param.width, param.height, 3);
    Planar<GrayF32> undistoredImg = new Planar<>(GrayF32.class, param.width, param.height, 3);
    ImageDistort distort = LensDistortionOps.changeCameraModel(adjustmentType, BorderType.ZERO, param, new CameraPinhole(param), paramAdj, (ImageType) distoredImg.getImageType());
    CalibrationIO.save(paramAdj, new File(outputDir, "intrinsicUndistorted.yaml").getAbsolutePath());
    BufferedImage out = new BufferedImage(param.width, param.height, BufferedImage.TYPE_INT_RGB);
    int numDigits = BoofMiscOps.numDigits(files.size() - 1);
    String format = "%0" + numDigits + "d";
    for (int i = 0; i < files.size(); i++) {
        File file = files.get(i);
        System.out.println("processing " + file.getName());
        BufferedImage orig = UtilImageIO.loadImage(file.getAbsolutePath());
        if (orig == null) {
            throw new RuntimeException("Can't load file: " + file.getAbsolutePath());
        }
        if (orig.getWidth() != param.width || orig.getHeight() != param.height) {
            System.err.println("intrinsic parameters and image size do not match!");
            System.exit(-1);
        }
        ConvertBufferedImage.convertFromPlanar(orig, distoredImg, true, GrayF32.class);
        distort.apply(distoredImg, undistoredImg);
        ConvertBufferedImage.convertTo(undistoredImg, out, true);
        String nameOut;
        if (rename) {
            nameOut = String.format("image" + format + ".png", i);
        } else {
            nameOut = file.getName().split("\\.")[0] + "_undistorted.png";
        }
        UtilImageIO.saveImage(out, new File(outputDir, nameOut).getAbsolutePath());
    }
}
Also used : ImageDistort(boofcv.alg.distort.ImageDistort) CameraPinhole(boofcv.struct.calib.CameraPinhole) BufferedImage(java.awt.image.BufferedImage) ConvertBufferedImage(boofcv.io.image.ConvertBufferedImage) AdjustmentType(boofcv.alg.distort.AdjustmentType) GrayF32(boofcv.struct.image.GrayF32) CameraPinholeRadial(boofcv.struct.calib.CameraPinholeRadial) Planar(boofcv.struct.image.Planar) File(java.io.File)

Aggregations

AdjustmentType (boofcv.alg.distort.AdjustmentType)1 ImageDistort (boofcv.alg.distort.ImageDistort)1 ConvertBufferedImage (boofcv.io.image.ConvertBufferedImage)1 CameraPinhole (boofcv.struct.calib.CameraPinhole)1 CameraPinholeRadial (boofcv.struct.calib.CameraPinholeRadial)1 GrayF32 (boofcv.struct.image.GrayF32)1 Planar (boofcv.struct.image.Planar)1 BufferedImage (java.awt.image.BufferedImage)1 File (java.io.File)1