Example #1
0
def main():
    try:
        stackdir = sys.argv[1]
    except IndexError:
        print "Usage: %s stack_dir [output_dir] [sdx] [sdy] [sdz]" % os.path.basename(sys.argv[0])
        sys.exit(1)

    ifiles = stackhandle.get_stack_files(stackdir)

    try:
        output_dir = sys.argv[2]
    except IndexError:
        output_dir = 'output/'
        print "Using default output directory output/"

    print "Called with", stackdir, output_dir

    try:
        sdx, sdy, sdz = int(sys.argv[3]), int(sys.argv[4]), int(sys.argv[5])
    except IndexError:
        print "Using default values for standard deviation"
        sdx, sdy, sdz = 8, 8, 6

    sds = 5

    print "Using standard deviations: %d, %d, %d" % (sdx, sdy, sdz)

    ma = load_png_stack(ifiles)

    flush_message("Applying 3D gaussian filter...")
    bl = nd.gaussian_filter(ma, [sdx, sdy, sdz])
    print "done"

    flush_message("Finding projection surface...")
    ps = proj.max_indices_z(bl)
    print "done"

    sps = nd.gaussian_filter(ps, sds)
    _, _, zmax = ma.shape
    vis_factor = 255 / zmax
    sfilename = os.path.join(output_dir, "surface-g3d-%d-%d-%d-%d.png" % (sdx, sdy, sdz, sds))
    scipy.misc.imsave(sfilename, sps * vis_factor)

    flush_message("Generating projection from surface...")
    res = proj.projection_from_surface_z(ma, sps, dm=3, dp=0) 
    print "done"

    filename = os.path.join(output_dir, "proj-g3d-%d-%d-%d-%d.png" % (sdx, sdy, sdz, sds))
    pmax = np.amax(res)
    vis_scale = 255 / pmax
    scipy.misc.imsave(filename, res * vis_scale)

    flush_message("Post processing...")
    pp = projpp.proj_filter(res * vis_scale, 3, 60, 15)
    print "done"

    filename = os.path.join(output_dir, 'proj-pp-%d-%d-%d-%d.png' % (sdx, sdy, sdz, sds))
    scipy.misc.imsave(filename, pp)
Example #2
0
def main():
    """Generate a Gaussian blurred maximal intensity projection from a directory
    containing a stack of image files"""

    try:
        stackdir = sys.argv[1]
        stackdir2 = sys.argv[2]
    except IndexError:
        print "Usage: %s stack_dir1 stack_dir2 [output_dir] [sdx] [sdy] [sdz]" % os.path.basename(sys.argv[0])
        sys.exit(1)

    #imgpattern, istart, iend = stackhandle.get_stack_pattern(stackdir)
    ifiles = stackhandle.get_stack_files(stackdir)
    ifiles2 = stackhandle.get_stack_files(stackdir2)

    try:
        output_dir = sys.argv[3]
    except IndexError:
        output_dir = 'output/'
        print "Using default output directory output/"

    print "Called with", stackdir, output_dir

    try:
        sdx, sdy, sdz = int(sys.argv[4]), int(sys.argv[5]), int(sys.argv[6])
    except IndexError:
        print "Using default values for standard deviation"
        sdx, sdy, sdz = 8, 8, 6

    # Gaussian blur applied to surface after projection is taken
    sds = 5

    print "Using standard deviations: (%d, %d, %d), %d" % (sdx, sdy, sdz, sds)

    # Load the two images from their stack representations
    ma = load_png_stack(ifiles)
    ma2 = load_png_stack(ifiles2)

    # Apply the initial Gaussian blur in 3D to the first stack (which we will
    # use to generate the surface)
    bl = apply_gaussian_filter(ma, [sdx, sdy, sdz])

    # Calculate the projection surface using numpy's function for finding the
    # max of an array along a particular axis
    ps = np.argmax(bl, 2)

    # Apply the blur to the resultant surface and save the result
    sps = nd.gaussian_filter(ps, sds)
    _, _, zmax = ma.shape
    vis_factor = 255 / zmax
    sfilename = os.path.join(output_dir, "surface-g3d-%d-%d-%d-%d.png" % (sdx, sdy, sdz, sds))
    save_numpy_as_png(sfilename, sps * vis_factor)

    # Calculate the projection of the first image using the generated surface
    # and save the result, scaling intensities after taking the projection
    res1 = projection_from_surface(ma, sps)
    filename = os.path.join(output_dir, "proj-1-g3d-%d-%d-%d-%d.png" % (sdx, sdy, sdz, sds))
    pmax = np.amax(res1)
    vis_scale = 255 / pmax
    scipy.misc.imsave(filename, res1 * vis_scale)

    res2 = projection_from_surface(ma2, sps)
    filename = os.path.join(output_dir, "proj-2-g3d-%d-%d-%d-%d.png" % (sdx, sdy, sdz, sds))
    pmax = np.amax(res2)
    vis_scale = 255 / pmax
    scipy.misc.imsave(filename, res1 * vis_scale)

    flush_message("Post processing...")
    pp = projpp.proj_filter(res1 * vis_scale, 3, 60, 15)
    print " done"
    filename = os.path.join(output_dir, 'proj-pp-%d-%d-%d-%d.png' % (sdx, sdy, sdz, sds))
    scipy.misc.imsave(filename, pp)