All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.github.mianalysis.mia.module.inputoutput.VideoLoader Maven / Gradle / Ivy

Go to download

ModularImageAnalysis (MIA) is an ImageJ plugin which provides a modular framework for assembling image and object analysis workflows. Detected objects can be transformed, filtered, measured and related. Analysis workflows are batch-enabled by default, allowing easy processing of high-content datasets.

There is a newer version: 1.6.12
Show newest version
// package io.github.mianalysis.mia.module.inputoutput;

// import java.awt.image.BufferedImage;
// import java.io.File;
// import java.io.FileNotFoundException;
// import java.util.Arrays;
// import java.util.TreeSet;
// import java.util.stream.Collectors;

// import org.apache.commons.io.FilenameUtils;
// import org.bytedeco.ffmpeg.global.avcodec;
// import org.bytedeco.javacv.FFmpegFrameGrabber;
// import org.bytedeco.javacv.FrameGrabber;
// import org.bytedeco.javacv.Java2DFrameConverter;
// import org.scijava.Priority;
// import org.scijava.plugin.Plugin;

// import com.drew.lang.annotations.Nullable;

// import fiji.stacks.Hyperstack_rearranger;
// import ij.IJ;
// import ij.ImagePlus;
// import ij.measure.Calibration;
// import ij.plugin.ChannelSplitter;
// import ij.plugin.HyperStackConverter;
// import ij.process.ImageProcessor;
// import io.github.mianalysis.mia.MIA;
// import io.github.mianalysis.mia.module.Categories;
// import io.github.mianalysis.mia.module.Category;
// import io.github.mianalysis.mia.module.Module;
// import io.github.mianalysis.mia.module.Modules;
// import io.github.mianalysis.mia.object.Objs;
// import io.github.mianalysis.mia.object.Workspace;
// import io.github.mianalysis.mia.object.image.Image;
// import io.github.mianalysis.mia.object.image.ImageFactory;
// import io.github.mianalysis.mia.object.parameters.BooleanP;
// import io.github.mianalysis.mia.object.parameters.ChoiceP;
// import io.github.mianalysis.mia.object.parameters.FilePathP;
// import io.github.mianalysis.mia.object.parameters.InputImageP;
// import io.github.mianalysis.mia.object.parameters.InputObjectsP;
// import io.github.mianalysis.mia.object.parameters.OutputImageP;
// import io.github.mianalysis.mia.object.parameters.Parameters;
// import io.github.mianalysis.mia.object.parameters.SeparatorP;
// import io.github.mianalysis.mia.object.parameters.text.DoubleP;
// import io.github.mianalysis.mia.object.parameters.text.IntegerP;
// import io.github.mianalysis.mia.object.parameters.text.StringP;
// import io.github.mianalysis.mia.object.parameters.text.TextAreaP;
// import io.github.mianalysis.mia.object.refs.collections.ImageMeasurementRefs;
// import io.github.mianalysis.mia.object.refs.collections.MetadataRefs;
// import io.github.mianalysis.mia.object.refs.collections.ObjMeasurementRefs;
// import io.github.mianalysis.mia.object.refs.collections.ObjMetadataRefs;
// import io.github.mianalysis.mia.object.refs.collections.ParentChildRefs;
// import io.github.mianalysis.mia.object.refs.collections.PartnerRefs;
// import io.github.mianalysis.mia.object.system.Status;
// import io.github.mianalysis.mia.object.units.SpatialUnit;
// import io.github.mianalysis.mia.object.units.TemporalUnit;
// import io.github.mianalysis.mia.object.metadata.Metadata;
// import io.github.mianalysis.mia.process.string.CommaSeparatedStringInterpreter;
// import ome.units.UNITS;
// import ome.units.quantity.Time;
// import ome.units.unit.Unit;

// @Plugin(type = Module.class, priority = Priority.LOW, visible = true)
// public class VideoLoader extends Module {
//     public static final String LOADER_SEPARATOR = "Core video loading controls";
//     public static final String OUTPUT_IMAGE = "Output image";
//     public static final String IMPORT_MODE = "Import mode";
//     public static final String NAME_FORMAT = "Name format";
//     public static final String GENERIC_FORMAT = "Generic format";
//     public static final String AVAILABLE_METADATA_FIELDS = "Available metadata fields";
//     public static final String PREFIX = "Prefix";
//     public static final String SUFFIX = "Suffix";
//     public static final String EXTENSION = "Extension";
//     public static final String INCLUDE_SERIES_NUMBER = "Include series number";
//     public static final String FILE_PATH = "File path";

//     public static final String RANGE_SEPARATOR = "Dimension ranges and cropping";
//     public static final String CHANNELS = "Channels";
//     public static final String FRAMES = "Frames";
//     public static final String CROP_MODE = "Crop mode";
//     public static final String REFERENCE_IMAGE = "Reference image";
//     public static final String LEFT = "Left coordinate";
//     public static final String TOP = "Top coordinate";
//     public static final String WIDTH = "Width";
//     public static final String HEIGHT = "Height";
//     public static final String OBJECTS_FOR_LIMITS = "Objects for limits";
//     public static final String SCALE_MODE = "Scale mode";
//     public static final String SCALE_FACTOR_X = "X scale factor";
//     public static final String SCALE_FACTOR_Y = "Y scale factor";

//     public static final String CALIBRATION_SEPARATOR = "Spatial calibration";
//     public static final String SET_CAL = "Set manual spatial calibration";
//     public static final String XY_CAL = "XY calibration (dist/px)";
//     public static final String Z_CAL = "Z calibration (dist/px)";

//     public VideoLoader(Modules modules) {
//         super("Load video", modules);
//     }

//     public interface ImportModes {
//         String CURRENT_FILE = "Current file";
//         String MATCHING_FORMAT = "Matching format";
//         String SPECIFIC_FILE = "Specific file";

//         String[] ALL = new String[] { CURRENT_FILE, MATCHING_FORMAT, SPECIFIC_FILE };

//     }

//     public interface NameFormats {
//         String GENERIC = "Generic (from metadata)";
//         String INPUT_FILE_PREFIX = "Input filename with prefix";
//         String INPUT_FILE_SUFFIX = "Input filename with suffix";

//         String[] ALL = new String[] { GENERIC, INPUT_FILE_PREFIX, INPUT_FILE_SUFFIX };

//     }

//     public interface CropModes {
//         String NONE = "None";
//         String FIXED = "Fixed";
//         String FROM_REFERENCE = "From reference";
//         String OBJECT_COLLECTION_LIMITS = "Object collection limits";

//         String[] ALL = new String[] { NONE, FIXED, FROM_REFERENCE, OBJECT_COLLECTION_LIMITS };

//     }

//     public interface ScaleModes {
//         String NONE = "No scaling";
//         String NO_INTERPOLATION = "Scaling (no interpolation)";
//         String BILINEAR = "Scaling (bilinear)";
//         String BICUBIC = "Scaling (bicubic)";

//         String[] ALL = new String[] { NONE, NO_INTERPOLATION, BILINEAR, BICUBIC };

//     }

//     public interface Measurements {
//         String ROI_LEFT = "VIDEO_LOADING // ROI_LEFT (PX)";
//         String ROI_TOP = "VIDEO_LOADING // ROI_TOP (PX)";
//         String ROI_WIDTH = "VIDEO_LOADING // ROI_WIDTH (PX)";
//         String ROI_HEIGHT = "VIDEO_LOADING // ROI_HEIGHT (PX)";

//     }

//     public String getGenericName(Metadata metadata, String genericFormat) {
//         String absolutePath = metadata.getFile().getAbsolutePath();
//         String path = FilenameUtils.getFullPath(absolutePath);
//         String filename;
//         try {
//             filename = ImageLoader.getGenericName(metadata, genericFormat);
//             return path + filename;
//         } catch (Exception e) {
//             MIA.log.writeWarning("Can't determine filename format");
//             return null;
//         }
//     }

//     public String getPrefixName(Metadata metadata, boolean includeSeries, String ext) {
//         String absolutePath = metadata.getFile().getAbsolutePath();
//         String path = FilenameUtils.getFullPath(absolutePath);
//         String name = FilenameUtils.removeExtension(FilenameUtils.getName(absolutePath));
//         String comment = metadata.getComment();
//         String series = includeSeries ? "_S" + metadata.getSeriesNumber() : "";

//         return path + comment + name + series + "." + ext;

//     }

//     public String getSuffixName(Metadata metadata, boolean includeSeries, String ext) {
//         String absolutePath = metadata.getFile().getAbsolutePath();
//         String path = FilenameUtils.getFullPath(absolutePath);
//         String name = FilenameUtils.removeExtension(FilenameUtils.getName(absolutePath));
//         String comment = metadata.getComment();
//         String series = includeSeries ? "_S" + metadata.getSeriesNumber() : "";

//         return path + name + series + comment + "." + ext;

//     }

//     @Override
//     public Category getCategory() {
//         return Categories.INPUT_OUTPUT;
//     }

//     @Override
//     public String getDescription() {
//         return "Uses JavaCV to import videos.";
//     }

//     public static ImagePlus getVideo(String path, String frameRange, String channelRange, @Nullable int[] crop,
//             double[] scaleFactors, String scaleMode)
//             throws FrameGrabber.Exception, FileNotFoundException, FrameOutOfRangeException {
//         String outputName = new File(path).getName();

//         // Initialising the video loader and converter
        
//         Java2DFrameConverter frameConverter = new Java2DFrameConverter();
//         FFmpegFrameGrabber loader = new FFmpegFrameGrabber(path);   
//         loader.start();  
        
//         // Getting an ordered list of frames to be imported
//         int[] framesList = CommaSeparatedStringInterpreter.interpretIntegers(frameRange, true,
//                 loader.getLengthInFrames());
//         int maxFrames = loader.getLengthInFrames();
//         if (framesList[framesList.length - 1] > maxFrames) {
//             loader.close();
//             throw new FrameOutOfRangeException("Specified frame range (" + framesList[0] + "-"
//                     + framesList[framesList.length - 1] + ") exceeds video length (" + maxFrames + " frames).");
//         }
//         TreeSet frames = Arrays.stream(framesList).boxed().collect(Collectors.toCollection(TreeSet::new));

//         int[] channelsList = CommaSeparatedStringInterpreter.interpretIntegers(channelRange, true,
//                 loader.getPixelFormat());
//         TreeSet channels = Arrays.stream(channelsList).boxed().collect(Collectors.toCollection(TreeSet::new));

//         int left = 0;
//         int top = 0;
//         int width = loader.getImageWidth();
//         int height = loader.getImageHeight();

//         if (crop != null) {
//             left = crop[0];
//             top = crop[1];
//             width = crop[2];
//             height = crop[3];
//         }

//         int widthOut = width;
//         int heightOut = height;

//         // Applying scaling
//         switch (scaleMode) {
//             case ScaleModes.NONE:
//                 scaleFactors[0] = 1;
//                 scaleFactors[1] = 1;
//                 break;
//             case ScaleModes.NO_INTERPOLATION:
//             case ScaleModes.BILINEAR:
//             case ScaleModes.BICUBIC:
//                 widthOut = (int) Math.round(width * scaleFactors[0]);
//                 heightOut = (int) Math.round(height * scaleFactors[1]);
//                 break;
//         }

//         ImagePlus ipl = IJ.createHyperStack(outputName, widthOut, heightOut, channelsList.length, 1, framesList.length,
//                 8);
//         int count = 1;
//         int total = frames.size();
        
//         for (int frame : frames) {
//             loader.setVideoFrameNumber(frame - 1);
            
//             BufferedImage im = frameConverter.convert(loader.grabImage());
//             MIA.log.writeDebug("B "+im);
//             ImagePlus frameIpl = new ImagePlus("Temporary", im);
//             MIA.log.writeDebug(frame + "_" + frameIpl.getNChannels() + "_" + frameIpl.getNSlices() + "_"
//                     + frameIpl.getNFrames() + "_" + frameIpl.getBitDepth());

//             for (int channel : channels) {
//                 ipl.setPosition(channel, 1, count);

//                 ImageProcessor ipr = ChannelSplitter.getChannel(frameIpl, channel).getProcessor(1);

//                 if (crop != null) {
//                     ipr.setRoi(left, top, width, height);
//                     ipr = ipr.crop();
//                 }

//                 // Applying scaling
//                 switch (scaleMode) {
//                     case ScaleModes.NO_INTERPOLATION:
//                         ipr.setInterpolationMethod(ImageProcessor.NONE);
//                         ipr = ipr.resize(widthOut, heightOut);
//                         break;
//                     case ScaleModes.BILINEAR:
//                         ipr.setInterpolationMethod(ImageProcessor.BILINEAR);
//                         ipr = ipr.resize(widthOut, heightOut);
//                         break;
//                     case ScaleModes.BICUBIC:
//                         ipr.setInterpolationMethod(ImageProcessor.BICUBIC);
//                         ipr = ipr.resize(widthOut, heightOut);
//                         break;
//                 }

//                 ipl.setProcessor(ipr);

//             }

//             // for (int channel:channels) {
//             // int frameIdx = frameIpl.getStackIndex(channel, 1, 1);
//             // int iplIdx = ipl.getStackIndex(channel, 1, count);
//             // ImageProcessor frameIpr = frameIpl.getStack().getProcessor(frameIdx);

//             // if (crop != null) {
//             // frameIpr.setRoi(left,top,width,height);
//             // frameIpr = frameIpr.crop();
//             // }

//             // ipl.getStack().setProcessor(frameIpr,iplIdx);

//             // }

//             count++;

//             writeProgressStatus(count, total, "Frames", "Video loader");

//         }

//         // This will probably load as a Z-stack rather than timeseries, so convert it to
//         // a stack
//         if (((ipl.getNFrames() == 1 && ipl.getNSlices() > 1) || (ipl.getNSlices() == 1 && ipl.getNFrames() > 1))) {
//             convertToTimeseries(ipl);
//             ipl.getCalibration().pixelDepth = 1;
//         }

//         double fps = loader.getFrameRate();
//         setTemporalCalibration(ipl, fps);

//         ipl.setPosition(1);
//         ipl.updateChannelAndDraw();

//         // Closing the loader
//         loader.close();

//         return ipl;

//     }

//     public static void convertToTimeseries(ImagePlus inputImagePlus) {
//         int nChannels = inputImagePlus.getNChannels();
//         int nFrames = inputImagePlus.getNFrames();
//         int nSlices = inputImagePlus.getNSlices();
//         if (inputImagePlus.getNSlices() != 1 || inputImagePlus.getNFrames() <= 1) {
//             ImagePlus processedImagePlus = HyperStackConverter.toHyperStack(inputImagePlus, nChannels, nFrames,
//                     nSlices);
//             processedImagePlus = Hyperstack_rearranger.reorderHyperstack(processedImagePlus, "CTZ", true, false);
//             inputImagePlus.setStack(processedImagePlus.getStack());
//         }
//     }

//     public static int[] extendRangeToEnd(int[] inputRange, int end) {
//         TreeSet values = new TreeSet<>();

//         int start;
//         for (start = 0; start < inputRange.length - 3; ++start) {
//             values.add(inputRange[start]);
//         }

//         start = inputRange[inputRange.length - 3];
//         int interval = inputRange[inputRange.length - 2] - start;

//         for (int i = start; i <= end; i += interval) {
//             values.add(i);
//         }

//         return values.stream().mapToInt(Integer::intValue).toArray();
//     }

//     public static void setTemporalCalibration(ImagePlus ipl, double fps) {
//         Unit




© 2015 - 2024 Weber Informatics LLC | Privacy Policy