@SuppressWarnings("unchecked") @Override public boolean process() { final long start = System.currentTimeMillis(); final ArrayImgFactory<FloatType> factory = new ArrayImgFactory<FloatType>(); final ArrayImg<FloatType, FloatArray> floatImage = (ArrayImg<FloatType, FloatArray>) factory.create(source, new FloatType()); // Copy to float. final Cursor<FloatType> cursor = floatImage.cursor(source); final RandomAccess<T> ra = source.randomAccess(source); while (cursor.hasNext()) { cursor.fwd(); ra.setPosition(cursor); cursor.get().set(ra.get().getRealFloat()); } // Create result holders. Dx = (ArrayImg<FloatType, FloatArray>) factory.create(source, new FloatType()); Dy = (ArrayImg<FloatType, FloatArray>) factory.create(source, new FloatType()); final int ndims = floatImage.numDimensions(); if (ndims == 3) { final long nz = floatImage.dimension(2); for (int z = 0; z < nz; z++) { final IntervalView<FloatType> slice = Views.hyperSlice(floatImage, 2, z); final IntervalView<FloatType> targetSliceX = Views.hyperSlice(Dx, 2, z); final IntervalView<FloatType> targetSliceY = Views.hyperSlice(Dy, 2, z); final boolean ok = processSlice(slice, targetSliceX, targetSliceY); if (!ok) { return false; } } } else { final boolean ok = processSlice(floatImage, Dx, Dy); if (!ok) { return false; } } components.clear(); components.add(Dx); components.add(Dy); final long end = System.currentTimeMillis(); processingTime = end - start; return true; }
/** Returns the gradient norm. */ @Override public ArrayImg<FloatType, FloatArray> getResult() { final ArrayImgFactory<FloatType> factory = new ArrayImgFactory<FloatType>(); @SuppressWarnings("unchecked") final ArrayImg<FloatType, FloatArray> norm = (ArrayImg<FloatType, FloatArray>) factory.create(Dx, new FloatType()); final Vector<Chunk> chunks = SimpleMultiThreading.divideIntoChunks(norm.size(), numThreads); final AtomicInteger ai = new AtomicInteger(); final Thread[] threads = new Thread[chunks.size()]; for (int i = 0; i < threads.length; i++) { threads[i] = new Thread("Gradient norm thread " + i) { @Override public void run() { final Chunk chunk = chunks.get(ai.getAndIncrement()); final Cursor<FloatType> cx = Dx.cursor(); final Cursor<FloatType> cy = Dy.cursor(); final Cursor<FloatType> cn = norm.cursor(); double x, y; cn.jumpFwd(chunk.getStartPosition()); cx.jumpFwd(chunk.getStartPosition()); cy.jumpFwd(chunk.getStartPosition()); for (long j = 0; j < chunk.getLoopSize(); j++) { cn.fwd(); cx.fwd(); cy.fwd(); // Ok because we have identical containers x = cx.get().get(); y = cy.get().get(); cn.get().setReal(Math.sqrt(x * x + y * y)); } } }; } SimpleMultiThreading.startAndJoin(threads); return norm; }
private Img<DoubleType> allocateImage() { final ArrayImgFactory<DoubleType> imgFactory = new ArrayImgFactory<DoubleType>(); return imgFactory.create(new long[] {XSIZE, YSIZE}, new DoubleType()); }