def Flat_Calibration():
    blobs_threshold_flat = 240  # 170 on laptop
    blobs_min_size_flat = 1000
    flat_calibration_done = False
    while not flat_calibration_done:  # Repeat until flat flap calibration is performed correctly
        flat_image = AcquireFlatImage()
        Calibration_coords = get_calibration_coordinates(flat_image)
        print "Approximate coordinates of flap: ", Calibration_coords
        Calibration_values = ColorAveraging(flat_image, Calibration_coords)
        filteredImage = Orange_Flap_V5.apply_filter(Calibration_values, flat_image)
        possible_flaps = filteredImage.findBlobs(threshval=blobs_threshold_flat, minsize=blobs_min_size_flat)
        if possible_flaps > 1:
            possible_flaps = possible_flaps.sortDistance(point=(Calibration_coords[0], Calibration_coords[1]))
            # for i in range(0, len(possible_flaps)):
            #     filteredImage.dl().rectangle2pts(possible_flaps[i].topLeftCorner(),
            #                                      possible_flaps[i].bottomRightCorner(),Color.GREEN, width = 5)
            #     filteredImage.dl().text("%s" %i, (possible_flaps[i].topLeftCorner()), color=Color.RED)
        elif possible_flaps < 1:
            print "No possible flaps were found, starting calibration again"
            continue
        flap = possible_flaps[0]
        flat_image.dl().rectangle2pts(flap.topLeftCorner(), flap.bottomRightCorner(), Color.RED, width=5)
        # closest flap
        print "Is the flap marked with red square? Exit the image by left clicking and answer."
        disp = Display()  # Create a display
        while disp.isNotDone():  # Loop until display is not needed anymore
            if disp.mouseLeft:  #   Check if left click was used on display
                disp.done = True  # Turn off Display
            flat_image.show()  # Show the image on Display
        Display().quit()  # Exit the display so it does not go to "Not responding"

        while True:  # Loop until valid response
            userInput = raw_input("Was the flap in a red rectangle in the image? Please, enter Y or N.\n")
            userInput = userInput.lower()
            if userInput[0] == "y":
                flat_calibration_done = True
                break
            elif userInput[0] == "n":
                break
            else:
                print "Incorrect value entered."
    FlapWHRatio = round(float(flap.width()) / float(flap.height()), 4)
    values = {
        "AvHue": Calibration_values["AvHue"],
        "AvSat": Calibration_values["AvSat"],
        "StdSat": Calibration_values["StdSat"],
        "FlatWHRatio": FlapWHRatio,
        "mouseX": Calibration_coords[0],
        "mouseY": Calibration_coords[1],
    }
    return values
def Slope_Calibration(AvHue, AvSat, StdSat, mouseX, mouseY):
    blobs_threshold_slope = 240  # 170 on laptop
    blobs_min_size_slope = 1000
    slope_calibration_done = False
    while not slope_calibration_done:
        slope_image = AcquireSlopeImage()
        filtered_image = Orange_Flap_V5.apply_filter({"AvHue": AvHue, "AvSat": AvSat, "StdSat": StdSat}, slope_image)
        possible_flaps = filtered_image.findBlobs(
            threshval=blobs_threshold_slope, minsize=blobs_min_size_slope
        )  # CAN ADD SIZES AND STUFF
        if possible_flaps > 1:
            possible_flaps = possible_flaps.sortDistance(point=(mouseX, mouseY))
            for i in range(0, len(possible_flaps)):
                filtered_image.dl().rectangle2pts(
                    possible_flaps[i].topLeftCorner(), possible_flaps[i].bottomRightCorner(), Color.GREEN, width=5
                )
                filtered_image.dl().text("%s" % i, (possible_flaps[i].bottomRightCorner()), color=Color.RED)
        elif possible_flaps < 1:
            print "No flap was found, please take another picture"
            continue
        flap = possible_flaps[0]
        slope_image.dl().rectangle2pts(flap.topLeftCorner(), flap.bottomRightCorner(), Color.RED, width=5)

        print "Is the flap marked with red square? Exit the image by left clicking and answer."
        disp = Display()  # Create a display
        while disp.isNotDone():  # Loop until display is not needed anymore
            if disp.mouseLeft:  #   Check if left click was used on display
                disp.done = True  # Turn off Display
            slope_image.show()  # Show the image on Display

        Display().quit()  # Exit the display so it does not go to "Not responding"

        while True:  # Loop until valid response
            userInput = raw_input("Was the flap in a red rectangle in the image? Please, enter Y or N.\n")
            userInput = userInput.lower()
            if userInput[0] == "y":
                slope_calibration_done = True
                break
            elif userInput[0] == "n":
                break
            else:
                print "Incorrect value entered."
    SlopeWHRatio = round(float(flap.width()) / float(flap.height()), 4)

    return SlopeWHRatio
def Slope_Calibration(AvHue, AvSat, StdSat, mouseX, mouseY):
    blobs_threshold_slope = 240  #170 on laptop
    blobs_min_size_slope = 1000
    slope_calibration_done = False
    while (not slope_calibration_done):
        slope_image = AcquireSlopeImage()
        filtered_image = Orange_Flap_V5.apply_filter(
            {
                "AvHue": AvHue,
                "AvSat": AvSat,
                "StdSat": StdSat
            }, slope_image)
        possible_flaps = filtered_image.findBlobs(
            threshval=blobs_threshold_slope,
            minsize=blobs_min_size_slope)  #CAN ADD SIZES AND STUFF
        if possible_flaps > 1:
            possible_flaps = possible_flaps.sortDistance(point=(mouseX,
                                                                mouseY))
            for i in range(0, len(possible_flaps)):
                filtered_image.dl().rectangle2pts(
                    possible_flaps[i].topLeftCorner(),
                    possible_flaps[i].bottomRightCorner(),
                    Color.GREEN,
                    width=5)
                filtered_image.dl().text(
                    "%s" % i, (possible_flaps[i].bottomRightCorner()),
                    color=Color.RED)
        elif possible_flaps < 1:
            print "No flap was found, please take another picture"
            continue
        flap = possible_flaps[0]
        slope_image.dl().rectangle2pts(flap.topLeftCorner(),
                                       flap.bottomRightCorner(),
                                       Color.RED,
                                       width=5)

        print "Is the flap marked with red square? Exit the image by left clicking and answer."
        disp = Display()  # Create a display
        while disp.isNotDone():  # Loop until display is not needed anymore
            if disp.mouseLeft:  #   Check if left click was used on display
                disp.done = True  # Turn off Display
            slope_image.show()  # Show the image on Display

        Display().quit(
        )  # Exit the display so it does not go to "Not responding"

        while True:  # Loop until valid response
            userInput = raw_input(
                "Was the flap in a red rectangle in the image? Please, enter Y or N.\n"
            )
            userInput = userInput.lower()
            if userInput[0] == "y":
                slope_calibration_done = True
                break
            elif userInput[0] == "n":
                break
            else:
                print "Incorrect value entered."
    SlopeWHRatio = round(float(flap.width()) / float(flap.height()), 4)

    return SlopeWHRatio
def Flat_Calibration():
    blobs_threshold_flat = 240  #170 on laptop
    blobs_min_size_flat = 1000
    flat_calibration_done = False
    while (not flat_calibration_done
           ):  # Repeat until flat flap calibration is performed correctly
        flat_image = AcquireFlatImage()
        Calibration_coords = get_calibration_coordinates(flat_image)
        print "Approximate coordinates of flap: ", Calibration_coords
        Calibration_values = ColorAveraging(flat_image, Calibration_coords)
        filteredImage = Orange_Flap_V5.apply_filter(Calibration_values,
                                                    flat_image)
        possible_flaps = filteredImage.findBlobs(
            threshval=blobs_threshold_flat, minsize=blobs_min_size_flat)
        if possible_flaps > 1:
            possible_flaps = possible_flaps.sortDistance(
                point=(Calibration_coords[0], Calibration_coords[1]))
            # for i in range(0, len(possible_flaps)):
            #     filteredImage.dl().rectangle2pts(possible_flaps[i].topLeftCorner(),
            #                                      possible_flaps[i].bottomRightCorner(),Color.GREEN, width = 5)
            #     filteredImage.dl().text("%s" %i, (possible_flaps[i].topLeftCorner()), color=Color.RED)
        elif possible_flaps < 1:
            print "No possible flaps were found, starting calibration again"
            continue
        flap = possible_flaps[0]
        flat_image.dl().rectangle2pts(flap.topLeftCorner(),
                                      flap.bottomRightCorner(),
                                      Color.RED,
                                      width=5)
        #closest flap
        print "Is the flap marked with red square? Exit the image by left clicking and answer."
        disp = Display()  # Create a display
        while disp.isNotDone():  # Loop until display is not needed anymore
            if disp.mouseLeft:  #   Check if left click was used on display
                disp.done = True  # Turn off Display
            flat_image.show()  # Show the image on Display
        Display().quit(
        )  # Exit the display so it does not go to "Not responding"

        while True:  # Loop until valid response
            userInput = raw_input(
                "Was the flap in a red rectangle in the image? Please, enter Y or N.\n"
            )
            userInput = userInput.lower()
            if userInput[0] == "y":
                flat_calibration_done = True
                break
            elif userInput[0] == "n":
                break
            else:
                print "Incorrect value entered."
    FlapWHRatio = round(float(flap.width()) / float(flap.height()), 4)
    values = {
        "AvHue": Calibration_values["AvHue"],
        "AvSat": Calibration_values["AvSat"],
        "StdSat": Calibration_values["StdSat"],
        "FlatWHRatio": FlapWHRatio,
        "mouseX": Calibration_coords[0],
        "mouseY": Calibration_coords[1]
    }
    return values