-
Notifications
You must be signed in to change notification settings - Fork 2
Batch Color Calibration
Jarek Sacha edited this page Oct 28, 2021
·
3 revisions
The color calibration can be applied to set of images using ImageJ scripting. Below is an example of doing this using JavaScript. Similar code can be used from Java, Jyton, and Scala. You will find mode examples in the test
directory, look for files with 'Demo' in the name. The code below is in file: ij_plugins/color/calibration/BatchCalibrationDemoJS.js
/**
* Example of batch color calibration.
*
* First a color correction recipe is computed using a reference color chart and ROI location of chart in the image.
*
* Next correction recipe is applied to all images in an input directory.
*
* Corrected Images are saved to the output directory.
*/
importPackage(Packages.ij_plugins.color.calibration)
importPackage(Packages.ij_plugins.color.calibration.chart)
importClass(Packages.java.io.File)
importClass(Packages.java.io.IOException)
IJ.log("Batch calibration - start...")
// Locations of images
baseDir = new File("../../test/data/batch_correction")
srcDir = new File(baseDir, "src");
dstDir = new File(baseDir, "dst");
chartImageFile = new File(srcDir, "im_1.jpg");
chartRoiFile = new File(srcDir, "im_1_chart.roi");
// Create color calibration recipe using an image of a color chart
IJ.log("Load chart image and ROI");
chartImage = IJ.openImage(chartImageFile.getCanonicalPath());
chartRoi = new RoiDecoder(chartRoiFile.getCanonicalPath()).getRoi();
IJ.log("Create calibration recipe.");
recipe = createCalibrationRecipe(chartImage, chartRoi);
// List files with extension ".jpg"
imageFiles = Java
.from(srcDir.listFiles())
.filter(function (val) {
return val.getName().endsWith(".jpg")
})
IJ.log("Got " + imageFiles.length + " images to convert...");
// Iterate trough images and correct
for (let i = 0; i < imageFiles.length; i++) {
file = imageFiles[i]
IJ.log("Correcting " + file.getName());
correct(recipe, file, dstDir);
}
IJ.log("Batch calibration - done.")
/**
* Create color calibration recipe
* @param chartImage image of a chart (assumed to be ImageScience ColorGauge Matte) [ImagePlus]
* @param chartRoi location of the chart [Roi]
* @returns calibration recipe
*/
function createCalibrationRecipe(chartImage, chartRoi) {
// Create a ColorGauge default calibration chart and align it to ROI from our chart image
chart = ColorCharts.ImageScienceColorGaugeMatte().copyAlignedTo(chartRoi);
refColorSpaceName = "sRGB";
mappingMethodName = "Linear Cross-band";
colorCalibrator = ColorCalibrator.apply(chart, refColorSpaceName, mappingMethodName);
calibrationFit = colorCalibrator.computeCalibrationMapping(chartImage);
imageType = chartImage.getType();
return CorrectionRecipe.apply(
calibrationFit.corrector(),
chart.colorConverter(),
colorCalibrator.referenceColorSpace(),
imageType);
}
/**
* Apply color calibration recipe
* @param recipe recipe to apply [CorrectionRecipe]
* @param srcImageFile file containing the image to correct [File]
* @param dstDir directory where corrected image should be saved [File]
*/
function correct(recipe, srcImageFile, dstDir) {
imp = IJ.openImage(srcImageFile.getCanonicalPath());
correctedBands = recipe.corrector().map(imp);
img = CalibrationUtils.convertToSRGB(correctedBands, recipe.referenceColorSpace(), recipe.colorConverter());
dstFile = new File(dstDir, srcImageFile.getName());
IJ.save(img, dstFile.getCanonicalPath());
}
For an alternate way of doing batch correction using a script see Calibrator Batch Apply.