Python cv2 模块,HoughCircles() 实例源码

我们从Python开源项目中,提取了以下12个代码示例,用于说明如何使用cv2.HoughCircles()

项目:eclipse2017    作者:google    | 项目源码 | 文件源码
def findCircles(fname, image, circles_directory):
    f = os.path.join(circles_directory, os.path.basename(fname) + ".pkl")
    if os.path.exists(f):
        circles = pickle.load(open(f, "rb"))
        return circles
    image_cols, image_rows, _ = image.shape

    gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    blurred = cv2.bilateralFilter(gray, 9, 75, 75)
    gray = cv2.addWeighted(gray, 1.5, blurred, -0.5, 0)
    gray = cv2.bilateralFilter(gray, 9, 75, 75)

    # # detect circles in the image
    dp = 1
    c1 = 100
    c2 = 15
    print "start hough", fname
    circles = cv2.HoughCircles(gray, cv2.cv.CV_HOUGH_GRADIENT, dp, image_cols / 8, param1=c1, param2=c2)
    print "finish hough", fname
    pickle.dump(circles, open(f, "wb"))
    if circles is None or not len(circles):
        return None
    return circles
项目:OpenAI_Challenges    作者:AlwaysLearningDeeper    | 项目源码 | 文件源码
def process_img(img):
    original_image=img
    processed_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    processed_img = cv2.Canny(processed_img, threshold1=200, threshold2=300)
    processed_img = cv2.GaussianBlur(processed_img, (3,3), 0 )
    copy=processed_img
    vertices = np.array([[30, 240], [30, 100], [195, 100], [195, 240]])
    processed_img = roi(processed_img, np.int32([vertices]))
    verticesP = np.array([[30, 270], [30, 230], [197, 230], [197, 270]])
    platform = roi(copy, np.int32([verticesP]))
    #                       edges
    #lines = cv2.HoughLinesP(platform, 1, np.pi/180, 180,np.array([]), 3, 2)
    #draw_lines(processed_img,lines)
    #draw_lines(original_image,lines)

    #Platform lines
    #imgray = cv2.cvtColor(platform,cv2.COLOR_BGR2GRAY)
    ret,thresh = cv2.threshold(platform,127,255,0)
    im2, contours, hierarchy = cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
    cv2.drawContours(original_image, contours, -1, (0,255,0), 3)
    try:
        platformpos=contours[0][0][0]
    except:
        platformpos=[[0]]
    circles = cv2.HoughCircles(processed_img, cv2.HOUGH_GRADIENT, 1, 20,
                               param1=90, param2=5, minRadius=1, maxRadius=3)

    ballpos=draw_circles(original_image,circles=circles)

    return processed_img,original_image,platform,platformpos,ballpos
项目:Yugioh-bot    作者:will7200    | 项目源码 | 文件源码
def read_captured_circles(self):
        img = cv2.cvtColor(self.query, cv2.COLOR_BGR2GRAY)
        img = cv2.medianBlur(img, 7)
        cimg = cv2.cvtColor(img, cv2.COLOR_GRAY2BGR)

        circles = cv2.HoughCircles(img, cv2.HOUGH_GRADIENT, 1, 30,
                                   param1=50, param2=30, minRadius=20, maxRadius=50)
        if circles is None:
            return
        circles = np.uint16(np.around(circles))
        for i in circles[0, :]:
            if i[1] < 400:
                continue
            self.circlePoints.append((i[0], i[1]))
        if self._debug:
            self.draw_circles(circles, cimg)
项目:eclipse2017    作者:google    | 项目源码 | 文件源码
def findCircles(image):
    image_cols, image_rows, _ = image.shape

    gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    first, second = getRescaledDimensions(gray.shape[1], gray.shape[0], HD_MAX_X, HD_MAX_Y)
    gray = cv2.resize(gray, (first, second))
    blurred = cv2.bilateralFilter(gray, 9, 75, 75)
    gray = cv2.addWeighted(gray, 1.5, blurred, -0.5, 0)
    gray = cv2.bilateralFilter(gray, 9, 75, 75)

    # # detect circles in the image
    dp = 1
    c1 = 100
    c2 = 15
    circles = cv2.HoughCircles(gray, cv2.cv.CV_HOUGH_GRADIENT, dp, second / 8, param1=c1, param2=c2)
    if not len(circles):
        return None
    return circles[0][0]
项目:Speedy-TSLSR    作者:talhaHavadar    | 项目源码 | 文件源码
def __findCircles(mask):
    """
        Finds circles from mask and returns HoughCircles
    """
    circles = cv2.HoughCircles(mask, cv2.HOUGH_GRADIENT, 1, 100, param1=30, param2=50)
    return circles
项目:esys-pbi    作者:fsxfreak    | 项目源码 | 文件源码
def find_hough_circles(img):
    circles = cv2.HoughCircles(pupil_img,cv2.HOUGH_GRADIENT,1,20,
                            param1=50,param2=30,minRadius=0,maxRadius=80)
    if circles is not None:
        circles = np.uint16(np.around(circles))
        for i in circles[0,:]:
            # draw the outer circle
            cv2.circle(img,(i[0],i[1]),i[2],(0,255,0),2)
            # draw the center of the circle
            cv2.circle(img,(i[0],i[1]),2,(0,0,255),3)
项目:Yugioh-bot    作者:will7200    | 项目源码 | 文件源码
def capture_white_circles(self):
        self.prep_for_white_circles()
        img = cv2.cvtColor(self.white_query, cv2.COLOR_BGR2GRAY)
        cimg = cv2.cvtColor(img, cv2.COLOR_GRAY2BGR)
        circles = cv2.HoughCircles(img, cv2.HOUGH_GRADIENT, 1, 40,
                                   param1=50, param2=30, minRadius=5, maxRadius=60)
        if circles is None:
            return
        circles = np.uint16(np.around(circles))
        for i in circles[0, :]:
            self.circlePoints.append((i[0], i[1]))
        if self._debug:
            self.draw_circles(circles, cimg)
项目:DoNotSnap    作者:AVGInnovationLabs    | 项目源码 | 文件源码
def findCircles(hue, intensity):
    houghCirclesMask = np.zeros(hue.shape, dtype=np.uint8)

    blurred_hue = cv2.GaussianBlur(hue, (9, 9), 2)
    blurred_intensity = cv2.GaussianBlur(intensity, (9, 9), 2)
    hue_circles = cv2.HoughCircles(blurred_hue, cv2.cv.CV_HOUGH_GRADIENT, 0.5, hue.shape[0] / 8, param1=10, param2=25, maxRadius=100)
    intensity_circles = cv2.HoughCircles(blurred_intensity, cv2.cv.CV_HOUGH_GRADIENT, 0.5, hue.shape[0] / 8, param1=185, param2=20, maxRadius=100)

    circles = np.vstack((hue_circles[0] if hue_circles is not None else np.empty((0, 3)),
                         intensity_circles[0] if intensity_circles is not None else np.empty((0, 3))))

    for (x, y, r) in circles:
        cv2.circle(houghCirclesMask, (int(round(x)), int(round(y))), int(round(r)), 255, -1)

    return houghCirclesMask
项目:TableSoccerCV    作者:StudentCV    | 项目源码 | 文件源码
def get_center_scale(self, calibration_image):
        """

        :param calibration_image: The HSV-image to use for calculation
        :return: Position of center point in image (tuple), ratio px per cm (reproduction scale)
        """
        gray = cv2.cvtColor(calibration_image, cv2.COLOR_HSV2BGR)
        gray = cv2.cvtColor(gray, cv2.COLOR_BGR2GRAY)
        gray = cv2.GaussianBlur(gray, (5, 5), 1)

        circles = cv2.HoughCircles(gray, cv2.HOUGH_GRADIENT, 1, 100, param1=50, param2=30, minRadius=50, maxRadius=300)

        center_circle = (0, 0, 0)
        min_dist = 0xFFFFFFFFFFF
        for circle in circles[0]:
            dist_x = abs(circle[0] - calibration_image.shape[1] / 2)
            dist_y = abs(circle[1] - calibration_image.shape[0] / 2)

            if(dist_x + dist_y) < min_dist:
                min_dist = dist_x + dist_y
                center_circle = circle

        rgb = cv2.cvtColor(calibration_image, cv2.COLOR_HSV2RGB)
        cv2.circle(rgb, (center_circle[0], center_circle[1]), center_circle[2], (0, 255, 0), 1)

        center = center_circle[0], center_circle[1]

        radius = center_circle[2]
        ratio_pxcm = radius / 10.25

        self.center = center
        self.ratio_pxcm = ratio_pxcm

        return [center, ratio_pxcm]
项目:pool-assist    作者:sahirv    | 项目源码 | 文件源码
def findCircles():
    # read image - 0 is greyscale, 1 - color
    table_img = cv2.imread('training_sets/tables/extable6.png', 1)
    table_img_col = table_img.copy()
    table_img_grey = cv2.cvtColor(table_img, cv2.COLOR_BGR2GRAY)
    table_orig = table_img_grey.copy()

    # smooth
    table_img_grey = cv2.blur(table_img_grey, (3,3))

    # perform canny edge detection
    table_canny = cv2.Canny(table_img_grey, 15, 30)
    t_c_copy = table_canny.copy()

    # Perform Hough circle transform
    circles = cv2.HoughCircles(table_canny, cv2.HOUGH_GRADIENT, 1, 25, param1=90, param2=30, maxRadius=50, minRadius=14)

    avgObjRadius = 0
    stripes = []
    solids = []
    cueBall = (0,0)
    pockets = []
    if circles is not None:
        print("Found circles")
        circles = np.round(circles[0, :]).astype("int")
        totAvgRadius = sum(i[2] for i in circles) // len(circles)
        objBallCounter = 0
        for x, y, r in circles:
            if r <= totAvgRadius:
                objBallCounter += 1
                avgObjRadius += r
        avgObjRadius = avgObjRadius // objBallCounter
        for x, y, r in circles:
            if r > 30:
                pockets.append([x, y, r])
                cv2.circle(table_img, (x, y), r, (0, 210, 30), 3)
            else:
                # store pixels within circle below
                ball = isolateBall(x, y, avgObjRadius, table_img)
                ballType = classifyBall(ball)
                if ballType == "stripe":
                    stripes.append((x, y))
                elif ballType == "solid":
                    solids.append((x, y))
                elif ballType == "cue":
                    cueBall = (x, y)
                else:
                    raise Exception("Ball can not be classified. X= " + x + " Y= " + y)
                cv2.circle(table_img, (x, y), avgObjRadius, (150, 100, 255), 4)

    #concatenate before+after images

    img = np.concatenate((table_img_col, cv2.cvtColor(t_c_copy, cv2.COLOR_GRAY2BGR), table_img), axis=0)

    filename = 'img.png'
    cv2.imwrite(filename, img)
    return filename
项目:service_vision    作者:JarbasAI    | 项目源码 | 文件源码
def find_eye_radius(self, eye):
        self.log.info("measuring eye radius")
        # gray = cv2.cvtColor(eye, cv2.COLOR_BGR2GRAY)
        # gray = cv2.GaussianBlur(gray, (5, 5), 0)

        # param_1 = 200: Upper threshold for the internal Canny edge detector
        # param_2 = 100*: Threshold for center detection.
        # circles = cv2.HoughCircles(gray, cv2.cv.CV_HOUGH_GRADIENT, 1, 10,
        #                           param1=200, param2=100, minRadius=0, maxRadius=0)
        # if circles is not None:
        # circles = np.uint16(np.around(circles))
        # pick smallest circle works half time
        # pick centralish circle
        # minimum radus of 7 pixels
        #   for i in circles[0, :]:
        #     if i[2] < self.radius:
        #         x = i[0]
        #         y = i[1]
        #        self.radius = i[2]
        # draw
        #         cv2.circle(eye, (x, y), self.radius, (0, 255, 0), 2)
        # draw a dot on the circle (pupil)
        #        cv2.circle(eye, (x, y), 2, (0, 0, 255), 3)
        # k = cv2.waitKey(0)
        # print self.radius
        # cv2.imshow("eye", eye)
        return self.radius
项目:SunFounder_PiCar-V    作者:sunfounder    | 项目源码 | 文件源码
def find_blob() :
    radius = 0
    # Load input image
    _, bgr_image = img.read()

    orig_image = bgr_image

    bgr_image = cv2.medianBlur(bgr_image, 3)

    # Convert input image to HSV
    hsv_image = cv2.cvtColor(bgr_image, cv2.COLOR_BGR2HSV)

    # Threshold the HSV image, keep only the red pixels
    lower_red_hue_range = cv2.inRange(hsv_image, (0, 100, 100), (10, 255, 255))
    upper_red_hue_range = cv2.inRange(hsv_image, (160, 100, 100), (179, 255, 255))
    # Combine the above two images
    red_hue_image = cv2.addWeighted(lower_red_hue_range, 1.0, upper_red_hue_range, 1.0, 0.0)

    red_hue_image = cv2.GaussianBlur(red_hue_image, (9, 9), 2, 2)

    # Use the Hough transform to detect circles in the combined threshold image
    circles = cv2.HoughCircles(red_hue_image, cv.CV_HOUGH_GRADIENT, 1, 120, 100, 20, 10, 0);

    # Loop over all detected circles and outline them on the original image
    all_r = np.array([])
    if circles is not None:
        for i in circles[0]:

            all_r = np.append(all_r, int(round(i[2])))
        closest_ball = all_r.argmax()
        center=(int(round(circles[0][closest_ball][0])), int(round(circles[0][closest_ball][1])))
        radius=int(round(circles[0][closest_ball][2]))
        if draw_circle_enable:
            cv2.circle(orig_image, center, radius, (0, 255, 0), 5);

    # Show images
    if show_image_enable:
        cv2.namedWindow("Threshold lower image", cv2.WINDOW_AUTOSIZE)
        cv2.imshow("Threshold lower image", lower_red_hue_range)
        cv2.namedWindow("Threshold upper image", cv2.WINDOW_AUTOSIZE)
        cv2.imshow("Threshold upper image", upper_red_hue_range)
        cv2.namedWindow("Combined threshold images", cv2.WINDOW_AUTOSIZE)
        cv2.imshow("Combined threshold images", red_hue_image)
        cv2.namedWindow("Detected red circles on the input image", cv2.WINDOW_AUTOSIZE)
        cv2.imshow("Detected red circles on the input image", orig_image)

    k = cv2.waitKey(5) & 0xFF
    if k == 27:
        return (0, 0), 0
    if radius > 3:
        return center, radius;
    else:
        return (0, 0), 0