How to load images from file, run a model and save the classification image

Hello,
I read most script here:
https://github.com/mstritt/orbit-image-analysis/tree/master/src/main/groovy/com/actelion/research/orbit/imageAnalysis/scripts
Those scripts load from a image id, which I have no idea how to get.
RawDataFile rdf = DALConfig.imageProvider.LoadRawDataFile(7683)

My goal is to:

  1. load image files (img_i##_j##.jpg) from local PC’s directory
  2. apply a previously-trained model from local file (D:/…/test.omo) on img_i##_j##.jpg
  3. save the classification image as cl_i##_j##.jpg
  4. repeat and loop through all files in the directory.

Thanks!

Dear @jena,

thanks for reading the groovy scripts and asking this fundamental question - I think it’s a good idea to explain it here in detail.

Background info:
So a RawDataFile in Orbit is a kind of file handle. Out of that you can create e.g. a so-called RecognitionFrame which then holds the image data.

Each RawDataFile/image in orbit has a unique ID. Now, if you load images form an image server like OMERO, the image server defines that ID. If you load images from local disk (e.g. in the GUI), the first time Orbit sees an image a unique ID is created based on the MD5 of (a part of) the image and stored in a squlite file (orbit.db in your user home). Therefore you can also move images from one folder to the other, the ID will remain. This unique ID is needed, because all meta data and annotations is linked to that ID. It also allows you to create annotations for images, then send the orbit.db to someone else and she/he will then see the annotations. You can also see the image ID if you load an image in the GUI, then on the right side tab ‘file’.

Now to answer your question:
If you don’t know the ID, you first have to register the file(s):
RawDataFile rdf = ((ImageProviderLocal)DALConfig.imageProvider).registerFile(new File("c:/test.jpg"),0)

Here we cast to ImageProviderLocal, because only this image provider has the ability to register.
If the file has already been registered, then this method will reuse the existing ID and load the rdf.

Does that answer your question?

Regards,
Manuel

1 Like

Thank you Manuel.
Is there a better code manual somewhere that will provide these guidance?

A followup question:

// for load image
import com.actelion.research.orbit.beans.RawDataFile
import com.actelion.research.orbit.imageAnalysis.components.RecognitionFrame
import com.actelion.research.orbit.imageAnalysis.dal.DALConfig
import com.actelion.research.orbit.imageAnalysis.dal.ImageProviderLocal;
import java.awt.image.Raster

// for model
import com.actelion.research.orbit.imageAnalysis.models.OrbitModel
import com.actelion.research.orbit.imageAnalysis.models.SegmentationResult
import com.actelion.research.orbit.imageAnalysis.utils.OrbitHelper
import java.awt.Shape

import com.actelion.research.mapReduceGeneric.utils.KeyValue
import com.actelion.research.orbit.imageAnalysis.utils.ClassificationResult

// for saving
import com.actelion.research.orbit.imageAnalysis.components.ImageFrame
import com.actelion.research.orbit.imageAnalysis.components.OrbitImageAnalysis
import com.actelion.research.orbit.imageAnalysis.utils.ClassImageRenderer
import com.actelion.research.orbit.imageAnalysis.utils.OrbitTiledImage2
import com.actelion.research.orbit.imageAnalysis.utils.TiledImagePainter

import javax.media.jai.TiledImage
import java.awt.image.BufferedImage


RawDataFile rdf = ((ImageProviderLocal)DALConfig.imageProvider).registerFile(new File("C:/.../test.jpg"),0)
OrbitModel model = OrbitModel.LoadFromFile("C:/.../test2.omo")

// classify the image
ClassificationResult res = OrbitHelper.Classify(rdf.rawDataFileId,model);

// and output the results
String resStr = rdf.fileName;
List<KeyValue<String, Double>> norm = res.normalizeRatio();
(0..model.getClassShapes().size()-1).each {
    resStr += "\t" + norm.get(it).value;
}
println resStr;
DALConfig.getImageProvider().close();  // only close if not executed within Orbit

Run this, I was able to get a output if each component ratio (numbers printed in console) just like clicking GUI F8-classify under classification tab.
But the GUI click F8-classify will also generate overlay classification map, which is what I desire to output to a jpg file.

Could you elaborate how to achieve that?
I have had success with template “SaveClassificationImage.groovy”, but that requires a map already being generated in GUI, which “ClassificationResult res” probably doesn’t have.

Thanks!

BTW, It would be nice if the current GUI batch function could batch load files and execute custom script instead of just run the loaded model. It saves time to run file IO script and focus on execution script.

Dear @jena,

I just combined your code with SaveClassificationImage.groovy and uploaded as a further example:

which reads a local file and model and writes a low-res classification map to disk.

The trick here is to set the last parameter in the OrbitHelper.classify() method to tell Orbit to store the classification map.

In general I strongly recommend to develop scripts in a proper IDE like IntelliJ or Eclipse to get code-completion, auto-import of classes and it also shows you the java-doc.
So you checkout the orbit-image-analysis source code, load it in your IDE and then create groovy scripts there. Once created, you can run it in the Orbit script editor (or do some small modifications there).

And a better documentation - sure, I also would like to have that, any contribution here is very welcome! :wink:

Regards,
Manuel

1 Like

For the sake of completeness, you also can load local images with
RecognitionFrame rf = new RecognitionFrame("test.jpg");
but then it’s not registered, so you cannot store annotations etc, but just for classification it’s sufficient.
Manuel

1 Like

Thank you very much.
It is working beautifully.