def populateInstances(instances, synth_imgs, class_index, mins, maxs): # Populate the training data: create the filter bank for each feature image # by reading values from the interval defined by mins and maxs target = ArrayImgs.floats([width, height]) interval = FinalInterval(mins, maxs) n_samples = Intervals.numElements(interval) for img in synth_imgs: vectors = [zeros(len(attributes), 'd') for _ in xrange(n_samples)] for k, op in enumerate(filterBank(img, sumType=DoubleType())): imgOp = compute(op).into(target) for i, v in enumerate(Views.interval(imgOp, interval)): vectors[i][k] = v.getRealDouble() for vector in vectors: vector[-1] = class_index instances.add(DenseInstance(1.0, vector))
pixels = [] ra = img.randomAccess() cursor = inside.cursor() # only True pixels of the sphere mask while cursor.hasNext(): cursor.fwd() ra.setPosition(cursor) pixels.append(ra.get().get()) print "Average:", sum(pixels) / float(len(pixels)) # prints: 68.91 # whereas ImageJ "measure" prints: 69.285 for the EllipseRoi # Try with Region.sample cursor = Regions.sample(inside, img).cursor() pixels = [] while cursor.hasNext(): pixels.append(cursor.next().get()) print "Average:", sum(pixels) / float(len(pixels)) iterableinterval = Regions.sample(inside, img) print len(pixels), Intervals.numElements( iterableinterval) # too many, of course: both True and False entries print sum(imap(lambda t: t.get(), iterableinterval)) / float( Regions.countTrue(inside)) from operator import methodcaller print sum(imap(methodcaller("get"), iterableinterval)) / float( Regions.countTrue(inside))
for vector in vectors: vector[-1] = class_index instances.add(DenseInstance(1.0, vector)) # pick pixels on the black line for class 0 (membrane), 4x4 populateInstances(training_data, synth_imgs_membrane, 0, [14, 14], [17, 17]) # pick pixels in the very center for class 1 (mitochondrial boundary), 2x2 populateInstances(training_data, synth_imgs_mit_boundary, 1, [15, 15], [16, 16]) # Populate the training data for class "other" from two images # entirely filled with background or foreground plus noise target = ArrayImgs.floats([width, height]) interval = FinalInterval([14, 14], [17, 17]) n_samples = Intervals.numElements(interval) for ci, v in enumerate([fillValue, backgroundValue]): for _ in xrange(training_data.size() / 4): # the other 2/4 are the membrane and mit boundary other = syntheticEM([], width, height, 0, v, noise=True) vectors = [zeros(len(attributes), 'd') for _ in xrange(n_samples)] for k, op in enumerate(filterBank(IL.wrap(other), sumType=DoubleType())): imgOp = compute(op).into(target) for i, v in enumerate(Views.interval(imgOp, interval)): vectors[i][k] = v.getRealDouble() for vector in vectors: vector[-1] = ci + 2 # class index training_data.add(DenseInstance(1.0, vector)) # Create a classifier: support vector machine (SVM, an SMO in WEKA)
# Now, iterate each peak, defining a small interval centered at each peak, # and measure the sum of total pixel intensity, # and display the results in an ImageJ ResultTable. table = ResultsTable() for peak in peaks: # Read peak coordinates into an array of integers peak.localize(p) # Define limits of the interval around the peak: # (sigmaSmaller is half the radius of the embryo) minC = [int(p[i] - sigmaSmaller) for i in xrange(img.numDimensions())] maxC = [int(p[i] + sigmaSmaller) for i in xrange(img.numDimensions())] # View the interval around the peak, as a flat iterable (like an array) square = Views.flatIterable(Views.zeroMin(Views.interval(img, minC, maxC))) s1 = sum(t.getInteger() for t in square) area1 = Intervals.numElements(square) # Use a sphere instead radius = sqrt(area1 / pi) # same area for both print sigmaSmaller, radius circle = Masks.toIterableRegion(GeomMasks.closedSphere(p, radius)) s2 = sum(t.getInteger() for t in Regions.sample(circle, imgE)) area2 = Intervals.numElements(circle) print area1, area2 # Compute sum of pixel intensity values of the interval # (The t is the Type that mediates access to the pixels, via its get* methods) # Add to results table table.incrementCounter() table.addValue("x", p[0])