Exemplo n.º 1
0
    def _tefi(self, note, second_grade_candidates):
        if self.debug:
            print 'Performing tefi'

        rotated_templates = []
        for cshift in range(self.settings['nr_of_rotation_angles']):
            rotation_matrix = cv2.getRotationMatrix2D((self.notesize / 2, self.notesize / 2), -cshift * 10, 1)
            rotated_note = cv2.warpAffine(note, rotation_matrix, (self.notesize, self.notesize))
            masked_rotated_note = flatten_disc(rotated_note, (self.notesize / 2, self.notesize / 2), self.notesize / 2)
            rotated_templates.append(masked_rotated_note)

        candidates_with_correlation = []
        for x, y, cshift in second_grade_candidates:
            for x2 in [x - 1, x, x + 1]:
                for y2 in [y - 1, y, y + 1]:
                    possible_match = imagefuncs.submatrix(self.board, x2, y2, self.notesize)
                    masked_possible_match = flatten_disc(possible_match, (self.notesize / 2, self.notesize / 2), self.notesize / 2)
                    corr = calculate_correlation(rotated_templates[cshift],
                                                 masked_possible_match,
                                                 self.settings['thresh_contrast'],
                                                 self.settings['thresh_brightness'])
                    #print 'correlation with second grade candidate at (%d,%d) is %f' % (x2, y2, corr)
                    candidates_with_correlation.append( (corr, x2, y2) )

        candidates_with_correlation.sort(reverse = True)

        final_match = candidates_with_correlation[0]

        if final_match[0] < self.settings['thresh_confidence']:
            if self.debug:
                print 'no match was found. The best was at (%d,%d) with correlation %f' % (final_match[1], final_match[2], final_match[0])
                imagefuncs.qimshow(imagefuncs.submatrix(self.board, final_match[1], final_match[2], self.notesize))
            return None
        else:
            if self.debug:
                print 'final match is at (%d,%d) with correlation %f' % (final_match[1], final_match[2], final_match[0])
                imagefuncs.qimshow(imagefuncs.submatrix(self.board, final_match[1], final_match[2], self.notesize))
                for corr, x, y in candidates_with_correlation:
                    diff_x = x - final_match[1]
                    diff_y = y - final_match[2]
                    if diff_x * diff_x + diff_y * diff_y > self.notesize * self.notesize:
                        print 'second best match is at (%d,%d) with correlation %f' % (x, y, corr)
                        imagefuncs.qimshow(imagefuncs.submatrix(self.board, x, y, self.notesize))
                        break

            return final_match
Exemplo n.º 2
0
def findsquares(scrumboard, image):
    start_of_done, _ = scrumboard.get_position_from_state('done')

    masked_image = image[:,:start_of_done,:]
    #imagefuncs.qimshow(masked_image)
    notepositions = findnotes(masked_image)

    squares = []
    for pos in notepositions:
        notebitmap = imagefuncs.submatrix(image, pos[0], pos[1], imagefuncs.NOTE_SIZE)
        squares.append(board.Square(notebitmap, (pos[0], pos[1])))

        #imagefuncs.qimshow(['found square', notebitmap])

    return squares
Exemplo n.º 3
0
    def _rafi(self, note, first_grade_candidates):
        if self.debug:
            print 'Performing rafi'

        rafi_masks = []
        rafi_means = []
        for i in range(self.settings['nr_of_rotation_angles']):
            angle = -(math.pi / 2) + (i * 2 * math.pi) / 36

            halfnotesize = self.notesize / 2

            p1 = (halfnotesize, halfnotesize)
            p2 = (int(halfnotesize + halfnotesize * math.cos(angle)),
                  int(halfnotesize + halfnotesize * math.sin(angle)))

            mask = np.zeros((self.notesize, self.notesize), dtype=np.uint8)
            cv2.line(mask, p1, p2, (255,255,255))
            rafi_masks.append(mask)
            rafi_means.append(cv2.mean(note, mask)[0])

        rafi_means = np.array(rafi_means)

        candidate_means = []
        for x, y in first_grade_candidates:
            candidate = imagefuncs.submatrix(self.board, x, y, self.notesize)
            this_candidate_means = []
            for mask in rafi_masks:
                this_candidate_means.append(cv2.mean(candidate, mask)[0])
            candidate_means.append(this_candidate_means)

        candidates_with_correlation = []
        for i, this_candidate_means in enumerate(candidate_means):
            x, y = first_grade_candidates[i]

            this_candidate_rascorrs = []

            max_rascorr = -2
            max_cshift = None

            for cshift in range(self.settings['nr_of_rotation_angles']):
                rotated_rafi_means = np.roll(rafi_means, cshift)

                rascorr = calculate_correlation(np.array(this_candidate_means),
                                                rotated_rafi_means,
                                                self.settings['thresh_contrast'],
                                                self.settings['thresh_brightness'])

                this_candidate_rascorrs.append(rascorr)

                if rascorr > max_rascorr:
                    max_rascorr = rascorr
                    max_cshift = cshift

            candidates_with_correlation.append( (max_rascorr, x, y, max_cshift) )

        nr_of_second_grade_candidates = int((len(candidates_with_correlation) * self.settings['percentage_of_second_grade_candidates']) / 100)
        if nr_of_second_grade_candidates < self.settings['min_nr_of_second_grade_candidates']:
            nr_of_second_grade_candidates = min(len(candidates_with_correlation), self.settings['min_nr_of_second_grade_candidates'])
        second_grade_candidates = sorted([ (x, y, cshift) for _, x, y, cshift in
                                           sorted(candidates_with_correlation, reverse=True)[:nr_of_second_grade_candidates] ])

        return second_grade_candidates