def houghTransform(image, bcFilterConstants, parenttags=None):
    """ Runs the hough circle detection against the image
    
    Keyword Arguments:
    image -- Image instance
    bcFilterConstants -- The configuration under which to run bcFilter detection
    parenttags -- tag string of the calling function
    
    Returns:
    a list of CirclesFilter_.Circle objects
    """

    if bcFilterConstants is None:
        constants = BCFilterConstants
    else:
        constants = bcFilterConstants
        
    cvImage = opencv.PIL2Ipl(image)
    
    # smoothen the Image
    # opencv.cvSmooth( cvImage, cvImage, opencv.CV_GAUSSIAN, BCFilterConstants.masksize, BCFilterConstants.masksize);
    
    storage = opencv.cvCreateMemStorage(0)

    # print the settings that were used to detect circles
    log.info('BCFilterConstants dp:{0}, '\
                                'minimum distance:{1}, '\
                                'high threshold:{2}, '\
                                'accumulator threshold:{3}, '\
                                'minimum radius:{4}, '\
                                'maximum radius:{5}'.format(constants.dp,
                                                      constants.minimumDistance,
                                                      constants.highThreshold,
                                                      constants.accumulatorThreshold,
                                                      constants.minimumRadius,
                                                      constants.maximumRadius)
                                , extra=parenttags)
    
    circles = opencv.cvHoughCircles(cvImage, 
                                    storage,
                                    opencv.CV_HOUGH_GRADIENT,
                                    constants.dp, 
                                    constants.minimumDistance,
                                    constants.highThreshold, 
                                    constants.accumulatorThreshold,
                                    constants.minimumRadius, 
                                    constants.maximumRadius)

    # unpack the circle into a generic tuple
    # !!something wrong with circle.__getitem__ (don't use "tuple(circle)")
    if constants.maximumRadius != 0:
        # neither minimumRadius nor maximumRadius seem to be an absolue
        circles = [(float(circle[0]), float(circle[1]), float(circle[2]))
                        for circle in circles
                        if constants.minimumRadius <= circle[2] <= constants.maximumRadius]
    else:
        circles = [(float(circle[0]), float(circle[1]), float(circle[2]))
                        for circle in circles]

    log.debug('Found circles: %s', circles, extra=parenttags)

    return circles
Ejemplo n.º 2
0
      opencv.cvAdaptiveThreshold(igray, iwhite, 255, opencv.CV_ADAPTIVE_THRESH_GAUSSIAN_C)
      num, contours = opencv.cvFindContours (iwhite, stor, opencv.sizeof_CvContour, opencv.CV_RETR_LIST)
  
      opencv.cvCvtColor(iwhite, im, opencv.CV_GRAY2BGR)
      staticImage = im

      retrieveObstacles(contours)
      switchMode(MODE_DETECT_OBSTACLES3)
      action = "Detection took: %.2f" % (time.time()-before)

    elif mode == MODE_READY:
      screen.fill(lgray)
      im  = queryWebcam()
      opencv.cvCvtColor(im, igray, opencv.CV_BGR2GRAY)
      opencv.cvSmooth(igray, igray, opencv.CV_GAUSSIAN, 5, 5)
      circles = opencv.cvHoughCircles(igray, stor, opencv.CV_HOUGH_GRADIENT, 2, height/4, 200, 150)

      #opencv.cvCvtColor(igray, im, opencv.CV_GRAY2BGR)
      #displayImage(im)

      for data in circles:
        circle = [(data[0]+3, data[1]+3)]
        if projC2U != None:
          cinu = trans(circle, projC2U)[0]
          cinp = trans(circle, projC2P)[0]
          pygame.draw.circle(screen, gray, cinp, 2)
          if distance(cinp, u2p(ballx, bally)) > 2 and distance(cinp, u2p(holex, holey)) > 2*BALL and point_inside_polygon(circle[0][0], circle[0][1], cameraSpace):
            if registerStroke(cinp):
              break
        else:
          pygame.draw.circle(screen, red, circle[0], 10)