Exemplo n.º 1
0
def calibration_radius_error(dist, images, must_latex=True):
    global detector, connect4
    titles = ["\\texttt{minRadius}", "\\texttt{maxRadius}", "\\texttt{minDist}", "Grid circles", "Noise circles",
              "Total", "Score"]
    results = []
    counter = 0
    factor = 3.0 * dist
    for img in images:
        table = []
        best_score = -1000
        # how many pixels for a circle radius on a 320x240px image when standing one meter away
        one_meter_value = 6
        best_value = []
        gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
        gray = cv2.GaussianBlur(gray, (3, 3), 0)
        gray = cv2.medianBlur(gray, 3)
        while one_meter_value < 8:
            dist_value = int(round(one_meter_value / dist))
            upper_bound = (dist_value + 1)
            while upper_bound < (factor * one_meter_value) / dist:
                lower_bound = (dist_value - 1)
                while lower_bound > (one_meter_value / factor) / dist:
                    min_radius = int(lower_bound)
                    max_radius = int(upper_bound)
                    max_error = connect4.computeMaxPixelError(min_radius)
                    min_dist = round(lower_bound * 1.125, 2)
                    circles = cv2.HoughCircles(gray, cv2.HOUGH_GRADIENT, 1, min_dist, param1=48, param2=10.5,
                                               minRadius=min_radius, maxRadius=max_radius)
                    if circles is None:
                        score = 0
                        nb_of_grid_circles = 0
                        circles = [[]]
                    else:
                        # circles = np.uint16(np.around(circles))
                        try:
                            detector.runDetection(circles[0], pixel_error_margin=max_error)
                            nb_of_grid_circles = len(detector.relativeCoordinates)
                            score = round(get_f_score(nb_of_grid_circles, len(circles[0]) - nb_of_grid_circles), 4)
                        except c4.CircleGridNotFoundException:
                            score = 0
                            nb_of_grid_circles = 0
                        if score > best_score:
                            best_score = score
                            best_value = [(min_radius, max_radius)]
                        elif abs(score - best_score) < 0.00001:
                            best_value.append((min_radius, max_radius))
                    line = [lower_bound, upper_bound, min_dist, nb_of_grid_circles,
                            len(circles[0]) - nb_of_grid_circles, len(circles[0]), score]
                    table.append(line)
                    lower_bound -= 1
                upper_bound += 1
            one_meter_value += 1
        print "radius : image " + str(counter) + " finished"
        results.append(best_value)
        if must_latex:
            latex_generator.generate_longtable(titles, "../../latex/generated_radius_" +
                                               str(dist) + "_" + str(counter), table)
        counter += 1
    return results
Exemplo n.º 2
0
def calibration_param1(dist, images, must_latex=True):
    global detector, connect4
    titles = ["\\texttt{param1}", "Grid circles", "Noise circles",
              "Total", "Score"]
    results = []
    counter = 0
    min_radius = connect4.estimateMinRadius(dist)
    max_radius = connect4.estimateMaxRadius(dist)
    max_error = connect4.computeMaxPixelError(min_radius)
    min_dist = int(min_radius * 1.195)
    param2 = 10.5
    for img in images:
        table = []
        best_value = []
        best_score = 0
        # how many pixels for a circle radius on a 320x240px image when standing one meter away
        param1 = 30
        gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
        gray = cv2.GaussianBlur(gray, (3, 3), 0)
        gray = cv2.medianBlur(gray, 3)
        while param1 < 200:
            circles = cv2.HoughCircles(gray, cv2.HOUGH_GRADIENT, 1, min_dist, param1=param1, param2=param2,
                                       minRadius=min_radius, maxRadius=max_radius)
            if circles is None:
                score = 0
                nb_of_grid_circles = 0
                circles = [[]]
            else:
                try:
                    detector.runDetection(circles[0], pixel_error_margin=max_error)
                    nb_of_grid_circles = len(detector.relativeCoordinates)
                    score = round(get_f_score(nb_of_grid_circles, len(circles[0]) - nb_of_grid_circles), 4)
                except c4.CircleGridNotFoundException:
                    score = 0
                    nb_of_grid_circles = 0
                if score > best_score:
                    best_score = score
                    best_value = [param1]
                elif abs(score - best_score) < 0.00001:
                    best_value.append(param1)
            line = [param1, nb_of_grid_circles, len(circles[0]) - nb_of_grid_circles, len(circles[0]), score]
            table.append(line)
            param1 += 1
        results.append(best_value)
        print "param1 : image " + str(counter) + " finished"
        if must_latex:
            latex_generator.generate_longtable(titles, "../../latex/generated_param1_" +
                                               str(dist) + "_" + str(counter), table)
        counter += 1
    return results
Exemplo n.º 3
0
def evaluate(best_values, param, dist):
    scores = {}
    titles = ["\\texttt{param" + param + "}", "Occurrences"]
    table = []
    for iteration in best_values:
        for value in iteration:
            if value in scores:
                scores[value] += 1
            else:
                scores[value] = 1
    for value in scores:
        line = [value, scores[value]]
        table.append(line)
    latex_generator.generate_longtable(titles, "../../latex/value/" + str(param) + "_" + str(dist), table)
    return best_values