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
Beispiel #2
0
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
Beispiel #3
0
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 add_box_automated(self):
        box_current, classIDs = self.get_box_objects(self.current_frame_ID,
                                                     self.current_markup_ID)
        if len(box_current) != 1: return
        box = numpy.array(box_current[0]).astype(int)

        image_current = cv2.imread(self.folder_in +
                                   self.filenames[self.current_frame_ID])

        cur_left = min(box[0], box[2])
        cur_right = max(box[0], box[2])
        cur_top = min(box[1], box[3])
        cur_bottom = max(box[1], box[3])

        R = 100
        next_left = max(0, cur_left - R)
        next_right = min(cur_right + R, image_current.shape[1])
        next_top = max(0, cur_top - R)
        next_bottom = min(cur_bottom + R, image_current.shape[0])

        if self.image_small is None:
            self.image_small = image_current[cur_top:cur_bottom,
                                             cur_left:cur_right]

        image_next = cv2.imread(self.folder_in +
                                self.filenames[self.current_frame_ID + 1])
        image_large = image_next[next_top:next_bottom, next_left:next_right]

        hitmap = tools_alg_match.calc_hit_field(image_large, self.image_small)
        hit = unravel_index(hitmap.argmax(), hitmap.shape)

        box = numpy.array([
            next_left + hit[1] - (cur_right - cur_left) // 2,
            next_top + hit[0] - (cur_bottom - cur_top) // 2,
            next_left + hit[1] + (cur_right - cur_left) // 2,
            next_top + hit[0] + (cur_bottom - cur_top) // 2
        ])

        self.Boxes.add(self.current_frame_ID + 1, box, self.current_markup_ID)
        #image_small_next = image_next[box[1]:box[3], box[0]:box[2]]
        #cv2.imwrite(self.folder_in + 'hit.png',tools_image.hitmap2d_to_jet(hitmap))
        #cv2.imwrite(self.folder_in + 'small.png', image_small)
        #cv2.imwrite(self.folder_in + 'large.png', image_large)
        #cv2.imwrite(self.folder_in + 'image_small_next.png', image_small_next)

        return
def get_hits(image_base, image_pattern,grid=0.1,path_debug=''):
    hitmap_2d = tools_alg_match.calc_hit_field(image_base, image_pattern)
    coordinates, chains_hor, chains_ver = detect_peaks_2d(image_base,image_pattern,hitmap_2d, grid=0.05,path_debug=path_debug)
    return coordinates,chains_hor, chains_ver,hitmap_2d, 0