Search in sources :

Example 1 with ObjectReferenceWrapper

use of org.vcell.util.ObjectReferenceWrapper in project vcell by virtualcell.

the class RegionImage method calculateRegions.

/**
 * Insert the method's description here.
 * Creation date: (3/28/2002 10:30:20 AM)
 * @param image cbit.image.VCImage
 */
private void calculateRegions(VCImage vcImage) throws cbit.image.ImageException {
    long time1 = System.currentTimeMillis();
    RegionMask[][] regionMasks = new RegionMask[numZ][];
    for (int k = 0; k < vcImage.getNumZ(); k++) {
        regionMasks[k] = calculateRegions3D(vcImage.getPixels(), k * numXY, numX, numY);
    }
    long time2 = System.currentTimeMillis();
    // time2 = System.currentTimeMillis();
    // RegionMask regionMasksFast[][] = new RegionMask[numZ][];
    // for (int k = 0; k < vcImage.getNumZ(); k++){
    // regionMasksFast[k] = calculateRegions3Dfaster(vcImage.getPixels(),k*numXY,numX,numY);
    // }
    // long time3 = System.currentTimeMillis();
    // System.out.println("4way recursive took "+((time2-time1)/1000.0)+" s, nonrecursive line took "+((time3-time2)/1000.0)+" s");
    // 
    // consolidate "off-plane contiguous" region indexes
    // build a graph of all 2D regions that touch
    // 
    // the final 3D regions are the set of nodes in each of the spanning trees.
    // 
    // 
    // build graph with 1 node per 2d region
    // 
    Graph connectionGraph = new Graph();
    for (int k = 0; k < numZ; k++) {
        for (int i = 0; i < regionMasks[k].length; i++) {
            Node node = new Node(k + "," + i, new org.vcell.util.ObjectReferenceWrapper(regionMasks[k][i]));
            connectionGraph.addNode(node);
        }
    }
    // 
    // add edges for any slice-slice touching of regions with same pixel value
    // 
    BitSet zeroBitSet = new BitSet();
    BitSet intersection = new BitSet(numXY);
    for (int k = 0; k < numZ - 1; k++) {
        for (int i = 0; i < regionMasks[k].length; i++) {
            Node node_i = connectionGraph.getNode(k + "," + i);
            for (int j = 0; j < regionMasks[k + 1].length; j++) {
                if (regionMasks[k][i].pixelValue == regionMasks[k + 1][j].pixelValue) {
                    // clear mask
                    intersection.and(zeroBitSet);
                    intersection.or(regionMasks[k][i].mask);
                    intersection.and(regionMasks[k + 1][j].mask);
                    if (!intersection.equals(zeroBitSet)) {
                        Node node_j = connectionGraph.getNode((k + 1) + "," + j);
                        connectionGraph.addEdge(new Edge(node_i, node_j));
                    }
                }
            }
        }
    }
    // 
    // get spanning forest, and for each spanning tree, assign a single regionID to each contained 2d mask
    // 
    Tree[] spanningForest = connectionGraph.getSpanningForest();
    regionInfos = new RegionInfo[spanningForest.length];
    for (int i = 0; i < spanningForest.length; i++) {
        Node[] nodes = spanningForest[i].getNodes();
        int pixelValue = -1;
        int numPixels = 0;
        BitSet fullMask = new BitSet(numXY * numZ);
        for (int j = 0; j < nodes.length; j++) {
            RegionMask regionMask = (RegionMask) ((ObjectReferenceWrapper) nodes[j].getData()).getObject();
            pixelValue = regionMask.pixelValue;
            for (int k = 0; k < numXY; k++) {
                if (regionMask.mask.get(k)) {
                    fullMask.set(regionMask.offset + k);
                    numPixels++;
                }
            }
        }
        regionInfos[i] = new RegionInfo(pixelValue, numPixels, i, fullMask);
    }
    long time3 = System.currentTimeMillis();
    System.out.println("4way recursive on slices took " + ((time2 - time1) / 1000.0) + " s, total RegionImage time " + ((time3 - time1) / 1000.0) + " s");
}
Also used : Node(cbit.util.graph.Node) BitSet(java.util.BitSet) ObjectReferenceWrapper(org.vcell.util.ObjectReferenceWrapper) Graph(cbit.util.graph.Graph) Tree(cbit.util.graph.Tree) Edge(cbit.util.graph.Edge)

Aggregations

Edge (cbit.util.graph.Edge)1 Graph (cbit.util.graph.Graph)1 Node (cbit.util.graph.Node)1 Tree (cbit.util.graph.Tree)1 BitSet (java.util.BitSet)1 ObjectReferenceWrapper (org.vcell.util.ObjectReferenceWrapper)1