Ejemplo n.º 1
0
def makeFRmosaic(dir_path, border=5):
    """ Make a mosaic out of an FR bin file. """

    dir_path, file_name = os.path.split(dir_path)

    # Load the FR file
    fr = readFR(dir_path, file_name)

    # Go through all lines in the file
    for i in range(fr.lines):

        # Determine the maximum size of the window
        max_size = max([fr.size[i][z] for z in range(fr.frameNum[i])])

        # Determine the width and the height in frame segments
        height = int(np.ceil(np.sqrt(fr.frameNum[i])))
        width = int(np.ceil(fr.frameNum[i] / height))

        # Compute the image width and height
        w_img = int(np.ceil(width * (border + max_size)))
        h_img = int(np.ceil(height * (border + max_size)))

        # Create an empty mosaic image
        mosaic_img = np.zeros((h_img, w_img), dtype=np.uint8)

        x_min = w_img
        x_max = 0
        y_min = h_img
        y_max = 0

        # Go through all frames
        for z in range(fr.frameNum[i]):

            # # Get the center position of the detection on the current frame
            # yc = fr.yc[i][z]
            # xc = fr.xc[i][z]

            # # Get the frame number
            # t = fr.t[i][z]

            # Get the size of the window
            size = fr.size[i][z]

            # Compute the position of the frame on the image (tiling)
            h_ind = int(z % height)
            v_ind = int(z / height)

            # Compute the position of the frame on the image in image coordinates
            frame_x = h_ind * max_size + border + (max_size - size) // 2 + 1
            frame_y = v_ind * max_size + border + (max_size - size) // 2 + 1

            # Get the frame size
            fr_y, fr_x = fr.frames[i][z].shape

            # Assign the frame to the mosaic
            mosaic_img[frame_y:(frame_y + fr_y),
                       frame_x:(frame_x + fr_x)] = fr.frames[i][z]

            # Keep track of the min and max value of the extent of the frames
            x_min = min(x_min, frame_x)
            x_max = max(x_max, frame_x + fr_x)
            y_min = min(y_min, frame_y)
            y_max = max(y_max, frame_y + fr_y)

        # Draw a grid
        for h_ind in range(height + 1):

            # Draw horizontal lines
            mosaic_img[h_ind * max_size + border, :] = 255

        for v_ind in range(width + 1):

            # Draw horizontal lines
            mosaic_img[:, v_ind * max_size + border] = 255

        # Cut the image to the size of the frames
        mosaic_img = mosaic_img[y_min:y_max, x_min:x_max]

        # Save the image to disk
        img_file_name = ".".join(file_name.split('.')[:-1]) + '_mosaic.png'
        scipy.misc.imsave(os.path.join(dir_path, img_file_name), mosaic_img)

        # Plot the image
        plt.imshow(mosaic_img, cmap='gray', vmin=0, vmax=255)
        plt.show()
Ejemplo n.º 2
0
def makeFRmosaic(dir_path, border=5, line=0, frame_beg=None, frame_end=None, every_nth=None, fps=25.0, dt=0):
    """ Make a mosaic out of an FR bin file. """

    dir_path, file_name = os.path.split(dir_path)

    # Load the FR file
    fr = readFR(dir_path, file_name)

    # # Go through all lines in the file
    # for i in range(fr.lines):

    # Select a given line
    i = line

    print("Line {:d}, of max index {:d}".format(line, fr.lines - 1))

    # Determine the maximum size of the window
    max_size = max([fr.size[i][z] for z in range(fr.frameNum[i])])


    # Get the frame range
    frame_range = []
    for z in range(fr.frameNum[i]):

        print("Frame:", z)

        # Skip all frames outside given frame range
        if frame_beg is not None:
            if z < frame_beg:
                print("... skipped")
                continue

        if frame_end is not None:
            if z > frame_end:
                print("... skipped")
                continue


        # Skip every Nth frame, if given
        if every_nth is not None:
            if not (z%every_nth == 0):
                print("... skipped")
                continue


        frame_range.append(z)

        
    total_frames = len(frame_range)

    if every_nth is None:
        every_nth = 1



    # Determine the width and the height in frame segments
    height = int(np.ceil(np.sqrt(total_frames)))
    width = int(np.ceil(total_frames/height))

    # Compute the image width and height
    w_img = int(np.ceil(width*(border + max_size)))
    h_img = int(np.ceil(height*(border + max_size)))

    print()
    print("Total frames:", total_frames)
    print("Frame segments (h, w):", height, width)
    print("Image dimensions (h, w) px:", h_img, w_img)

    # Create an empty mosaic image
    mosaic_img = np.zeros((h_img, w_img), dtype=np.uint8)

        
    # Cut image to max extent of frames
    #x_min = w_img
    # x_max = 0
    #y_min = h_img
    #y_max = 0

    # Don't cut the image
    x_min = 0
    x_max = w_img
    y_min = 0
    y_max = h_img

    # Go through all visible frames
    print()
    for tile_index, z in enumerate(frame_range):

        # # Get the center position of the detection on the current frame
        # yc = fr.yc[i][z]
        # xc = fr.xc[i][z]

        # Get the real frame number
        t = fr.t[i][z]

        # Get the size of the window
        size = fr.size[i][z]

        # Compute the position of the frame on the image (tiling)
        w_ind = int(tile_index%width)
        h_ind = int(tile_index/width)

        print("Plotting frame:", z, "position (h, w):", h_ind, w_ind)

        # Compute the position of the frame on the image in image coordinates
        frame_x = w_ind*max_size + border + (max_size - size)//2 + 1
        frame_y = h_ind*max_size + border + (max_size - size)//2 + 1

        # Get the frame size
        fr_y, fr_x = fr.frames[i][z].shape

        # Assign the frame to the mosaic
        print(frame_y, (frame_y + fr_y), frame_x, (frame_x + fr_x))
        mosaic_img[frame_y:(frame_y + fr_y), frame_x:(frame_x + fr_x)] = fr.frames[i][z]

        # Keep track of the min and max value of the extent of the frames
        #x_min = min(x_min, frame_x)
        x_max = max(x_max, frame_x + fr_x)
        #y_min = min(y_min, frame_y)
        y_max = max(y_max, frame_y + fr_y)

        # Add the frame time
        fr_time = t/fps + dt
        plt.text(w_ind*max_size + border, 10 + h_ind*max_size + border, '{:5.2f} s'.format(fr_time), \
            va='top', ha='left', size=10, color='w')


    # Draw a grid (skip edges)
    for h_ind in range(1, height):

        print(h_ind*max_size + border)
        
        # Draw horizontal lines
        mosaic_img[h_ind*max_size + border, :] = 255

    for v_ind in range(1, width):
        
        # Draw horizontal lines
        mosaic_img[:, v_ind*max_size + border] = 255


    # Cut the image to the size of the frames
    mosaic_img = mosaic_img[y_min:y_max, x_min:x_max]

    # Plot the image
    plt.imshow(mosaic_img, cmap='gray', vmin=0, vmax=255, aspect='equal')
    plt.gca().set_axis_off()

    # Save the image to disk
    img_file_name = ".".join(file_name.split('.')[:-1]) + '_line_{:d}_mosaic.png'.format(line)
    #saveImage(os.path.join(dir_path, img_file_name), mosaic_img)
    plt.savefig(os.path.join(dir_path, img_file_name), dpi=300, bbox_inches='tight',transparent=True, pad_inches=0)

    plt.show()
Ejemplo n.º 3
0
def makeFRmosaic(dir_path, border=5):
    """ Make a mosaic out of an FR bin file. """

    dir_path, file_name = os.path.split(dir_path)

    # Load the FR file
    fr = readFR(dir_path, file_name)

    # Go through all lines in the file
    for i in range(fr.lines):

        # Determine the maximum size of the window
        max_size = max([fr.size[i][z] for z in range(fr.frameNum[i])])

        # Determine the width and the height in frame segments
        height = int(np.ceil(np.sqrt(fr.frameNum[i])))
        width = int(np.ceil(fr.frameNum[i]/height))

        # Compute the image width and height
        w_img = int(np.ceil(width*(border + max_size)))
        h_img = int(np.ceil(height*(border + max_size)))

        # Create an empty mosaic image
        mosaic_img = np.zeros((h_img, w_img), dtype=np.uint8)

        
        x_min = w_img
        x_max = 0
        y_min = h_img
        y_max = 0

        # Go through all frames
        for z in range(fr.frameNum[i]):

            # # Get the center position of the detection on the current frame
            # yc = fr.yc[i][z]
            # xc = fr.xc[i][z]

            # # Get the frame number
            # t = fr.t[i][z]

            # Get the size of the window
            size = fr.size[i][z]

            # Compute the position of the frame on the image (tiling)
            h_ind = int(z%height)
            v_ind = int(z/height)

            # Compute the position of the frame on the image in image coordinates
            frame_x = h_ind*max_size + border + (max_size - size)//2 + 1
            frame_y = v_ind*max_size + border + (max_size - size)//2 + 1

            # Get the frame size
            fr_y, fr_x = fr.frames[i][z].shape

            # Assign the frame to the mosaic
            mosaic_img[frame_y:(frame_y + fr_y), frame_x:(frame_x + fr_x)] = fr.frames[i][z]

            # Keep track of the min and max value of the extent of the frames
            x_min = min(x_min, frame_x)
            x_max = max(x_max, frame_x + fr_x)
            y_min = min(y_min, frame_y)
            y_max = max(y_max, frame_y + fr_y)


        # Draw a grid
        for h_ind in range(height+1):
        	
        	# Draw horizontal lines
        	mosaic_img[h_ind*max_size + border, :] = 255

        for v_ind in range(width+1):
            
            # Draw horizontal lines
            mosaic_img[:, v_ind*max_size + border] = 255


        # Cut the image to the size of the frames
        mosaic_img = mosaic_img[y_min:y_max, x_min:x_max]


        # Save the image to disk
        img_file_name = ".".join(file_name.split('.')[:-1]) + '_mosaic.png'
        scipy.misc.imsave(os.path.join(dir_path, img_file_name), mosaic_img)

        # Plot the image
        plt.imshow(mosaic_img, cmap='gray', vmin=0, vmax=255)
        plt.show()
Ejemplo n.º 4
0
    def __init__(self,
                 config,
                 input1,
                 input2,
                 first_frame=None,
                 fps=None,
                 deinterlace_mode=-1,
                 png_mode=False):
        """ Tool for manually picking meteor centroids and photometry. 
        
        Arguments:
            config: [config] Configuration structure.
            input1: [str] Path to the FF or FR file. Or, path to the directory with PNGs.
            inptu2: [str] Path to the FR file, if given (can be None). In PNG mode this must be the datetime 
                of the first frame.

        Keyword Arguments:
            first_frame: [int] First frame to start with. None by default, which will start with the first one.
            fps: [float] Frames per second. None by default, which will read the fps from the config file.
            deinterlace_mode: [int]
                -1 - no deinterlace
                 0 - odd first
                 1 - even first
            png_mode: [bool] If True, the frames are taken from a directory which contains a list of PNG 
                images. False by default, in which case FF and/or FR files are used.
        """

        self.config = config

        self.fps = fps

        self.deinterlace_mode = deinterlace_mode

        self.png_mode = png_mode

        # Compute the frame step
        if self.deinterlace_mode > -1:
            self.frame_step = 0.5
        else:
            self.frame_step = 1

        # Load the PNG files if in PNG mode
        if self.png_mode:

            self.png_dir = input1
            self.frame0_time = input2

            print('PNG dir:', self.png_dir)

            self.png_list = sorted([fname for fname in os.listdir(self.png_dir) \
                if fname.lower().endswith('.png')])

            self.nframes = len(self.png_list)

            self.png_img_path = ''

            # Variables for FF mode that are not used
            self.ff = None
            self.fr = None

        # FF mode
        else:

            # Variables for PNG mode that are not used
            self.png_list = None
            self.frame0_time = None

            self.ff_file = input1
            self.fr_file = input2

            # Load the FF file if given
            if self.ff_file is not None:
                self.ff = readFF(*os.path.split(self.ff_file))
            else:
                self.ff = None

            # Load the FR file is given
            if self.fr_file is not None:
                self.fr = readFR(*os.path.split(self.fr_file))

                print('Total lines:', self.fr.lines)
            else:
                self.fr = None

            self.nframes = 256

        ###########

        self.img_gamma = 1.0
        self.show_key_help = True

        self.current_image = None

        self.fr_xmin = None
        self.fr_xmax = None
        self.fr_ymin = None
        self.fr_ymax = None

        # Previous zoom
        self.prev_xlim = None
        self.prev_ylim = None

        self.circle_aperture = None
        self.circle_aperture_outer = None
        self.aperture_radius = 5
        self.scroll_counter = 0

        self.centroid_handle = None

        self.photometry_coloring_mode = False
        self.photometry_coloring_color = False
        self.photometry_aperture_radius = 3
        self.photometry_add = True
        self.photometry_coloring_handle = None

        self.pick_list = []

        ###########

        # Each FR bin can have multiple detections, the first one is by default
        self.current_line = 0

        if first_frame is not None:
            self.current_frame = first_frame % self.nframes

        else:

            # Set the first frame to the first frame in FR, if given
            if self.fr is not None:
                self.current_frame = self.fr.t[self.current_line][0]

            else:
                self.current_frame = 0

        ### INIT IMAGE ###

        plt.figure(facecolor='black')

        # Init the first image
        self.updateImage()
        self.printStatus()

        self.ax = plt.gca()

        # Set the bacground color to black
        #matplotlib.rcParams['axes.facecolor'] = 'k'

        # Disable standard matplotlib keyboard shortcuts
        plt.rcParams['keymap.save'] = ''
        plt.rcParams['keymap.fullscreen'] = ''
        plt.rcParams['keymap.all_axes'] = ''
        plt.rcParams['keymap.quit'] = ''

        # Register event handlers
        self.ax.figure.canvas.mpl_connect('key_press_event', self.onKeyPress)
        self.ax.figure.canvas.mpl_connect('key_release_event',
                                          self.onKeyRelease)
        self.ax.figure.canvas.mpl_connect('motion_notify_event',
                                          self.onMouseMotion)
        self.ax.figure.canvas.mpl_connect('button_press_event',
                                          self.onMousePress)
        self.ax.figure.canvas.mpl_connect('button_release_event',
                                          self.onMouseRelease)
        self.ax.figure.canvas.mpl_connect('scroll_event', self.onScroll)