Ejemplo n.º 1
0
def find_axis_rotate(im0, im1, angmin, angmax, da):
    """
    Find axis rotation position by 2 images in range of rotation [angmin, angmax, da]

    :param im0:
    :param im1:
    :param angmin:
    :param angmax:
    :param da:
    :return:
    """
    if angmin==angmax:
        return 1.0, angmin

    mean_im0=numpy.mean(im0)
    mean_im1=numpy.mean(im1)
    coeff_corr=((im0-mean_im0)**2).sum()*((im1-mean_im1)**2).sum()
    corr_rot = []
    for ang in numpy.arange(angmin, angmax, da):
        tmp_im0=rotate(im0, ang)
        tmp_im1=rotate(im1, -ang)
        tmp_corr = (tmp_im0 - mean_im0)*(tmp_im1 - mean_im1)
        corr_rot.append(numpy.sqrt(tmp_corr * tmp_corr).sum()/numpy.sqrt(coeff_corr))
    max_ind = numpy.array(corr_rot).argmax()
    rot_val = numpy.arange(angmin, angmax, da)[max_ind]
    logging.info(str.format('Found rotation angle {0:.5} with corr_factor={1:.3}',rot_val,corr_rot[max_ind]))
    return corr_rot,rot_val
Ejemplo n.º 2
0
def fix_axis_position(angle_val, shift_val, normalized_data, pixel_size):
    """
    Inplace fix axis position for list of frames.

    :param angle_val:
    :param shift_val:
    :param normalized_data:
    :param pixel_size:
    """
    for i in range(len(normalized_data)):
        normalized_data[i]['data'] = roll_zeropad(normalized_data[i]['data'], shift=shift_val, axis=1)
        normalized_data[i]['data'] = rotate(normalized_data[i]['data'], angle_val)
        normalized_data[i]['data'] = normalized_data[i]['data'] / pixel_size*1000 #absorption in mm^-1
Ejemplo n.º 3
0
def find_shift_and_rotation(data0, data1, image_process_config, res_folder=None):
    """
    Find axis rotation and shift

    :param data0:
    :param data1:
    :param image_process_config:
    :param res_folder: folder to save diagnostic image
    :return:
    """
    ipc = image_process_config['axis_serch']
    x0 = ipc['horizontal'][0]
    x1 = ipc['horizontal'][1]
    a0 = ipc['rotation'][0]
    a1 = ipc['rotation'][1]
    x0_start=x0
    x1_start=x1
    a0_start=a0
    a1_start=a1

    delta_x = int(x1 - x0) / 2
    xc = int(x0 + x1) / 2
    delta_a = float(a1 - a0) / 2.0
    ac = (a0 + a1) / 2
    angle_step = 0.01
    tmp_data0 = data0
    tmp_data1 = data1

    corr=None
    corr_rot=None

    for i in range(10):
        corr, shift_val = find_axis_hor_shift(tmp_data0, tmp_data1, xc - delta_x, xc + delta_x)

        if shift_val<x0_start:
            shift_val=x0_start
        elif shift_val>x1_start:
            shift_val=x1_start

        tmp_data0 = roll_zeropad(data0, shift=shift_val, axis=1)
        tmp_data1 = roll_zeropad(data1, shift=-shift_val, axis=1)

        corr_rot, rot_ang = find_axis_rotate(tmp_data0, tmp_data1, ac - delta_a, ac + delta_a, angle_step)

        if rot_ang<a0_start:
            rot_ang=a0_start
        elif rot_ang>a1_start:
            rot_ang=a1_start

        if not delta_a==0:
            delta_a = max(0.1, 0.8 * delta_a)
            angle_step = max(0.001, angle_step * 0.8)

        if not delta_x==0:
            delta_x = max(10, int(delta_x * 0.5))

        tmp_data0 = rotate(data0, rot_ang)
        tmp_data1 = rotate(data1, -rot_ang)

        if  xc == shift_val and ac == rot_ang:
            break

        xc = shift_val
        ac = rot_ang

        logging.info(str.format('x={0} delta_x={1} ac={2:.3} delta_a={3:.3} angle_step={4:.3}',
            xc, delta_x, ac, delta_a, angle_step))
    data0 = roll_zeropad(data0, shift=xc, axis=1)
    data1 = roll_zeropad(data1, shift=-xc, axis=1)
    data0 = rotate(data0, ac)
    data1 = rotate(data1, -ac)
    if not res_folder is None:
        save_shifted_image(corr, corr_rot, data0, data1,os.path.join(res_folder,'shifting_pic.png'))
    return {'angle':ac, 'shift':xc}