def main(): opt = process_command_line() print opt ensure_dir(opt.work_dir) basenames = [os.path.basename(fi) for fi in opt.files] # Initiate SIFT detector sift = cv2.xfeatures2d.SIFT_create( contrastThreshold=opt.contrast_threshold, sigma=opt.sigma) # queryImage #img1 = cvt.image_load_resize(opt.files[0], opt.reduction) img1 = cvt.file_to_cv2(opt.files[0]) img1_subg = cvt.image_resize(cvt.color_to_gray(img1), opt.reduction) # trainImage #img2 = cvt.image_load_resize(opt.files[1], opt.reduction) img2 = cvt.file_to_cv2(opt.files[1]) img2_subg = cvt.image_resize(cvt.color_to_gray(img2), opt.reduction) # find the keypoints and descriptors debug_log("Gather features of images") kp1, des1 = sift.detectAndCompute(img1_subg, None) kp2, des2 = sift.detectAndCompute(img2_subg, None) ref_package = (kp1, des1) target_package = (kp2, des2) """ Direct img2_aligned = match_and_align(ref_package, target_package, opt.min_matches) success = (img2_aligned is not None) if success: cvt.cv2_to_file(img1, os.path.join(opt.work_dir, basenames[0])) cvt.cv2_to_file(img2, os.path.join(opt.work_dir, basenames[1])) aligned_file = os.path.join(opt.work_dir, "reg-" + basenames[1]) cvt.cv2_to_file(img2_aligned, aligned_file) """ warp_matrix = match_get_transf(ref_package, target_package, opt.min_matches) print("Warp matrix:") print(warp_matrix) success = (warp_matrix is not None) if success: matrix_scaling = get_size_scaling_matrix(opt.reduction) scaled_wm = matrix_scaling * warp_matrix #scaled_wm = np.dot(matrix_scaling, warp_matrix) img2_aligned = align_image(img2, scaled_wm, img1.shape[:2][::-1]) aligned_file = os.path.join(opt.work_dir, "reg-" + basenames[1]) cvt.cv2_to_file(img2_aligned, aligned_file) result = "done" if success else "failed" debug_log("Alignment of", opt.files[1], result)
def main(): opt = process_command_line() print opt work_dir = tempfile.mkdtemp(dir=opt.work_dir) work_dirs = setup_directories(work_dir) debug_log("JOB", "Staging files in", work_dir) output_file = os.path.abspath(opt.output_file) img_path, img_fname_ext = os.path.split(output_file) img_fname, img_ext = os.path.splitext(img_fname_ext) if opt.job_type == 'row': success = assemble_row(work_dirs, opt.files, output_file) elif opt.job_type == 'col': success = assemble_column(work_dirs, opt.files, output_file) elif opt.job_type == 'image-by-rows': success = assemble_image_by_rows(work_dirs, opt.files, output_file, opt.threads) elif opt.job_type == 'image-by-blocks': success = assemble_image_by_blocks(work_dirs, opt.files, output_file, opt.threads) elif opt.job_type == 'two-step': success = assemble_twostep(work_dirs, opt.files, output_file) elif opt.job_type == 'multi-step': success = assemble_multistep(work_dirs, opt.files, output_file, keep_files=opt.keep_work_dir, threads=opt.threads, overlap=opt.tile_overlap) elif opt.job_type == 'block-links': success, img, _ = assemble_blocklink(work_dirs, opt.files, output_file, threads=opt.threads, keep_files=opt.keep_work_dir, overlap=opt.tile_overlap, do_contrast_stretch=opt.contrast) if success and opt.also_save_reduced < 100: img_path, img_fname = os.path.split(output_file) reduced_fname = os.path.splitext(img_fname)[0] + ".small.jpg" reduced_fpath = os.path.join(img_path, reduced_fname) reduced_img = image_resize(img, opt.also_save_reduced) cv2_to_file(reduced_img, reduced_fpath) if success and not opt.keep_work_dir: shutil.rmtree(work_dir) return shell_retcode(success)
def apply_displacements(target_dir, angle, displacements, work_dirs, kind, do_register, blur, files_in): file_out = os.path.join(target_dir, "full-{}.{}".format(angle, img_data_fmt)) if not pth.isfile(file_out): kind_dir = ensure_dir(pth.join(work_dirs['temp'], kind)) my_work_dirs = setup_directories(pth.join(kind_dir, str(angle))) if blur is not None: new_dir = blankfield_guard(files_in, kind, my_work_dirs['in'], blur) else: new_dir = pth.dirname(files_in[0]) coords_fi = pth.join(new_dir, "tiles.txt") TileConfigurator().generate(displacements, coords_fi) success, img, _ = fiji_grid_fuse(my_work_dirs, coords_fi, file_out, do_crop=False) shutil.rmtree(my_work_dirs['in']) else: debug_log("Ensemble", file_out, "already exists.") img = None cropped_fpath = os.path.join(target_dir, "center-crop-{}.jpg".format(angle)) if not pth.isfile(cropped_fpath): load_img = img if img is not None else file_to_cv2(file_out) cropped_img = crop_center_chunk(load_img, 1024) cv2_to_file(cropped_img, cropped_fpath) else: debug_log("Center-cropped chunk", cropped_fpath, "already exists.") reduced_fpath = os.path.join(target_dir, "full-{}.small.jpg".format(angle)) if not pth.isfile(reduced_fpath): load_img = img if img is not None else file_to_cv2(file_out) reduced_img = image_resize(load_img, 30) cv2_to_file(reduced_img, reduced_fpath) else: debug_log("Reduced size image", reduced_fpath, "already exists.") if not do_register: pyramid_archive = pth.join(target_dir, "pyramid-{}.tar".format(angle)) if not pth.isfile(pyramid_archive): make_pyramid(img, pyramid_archive, "{}-{}.dzi".format(kind, angle)) else: debug_log("Pyramid archive", pyramid_archive, "already exists.") return file_out
def file_image_preprocess(source_file, target_file, reduction_percentage=100, blankfield=None): if reduction_percentage == 100 and blankfield is None: shutil.copyfile(source_file, target_file) else: image = image_resize(file_to_cv2(source_file), reduction_percentage) corrected = (image if blankfield is None else blankfield_linear_correct(image, blankfield)) cv2_to_file(corrected, target_file, 99) return target_file
def main(): opt = process_command_line() print opt ensure_dir(opt.work_dir) # Feature detector sift_options = dict(sigma=opt.sigma, contrastThreshold=opt.contrast_threshold) sift = cv2.xfeatures2d.SIFT_create(**sift_options) strip_width = opt.strip_width # Reference image basename_ref = os.path.basename(opt.reference) image_ref = cvt.file_to_cv2(opt.reference) imgref_subg = cvt.color_to_gray(image_ref) target_size = imgref_subg.shape[::-1] imgref_clearance = -1 kp_ref, des_ref = [], None ref_max_clearance = min(target_size) - 2 * strip_width # Target images 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_subg = cvt.color_to_gray(img2) keypoints = [] descriptors = [] s_clearance = opt.clearance tgt_max_clearance = min(img2_subg.shape) - 2 * strip_width converged = False while not converged: if s_clearance > imgref_clearance: imgref_clearance = s_clearance if ref_max_clearance < imgref_clearance: debug_log("Cannot expand feature extraction for reference", basename_ref, "; using current data") else: imgref_bands = get_image_bands(imgref_subg, strip_width, imgref_clearance) debug_log("Gather features of reference image", basename_ref, "with w =", strip_width, "and c =", imgref_clearance) kp_, des_ = get_features_on_bands(imgref_subg, imgref_bands, sift) kp_ref.extend(kp_) des_ref = (des_ if des_ref is None else np.vstack( [des_ref, des_])) ref_package = (kp_ref, des_ref) if tgt_max_clearance < s_clearance: debug_log("Cannot expand feature extraction for target", basename_ref, "; Aborting") success = False break debug_log("Gather features of image", basename, "with w =", strip_width, "and c =", s_clearance) img2_bands = get_image_bands(img2_subg, strip_width, s_clearance) kp_tgt, des_tgt = get_features_on_bands(img2_subg, img2_bands, sift) keypoints.extend(kp_tgt) descriptors.append(des_tgt) target_package = (keypoints, np.vstack(descriptors)) src_pts_flann, dst_pts_flann = match_points_flann( ref_package, target_package, opt.min_matches) similarity = get_transf_similarity(src_pts_flann, dst_pts_flann) scale_change_pct = 100 * abs(similarity.scale - 1) success = (scale_change_pct < 1) converged = success debug_log("Similarity transform: scale change pct.:", scale_change_pct, "Trl:", similarity.translation, "Rot:", similarity.rotation) if not success: debug_log("Not good enough matching achieved.", "Increasing bands width") s_clearance += strip_width continue transform = SimilarityTransform(scale=1, rotation=similarity.rotation, translation=similarity.translation) warp_matrix = get_transf_homography(src_pts_flann, dst_pts_flann) print( "Compare Flann matrices:\n Homography:\n{}\n Similarity:\n{}". format(warp_matrix, transform.params)) """ BFmatcher test src_pts_bf, dst_pts_bf = match_points_bf(ref_package, target_package, opt.min_matches) warp_matrix_bf = get_transf_homography(src_pts_bf, dst_pts_bf) similarity = get_transf_similarity(src_pts_bf, dst_pts_bf, min_samples=2) transform_bf = SimilarityTransform(scale=1, rotation=similarity.rotation, translation=similarity.translation) print("Compare BF matrices:\n Homography:\n{}\n Similarity:\n{}".format(warp_matrix_bf, transform_bf.params)) """ img2_aligned = align_image(img2, transform.params, target_size) aligned_file = os.path.join(opt.work_dir, "reg-" + basename) success = cvt.cv2_to_file(img2_aligned, aligned_file) if success: center_crop = crop_center_chunk(img2_aligned, 1024) center_crop_name = "crop-" + basename.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 opt.small_images: small = cvt.image_resize(img2_aligned, 30) small_name = "small-" + basename.replace(".png", ".jpg") small_file = os.path.join(opt.work_dir, small_name) cvt.cv2_to_file(small, small_file) result = "done" if success else "failed" debug_log("Alignment of", img_file, result)
def main(): opt = process_command_line() print opt ensure_dir(opt.work_dir) # Feature detector sift_options = dict(sigma=opt.sigma, contrastThreshold=opt.contrast_threshold) base_csize = opt.crop_size # Reference image basename_ref = os.path.basename(opt.reference) image_ref = cvt.file_to_cv2(opt.reference) imgref_subg = cvt.color_to_gray(image_ref) target_size = imgref_subg.shape[::-1] imgref_csize = -1 kp_ref, des_ref = [], None ref_max_size = min(target_size) # Target images 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_subg = cvt.color_to_gray(img2) keypoints = [] descriptors = [] csize = base_csize tgt_max_size = min(img2_subg.shape) converged = False while not converged: if csize > imgref_csize: imgref_csize = csize if ref_max_size < imgref_csize: debug_log("Cannot expand feature extraction for reference", basename_ref, "; using current data") else: debug_log("Gather features of reference image", basename_ref, "with c =", imgref_csize) imgref_bands = get_center_crop_bounding_box( imgref_subg, imgref_csize) print("Crop box:", imgref_bands) sift = cv2.xfeatures2d.SIFT_create(**sift_options) roi = crop_to_bounding_box(imgref_subg, imgref_bands) #borders = cvt.simple_grayscale_stretch(scharr(roi)) kp_ref, des_ref = sift.detectAndCompute(roi, None) ref_package = (kp_ref, des_ref) if tgt_max_size < csize: debug_log("Cannot expand feature extraction for target", basename_ref, "; Aborting") success = False break debug_log("Gather features of image", basename, "with c =", csize) sift = cv2.xfeatures2d.SIFT_create(**sift_options) roi = crop_to_bounding_box(img2_subg, imgref_bands) #borders = cvt.simple_grayscale_stretch(scharr(roi)) keypoints, descriptors = sift.detectAndCompute(roi, None) target_package = (keypoints, descriptors) src_pts_flann, dst_pts_flann = match_points_flann( ref_package, target_package, opt.min_matches) similarity = get_transf_similarity(src_pts_flann, dst_pts_flann) scale_change_pct = 100 * abs(similarity.scale - 1) debug_log("FLANN Similarity transform: scale change pct.:", scale_change_pct, "Trl:", similarity.translation, "Rot:", similarity.rotation) warp_matrix = get_transf_homography(src_pts_flann, dst_pts_flann) print( "Compare FLANN matrices:\n Homography:\n{}\n Similarity:\n{}". format(warp_matrix, similarity.params)) """ BFmatcher test src_pts_bf, dst_pts_bf = match_points_bf(ref_package, target_package, opt.min_matches) similarity = get_transf_similarity(src_pts_bf, dst_pts_bf, min_samples=2) scale_change_pct = 100*abs(similarity.scale - 1) debug_log("BF Similarity transform: scale change pct.:", scale_change_pct, "Trl:", similarity.translation, "Rot:", similarity.rotation) transform = SimilarityTransform(scale=1, rotation=similarity.rotation, translation=similarity.translation) warp_matrix_bf = get_transf_homography(src_pts_bf, dst_pts_bf) print("Compare BF matrices:\n Homography:\n{}\n Similarity:\n{}".format(warp_matrix_bf, transform.params)) """ success = (scale_change_pct < 1) converged = success if not success: debug_log("Not good enough matching achieved.", "Increasing bands width") csize += base_csize continue transform = SimilarityTransform(scale=1, rotation=similarity.rotation, translation=similarity.translation) img2_aligned = align_image(img2, transform.params, target_size) aligned_file = os.path.join(opt.work_dir, "reg-" + basename) success = cvt.cv2_to_file(img2_aligned, aligned_file) if success: center_crop = crop_center_chunk(img2_aligned, 1024) center_crop_name = "crop-" + basename.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 opt.small_images: small = cvt.image_resize(img2_aligned, 30) small_name = "small-" + basename.replace(".png", ".jpg") small_file = os.path.join(opt.work_dir, small_name) cvt.cv2_to_file(small, small_file) result = "done" if success else "failed" debug_log("Alignment of", img_file, result)
def image_load_resize(image_file, percentage=100): image = cv2.imread(image_file) return image_resize(image)
def assemble_multigrid(directories, files, out_file_name, pre_resize=100, base_cell_size=4, max_threads=cpu_count(), keep_uncropped=False, blankfield_file=None): n_rows, n_cols, row_cells, img_type, _, _, _ = parse_image_grid_list(files) pool = Pool(processes=max_threads) cs = float(base_cell_size) base_cells_dims = np.ceil(np.r_[n_rows, n_cols] / cs).astype(int) cells_dir = ensure_dir(os.path.join(directories['out'], "cells")) cell_jobs = [] cell_files = [] blankfield = (None if blankfield_file is None else image_resize( file_to_cv2(blankfield_file), pre_resize)) digits_row, digits_col = map(get_digits, base_cells_dims) for b_row in range(base_cells_dims[0]): for b_col in range(base_cells_dims[1]): base_name = "{:0{w_r}d}_{:0{w_c}d}".format(b_row, b_col, w_r=digits_row, w_c=digits_col) temp_dirs = setup_directories(os.path.join(cells_dir, base_name)) files = [ file_image_preprocess( row_cells[i][j], os.path.join(temp_dirs['in'], os.path.basename(row_cells[i][j])), pre_resize, blankfield) for i in range(base_cell_size * b_row, min(base_cell_size * (b_row + 1), n_rows)) for j in range(base_cell_size * b_col, min(base_cell_size * (b_col + 1), n_cols)) ] out_file = os.path.join(cells_dir, "{}.{}".format(base_name, img_data_fmt)) cell_jobs.append( pool.apply_async(fiji_grid_stitch, (temp_dirs, files, out_file), { 'pre_copy_files': False, 'keep_uncropped': keep_uncropped })) cell_files.append(out_file) pool.close() pool.join() cell_successes = [job.get() for job in cell_jobs] if all(cell_successes): debug_log("Assembling of base cells into", cells_dir, "is complete.", "Assembling image...") return fiji_grid_stitch(directories, cell_files, out_file_name, keep_uncropped=keep_uncropped) else: return False