def detect(image): image_size = cv.GetSize(image) # create grayscale version grayscale = cv.CreateImage(image_size, 8, 1) cv.CvtColor(image, grayscale, cv.BGR2GRAY) # create storage storage = cv.CreateMemStorage(0) cv.ClearMemStorage(storage) # equalize histogram cv.EqualizeHist(grayscale, grayscale) # detect objects cascade = cv.LoadHaarClassifierCascade('haarcascade_frontalface_alt.xml', cv.Size(1, 1)) faces = cv.HaarDetectObjects(grayscale, cascade, storage, 1.2, 2, cv.HAAR_DO_CANNY_PRUNING, cv.Size(50, 50)) if faces: print 'face detected!' for i in faces: cv.Rectangle(image, cv.Point(int(i.x), int(i.y)), cv.Point(int(i.x + i.width), int(i.y + i.height)), cv.RGB(0, 255, 0), 3, 8, 0) # create windows cv.NamedWindow('Camera', cv.WINDOW_AUTOSIZE) # create capture device device = 0 # assume we want first device capture = cv.CreateCameraCapture(0) cv.SetCaptureProperty(capture, cv.CAP_PROP_FRAME_WIDTH, 640) cv.SetCaptureProperty(capture, cv.CAP_PROP_FRAME_HEIGHT, 480)
def detectObject(image): grayscale = cv.CreateImage(cv.CvSize(640, 480), 8, 1) cv.CvtColor(image, grayscale, CV_BGR2GRAY) storage = cv.CreateMemStorage(0) cv.ClearMemStorage(storage) cv.EqualizeHist(grayscale, grayscale) cascade = cv.LoadHaarClassifierCascade('haarcascade_frontalface_alt.xml', cv.CvSize(1, 1)) faces = cv.HaarDetectObjects(grayscale, cascade, storage, 1.2, 2, CV_HAAR_DO_CANNY_PRUNING, cv.Size(100, 100)) if faces: for i in faces: cv.Rectangle(image, cv.Point(int(i.x), int(i.y)), cv.Point(int(i.x + i.width), int(i.y + i.height)), CV_RGB(0, 255, 0), 3, 8, 0)
def detect(image): image_size = cv.GetSize(image) # create grayscale version grayscale = cv.CreateImage(image_size, 8, 1) cv.CvtColor(image, grayscale, cv.BGR2GRAY) # create storage storage = cv.CreateMemStorage(0) cv.ClearMemStorage(storage) # equalize histogram cv.EqualizeHist(grayscale, grayscale) # detect objects cascade = cv.LoadHaarClassifierCascade('haarcascade_frontalface_alt.xml', cv.Size(1,1)) faces = cv.HaarDetectObjects(grayscale, cascade, storage, 1.2, 2, cv.HAAR_DO_CANNY_PRUNING, cv.Size(50, 50)) if faces: print 'face detected!' for i in faces: cv.Rectangle(image, cv.Point( int(i.x), int(i.y)), cv.Point(int(i.x + i.width), int(i.y + i.height)), cv.RGB(0, 255, 0), 3, 8, 0)
def findSquares4(img, storage): N = 11 sz = cv.Size(img.width & -2, img.height & -2) timg = CloneImage(img) # make a copy of input image gray = CreateImage(sz, 8, 1) pyr = CreateImage(cv.Size(sz.width / 2, sz.height / 2), 8, 3) # create empty sequence that will contain points - # 4 points per square (the square's vertices) squares = CreateSeq(0, sizeof_CvSeq, sizeof_CvPoint, storage) squares = CvSeq_CvPoint.cast(squares) # select the maximum ROI in the image # with the width and height divisible by 2 subimage = GetSubRect(timg, Rect(0, 0, sz.width, sz.height)) # down-scale and upscale the image to filter out the noise PyrDown(subimage, pyr, 7) PyrUp(pyr, subimage, 7) tgray = CreateImage(sz, 8, 1) # find squares in every color plane of the image for c in range(3): # extract the c-th color plane channels = [None, None, None] channels[c] = tgray Split(subimage, channels[0], channels[1], channels[2], None) for l in range(N): # hack: use Canny instead of zero threshold level. # Canny helps to catch squares with gradient shading if (l == 0): # apply Canny. Take the upper threshold from slider # and set the lower to 0 (which forces edges merging) Canny(tgray, gray, 0, thresh, 5) # dilate canny output to remove potential # holes between edge segments Dilate(gray, gray, None, 1) else: # apply threshold if l!=0: # tgray(x,y) = gray(x,y) < (l+1)*255/N ? 255 : 0 Threshold(tgray, gray, (l + 1) * 255 / N, 255, CV_THRESH_BINARY) # find contours and store them all as a list count, contours = FindContours(gray, storage, sizeof_CvContour, CV_RETR_LIST, CV_CHAIN_APPROX_SIMPLE, Point(0, 0)) if not contours: continue # test each contour for contour in contours.hrange(): # approximate contour with accuracy proportional # to the contour perimeter result = ApproxPoly(contour, sizeof_CvContour, storage, CV_POLY_APPROX_DP, ContourPerimeter(contours) * 0.02, 0) # square contours should have 4 vertices after approximation # relatively large area (to filter out noisy contours) # and be convex. # Note: absolute value of an area is used because # area may be positive or negative - in accordance with the # contour orientation if (result.total == 4 and abs(ContourArea(result)) > 1000 and CheckContourConvexity(result)): s = 0 for i in range(5): # find minimum angle between joint # edges (maximum of cosine) if (i >= 2): t = abs( angle(result[i], result[i - 2], result[i - 1])) if s < t: s = t # if cosines of all angles are small # (all angles are ~90 degree) then write quandrange # vertices to resultant sequence if (s < 0.3): for i in range(4): squares.append(result[i]) return squares
class RobotVision: #size = None #cvImage hsv_frame, thresholded, thresholded2 #cvScalar hsv_min, hsv_max, hsv_min2, hsv_max2 #cvCapture capture; if __name__ == '__main__': #globals size, hsv_frame, thresholded, thresholded2, hsv_min, hsv_max, hsv_min2, hsv_max2, capture print "Initializing ball Tracking" size = cv.Size(640, 480) hsv_frame = cv.CreateImage(size, cv.IPL_DEPTH_8U, 3) thresholded = cv.CreateImage(size, cv.IPL_DEPTH_8U, 1) thresholded2 = cv.CreateImage(size, cv.IPL_DEPTH_8U, 1) hsv_min = cv.Scalar(0, 50, 170, 0) hsv_max = cv.Scalar(10, 180, 256, 0) hsv_min2 = cv.Scalar(170, 50, 170, 0) hsv_max2 = cv.Scalar(256, 180, 256, 0) storage = cv.CreateMemStorage(0) # start capturing form webcam capture = cv.CreateCameraCapture(0) if not capture: print "Could not open webcam" sys.exit(1) #CV windows cv.NamedWindow("Camera", cv.CV_WINDOW_AUTOSIZE) #globals size, hsv_frame, thresholded, thresholded2, hsv_min, hsv_max, hsv_min2, hsv_max2, capture while True: # get a frame from the webcam frame = cv.QueryFrame(capture) if frame is not None: # convert to HSV for color matching # as hue wraps around, we need to match it in 2 parts and OR together cv.CvtColor(frame, hsv_frame, cv.CV_BGR2HSV) cv.InRangeS(hsv_frame, hsv_min, hsv_max, thresholded) cv.InRangeS(hsv_frame, hsv_min2, hsv_max2, thresholded2) cv.Or(thresholded, thresholded2, thresholded) # pre-smoothing improves Hough detector cv.Smooth(thresholded, thresholded, cv.CV_GAUSSIAN, 9, 9) circles = cv.HoughCircles(thresholded, storage, cv.CV_HOUGH_GRADIENT, 2, thresholded.height / 4, 100, 40, 20, 200) # find largest circle maxRadius = 0 x = 0 y = 0 found = False for i in range(circles.total): circle = circles[i] if circle[2] > maxRadius: found = True maxRadius = circle[2] x = circle[0] y = circle[1] cv.ShowImage("Camera", frame) if found: print "ball detected at position:", x, ",", y, " with radius:", maxRadius else: print "no ball"