pos = zeros(img1.numDimensions(), 'l')
    while c3.hasNext():
        t1 = c1.next()
        t2 = c2.next()
        t3 = c3.next()
        sign1 = -1 if 0 == t1.get() else 1
        sign2 = -1 if 0 == t2.get() else 1
        search1.search(c1)
        search2.search(c2)
        value1 = sign1 * search1.getDistance() * (1 - weight)
        value2 = sign2 * search2.getDistance() * weight
        if value1 + value2 > 0:
            t3.setOne()
    return img3


edge_pix1 = findEdgePixels(img1M)
kdtree1 = KDTree(edge_pix1, edge_pix1)
search1 = NearestNeighborSearchOnKDTree(kdtree1)
edge_pix2 = findEdgePixels(img2M)
kdtree2 = KDTree(edge_pix2, edge_pix2)
search2 = NearestNeighborSearchOnKDTree(kdtree2)

steps = []
for weight in [0.2, 0.4, 0.6, 0.8]:
    steps.append(makeInterpolatedImage(img1M, search1, img2M, search2, weight))

img3 = Views.stack([img1M] + steps + [img2M])
imp3 = IL.wrap(img3, "interpolated " + str(weight))
imp3.setDisplayRange(0, 1)
imp3.show()
Exemplo n.º 2
0
affine.rotate(angle_rad)
affine.translate(center)
 
# Get the interpolator
interpolator = LanczosInterpolatorFactory()
 
# Iterate over all frame in the stack
axis = Axes.TIME
output = []
for d in range(data.dimension(axis)):
 
    # Get the current frame
    frame = crop_along_one_axis(ops, data, [d, d], "TIME")
 
    # Get the interpolate view of the frame
    extended = ops.run("transform.extendZeroView", frame)
    interpolant = ops.run("transform.interpolateView", extended, interpolator)
 
    # Apply the transformation to it
    rotated = RealViews.affine(interpolant, affine)
     
    # Set the intervals
    rotated = ops.transform().offset(rotated, frame)
 
    output.append(rotated)
 
output = Views.stack(output)

# Create output Dataset
output = ds.create(output)
Exemplo n.º 3
0
# Rotate the cube on the Y axis to the left
img2 = Views.zeroMin(Views.rotate(img1, 2, 0))  # zeroMin is CRITICAL

# Rotate the cube on the X axis to the top
img3 = Views.zeroMin(Views.rotate(img1, 2, 1))

# Reduce Z resolution: make them anisotropic but in a different direction
nth = 2
img1 = dropSlices(img1, nth)
img2 = dropSlices(img2, nth)
img3 = dropSlices(img3, nth)

# The sequence of images to transform, each relative to the previous
images = [img1, img2, img3]

IL.wrap(Views.stack(images), "unregistered").show()

# PARAMETERS
calibrations = [[1.0, 1.0, 1.0 * nth], [1.0, 1.0, 1.0 * nth],
                [1.0, 1.0, 1.0 * nth]]

# Pretend file names:
img_filenames = ["img1", "img2", "img3"]


# Pretend loader:
class ImgLoader():
    def load(self, img_filename):
        return globals()[img_filename]

from net.imglib2.view import Views

# Find image files
images_sequence_dir = str(images_sequence_dir)
fnames = []
for fname in os.listdir(images_sequence_dir):
    if fname.endswith(image_extension):
        fnames.append(os.path.join(images_sequence_dir, fname))
fnames = sorted(fnames)

if len(fnames) < 1:
    raise Exception("Not image files found in %s" % images_sequence_dir)

# Open and stack images
stack = []
for fname in fnames:
    data = ds.open(fname)

    # Discard image with others than 2 dimensions
    if data.numDimensions() == 2:
        stack.append(data)

output = Views.stack(stack)
output = ds.create(output)

# Set the third axis to the correct type
output.axis(2).setType(getattr(Axes, axis_type))

# Set the name of the dataset to the directory name
output.setName(os.path.basename(images_sequence_dir) + image_extension)
Exemplo n.º 5
0
  # And notice search instances are copied: they are stateful.
  futures = [exe.submit(Task(makeInterpolatedImage,
                             img1, search1.copy(), img2, search2.copy(), w * 0.1))
             for w in xrange(2, 10, 2)] # list: eager
  # Get each step, waiting until all are built
  steps = [f.get() for f in futures] # list: eager, for concatenation below in Views.stack
finally:
  # This 'finally' block executes even in the event of an error
  # guaranteeing that the executing threads will be shut down no matter what.
  exe.shutdown()


# ISSUE: Does not work with IntervalView from View.rotate,
# so img1 and img2 were copied into ArrayImg
# (The error would occur when iterating vol4d pixels beyond the first element in the 4th dimension.)
vol4d = Views.stack([img1] + steps + [img2])

# Convert 1 -> 255 for easier volume rendering in 3D Viewer
#compute(mul(vol4d, 255)).into(vol4d)
for t in Views.iterable(vol4d):
  if 0 != t.getByte():
    t.setReal(255)

# Construct an ij.VirtualStack from vol4d
virtualstack = IL.wrap(vol4d, "interpolations").getStack()
imp = ImagePlus("interpolations", virtualstack)
imp.setDimensions(1, vol4d.dimension(2), vol4d.dimension(3))
imp.setDisplayRange(0, 255)
# Show as a hyperstack with 4 dimensions, 1 channel
com = CompositeImage(imp, CompositeImage.GRAYSCALE)
com.show()
Exemplo n.º 6
0
def test(red, green, blue, easy=True):
    saturation = let(
        "red", red, "green", green, "blue", blue, "max",
        maximum("red", "green", "blue"), "min",
        minimum("red", "green", "blue"),
        IF(EQ(0, "max"), THEN(0), ELSE(div(sub("max", "min"), "max"))))

    brightness = div(maximum(red, green, blue), 255.0)

    hue = IF(
        EQ(0, saturation), THEN(0),
        ELSE(
            let(
                "red", red, "green", green, "blue", blue, "max",
                maximum("red", "green", "blue"), "min",
                minimum("red", "green", "blue"), "range", sub("max", "min"),
                "redc", div(sub("max", "red"), "range"), "greenc",
                div(sub("max", "green"), "range"), "bluec",
                div(sub("max", "blue"), "range"), "hue",
                div(
                    IF(
                        EQ("red", "max"), THEN(sub("bluec", "greenc")),
                        ELSE(
                            IF(EQ("green", "max"),
                               THEN(sub(add(2, "redc"), "bluec")),
                               ELSE(sub(add(4, "greenc"), "redc"))))), 6),
                IF(LT("hue", 0), THEN(add("hue", 1)), ELSE("hue")))))

    #print hierarchy(hue)

    #print "hue view:", hue.view( FloatType() ).iterationOrder()

    if easy:
        # About 26 ms
        """
    hsb = Views.stack( hue.view( FloatType() ),
                       saturation.view( FloatType() ),
                       brightness.view( FloatType() ) )
    """

        # About 13 ms: half! Still much worse than plain ImageJ,
        # but the source images are iterated 4 times, rather than just once,
        # and the saturation is computed twice,
        # and the min, max is computed 3 and 4 times, respectively.
        hsb = Views.stack(hue.viewDouble(FloatType()),
                          saturation.viewDouble(FloatType()),
                          brightness.viewDouble(FloatType()))
        """
    # Even worse: ~37 ms
    width, height = rgb.dimension(0), rgb.dimension(1)
    h = compute(hue).into(ArrayImgs.floats([width, height]))
    s = compute(saturation).into(ArrayImgs.floats([width, height]))
    b = compute(brightness).into(ArrayImgs.floats([width, height]))
    hsb = Views.stack( h, s, b )
    """

        imp = IL.wrap(hsb, "HSB view")
    else:
        # Tested it: takes more time (~40 ms vs 26 ms above)
        width, height = rgb.dimension(0), rgb.dimension(1)
        hb = zeros(width * height, 'f')
        sb = zeros(width * height, 'f')
        bb = zeros(width * height, 'f')
        h = ArrayImgs.floats(hb, [width, height])
        s = ArrayImgs.floats(sb, [width, height])
        b = ArrayImgs.floats(bb, [width, height])
        #print "ArrayImg:", b.iterationOrder()
        ImgUtil.copy(ImgView.wrap(hue.view(FloatType()), None), h)
        ImgUtil.copy(ImgView.wrap(saturation.view(FloatType()), None), s)
        ImgUtil.copy(ImgView.wrap(brightness.view(FloatType()), None), b)
        stack = ImageStack(width, height)
        stack.addSlice(FloatProcessor(width, height, hb, None))
        stack.addSlice(FloatProcessor(width, height, sb, None))
        stack.addSlice(FloatProcessor(width, height, bb, None))
        imp = ImagePlus("hsb", stack)
    return imp
    c3 = img3.cursor()
    while c3.hasNext():
        t1 = c1.next()
        t2 = c2.next()
        t3 = c3.next()
        sign1 = -1 if 0 == t1.get() else 1
        sign2 = -1 if 0 == t2.get() else 1
        search1.search(c1)
        search2.search(c2)
        value1 = sign1 * search1.getDistance() * (1 - weight)
        value2 = sign2 * search2.getDistance() * weight
        if value1 + value2 > 0:
            t3.setOne()
    return img3


edge_pix1 = findEdgePixels(img1)
kdtree1 = KDTree(edge_pix1, edge_pix1)
search1 = NearestNeighborSearchOnKDTree(kdtree1)
edge_pix2 = findEdgePixels(img2)
kdtree2 = KDTree(edge_pix2, edge_pix2)
search2 = NearestNeighborSearchOnKDTree(kdtree2)

steps = []
for weight in [x / 10.0 for x in xrange(2, 10, 2)]:
    steps.append(makeInterpolatedImage(img1, search1, img2, search2, weight))

imp3 = IL.wrap(Views.stack([img1] + steps + [img2]), "interpolations")
imp3.setDisplayRange(0, 1)
imp3.show()