Example #1
0
def focus_point(mmc, delta_z=10, total_z=150):
    # Get focus model
    focus_model = miq.get_classifier()
    
    # make z position array
    cur_pos = mmc.getPosition()
    start_pos = cur_pos + total_z/2
    end_pos = cur_pos - total_z/2
    num_steps = (start_pos-end_pos) / delta_z
    z = np.linspace(start_pos, end_pos, num_steps)

    cam = utils.start_cam()

    preds = []
    for curr_z in z:
        pos.set_pos(mmc, z=curr_z)
        frame = cam.get_frame(exp_time=1).reshape(cam.sensor_size[::-1])
        preds.append(focus_model.score(frame))
    # find the index of the min focus prediction
    best_focus_index = np.argmin(preds)
    # append to the PositionList 
    last_z = z[best_focus_index]

    utils.close_cam(cam)
    return last_z
Example #2
0
def get_frame():
    cam = sc_utils.start_cam()
    # frame = cam.get_frame(exp_time=1).reshape(cam.sensor_size[::-1])
    frame = cam.get_frame(exp_time=1)
    sc_utils.close_cam(cam)
    print(frame.shape)
    return frame
Example #3
0
def opencv():
    cam = utils.start_cam()
    try:
        print("Camera started")
        while True:
            frame = cam.get_live_frame().reshape(cam.sensor_size[::-1])
            frame = cv2.resize(frame, dim, interpolation=cv2.INTER_AREA)
            cv2.imshow('Live Mode', frame)
    except KeyboardInterrupt:
        utils.close_cam(cam)
Example #4
0
def focus_from_image_stack(xy_points, mmc, delta_z=5, total_z=150):
    ''' Brute force focus algorthim 
    
    args: 
        xy_points: a PositionList() of the locations to 
            focus at
        mmc: Micromanger instance
        delta_z: distacne between images (um)
        total_z: total imaging distance (all focused points on 
            chip should be in this range)
    
    returns:
        focused PositionList()
    '''
    start_time = time.time()

    # Get focus model
    focus_model = miq.get_classifier()

    pos_list = pos.PositionList()

    # make z position array
    cur_pos = mmc.getPosition()
    start_pos = cur_pos + total_z/2
    end_pos = cur_pos - total_z/2
    num_steps = (start_pos-end_pos) / delta_z
    z = np.linspace(start_pos, end_pos, num_steps)

    cam = utils.start_cam()

    for posit in xy_points:
        # Go to the x,y position 
        pos.set_pos(mmc, x=posit.x, y=posit.y)

        preds = []
        for curr_z in z:
            mmc.setPosition(curr_z)
            mmc.waitForSystem()
            frame = cam.get_frame(exp_time=1).reshape(cam.sensor_size[::-1])
            preds.append(focus_model.score(frame))
        # find the index of the min focus prediction
        best_focus_index = np.argmin(preds)
        # append to the PositionList 
        sp = pos.StagePosition(x=posit.x, y=posit.y,
                               z=z[best_focus_index])
        pos_list.append(sp)
    
    utils.close_cam(cam)
    total_time = time.time() - start_time
    # print ('Completed focus in', total_time, 'seconds')
    return pos_list
Example #5
0
def py_lab():
    cam = utils.start_cam()
    try:
        while True:
            t1 = time.time()
            t2 = time.time()
            frame = cam.get_frame(exp_time=1).reshape(cam.sensor_size[::-1])
            print(frame.shape)
            plt.imshow(frame, cmap="gray")
            t3 = time.time()
            plt.imshow(frame, cmap="gray")
            plt.pause(.1)
            plt.cla()
            t4 = time.time()
            print('time to get: ' + str(t2 - t1))
            print('time to imshow: ' + str(t3 - t2))
            print('time to save: ' + str(t4 - t3) + '\n\n')

    except KeyboardInterrupt:
        utils.close_cam(cam)
Example #6
0
    def image(self, mmc, save_dir):
        ''' Images the positions in the PositionList

        args: 
            mmc: Micormanager instance
            save_dir: Directory to save tiff files 
        '''
        # Make the directory to save to and change into it
        dir_name = save_dir + '/' + time.strftime("%Y-%m-%d_%H:%M")
        os.makedirs(dir_name)
        os.chdir(dir_name)

        cam = utils.start_cam()

        for ctr, pos in enumerate(self.positions):
            # set position and wait
            set_pos(mmc, pos.x, pos.y, z=pos.z)

            # Get image and save
            frame = cam.get_frame(exp_time=1).reshape(cam.sensor_size[::-1])
            tif.imwrite('img' + '_' + str(ctr) + '.tif', frame)
            time.sleep(0.05)

        utils.close_cam(cam)
Example #7
0
 def test_get_live_frame(self):
     cam = sc_utils.start_cam()
     frame = sc_utils.get_live_frame(cam, 1)
     sc_utils.close_cam(cam)
     assertIsNotNone(frame)
Example #8
0
def focus_from_last_point(xy_points, mmc, delta_z=10, total_z=150):
    start_time = time.time()

    # Get focus model
    focus_model = miq.get_classifier()

    pos_list = pos.PositionList()

    # make z position array
    cur_pos = mmc.getPosition()
    start_pos = cur_pos + total_z/2
    end_pos = cur_pos - total_z/2
    num_steps = (start_pos-end_pos) / delta_z
    z = np.linspace(start_pos, end_pos, num_steps)

    cam = utils.start_cam()

    # Go to the first x,y position 
    pos.set_pos(mmc, x=xy_points[0].x, y=xy_points[0].y)

    preds = []
    for curr_z in z:
        pos.set_pos(mmc, z=curr_z)
        frame = cam.get_frame(exp_time=1).reshape(cam.sensor_size[::-1])
        preds.append(focus_model.score(frame))
    # find the index of the min focus prediction
    best_focus_index = np.argmin(preds)
    # append to the PositionList 
    last_z = z[best_focus_index]
    sp = pos.StagePosition(x=xy_points[0].x, y=xy_points[0].y,
                            z=last_z)
    pos_list.append(sp)

    z_range = range(-35, 35, 10)

    for i, posit in enumerate(xy_points):
        # We already did the first point
        if i == 0:
            best_focus_index = 0
            continue
        
        preds = []
        # Go to the next x,y position with the previous best focus 
        pos.set_pos(mmc, x=posit.x, y=posit.y, z=last_z)
        # frame = cam.get_frame(exp_time=1).reshape(cam.sensor_size[::-1])
        # first_score = focus_model.score(frame)
        
        if best_focus_index > 3.5:
            # Reverse the order of the list
            z_range = z_range[::-1]

        # Build list from last position
        # in order that makes sense 
        z_list = [(last_z+i) for i in z_range]

        for j, curr_z in enumerate(z_list):
            pos.set_pos(mmc, z=curr_z)
            frame = cam.get_frame(exp_time=1).reshape(cam.sensor_size[::-1])
            preds.append(focus_model.score(frame))

            if j > 0:
                if preds[j] > preds[j-1]:
                    # Focus got worse
                    break

        
        # find the index of the min focus prediction
        best_focus_index = np.argmin(preds)
        # append to the PositionList 
        last_z = z_list[best_focus_index]
        sp = pos.StagePosition(x=posit.x, y=posit.y,
                                z=last_z)
        pos_list.append(sp)
    
    utils.close_cam(cam)
    total_time = time.time() - start_time
    # print ('Completed focus in', total_time, 'seconds')
    return pos_list