def displacement_compute_worker(index, data_dict):

    crops = data_dict['crops']
    targets = data_dict['targets']

    crop_ref = cvt.color_to_gray(crops[index-1])
    crop = cvt.color_to_gray(crops[index])

    peak_loc, peak_val = get_phasecorr_peak(crop_ref, crop, 100)

    print ("Translation of", pth.basename(targets[index]),
           "relative to", pth.basename(targets[index-1]), "is",
           peak_loc, "value:", peak_val)

    return (index, peak_loc)
Example #2
0
def pairwise_displacement_worker(index, data_dict, kind):

    crops = data_dict['crops']
    targets = data_dict['targets']

    crop_previous = crops[index - 1]
    crop = crops[index]

    peak_loc, peak_val = get_phasecorr_peak(crop_previous, crop, 100)

    debug_log("Translation of",
              os.path.basename(targets[index]), "relative to",
              os.path.basename(targets[index - 1]), "is", peak_loc, "value:",
              peak_val)

    return (kind, index, peak_loc)
Example #3
0
def patch_body_corner_inmem(body, body_mat, corner_rot, corner_mat):

    print "Body dim:", body.shape
    print "Corner dim:", corner_rot.shape

    corner_shape = corner_rot.shape[:2]
    c_h, c_w = corner_shape
    body_mat[2, :2] = 0
    body_mat[:2, 2] = 0
    corner_mat[:2, 2] = 0
    corner_mat[2, :2] = 0

    corner_body_rotation = np.dot(body_mat, np.linalg.inv(corner_mat))
    corner = cv2.warpPerspective(corner_rot, corner_body_rotation,
                                 tuple(corner_rot.shape[:2][::-1]))

    g_body_chunk = cvt.color_to_gray(body[:c_h, :c_w, :])
    bl_left = find_left_border(g_body_chunk, c_w)
    bl_top = find_top_border(g_body_chunk, c_h)
    blacks = np.r_[bl_top, bl_left]
    chunk_dim = np.minimum(2 * blacks, corner_shape)

    g_body_chunk = g_body_chunk[:chunk_dim[0], :chunk_dim[1]]
    g_corner_chunk = cvt.color_to_gray(corner[:chunk_dim[0], :chunk_dim[1], :])

    corr_peak, peak_val = dsl.get_phasecorr_peak(g_body_chunk, g_corner_chunk)
    mat = dsl.get_translation_matrix(corr_peak)[:2]
    corner_trans = dsl.warp_image(corner[:chunk_dim[0], :chunk_dim[1], :], mat,
                                  tuple(g_body_chunk.shape[::-1]))

    blend_area = np.minimum((1.5 * blacks).astype(int), corner_shape)

    body_blend_area = body[:blend_area[0], :blend_area[1], :]
    corner_blend_area = corner_trans[:blend_area[0], :blend_area[1], :]

    weight_gen = lp2d.CornerDirichlet(blend_area[1], blend_area[0], bl_left,
                                      bl_top)
    weight_gen.set_boundaries([[1, 1, 0], [1, 1, 0]])
    solu, residuals = weight_gen.solve()
    corner_weight = weight_gen.get_solution()
    body_weight = 1 - corner_weight
    blend = np.uint8(corner_blend_area * corner_weight[:, :, np.newaxis] +
                     body_blend_area * body_weight[:, :, np.newaxis])
    body[:blend_area[0], :blend_area[1], :] = blend

    return body
Example #4
0
def compute_displacements_direct(crops, reference_index):

    displacements = []
    crop_ref = crops[reference_index]

    for i, crop in enumerate(crops):

        if i == reference_index:
            displacements.append(np.r_[0, 0])
            continue

        debug_log("Computing translation of", i, "relative to", reference_index)
        peak_loc, peak_val = get_phasecorr_peak(crop_ref, crop, 100)

        displacements.append(peak_loc)
        debug_log("Translation (row, col) is", peak_loc, "value:", peak_val)

    return displacements
Example #5
0
def compute_displacements_direct(crops, reference_index, use_old=False):

    displacements = []
    crop_ref = crops[reference_index]

    for i, crop in enumerate(crops):

        if i == reference_index:
            displacements.append(np.r_[0, 0])
            continue

        print "Computing translation (row, col) of", i, "relative to", reference_index, 

        if use_old:
            peak_loc, peak_val = get_phasecorr_peak__old(crop_ref, crop)
        else:
            peak_loc, peak_val = get_phasecorr_peak(crop_ref, crop, 100)

        displacements.append(peak_loc)
        print ":", peak_loc, "value:", peak_val

    return displacements
Example #6
0
def main():

    opt = process_command_line()
    print opt

    ensure_dir(opt.work_dir)

    targets = opt.files
    first_img = opt.files[0]
    basename_ref = os.path.basename(first_img)

    img1 = cvt.file_to_cv2(first_img)
    img1_crop = cvt.color_to_gray(crop_center_chunk(img1, opt.crop_size))
    img1_bb = get_bounding_box(img1.shape[:2])

    all_bboxes = [
        img1_bb,
    ]
    relative_displacements = []

    # Get pairwise relative displacements
    for img_file in targets[1:]:

        basename = os.path.basename(img_file)
        img2 = cvt.file_to_cv2(img_file)
        img2_crop = cvt.color_to_gray(crop_center_chunk(img2, opt.crop_size))

        debug_log("Computing translation of", basename, "relative to",
                  basename_ref)

        peak_loc, peak_val = get_phasecorr_peak(img1_crop, img2_crop, 100)
        debug_log("Translation is", peak_loc, "value:", peak_val)

        relative_displacements.append(peak_loc)
        all_bboxes.append(get_bounding_box(img2.shape[:2]))

        img1, img1_crop, basename_ref = img2, img2_crop, basename

    del img1, img2, img1_crop, img2_crop

    # Determine largest bounding box
    bboxes_area = np.array(
        [get_bounding_box_area(bbox) for bbox in all_bboxes])
    largest_area = np.argmax(bboxes_area)
    largest_bbox = all_bboxes[largest_area]
    target_size = get_bounding_box_size(largest_bbox)
    reference = targets[largest_area]
    basename_ref = os.path.basename(reference)

    print "disps:", relative_displacements
    debug_log("Largest area image is", reference, "({})".format(largest_area))

    # Propagate displacements
    pre_aligned_files = []
    for i, img_file in enumerate(targets):

        # Displacements are applied relative to largest bbox
        if i == largest_area:
            pre_aligned_files.append(reference)
            continue

        basename = os.path.basename(img_file)
        img = cvt.file_to_cv2(img_file)

        # displacement[i] = pos[i+1] - pos[i]
        if largest_area < i:
            disp_chain = range(largest_area, i)
            direction = 1
        else:
            disp_chain = range(i, largest_area)
            direction = -1

        total_displacement = direction * sum(relative_displacements[j]
                                             for j in disp_chain)
        debug_log("Displacement from", reference, "to", img_file, "is",
                  total_displacement)
        print "dir", direction, "; chain", disp_chain
        warp_matrix = get_translation_matrix(total_displacement)[:2]

        img_aligned = align_image(img, warp_matrix, target_size)
        aligned_file = os.path.join(opt.work_dir, "pre-" + basename)
        success = cvt.cv2_to_file(img_aligned, aligned_file)
        if success:
            pre_aligned_files.append(aligned_file)

        result = "done" if success else "failed"
        debug_log("Alignment of", img_file, "into", aligned_file, result)

    common_box = intersect_bounding_boxes(target_size, all_bboxes)

    for fi_aligned in pre_aligned_files:
        debug_log("Cropping", fi_aligned, newline=False)
        aligned = cvt.file_to_cv2(fi_aligned)
        cropped = crop_to_bounding_box(aligned, common_box)

        cf_name = (("reg-" + basename_ref) if fi_aligned == reference else
                   os.path.basename(fi_aligned).replace("pre-", "reg-"))
        cropped_file = os.path.join(opt.work_dir, cf_name)
        success = cvt.cv2_to_file(cropped, cropped_file)

        if success:
            center_crop = crop_center_chunk(cropped, 1024)
            center_crop_name = "crop-" + cf_name.replace(".png", ".jpg")
            center_crop_file = os.path.join(opt.work_dir, center_crop_name)
            cvt.cv2_to_file(center_crop, center_crop_file)

            if not opt.keep_uncropped and fi_aligned != reference:
                os.remove(fi_aligned)

        result = "done" if success else "failed"
        print(result)
Example #7
0
def main():

    opt = process_command_line()
    print opt

    ensure_dir(opt.work_dir)

    band = opt.crop_size

    basename_ref = os.path.basename(opt.reference)
    img1 = cvt.file_to_cv2(opt.reference)
    img1_bb = get_bounding_box(img1.shape)
    img1_br = img1_bb[1]
    img1_crop_bb = [(img1_br - band) / 2, (img1_br + band) / 2]
    target_size = get_bounding_box_size(img1_bb)

    img1_crop = cvt.color_to_gray(crop_to_bounding_box(img1, img1_crop_bb))
    """
    Using crop chunks centered at each images' center impedes resolving
    the image-to-image displacement
    """
    #img1_crop = cvt.color_to_gray(crop_center_chunk(img1, band))

    all_bboxes = [
        img1_bb.tolist(),
    ]
    pre_aligned_files = [
        opt.reference,
    ]

    # trainImages
    targets = sorted(set(opt.files) - set([
        opt.reference,
    ]))
    for img_file in targets:

        basename = os.path.basename(img_file)
        img2 = cvt.file_to_cv2(img_file)
        img2_crop = cvt.color_to_gray(crop_to_bounding_box(img2, img1_crop_bb))
        #img2_crop = cvt.color_to_gray(crop_center_chunk(img2, band))

        debug_log("Computing translation of", basename, "relative to",
                  basename_ref)

        peak_loc, peak_val = get_phasecorr_peak(img1_crop, img2_crop, 100)
        debug_log("Translation is", peak_loc, "value:", peak_val)
        warp_matrix = get_translation_matrix(peak_loc)[:2]

        img2_aligned = warp_image(img2, warp_matrix, target_size)
        img2_adj_bb = get_adjusted_bounding_box(img1_bb,
                                                get_bounding_box(img2.shape),
                                                warp_matrix)
        aligned_file = os.path.join(opt.work_dir, "pre-" + basename)
        cvt.cv2_to_file(img2_aligned, aligned_file)
        all_bboxes.append(img2_adj_bb)
        pre_aligned_files.append(aligned_file)

        debug_log("Alignment of", img_file, "done")

    common_box = intersect_bounding_boxes(all_bboxes)

    for fi_aligned in pre_aligned_files:
        debug_log("Cropping", fi_aligned, newline=False)
        aligned = cvt.file_to_cv2(fi_aligned)
        cropped = crop_to_bounding_box(aligned, common_box)

        cf_name = (("reg-" + basename_ref) if fi_aligned == opt.reference else
                   os.path.basename(fi_aligned).replace("pre-", "reg-"))
        cropped_file = os.path.join(opt.work_dir, cf_name)
        success = cvt.cv2_to_file(cropped, cropped_file)

        if success:
            center_crop = crop_center_chunk(cropped, 1024)
            center_crop_name = "crop-" + cf_name.replace(".png", ".jpg")
            center_crop_file = os.path.join(opt.work_dir, center_crop_name)
            cvt.cv2_to_file(center_crop, center_crop_file)

            if not opt.keep_uncropped and fi_aligned != opt.reference:
                os.remove(fi_aligned)

        result = "done" if success else "failed"
        print(result)