Example – “chaining” multiple operations together into a single operation with Myriad’s ChainedDatasetOperation.

Original input data
Input data
Blurred + Edge Detection + Normalized data
Results of chained blur / Sobel edge detection / normalization operation

Source code for above operation:


/*
 * Copyright (c) 2016 Emphysic LLC.  All rights reserved.
 */

package com.emphysic.myriad.core.examples;

import com.emphysic.myriad.core.data.io.IODataset;
import com.emphysic.myriad.core.data.ops.ChainedDatasetOperation;
import com.emphysic.myriad.core.data.ops.GaussianBlur;
import com.emphysic.myriad.core.data.ops.NormalizeSignalOperation;
import com.emphysic.myriad.core.data.ops.SobelOperation;
import com.emphysic.myriad.core.data.util.FileSniffer;

import java.io.File;
import java.net.URL;

/**
 * Demonstrates how to "chain" Dataset operations by running a blur, edge detection, and normalization.
 * A ChainedDatasetOperation performs multiple DatasetOperations in sequence but acts like a single operation.
 * Operations can be added at any time.
 * Created by ccoughlin on 9/9/16.
 */
public class ChainedOperations {
    public static void main(String[] args) {
        IODataset input = null;
        try {
            URL testJpgURL = Thread.currentThread().getContextClassLoader().getResource("data/io/test.jpg");
            File output = new File("blur_sobel_norm.txt");
            if (args == null || args.length == 0) {
                System.out.print("No input file specified, attempting to read demo image...");
                input = FileSniffer.read(new File(testJpgURL.getPath()), true);
                System.out.println("ok");
            } else {
                System.out.println("Attempting to read file " + args[0]);
                input = FileSniffer.read(new File(args[0]), true);
            }
            if (input != null) {
                System.out.println("Input file " + input.getWidth() + "x" + input.getHeight());

                ChainedDatasetOperation chain = new ChainedDatasetOperation();
                chain.withOp(new GaussianBlur(1)).withOp(new SobelOperation()).withOp(new NormalizeSignalOperation());
                System.out.println("Performing Gaussian Blur --> Sobel edge detection --> normalization...");
                chain.run(input).write(output);

                System.out.println("Output saved to " + output.getAbsolutePath());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

 

ChainedDatasetOperation is useful for simplifying a sequential set of operations to perform on a Dataset in Myriad. After creating the chain, operations can be added with the withOp() method:

ChainedDatasetOperation chain = new ChainedDatasetOperation();
chain.withOp(new GaussianBlur(1)).withOp(new SobelOperation()).withOp(new NormalizeSignalOperation());

 

The chain is treated like any other single DatasetOperation – calling its run() method returns the results of running each of its operations in turn:

chain.run(input).write(output);

 
To use the chain as a pre-processing operation, we can use Myriad’s DatasetOperationPool:

/**
 * system - Akka system
 * numWorkers - number of processes
 */
ActorRef preprocessor = system.actorOf(Props.create(DatasetOperationPool.class, numWorkers, chain), 
                                       genID("Preprocessor"));