def detect(image): image_size = cv.GetSize(image) #create greyscale version grayscale = cv.CreateImage(image_size, 8, 1) cv.CvtColor(image, grayscale, cv.CV_BGR2GRAY) # create storage storage = cv.CreateMemStorage(0) #cv.ClearMemStorage(storage) # equalize histogram cv.EqualizeHist(grayscale, grayscale) # detect objects cascade = cv.Load('haarcascade_frontalface_alt.xml') 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) cv.ShowImage('Video', image) return faces
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 draw_vector(image, position, color, magnitude, max_param): scaled_mag = [0, 0] for dim in [0, 1]: if magnitude[dim] > 0: space_left = image.size[dim] - position[dim] else: space_left = position[dim] scaled_mag[dim] = int(space_left * (float(magnitude[dim]) / float(max_param[dim]))) end_pt = cv.Point(position[0] + scaled_mag[0], position[1] + scaled_mag[1]) cv.Line(image, position, end_pt, (0, 255, 0), thickness = self.marker_rad) return image
def draw_contours(frame, contours): out = cv.CreateImage(cv.GetSize(frame), cv.IPL_DEPTH_8U, 3) cv.CvtColor(frame, out, cv.CV_GRAY2BGR) # draw contours in red and green cv.DrawContours( out, contours, _green, _red, contour_max_level, 1, cv.CV_AA, cv.Point(5, 5) ) return out
def label_sift_point(image, xx, yy, label): line_type = cv.CV_AA # change it to 8 to see non-antialiased graphics pt1 = cv.Point(int(xx - 6), int(yy + 8)) font = cv.InitFont(cv.CV_FONT_HERSHEY_SIMPLEX, 1.0, 0.1, 0, 1, cv.CV_AA) cvPutText(image, label, pt1, font, cv.CV_RGB(255, 255, 0))
def main(): # Initialize capturing live feed from the camera capture = 0 capture = cv.CaptureFromCAM(0) # Couldn't get a device? Throw an error and quit if (not capture): print "Could not initialize capturing...\n" return -1 # The two windows we'll be using cv.NamedWindow("video") cv.NamedWindow("thresh") # This image holds the "scribble" data... # the tracked positions of the ball imgScribble = 0 # An infinite loop while (True): # Will hold a frame captured from the camera frame = 0 frame = cv.QueryFrame(capture) # If we couldn't grab a frame... quit if (not frame): break # If this is the first frame, we need to initialize it if (imgScribble == 0): imgScribble = cv.CreateImage(cv.GetSize(frame), 8, 3) # Holds the yellow thresholded image (yellow = white, rest = black) imgYellowThresh = GetThresholdedImage(frame) moments = 0 # Calculate the moments to estimate the position of the ball moments = cv.Moments(imgYellowThresh) # The actual moment values moment01 = cv.GetSpatialMoment(moments, 0, 1) moment10 = cv.GetSpatialMoment(moments, 1, 0) area = cv.GetSpatialMoment(moments, 0, 0) # if area==0: continue # Holding the last and current ball positions posX = 0 posY = 0 lastX = posX lastY = posY posX = moment10 / area posY = moment01 / area # Print it out for debugging purposes print "position " + str(posX) + ' ' + str(posY) + '\n' # We want to draw a line only if its a valid position if (lastX > 0 and lastY > 0 and posX > 0 and posY > 0): # Draw a yellow line from the previous point to the current point cv.Line(imgScribble, cv.Point(posX, posY), cv.Point(lastX, lastY), cv.Scalar(0, 255, 255), 5) # Add the scribbling image and the frame... and we get a combination of the two cv.Add(frame, imgScribble, frame) cv.ShowImage("thresh", imgYellowThresh) cv.ShowImage("video", frame) # Wait for a keypress c = cv.WaitKey(10) if not (c == -1): # If pressed, break out of the loop break # Release the thresholded image... we need no memory leaks.. please # We're done using the camera. Other applications can now use it return 0
def DetectFaces(self): faces = cv.HaarDetectObjects(self.frame, self.cascade, self.storage, 1.1, 3, cv.CV_HAAR_DO_CANNY_PRUNING, (100, 100)) if faces.total > 0: for f in faces: cv.Rectangle(self.frame, cv.Point(f.x, f.y), cv.Point(f.x+f.width, f.y+f.height), cv.CV_RGB(255,0,0), 1, 4, 0) return
def __init__(self, poly, depth): self.depth = depth self.points = [] self.angles = [.0] self.touching = [] self.children = [] self.parents = [] xavg = [] yavg = [] xmin = ymin = xmax = ymax = None for j in range(poly.total): ptr = cv.GetSeqElem(poly, j) #point = ctypes.cast(_ptr, cv.Point.CAST ) #x = point.contents.x; y = point.contents.y point = cv.Point(pointer=ptr, cast=True) x = point.x y = point.y p = Point(x, y) if self.points: a = math.degrees(self.points[-1].angle(p)) self.angles.append(a) self.points.append(p) xavg.append(x) yavg.append(y) if j == 0: xmin = xmax = x ymin = ymax = y else: if x < xmin: xmin = x if x > xmax: xmax = x if y < ymin: ymin = y if y > ymax: ymax = y self.avariance = .0 self.avariance_points = [.0, .0] if self.angles: print(self.angles) prev = self.angles[0] for a in self.angles[1:]: v = abs(prev - a) self.avariance_points.append(v) self.avariance += v prev = a #print 'variance', self.avariance #print 'variance-points', self.avariance_points #print 'len len', len(self.points), len(self.avariance_points) n = len(self.points) self.weight = (sum(xavg) / float(n), sum(yavg) / float(n)) self.width = xmax - xmin self.height = ymax - ymin self.center = (int(xmin + (self.width / 2)), int(ymin + (self.height / 2))) self.rectangle = ((xmin, ymin), (xmax, ymax)) self.dwidth = xmax - xmin self.dheight = ymax - ymin self.dcenter = (xmin + (self.dwidth / 2), ymin + (self.dheight / 2)) self.drectangle = ((xmin, ymin), (xmax, ymax)) self.defects = [] self.center_defects = None self.convex = cv.CheckContourConvexity(poly) if not self.convex: T = 80 dxavg = [] dyavg = [] hull = cv.ConvexHull2(poly, self.storage_hull, 1, 0) defects = cv.ConvexityDefects(poly, hull, self.storage_defects) n = defects.total for j in range(n): D = cv.ConvexityDefect(pointer=cv.GetSeqElem(defects, j), cast=True) s = D.start.contents e = D.end.contents d = D.depth_point.contents start = (s.x, s.y) end = (e.x, e.y) depth = (d.x, d.y) ## ignore large defects ## if abs(end[0] - depth[0]) > T or abs(end[1] - depth[1]) > T or abs( start[0] - end[0]) > T or abs(start[1] - end[1]) > T: continue dxavg.append(depth[0]) dyavg.append(depth[1]) self.defects.append((start, end, depth)) xmin = ymin = 999999 xmax = ymax = -1 if self.defects: n = len(self.defects) self.center_defects = (int(sum(dxavg) / float(n)), int(sum(dyavg) / float(n))) for j, f in enumerate(self.defects): s, e, d = f if s[0] < xmin: xmin = s[0] if e[0] < xmin: xmin = e[0] if s[0] > xmax: xmax = s[0] if e[0] > xmax: xmax = e[0] if s[1] < ymin: ymin = s[1] if e[1] < ymin: ymin = e[1] if s[1] > ymax: ymax = s[1] if e[1] > ymax: ymax = e[1] self.dwidth = xmax - xmin self.dheight = ymax - ymin self.dcenter = (xmin + (self.dwidth / 2), ymin + (self.dheight / 2)) self.drectangle = ((xmin, ymin), (xmax, ymax)) cv.ClearMemStorage(self.storage_hull) cv.ClearMemStorage(self.storage_defects)