Пример #1
0
def renderImage(image, x, y, sigma):
    """
    A helper function that does the actual rendering.
    """
    # Histogram.
    if sigma is None:
        gridC.grid2D(numpy.round(y), numpy.round(x), image)
    # Gaussians.
    else:
        dg.drawGaussiansXYOnImage(image, y, x, sigma=sigma)
Пример #2
0
def render2DImage(i3_reader,
                  shape,
                  category=None,
                  offsets=None,
                  scale=2,
                  sigma=None):
    """
    Create a grayscale image from a Insight3 format binary data.

    i3_reader - A readinsight3.I3Reader object.
    shape - The shape of the output image. This will be multiplied by scale.
    category - Filter for localizations of this category. The default is all categories.
    offsets - X,Y offset of the image origin.  The default is 0.0.
    scale - The 'zoom' level of the output image, i.e. if the original STORM movie was
            256x256 and scale = 2 then the output image will be 512x512.
    sigma - The sigma to use when rendering gaussians (pixels). If this is None then
            the image will be a histogram.
    """
    image = numpy.zeros((shape[0] * scale, shape[1] * scale))

    # Make sure we are starting at the beginning.
    i3_reader.resetFp()

    # Load the first block of data.
    i3_data = i3_reader.nextBlock()
    while (i3_data is not False):
        sys.stdout.write(".")
        sys.stdout.flush()

        # Filter by category, if requested.
        if category is not None:
            i3_data = i3dtype.maskData(i3_data, (i3_data['c'] == category))

        # Adjust by offsets, if specified. Note, not adjusted for scale.
        if offsets is not None:
            i3_data['xc'] -= offsets[0]
            i3_data['yc'] -= offsets[1]

        # Adjust x,y by scale.
        xc = i3_data['xc'] * scale
        yc = i3_data['yc'] * scale

        # Histogram.
        if sigma is None:
            image += gridC.grid2D(numpy.round(xc), numpy.round(yc),
                                  image.shape)
        # Gaussians.
        else:
            dg.drawGaussiansXYOnImage(image, xc, yc, sigma=sigma)

        # Load next block of data.
        i3_data = i3_reader.nextBlock()

    print()
    return image
Пример #3
0
def renderImage(image, x, y, sigma):
    """
    A helper function that does the actual rendering.
    """
    # Histogram.
    if sigma is None:
        gridC.grid2D(numpy.round(y),
                     numpy.round(x),
                     image)
    # Gaussians.
    else:
        dg.drawGaussiansXYOnImage(image, y, x, sigma = sigma)
Пример #4
0
def clusterImages(mlist_name, title, min_size, image_max, output, image_size):
    
    i3_data = readinsight3.loadI3GoodOnly(mlist_name)

    print("Only coloring clusters with at least", min_size, "localizations.")

    rand_color = randomcolor.RandomColor()

    scale = 4
    image_size[0] = scale * image_size[0]
    image_size[1] = scale * image_size[1]

    red_image = numpy.zeros(image_size)
    grn_image = numpy.zeros(image_size)
    blu_image = numpy.zeros(image_size)
    sum_image = numpy.zeros(image_size)
    
    labels = i3_data['lk']
    start = int(numpy.min(labels))
    stop = int(numpy.max(labels)) + 1
    
    num_clusters = 0
    for k in range(start,stop):
        if ((k%200)==0):
            print("Processing cluster", k)
        
        mask = (labels == k)
        csize = mask.sum()

        x = scale * i3_data['xc'][mask]
        y = scale * i3_data['yc'][mask]

        if (k == -1) or (csize < min_size):
            r = 1.0
            g = 1.0
            b = 1.0
        else:
            num_clusters += 1
            color = rand_color.generate()
            r = int(color[0][1:3], 16)/255.0
            g = int(color[0][3:5], 16)/255.0        
            b = int(color[0][5:7], 16)/255.0

        if False:
            temp = numpy.zeros(image_size)
            dg.drawGaussiansXYOnImage(temp, x, y, sigma = 1.5)

            red_image += r * temp
            grn_image += g * temp
            blu_image += b * temp
            sum_image += temp
        
        else:
            for i in range(x.size):
                yi = int(x[i])
                xi = int(y[i])
                if (xi >= 0) and (xi < image_size[0]) and (yi >= 0) and (yi < image_size[1]):
                    red_image[xi,yi] += r
                    grn_image[xi,yi] += g
                    blu_image[xi,yi] += b
                    sum_image[xi,yi] += 1
        
    # Some wacky normalization scheme..
    mask = (sum_image > image_max)

    red_image[mask] = (red_image[mask]/sum_image[mask]) * image_max
    grn_image[mask] = (grn_image[mask]/sum_image[mask]) * image_max
    blu_image[mask] = (blu_image[mask]/sum_image[mask]) * image_max
    sum_image[mask] = image_max

    rgb_image = numpy.zeros((image_size[0], image_size[1], 3))
    rgb_image[:,:,0] = red_image
    rgb_image[:,:,1] = grn_image
    rgb_image[:,:,2] = blu_image

    rgb_image = (255.0/image_max)*rgb_image
    sum_image = (255.0/image_max)*sum_image

    rgb_image = rgb_image.astype(numpy.uint8)
    sum_image = sum_image.astype(numpy.uint8)

    title += " (" + str(num_clusters) + ")"

    # Save RGB image.
    img1 = Image.fromarray(rgb_image, "RGB")
    try:
        draw1 = ImageDraw.Draw(img1)
        font1 = ImageFont.truetype("FreeMono.ttf", 24)
        draw1.text((2,2), title, (255,255,255), font = font1)
    except IOError:
        print("Text drawing disabled, true type font file may be missing?")
    
    img1.save(output + "_01.png")

    # Save grayscale image.
    img2 = Image.fromarray(sum_image, "L")
    try:
        img2 = img2.convert("RGB")
        draw2 = ImageDraw.Draw(img2)
        font2 = ImageFont.truetype("FreeMono.ttf", 24)
        draw2.text((2,2), title, (255,255,255), font = font2)
    except IOError:
        print("Text drawing disabled, true type font file may be missing?")
    
    img2.save(output + "_02.png")
Пример #5
0
def render3DImage(i3_reader,
                  shape,
                  category=None,
                  offsets=None,
                  scale=2,
                  sigma=None,
                  z_edges=None):
    """
    Create a stack of grayscale images from a Insight3 format binary data.

    i3_reader - A readinsight3.I3Reader object.
    shape - The shape of the output image. This will be multiplied by scale.
    category - Filter for localizations of this category. The default is all categories.
    offsets - X,Y offset of the image origin.  The default is 0.0.
    scale - The 'zoom' level of the output image, i.e. if the original STORM movie was
            256x256 and scale = 2 then the output image will be 512x512.
    sigma - The sigma to use when rendering gaussians (pixels). If this is None then
            the image will be a histogram.
    z_edges - A list of z values specifying the z range for each image. This should be
            in nanometers.
    """
    num_z = len(z_edges) - 1
    images = []
    for i in range(num_z):
        images.append(numpy.zeros((shape[0] * scale, shape[1] * scale)))

    # Make sure we are starting at the beginning.
    i3_reader.resetFp()

    # Load the first block of data.
    i3_data = i3_reader.nextBlock()
    while (i3_data is not False):
        sys.stdout.write(".")
        sys.stdout.flush()

        # Filter by category, if requested.
        if category is not None:
            i3_data = i3dtype.maskData(i3_data, (i3_data['c'] == category))

        # Adjust by offsets, if specified. Note, not adjusted for scale.
        if offsets is not None:
            i3_data['xc'] -= offsets[0]
            i3_data['yc'] -= offsets[1]

        # Adjust x,y by scale.
        xc = i3_data['xc'] * scale
        yc = i3_data['yc'] * scale

        # Iterate through z ranges.
        for i in range(num_z):
            z_mask = (i3_data['zc'] > z_edges[i]) & (i3_data['zc'] <
                                                     z_edges[i + 1])

            xc_z = xc[z_mask]
            yc_z = yc[z_mask]

            # Histogram.
            if sigma is None:
                images[i] += gridC.grid2D(numpy.round(xc_z), numpy.round(yc_z),
                                          images[0].shape)
            # Gaussians.
            else:
                dg.drawGaussiansXYOnImage(images[i], xc_z, yc_z, sigma=sigma)

        # Load next block of data.
        i3_data = i3_reader.nextBlock()

    print()
    return images
Пример #6
0
def clusterImages(h5_name, min_size, image_max, scale = 4, show_unclustered = True):
    """
    Creates a RGB and an intensity array of the clusters with more than
    min_size elements.

    h5_name - The name of the HDF5 file with clustering information.
    min_size - The minimum size cluster to color.
    image_max - Maximum value for color normalization.
    scale - (int) Image rendering scale, default is 4.
    show_unclustered - Include unclustered localizations (in white).
    """
    rand_color = randomcolor.RandomColor()
    
    with clSAH5Py.SAH5Clusters(h5_name) as cl_h5:
        [movie_x, movie_y] = cl_h5.getMovieInformation()[:2]

        image_size = [movie_y * scale, movie_x * scale]

        red_image = numpy.zeros(image_size)
        grn_image = numpy.zeros(image_size)
        blu_image = numpy.zeros(image_size)
        sum_image = numpy.zeros(image_size)
    
        num_clusters = 0
        for index, cluster in cl_h5.clustersIterator(skip_unclustered = False, fields = ["x", "y"]):
            if ((index%200)==0):
                print("Processing cluster", index)

            x = scale * cluster['x']
            y = scale * cluster['y']

            if (index == 0) or (x.size < min_size):

                # Skip to next cluster if this cluster is too small and we are
                # not supposed to draw the unclustered localizations.
                if not show_unclustered:
                    continue

                else:
                    r = 1.0
                    g = 1.0
                    b = 1.0
            else:
                num_clusters += 1
                color = rand_color.generate()
                r = int(color[0][1:3], 16)/255.0
                g = int(color[0][3:5], 16)/255.0        
                b = int(color[0][5:7], 16)/255.0

            # FIXME: This would draw the image clusters as Gaussians. Not sure
            #        how well it actually works. Sigma should also probably be
            #        a user argument.
            #
            if False:
                temp = numpy.zeros(image_size)
                dg.drawGaussiansXYOnImage(temp, y, x, sigma = 1.5)

                red_image += r * temp
                grn_image += g * temp
                blu_image += b * temp
                sum_image += temp
        
            else:
                for i in range(x.size):
                    yi = int(x[i])
                    xi = int(y[i])
                    if (xi >= 0) and (xi < image_size[0]) and (yi >= 0) and (yi < image_size[1]):
                        red_image[xi,yi] += r
                        grn_image[xi,yi] += g
                        blu_image[xi,yi] += b
                        sum_image[xi,yi] += 1
        
        # Some wacky normalization scheme..
        mask = (sum_image > image_max)

        red_image[mask] = (red_image[mask]/sum_image[mask]) * image_max
        grn_image[mask] = (grn_image[mask]/sum_image[mask]) * image_max
        blu_image[mask] = (blu_image[mask]/sum_image[mask]) * image_max
        sum_image[mask] = image_max

        rgb_image = numpy.zeros((image_size[0], image_size[1], 3))
        rgb_image[:,:,0] = red_image
        rgb_image[:,:,1] = grn_image
        rgb_image[:,:,2] = blu_image

        rgb_image = (255.0/image_max)*rgb_image
        sum_image = (255.0/image_max)*sum_image

        rgb_image = rgb_image.astype(numpy.uint8)
        sum_image = sum_image.astype(numpy.uint8)

    return [rgb_image, sum_image, num_clusters]
Пример #7
0
def clusterImages(mlist_name, title, min_size, image_max, output, image_size):
    
    i3_data = readinsight3.loadI3GoodOnly(mlist_name)

    print("Only coloring clusters with at least", min_size, "localizations.")

    rand_color = randomcolor.RandomColor()

    scale = 4
    image_size[0] = scale * image_size[0]
    image_size[1] = scale * image_size[1]

    red_image = numpy.zeros(image_size)
    grn_image = numpy.zeros(image_size)
    blu_image = numpy.zeros(image_size)
    sum_image = numpy.zeros(image_size)
    
    labels = i3_data['lk']
    start = int(numpy.min(labels))
    stop = int(numpy.max(labels)) + 1
    
    num_clusters = 0
    for k in range(start,stop):
        if ((k%200)==0):
            print("Processing cluster", k)
        
        mask = (labels == k)
        csize = mask.sum()

        x = scale * i3_data['xc'][mask]
        y = scale * i3_data['yc'][mask]

        if (k == -1) or (csize < min_size):
            r = 1.0
            g = 1.0
            b = 1.0
        else:
            num_clusters += 1
            color = rand_color.generate()
            r = int(color[0][1:3], 16)/255.0
            g = int(color[0][3:5], 16)/255.0        
            b = int(color[0][5:7], 16)/255.0

        if False:
            temp = numpy.zeros(image_size)
            dg.drawGaussiansXYOnImage(temp, x, y, sigma = 1.5)

            red_image += r * temp
            grn_image += g * temp
            blu_image += b * temp
            sum_image += temp
        
        else:
            for i in range(x.size):
                yi = int(x[i])
                xi = int(y[i])
                if (xi >= 0) and (xi < image_size[0]) and (yi >= 0) and (yi < image_size[1]):
                    red_image[xi,yi] += r
                    grn_image[xi,yi] += g
                    blu_image[xi,yi] += b
                    sum_image[xi,yi] += 1
        
    # Some wacky normalization scheme..
    mask = (sum_image > image_max)

    red_image[mask] = (red_image[mask]/sum_image[mask]) * image_max
    grn_image[mask] = (grn_image[mask]/sum_image[mask]) * image_max
    blu_image[mask] = (blu_image[mask]/sum_image[mask]) * image_max
    sum_image[mask] = image_max

    rgb_image = numpy.zeros((image_size[0], image_size[1], 3))
    rgb_image[:,:,0] = red_image
    rgb_image[:,:,1] = grn_image
    rgb_image[:,:,2] = blu_image

    rgb_image = (255.0/image_max)*rgb_image
    sum_image = (255.0/image_max)*sum_image

    rgb_image = rgb_image.astype(numpy.uint8)
    sum_image = sum_image.astype(numpy.uint8)

    title += " (" + str(num_clusters) + ")"

    # Save RGB image.
    img1 = Image.fromarray(rgb_image, "RGB")
    try:
        draw1 = ImageDraw.Draw(img1)
        font1 = ImageFont.truetype("FreeMono.ttf", 24)
        draw1.text((2,2), title, (255,255,255), font = font1)
    except IOError:
        print("Text drawing disabled, true type font file may be missing?")
    
    img1.save(output + "_01.png")

    # Save grayscale image.
    img2 = Image.fromarray(sum_image, "L")
    try:
        img2 = img2.convert("RGB")
        draw2 = ImageDraw.Draw(img2)
        font2 = ImageFont.truetype("FreeMono.ttf", 24)
        draw2.text((2,2), title, (255,255,255), font = font2)
    except IOError:
        print("Text drawing disabled, true type font file may be missing?")
    
    img2.save(output + "_02.png")