def example_01(): folder_input = 'images/ex_match/' folder_output = 'images/output/' if not os.path.exists(folder_output): os.makedirs(folder_output) else: tools_IO.remove_files(folder_output) #img_base = cv2.imread(folder_input + 'boxes.png') #img_tmpl = cv2.imread(folder_input + 'pattern2.png') img_base = cv2.imread(folder_input + 'beach.png', 0) img_tmpl = cv2.imread(folder_input + 'waldo.png', 0) hitmap_2d = tools_alg_match.calc_hit_field_basic(img_base, img_tmpl) cv2.imwrite(folder_output + 'hitmap_basic.png', hitmap_2d) cv2.imwrite(folder_output + 'hitmap_jet.png', tools_image.hitmap2d_to_jet(hitmap_2d)) cv2.imwrite(folder_output + 'hitmap_viridis.png', tools_image.hitmap2d_to_viridis(hitmap_2d)) hitmap_2d = tools_alg_match.calc_hit_field(img_base, img_tmpl) cv2.imwrite(folder_output + 'hitmap_advanced.png', hitmap_2d) cv2.imwrite(folder_output + 'hitmap_advanced_jet.png', tools_image.hitmap2d_to_jet(hitmap_2d)) cv2.imwrite(folder_output + 'hitmap_advanced_viridis.png', tools_image.hitmap2d_to_viridis(hitmap_2d)) return
def draw_hits(path, coordinates, chains_hor, chains_ver, hitmap_2d): hitmap_RGB_gre, hitmap_RGB_jet = tools_image.hitmap2d_to_jet(hitmap_2d) for each in chains_hor: for i in range(0, len(each) - 1): hitmap_RGB_gre = tools_draw_numpy.draw_line( hitmap_RGB_gre, coordinates[each[i]][0], coordinates[each[i]][1], coordinates[each[i + 1]][0], coordinates[each[i + 1]][1], [128, 128, 0]) for each in chains_ver: for i in range(0, len(each) - 1): hitmap_RGB_gre = tools_draw_numpy.draw_line( hitmap_RGB_gre, coordinates[each[i]][0], coordinates[each[i]][1], coordinates[each[i + 1]][0], coordinates[each[i + 1]][1], [128, 128, 0]) for i in range(0, coordinates.shape[0]): hitmap_RGB_gre = tools_draw_numpy.draw_circle(hitmap_RGB_gre, coordinates[i][0], coordinates[i][1], 4, [255, 32, 0]) return hitmap_RGB_gre, hitmap_RGB_jet
def estimate_pattern_position(filename_in, folder_out, pattern_width, pattern_height): scale = 4.0 image_base = cv2.cvtColor(cv2.imread(filename_in), cv2.COLOR_RGB2BGR) image_base_scaled = cv2.resize( image_base, (int(image_base.shape[1] / scale), int(image_base.shape[0] / scale))) tools_IO.remove_files(folder_out) for shift_row in range(0, pattern_height): for shift_col in range(0, pattern_width): row = int(image_base.shape[0] / 2) + shift_row col = int(image_base.shape[1] / 2) + shift_col image_pattern = image_base[ int(row - pattern_height / 2):int(row + pattern_height / 2), int(col - pattern_width / 2):int(col + pattern_width / 2), :] image_pattern_scaled = cv2.resize( image_pattern, (int(image_pattern.shape[1] / scale), int(image_pattern.shape[0] / scale))) hitmap_2d = tools_alg_match.calc_hit_field(image_base_scaled, image_pattern_scaled) hitmap_RGB_jet = tools_image.hitmap2d_to_jet(-hitmap_2d) e = entropy(hitmap_2d.flatten() / 255.0) toimage(hitmap_RGB_jet).save(folder_out + ('hit_%1.3f_%03d_%03d.bmp' % (e, shift_row, shift_col))) toimage(image_pattern_scaled).save(folder_out + ('pat_%03d_%03d.bmp' % (shift_row, shift_col))) return
def get_disparity_from_matches(rows, cols, coord1, coord2, disp_v1, disp_v2, disp_h1, disp_h2): value = [] for i in range(0, coord1.shape[0]): dx = 255 - 255 * (coord2[i, 0] - coord1[i, 0] - disp_h1) / (disp_h2 - disp_h1) value.append(dx) value = numpy.array(value) displ = interpolate_image_by_matches(rows, cols, coord1, value) dispr = interpolate_image_by_matches(rows, cols, coord2, value) displ_jet = tools_image.hitmap2d_to_jet(displ) dispr_jet = tools_image.hitmap2d_to_jet(dispr) return displ_jet, dispr_jet
def build_hitmap(folder_in, filename_in_field, filename_in_pattern, folder_out): image_field = cv2.imread(folder_in + filename_in_field) img_pattern = cv2.imread(folder_in + filename_in_pattern) hitmap_2d = tools_alg_match.calc_hit_field(image_field, img_pattern) cv2.imwrite(folder_out + 'hitmap_advanced.png', hitmap_2d) cv2.imwrite(folder_out + 'hitmap_advanced_jet.png', tools_image.hitmap2d_to_jet(hitmap_2d)) cv2.imwrite(folder_out + 'hitmap_advanced_viridis.png', tools_image.hitmap2d_to_viridis(hitmap_2d)) return
def visualize_matches_map(disp_col, disp_v1, disp_v2, disp_h1, disp_h2): image_disp_hor = numpy.zeros( (disp_col.shape[0], disp_col.shape[1])).astype(numpy.float32) image_disp_hor[:, :] = disp_col[:, :] image_disp_hor = (image_disp_hor - disp_h1) * 255 / (disp_h2 - disp_h1) image_disp_hor_RGB_jet = tools_image.hitmap2d_to_jet( -image_disp_hor.astype(numpy.uint8)) # image_disp_hor_RGB_gre, image_disp_hor_RGB_jet = tools_image.hitmap2d_to_viridis(image_disp_hor.astype(numpy.uint8)) image_disp_hor_RGB_jet[disp_col >= disp_h2 + 1] = [128, 128, 128] image_disp_hor_RGB_jet[disp_col <= disp_h1 - 1] = [128, 128, 128] return image_disp_hor_RGB_jet
def visualize_matches(image, disp_row,disp_col,disp_v1, disp_v2, disp_h1, disp_h2): res=image.copy() image_disp_hor = numpy.zeros((image.shape[0],image.shape[1])).astype(numpy.float32) image_disp_hor [:,:]=disp_col[:,:] for row in range(0,image.shape[0]): for col in range(0, image.shape[1]): r = row + disp_row[row, col] c = col + disp_col[row, col] r= numpy.maximum(0,numpy.minimum(image.shape[0]-1,r)) c= numpy.maximum(0,numpy.minimum(image.shape[1]-1,c)) res[row,col] =image[r,c] image_disp_hor=(image_disp_hor-disp_h1)*255/(disp_h2-disp_h1) image_disp_hor_RGB_jet = tools_image.hitmap2d_to_jet(image_disp_hor.astype(numpy.uint8)) return res,image_disp_hor_RGB_jet
def get_labeled_image(self, mask): kernel = numpy.ones((3, 3), numpy.uint8) mask = cv2.erode(mask, kernel, iterations=1) mask = cv2.dilate(mask, kernel, iterations=1) #labels = (skimage.measure.label(mask, connectivity=2)).astype(numpy.float) #nb_labels = int(labels.max()+1) labels, nb_labels = ndimage.label(mask) sizes = ndimage.sum(mask, labels, range(nb_labels + 1)) #H = numpy.histogram(labels.flatten(),bins=numpy.arange(0,nb_labels+1)) labels_new = 0 * labels.copy() for l in range(nb_labels): labels_new[labels == l] = sizes[l] labels = labels_new.astype(numpy.float) labels *= 255 / labels.max() labels = tools_image.hitmap2d_to_jet(labels) return labels
def E2E_detect_patterns(folder_in, filename_in_field, folder_out, pattern_height, pattern_width, max_period, window_size=25, step=10): print(folder_in) image = cv2.imread(folder_in + filename_in_field) image_gray = tools_image.desaturate(image) peak_factor = 0.25 cut_factor = 0.95 #pairs hitmap if os.path.isfile(folder_out + 'hitmap.png'): hitmap = cv2.imread(folder_out + 'hitmap.png', 0) tools_IO.remove_files(folder_out) cv2.imwrite(folder_out + '%d-hitmap_00.png' % (window_size), tools_image.hitmap2d_to_jet(hitmap)) cv2.imwrite(folder_out + 'hitmap.png', hitmap) else: tools_IO.remove_files(folder_out) hitmap, pattern_height, pattern_width = get_self_hits( image, max_period, pattern_height, pattern_width, window_size, step, folder_debug=folder_out) cv2.imwrite(folder_out + '%d-hitmap_00.png' % (window_size), tools_image.hitmap2d_to_jet(hitmap)) cv2.imwrite(folder_out + 'hitmap.png', hitmap) #candidates to patterns coord = tools_alg_grid_templates.find_local_peaks( hitmap, int(pattern_height * peak_factor), int(pattern_width * peak_factor)) for i in range(0, coord.shape[0]): image_gray = tools_draw_numpy.draw_circle(image_gray, coord[i, 0], coord[i, 1], 5, (0, 64, 255)) cv2.imwrite(folder_out + '%d-hits_00.png' % window_size, image_gray) patterns_candidates = tools_alg_grid_templates.coordinates_to_images( image, coord, cut_factor * pattern_height, cut_factor * pattern_width) hitmap_candidates = tools_alg_grid_templates.coordinates_to_images( hitmap, coord, cut_factor * pattern_height, cut_factor * pattern_width) #best pattern pattern = get_best_pattern(patterns_candidates, hitmap_candidates) cv2.imwrite(folder_out + '%d-pattern_00.png' % (window_size), pattern) #template match with pattern hitmap = tools_alg_match.calc_hit_field_basic(image, pattern, rotation_tol=10, rotation_step=0.5) cv2.imwrite(folder_out + '%d-hitmap_01_jet.png' % (window_size), tools_image.hitmap2d_to_jet(hitmap)) cv2.imwrite(folder_out + '%d-hitmap_01_vrd.png' % (window_size), tools_image.hitmap2d_to_viridis(hitmap)) #visualize hits min_value = 200 coord, image_miss = tools_alg_grid_templates.find_local_peaks_greedy( hitmap, int(1.0 * pattern_height), int(1.0 * pattern_width), min_value) image_gray = tools_image.desaturate(image) for i in range(0, coord.shape[0]): value = int(85 * (hitmap[coord[i, 0], coord[i, 1]] - min_value) / (255 - min_value)) image_gray = tools_draw_numpy.draw_circle( image_gray, coord[i, 0], coord[i, 1], 9, tools_image.hsv2bgr((value, 255, 255))) image_gray_and_miss = tools_image.put_layer_on_image(image_gray, image_miss, background_color=(0, 0, 0)) image_gray_and_miss = tools_image.blend_avg(image_gray, image_gray_and_miss, weight=0.5) cv2.imwrite(folder_out + '%d-hits_01.png' % window_size, image_gray_and_miss) #reproject pattern image_gen = generate_input(image, coord, pattern) cv2.imwrite(folder_out + '%d-gen.png' % window_size, image_gen) return # ----------------------------------------------------------------------------------------------------------------------