def getthresholdedimgR4(imhsv): # A little change here. Creates images for blue and yellow (or whatever color you like). imgRed = cv.CreateImage(cv.GetSize(imhsv), 8, 1) imgYellow = cv.CreateImage(cv.GetSize(imhsv), 8, 1) imgGreen = cv.CreateImage(cv.GetSize(imhsv), 8, 1) imgthreshold = cv.CreateImage(cv.GetSize(imhsv), 8, 1) cv.InRangeS(imghsv, cv.Scalar(0, 190, 130), cv.Scalar(18, 255, 190), imgRed) # Select a range of red color cv.InRangeS(imghsv, cv.Scalar(100, 100, 100), cv.Scalar(120, 255, 255), imgYellow) # Select a range of blue color cv.InRangeS(imghsv, cv.Scalar(67, 103, 46), cv.Scalar(100, 209, 184), imgGreen) # Select a range of green color storage = cv.CreateMemStorage(0) redContour = cv.FindContours(imgRed, storage, cv.CV_RETR_CCOMP, cv.CV_CHAIN_APPROX_SIMPLE) points = [] while redContour: # Draw bounding rectangles bound_rect = cv.BoundingRect(list(redContour)) #bound_rect = cv.BoundingRect(contour) # for more details about cv.BoundingRect,see documentation pt1 = (bound_rect[0], bound_rect[1]) pt2 = (bound_rect[0] + bound_rect[2], bound_rect[1] + bound_rect[3]) points.append(pt1) points.append(pt2) cv.Rectangle(imhsv, pt1, pt2, cv.CV_RGB(255, 0, 0), 1) #Calculating centroids centroidx = cv.Round((pt1[0] + pt2[0]) / 2) centroidy = cv.Round((pt1[1] + pt2[1]) / 2) area = cv.ContourArea(list(redContour)) redContour = redContour.h_next() while yellowContour: # Draw bounding rectangles bound_rect = cv.BoundingRect(list(redContour)) #bound_rect = cv.BoundingRect(contour) # for more details about cv.BoundingRect,see documentation pt1 = (bound_rect[0], bound_rect[1]) pt2 = (bound_rect[0] + bound_rect[2], bound_rect[1] + bound_rect[3]) points.append(pt1) points.append(pt2) cv.Rectangle(imhsv, pt1, pt2, cv.CV_RGB(255, 0, 0), 1) #Calculating centroids centroidx = cv.Round((pt1[0] + pt2[0]) / 2) centroidy = cv.Round((pt1[1] + pt2[1]) / 2) area = cv.ContourArea(list(redContour)) redContour = redContour.h_next() cv.ShowImage("Ceva", imhsv) cv.Add(imgRed, imgBlue, imgthreshold) cv.Add(imgGreen, imgGreen, imgthreshold) return imgthreshold
bestFaceW = w bestFaceH = h reScale = 0.3 horScl = int(bestFaceW * reScale) verScl = int(bestFaceH * reScale) #rect of middle of face rect = (bestFaceX + horScl, bestFaceY + verScl, bestFaceW - (horScl * 2), bestFaceH - (verScl * 2) ) # middle part of face pt1 = (int(bestFaceX), int(bestFaceY)) pt2 = (int( (bestFaceX + bestFaceW)), int((bestFaceY + bestFaceH))) cv.Rectangle(frameSmall, pt1, pt2, cv.RGB(0, 255, 0), 3, 8, 0) ################################### ########calculate histogram######## if biggestFace > 0: #sets the Region of Interest (face) in HSV image cv.SetImageROI(frameSmallHSV, rect) face = cv.CreateImage(cv.GetSize(frameSmallHSV), frameSmallHSV.depth, frameSmallHSV.nChannels) cv.Copy(frameSmallHSV, face) cv.ResetImageROI(frameSmallHSV) #get size of face area faceArea = face.height * face.width hist = hs_histogram(face) myHist = hist.getHist(hBins, sBins)
def detect(image, debug=False, display=None): work_image = cv.CreateImage((image.width, image.height), 8, 1) cv.CvtColor(image, work_image, cv.CV_BGR2GRAY) image = work_image edge = cv.CloneImage(image) thresholded = cv.CloneImage(image) v_edges = cv.CloneImage(image) h_edges = cv.CloneImage(image) vertical = cv.CreateImage(cv.GetSize(image), cv.IPL_DEPTH_16S, 1) cv.Sobel(image, vertical, 1, 0, 1) cv.Abs(vertical, vertical) cv.Convert(vertical, v_edges) storage = cv.CreateMemStorage(0) result = np.asarray(cv.GetMat(v_edges), dtype=np.float) threshold = 6 rects = [] while len(rects) < 1 and threshold > 0: rects = [] cv.Convert(vertical, v_edges) cv.AdaptiveThreshold(v_edges, v_edges, 255, cv.CV_ADAPTIVE_THRESH_MEAN_C, cv.CV_THRESH_BINARY_INV, 17, threshold) threshold -= 1 storage = cv.CreateMemStorage(0) contour_image = cv.CloneImage(v_edges) contours = cv.FindContours(contour_image, storage, cv.CV_RETR_LIST, cv.CV_CHAIN_APPROX_NONE, (0, 0)) ext.filter_contours(contours, 30, ext.LESSTHAN) max_size = int(image.width * image.height * 0.1) # ext.filter_contours(contours, 200**2, ext.GREATERTHAN) ext.filter_contours(contours, max_size, ext.GREATERTHAN) if display: cv.Merge(v_edges, v_edges, v_edges, None, display) seeds = [] if contours: seq = contours rects = [] while seq: c = ext.as_contour(ext.wrapped(seq)) r = (c.rect.x, c.rect.y, c.rect.width, c.rect.height) rects.append(r) if display: cv.Rectangle( display, (c.rect.x, c.rect.y), (c.rect.x + c.rect.width, c.rect.y + c.rect.height), (0, 0, 255), 1) seq = seq.h_next() rects.sort(lambda x, y: cmp(x[0] + x[2] / 2, y[0] + y[2] / 2)) seeds = rects[:] seeds.sort(lambda x, y: cmp(y[2] * y[3], x[2] * x[3])) groups = [] skip = False for seed in seeds: if seed not in rects: break found = False for group in groups: if seed in group: found = True if found: continue r = seed start = seed start_index = rects.index(seed) groups.append([seed]) i = start_index - 1 # delta = max(150, seed[2]/2) delta = seed[2] * 0.66 if debug: print "left", seed, delta col = (randint(0, 255), randint(0, 255), randint(0, 255)) cv.Rectangle(display, (r[0], r[1]), (r[0] + r[2], r[1] + r[3]), (255, 255, 255), 3) cv.Rectangle(display, (r[0], r[1]), (r[0] + r[2], r[1] + r[3]), col, -1) cv.ShowImage("main", display) if not skip: c = cv.WaitKey(0) if c == ord("a"): skip = True # scan left while 1: if i < 0: break rect = rects[i] if rect[0] + rect[2] < seed[0] - delta: if debug: print "esc1", rect break if in_vertical(seed, start, rect): seed = rect groups[-1].append(rect) r = rect if debug: print rect cv.Rectangle(display, (r[0], r[1]), (r[0] + r[2], r[1] + r[3]), col, -1) cv.ShowImage("main", display) if not skip: c = cv.WaitKey(0) if c == ord("a"): skip = True else: if debug: print "rej1", rect i -= 1 # scan right seed = start start_index = rects.index(seed) i = start_index + 1 if debug: print print "right", seed while 1: if i >= len(rects): break rect = rects[i] if rect[0] > seed[0] + seed[2] + delta: if debug: print "esc2", rect, rect[0] + rect[2] / 2, seed[ 0] + seed[2] / 2 + delta break if in_vertical(seed, start, rect): seed = rect groups[-1].append(rect) r = rect if debug: print rect cv.Rectangle(display, (r[0], r[1]), (r[0] + r[2], r[1] + r[3]), col, -1) cv.ShowImage("main", display) if not skip: c = cv.WaitKey(0) if c == ord("a"): skip = True else: if debug: print "rej2", rect i += 1 if debug: print # find min and max extent of group group_rects = [] for group in groups: min_x, min_y = 1E6, 1E6 max_x, max_y = -1, -1 dev = [] col = (randint(0, 255), randint(0, 255), randint(0, 255)) for rect in group: r = rect if display: if r == group[0]: cv.Rectangle(display, (r[0], r[1]), (r[0] + r[2], r[1] + r[3]), (255, 255, 255), 3) cv.Rectangle(display, (r[0], r[1]), (r[0] + r[2], r[1] + r[3]), col, -1) min_x = min(min_x, r[0]) min_y = min(min_y, r[1]) max_x = max(max_x, r[0] + r[2]) max_y = max(max_y, r[1] + r[3]) if display: cv.Rectangle(display, (min_x, min_y), (max_x, max_y), (0, 255, 0), 1) width = max_x - min_x height = max_y - min_y rect = (min_x, min_y, width, height) group_rects.append(rect) return group_rects
freenect.sync_stop() sys.exit() color = (100, 255, 100) cv.NamedWindow('Depth') cv.SetMouseCallback('Depth', close, param=None) depthThreshold = Threshold(0) cv.CreateTrackbar('Depth Threshold', 'Depth', 0, 1200, depthThreshold) depth_history = [] while 1: depth, dts = freenect.sync_get_depth_np() #rgb, rts = freenect.sync_get_rgb_np() depth = ((depth <= depthThreshold.p).astype(np.uint8) * depth).astype( np.uint16) dP = Points(np.argwhere(depth != 0)) avg_dep = 0 #depth = depth.astype(np.uint8) if dP.points.any(): # Average of the depth values that meet threshold depth_values = depth[depth != 0] avg_dep = sum(depth_values) / len(depth_values) #if len(depth_history) == 5: depth = array2cv(depth.astype(np.uint8)) bound_Rect = dP.boundingBox() cv.Rectangle(depth, bound_Rect[0], bound_Rect[1], color, thickness=3) #cv.PutText(detph, str(avg_dep), (50,50), , cv.Circle(depth, dP.calculateCenter(), 48, color) cv.ShowImage('Depth', depth) cv.WaitKey(10)
def track_lk(self, cv_image, face): feature_box = None """ Initialize intermediate images if necessary """ if not face.pyramid: face.grey = cv.CreateImage(cv.GetSize(cv_image), 8, 1) face.prev_grey = cv.CreateImage(cv.GetSize(cv_image), 8, 1) face.pyramid = cv.CreateImage(cv.GetSize(cv_image), 8, 1) face.prev_pyramid = cv.CreateImage(cv.GetSize(cv_image), 8, 1) face.features = [] """ Create a grey version of the image """ cv.CvtColor(cv_image, face.grey, cv.CV_BGR2GRAY) """ Equalize the histogram to reduce lighting effects """ cv.EqualizeHist(face.grey, face.grey) if face.track_box and face.features != []: """ We have feature points, so track and display them """ """ Calculate the optical flow """ face.features, status, track_error = cv.CalcOpticalFlowPyrLK( face.prev_grey, face.grey, face.prev_pyramid, face.pyramid, face.features, (self.win_size, self.win_size), 3, (cv.CV_TERMCRIT_ITER | cv.CV_TERMCRIT_EPS, 20, 0.01), self.flags) """ Keep only high status points """ face.features = [p for (st, p) in zip(status, face.features) if st] elif face.track_box and self.is_rect_nonzero(face.track_box): """ Get the initial features to track """ """ Create a mask image to be used to select the tracked points """ mask = cv.CreateImage(cv.GetSize(cv_image), 8, 1) """ Begin with all black pixels """ cv.Zero(mask) """ Get the coordinates and dimensions of the track box """ try: x, y, w, h = face.track_box except: return None if self.auto_face_tracking: # """ For faces, the detect box tends to extend beyond the actual object so shrink it slightly """ # x = int(0.97 * x) # y = int(0.97 * y) # w = int(1 * w) # h = int(1 * h) """ Get the center of the track box (type CvRect) so we can create the equivalent CvBox2D (rotated rectangle) required by EllipseBox below. """ center_x = int(x + w / 2) center_y = int(y + h / 2) roi_box = ((center_x, center_y), (w, h), 0) """ Create a filled white ellipse within the track_box to define the ROI. """ cv.EllipseBox(mask, roi_box, cv.CV_RGB(255, 255, 255), cv.CV_FILLED) else: """ For manually selected regions, just use a rectangle """ pt1 = (x, y) pt2 = (x + w, y + h) cv.Rectangle(mask, pt1, pt2, cv.CV_RGB(255, 255, 255), cv.CV_FILLED) """ Create the temporary scratchpad images """ eig = cv.CreateImage(cv.GetSize(self.grey), 32, 1) temp = cv.CreateImage(cv.GetSize(self.grey), 32, 1) if self.feature_type == 0: """ Find keypoints to track using Good Features to Track """ face.features = cv.GoodFeaturesToTrack( face.grey, eig, temp, self.max_count, self.quality, self.good_feature_distance, mask=mask, blockSize=self.block_size, useHarris=self.use_harris, k=0.04) elif self.feature_type == 1: """ Get the new features using SURF """ (surf_features, descriptors) = cv.ExtractSURF( face.grey, mask, cv.CreateMemStorage(0), (0, self.surf_hessian_quality, 3, 1)) for feature in surf_features: face.features.append(feature[0]) # if self.auto_min_features: """ Since the detect box is larger than the actual face or desired patch, shrink the number of features by 10% """ face.min_features = int(len(face.features) * 0.9) face.abs_min_features = int(0.5 * face.min_features) """ Swapping the images """ face.prev_grey, face.grey = face.grey, face.prev_grey face.prev_pyramid, face.pyramid = face.pyramid, face.prev_pyramid """ If we have some features... """ if len(face.features) > 0: """ The FitEllipse2 function below requires us to convert the feature array into a CvMat matrix """ try: self.feature_matrix = cv.CreateMat(1, len(face.features), cv.CV_32SC2) except: pass """ Draw the points as green circles and add them to the features matrix """ i = 0 for the_point in face.features: if self.show_features: cv.Circle(self.marker_image, (int(the_point[0]), int(the_point[1])), 2, (0, 255, 0, 0), cv.CV_FILLED, 8, 0) try: cv.Set2D(self.feature_matrix, 0, i, (int(the_point[0]), int(the_point[1]))) except: pass i = i + 1 """ Draw the best fit ellipse around the feature points """ if len(face.features) > 6: feature_box = cv.FitEllipse2(self.feature_matrix) else: feature_box = None """ Publish the ROI for the tracked object """ # try: # (roi_center, roi_size, roi_angle) = feature_box # except: # logger.info("Patch box has shrunk to zeros...") # feature_box = None # if feature_box and not self.drag_start and self.is_rect_nonzero(face.track_box): # self.ROI = RegionOfInterest() # self.ROI.x_offset = min(self.image_size[0], max(0, int(roi_center[0] - roi_size[0] / 2))) # self.ROI.y_offset = min(self.image_size[1], max(0, int(roi_center[1] - roi_size[1] / 2))) # self.ROI.width = min(self.image_size[0], int(roi_size[0])) # self.ROI.height = min(self.image_size[1], int(roi_size[1])) # self.pubROI.publish(self.ROI) if feature_box is not None and len(face.features) > 0: return feature_box else: return None
def draw_rectangle(self, x, y, width, height): cv.Rectangle(self.image, (int(x), int(y)), (int(x + width), int(y + height)), cv.Scalar(255, 255, 255, 1.0))
if face: ((hx, hy, hw, hh), hn) = face cv.SetImageROI(image, (int(hx), int(hy), hw, hh)) template = cv.CloneImage(image) cv.ResetImageROI(image) w, h = cv.GetSize(template) width = W - w + 1 height = H - h + 1 result = cv.CreateImage((width, height), 32, 1) #calculate center centerX = int(hx) + cv.Round(hw / 2) centerY = int(hy) + cv.Round(hh / 2) if centerX > centerScreenX + 200: #print centerX-centerScreenX directSentry(4) elif centerX < centerScreenX - 200: #print centerScreenX-centerX directSentry(2) if centerY > centerScreenY + 200: #print centerY - centerScreenY directSentry(1) elif centerY < centerScreenY - 200: #print centerScreenY - centerY directSentry(3) #white box for face detection cv.Rectangle(image, (int(hx), int(hy)), (int(hx) + hw, int(hy) + hh), (255, 255, 255), 3, 0) cv.ShowImage('tempwindow', image) if cv.WaitKey(5) >= 0: break
def findHoughLines(): """ Uses the Hough transformation to find lines from the sensor readings and displays them """ global D # initialize lists D.lines = [] D.theta = [] D.distance = [] D.midpoint = [] # sensitivity options for Hough transformation threshold = 20 min_line_len = 10 max_gap_len = 30 line_width = 1 # source for Hough transformation has dots instead of lines src = D.hough # prepare image and destination for Hough transformation dst = cv.CreateImage(cv.GetSize(src), 8, 1) D.color_dst = cv.CreateImage(cv.GetSize(src), 8, 3) storage = cv.CreateMemStorage(0) lines = 0 cv.Canny(src, dst, 50, 200, 3) cv.CvtColor(dst, D.color_dst, cv.CV_GRAY2BGR) # apply Hough transformation to find walls # For more information, see: # http://docs.opencv.org/doc/tutorials/imgproc/imgtrans/hough_lines/hough_lines.html lines = cv.HoughLines2(dst, storage, cv.CV_HOUGH_PROBABILISTIC, line_width, \ pi / 180, threshold, min_line_len, max_gap_len) # draw the danger zone cv.Rectangle(D.image, (CENTER - 30, CENTER - 90), (CENTER + 30, CENTER - 30), cv.RGB(25, 25, 112), 2, 8) for line in lines: cv.Line(D.color_dst, line[0], line[1], cv.CV_RGB(0, 255, 0), 1, 8) # storing the lines and their distances D.lines.append((line[0], line[1])) x1 = float(line[0][0]) y1 = float(line[0][1]) x2 = float(line[1][0]) y2 = float(line[1][1]) x3 = float(CENTER) y3 = float(CENTER) # find the midpoint, the angle, and the distance to center midpoint = (int((x1 + x2) / 2), int((y1 + y2) / 2)) theta = atan2((y2 - y1), (x2 - x1)) / pi * 180 if (x2 - x1) != 0: slope = (y2 - y1) / (x2 - x1) intercept = (x2 * y1 - x1 * y2) / (x2 - x1) distance = abs(y3 - slope * x3 - intercept) / sqrt(slope**2 + 1) else: distance = abs(x2 - x3) cv.Line(D.image, line[0], line[1], cv.CV_RGB(0, 255, 0), 1, 8) cv.Line(D.image, midpoint, midpoint, cv.RGB(255, 255, 255), 4, 8) # add data to the list D.theta.append(theta) D.distance.append(distance) D.midpoint.append(midpoint)
# create the wanted images import cv image=cv.LoadImage('picture.png', cv.CV_LOAD_IMAGE_COLOR) grey=cv.CreateImage((100,100),8,1) eig = cv.CreateImage (cv.GetSize (grey), 32, 1) temp = cv.CreateImage (cv.GetSize (grey), 32, 1) # the default parameters quality = 0.01 min_distance = 10 # search the good points features = cv.GoodFeaturesToTrack ( grey, eig, temp, 1000, quality, min_distance, None, 3, 0, 0.04) for (x,y) in features: x) + ',' + str(y) cv.Circle (image, (int(x), int(y)), 3, (0, 255, 0), -1, 8, 0) cv.ResetImageROI(image) W,H=cv.GetSize(image) w,h=cv.GetSize(template) width=W-w+1 height=H-h+1 result=cv.CreateImage((width,height),32,1) cv.MatchTemplate(frame,template, result,cv.CV_TM_SQDIFF) (min_x,max_y,minloc,maxloc)=cv.MinMaxLoc(result) (x,y)=minloc cv.Rectangle(image2,(int(x),int(y)),(int(x)+w,int(y)+h),(255,255,255),1,0)
def red_eye(self): self.load_cascade_file() faces = [face for face in self.context.request.focal_points if face.origin == 'Face Detection'] if faces: engine = self.context.modules.engine mode, data = engine.image_data_as_rgb() mode = mode.lower() sz = engine.size image = cv.CreateImageHeader(sz, cv.IPL_DEPTH_8U, 3) cv.SetData(image, data) for face in faces: face_x = int(face.x - face.width / 2) face_y = int(face.y - face.height / 2) face_roi = ( int(face_x), int(face_y), int(face.width), int(face.height) ) cv.SetImageROI(image, face_roi) eyes = cv.HaarDetectObjects( image, self.cascade, cv.CreateMemStorage(0), HAAR_SCALE, MIN_NEIGHBORS, HAAR_FLAGS, MIN_SIZE) for (x, y, w, h), other in self.filter_eyes(eyes): # Set the image Region of interest to be the eye area [this reduces processing time] cv.SetImageROI(image, (face_x + x, face_y + y, w, h)) if self.context.request.debug: cv.Rectangle( image, (0, 0), (w, h), cv.RGB(255, 255, 255), 2, 8, 0 ) for pixel in self.get_pixels(image, w, h, mode): green_blue_avg = (pixel['g'] + pixel['b']) / 2 if not green_blue_avg: red_intensity = RED_THRESHOLD else: # Calculate the intensity compared to blue and green average red_intensity = pixel['r'] / green_blue_avg # If the red intensity is greater than 2.0, lower the value if red_intensity >= RED_THRESHOLD: new_red_value = (pixel['g'] + pixel['b']) / 2 # Insert the new red value for the pixel to the image cv.Set2D( image, pixel['y'], pixel['x'], cv.RGB(new_red_value, pixel['g'], pixel['b']) ) # Reset the image region of interest back to full image cv.ResetImageROI(image) self.context.modules.engine.set_image_data(image.tostring())
storage = cv.CreateMemStorage(0) contour = cv.FindContours(thresh_img, storage, cv.CV_RETR_CCOMP, cv.CV_CHAIN_APPROX_NONE) points = [] while contour: # Draw bounding rectangles bound_rect = cv.BoundingRect(list(contour)) if (cv.ContourArea(contour) > 200): pt1 = (bound_rect[0], bound_rect[1]) pt2 = (bound_rect[0] + bound_rect[2], bound_rect[1] + bound_rect[3]) points.append(pt1) points.append(pt2) cv.Rectangle(frame, pt1, pt2, cv.CV_RGB(255, 0, 0), 1) obj_mid = bound_rect[0] + (bound_rect[2] / 2) frame_mid = frame.width / 2 mid = frame_mid - obj_mid # only move if not near middle offset = abs(mid) if offset > 20: pandir = (mid / offset) else: pandir = 0 contour = contour.h_next() (leftmost, rightmost, topmost, bottommost) = getpositions(thresh_img)
def run(self): # Capture first frame to get size frame = cv.QueryFrame(self.capture) frame_size = cv.GetSize(frame) color_image = cv.CreateImage(cv.GetSize(frame), 8, 3) grey_image = cv.CreateImage(cv.GetSize(frame), cv.IPL_DEPTH_8U, 1) moving_average = cv.CreateImage(cv.GetSize(frame), cv.IPL_DEPTH_32F, 3) first = True while True: closest_to_left = cv.GetSize(frame)[0] closest_to_right = cv.GetSize(frame)[1] color_image = cv.QueryFrame(self.capture) # Smooth to get rid of false positives cv.Smooth(color_image, color_image, cv.CV_GAUSSIAN, 3, 0) if first: difference = cv.CloneImage(color_image) temp = cv.CloneImage(color_image) cv.ConvertScale(color_image, moving_average, 1.0, 0.0) first = False else: cv.RunningAvg(color_image, moving_average, 0.020, None) # Convert the scale of the moving average. cv.ConvertScale(moving_average, temp, 1.0, 0.0) # Minus the current frame from the moving average. cv.AbsDiff(color_image, temp, difference) # Convert the image to grayscale. cv.CvtColor(difference, grey_image, cv.CV_RGB2GRAY) # Convert the image to black and white. cv.Threshold(grey_image, grey_image, 70, 255, cv.CV_THRESH_BINARY) # Dilate and erode to get people blobs cv.Dilate(grey_image, grey_image, None, 18) cv.Erode(grey_image, grey_image, None, 10) storage = cv.CreateMemStorage(0) contour = cv.FindContours(grey_image, storage, cv.CV_RETR_CCOMP, cv.CV_CHAIN_APPROX_SIMPLE) points = [] while contour: bound_rect = cv.BoundingRect(list(contour)) contour = contour.h_next() pt1 = (bound_rect[0], bound_rect[1]) pt2 = (bound_rect[0] + bound_rect[2], bound_rect[1] + bound_rect[3]) points.append(pt1) points.append(pt2) cv.Rectangle(color_image, pt1, pt2, cv.CV_RGB(255, 0, 0), 1) if len(points): center_point = reduce( lambda a, b: ((a[0] + b[0]) / 2, (a[1] + b[1]) / 2), points) cv.Circle(color_image, center_point, 40, cv.CV_RGB(255, 255, 255), 1) # cv.Circle(color_image, center_point, 30, cv.CV_RGB(255, 100, 0), 1) # cv.Circle(color_image, center_point, 20, cv.CV_RGB(255, 255, 255), 1) # cv.Circle(color_image, center_point, 10, cv.CV_RGB(255, 100, 0), 1) print center_point cv.ShowImage("Target", color_image) # Listen for ESC key c = cv.WaitKey(7) % 0x100 if c == 27: break
# copy some pixels from the original image to the second one (minx, miny) = (150, 100) (maxx, maxy) = (300, 250) for x in range(minx, maxx): for y in range(miny, maxy): value = cv.Get2D(image, y, x) cv.Set2D(empty_image, y, x, value) # display both images print "showing original image plus a copied section (press any key to continue)" cv.ShowImage('window1', image) cv.ShowImage('window2', empty_image) cv.WaitKey(10000) # draw a blue rectangle around the copied section cv.Rectangle(empty_image, (minx, miny), (maxx, maxy), (255, 0, 0), 4) # draw green circles on the vertices cv.Circle(empty_image, (minx, miny), 8, (0, 255, 0), 2) cv.Circle(empty_image, (maxx, miny), 8, (0, 255, 0), 2) cv.Circle(empty_image, (minx, maxy), 8, (0, 255, 0), 2) cv.Circle(empty_image, (maxx, maxy), 8, (0, 255, 0), 2) # display both images print "showing original image plus a copied section with shapes (press any key to continue)" cv.ShowImage('window1', image) cv.ShowImage('window2', empty_image) cv.WaitKey(10000) # save the new image to the filesystem cv.SaveImage('image.png', empty_image)
def show_mask(name, m, rect): im = cv.CreateImage((m.shape[1], m.shape[0]), 32, 3) cv.SetData(im, np.ascontiguousarray(np.dstack(3 * [m]))) (t, l), (b, r) = rect cv.Rectangle(im, (t, l), (b, r), (255, 255, 0)) cv.ShowImage(name, im)
def process(args): '''process a set of files''' global slipmap, mosaic scan_count = 0 files = [] for a in args: if os.path.isdir(a): files.extend(glob.glob(os.path.join(a, '*.pgm'))) else: files.append(a) files.sort() num_files = len(files) print("num_files=%u" % num_files) region_count = 0 joes = [] if opts.mavlog: mpos = mav_position.MavInterpolator(gps_lag=opts.gps_lag) mpos.set_logfile(opts.mavlog) else: mpos = None if opts.boundary: boundary = cuav_util.polygon_load(opts.boundary) else: boundary = None if opts.mosaic: slipmap = mp_slipmap.MPSlipMap(service='GoogleSat', elevation=True, title='Map') icon = slipmap.icon('planetracker.png') slipmap.add_object(mp_slipmap.SlipIcon('plane', (0,0), icon, layer=3, rotation=0, follow=True, trail=mp_slipmap.SlipTrail())) C_params = cam_params.CameraParams(lens=opts.lens) path = os.path.join(os.path.dirname(os.path.realpath(__file__)), '..', '..', 'cuav', 'data', 'chameleon1_arecont0.json') C_params.load(path) mosaic = cuav_mosaic.Mosaic(slipmap, C=C_params) if boundary is not None: mosaic.set_boundary(boundary) if opts.joe: joes = cuav_util.polygon_load(opts.joe) if boundary: for i in range(len(joes)): joe = joes[i] if cuav_util.polygon_outside(joe, boundary): print("Error: joe outside boundary", joe) return icon = slipmap.icon('flag.png') slipmap.add_object(mp_slipmap.SlipIcon('joe%u' % i, (joe[0],joe[1]), icon, layer=4)) joelog = cuav_joe.JoeLog('joe.log') if opts.view: viewer = mp_image.MPImage(title='Image') frame_time = 0 for f in files: if mpos: frame_time = cuav_util.parse_frame_time(f) try: if opts.roll_stabilised: roll = 0 else: roll = None pos = mpos.position(frame_time, opts.max_deltat,roll=roll) slipmap.set_position('plane', (pos.lat, pos.lon), rotation=pos.yaw) except mav_position.MavInterpolatorException as e: print e pos = None else: pos = None # check for any events from the map if opts.mosaic: slipmap.check_events() mosaic.check_events() if f.endswith('.pgm'): pgm = cuav_util.PGM(f) im = pgm.array if pgm.eightbit: im_8bit = im else: im_8bit = numpy.zeros((960,1280,1),dtype='uint8') if opts.gamma != 0: scanner.gamma_correct(im, im_8bit, opts.gamma) else: scanner.reduce_depth(im, im_8bit) im_full = numpy.zeros((960,1280,3),dtype='uint8') scanner.debayer(im_8bit, im_full) im_640 = numpy.zeros((480,640,3),dtype='uint8') scanner.downsample(im_full, im_640) else: im_orig = cv.LoadImage(f) (w,h) = cuav_util.image_shape(im_orig) im_full = im_orig im_640 = cv.CreateImage((640, 480), 8, 3) cv.Resize(im_full, im_640, cv.CV_INTER_NN) im_640 = numpy.ascontiguousarray(cv.GetMat(im_640)) im_full = numpy.ascontiguousarray(cv.GetMat(im_full)) count = 0 total_time = 0 img_scan = im_640 t0=time.time() for i in range(opts.repeat): if opts.fullres: regions = scanner.scan(im_full) regions = cuav_region.RegionsConvert(regions, 1280, 960) else: regions = scanner.scan(img_scan) regions = cuav_region.RegionsConvert(regions) count += 1 t1=time.time() if opts.filter: regions = cuav_region.filter_regions(im_full, regions, frame_time=frame_time, min_score=opts.minscore, filter_type=opts.filter_type) scan_count += 1 # optionally link all the images with joe into a separate directory # for faster re-running of the test with just joe images if pos and opts.linkjoe and len(regions) > 0: cuav_util.mkdir_p(opts.linkjoe) if not cuav_util.polygon_outside((pos.lat, pos.lon), boundary): joepath = os.path.join(opts.linkjoe, os.path.basename(f)) if os.path.exists(joepath): os.unlink(joepath) os.symlink(f, joepath) if pos and len(regions) > 0: joelog.add_regions(frame_time, regions, pos, f, width=1280, height=960, altitude=opts.altitude) if boundary: regions = cuav_region.filter_boundary(regions, boundary, pos) region_count += len(regions) if opts.mosaic and len(regions) > 0: composite = cuav_mosaic.CompositeThumbnail(cv.GetImage(cv.fromarray(im_full)), regions) thumbs = cuav_mosaic.ExtractThumbs(composite, len(regions)) mosaic.add_regions(regions, thumbs, f, pos) if opts.compress: jpeg = scanner.jpeg_compress(im_full, opts.quality) jpeg_filename = f[:-4] + '.jpg' if os.path.exists(jpeg_filename): print('jpeg %s already exists' % jpeg_filename) continue chameleon.save_file(jpeg_filename, jpeg) if opts.view: if opts.fullres: img_view = im_full else: img_view = img_scan mat = cv.fromarray(img_view) for r in regions: (x1,y1,x2,y2) = r.tuple() (w,h) = cuav_util.image_shape(img_view) x1 = x1*w//1280 x2 = x2*w//1280 y1 = y1*h//960 y2 = y2*h//960 cv.Rectangle(mat, (max(x1-2,0),max(y1-2,0)), (x2+2,y2+2), (255,0,0), 2) cv.CvtColor(mat, mat, cv.CV_BGR2RGB) viewer.set_image(mat) total_time += (t1-t0) if t1 != t0: print('%s scan %.1f fps %u regions [%u/%u]' % ( f, count/total_time, region_count, scan_count, num_files))
def closest_point(self, pt): pos0, dpos = zip(*self.objects) est_pts = np.asarray(pos0)+np.asarray(dpos) min_dist = np.sum( est_pts def run(self): # Capture first frame to get size frame = cv.QueryFrame(self.capture) frame_size = cv.GetSize(frame) color_image = cv.CreateImage(cv.GetSize(frame), 8, 3) grey_image = cv.CreateImage(cv.GetSize(frame), cv.IPL_DEPTH_8U, 1) moving_average = cv.CreateImage(cv.GetSize(frame), cv.IPL_DEPTH_32F, 3) first = True while True: closest_to_left = cv.GetSize(frame)[0] closest_to_right = cv.GetSize(frame)[1] color_image = cv.QueryFrame(self.capture) # Smooth to get rid of false positives cv.Smooth(color_image, color_image, cv.CV_GAUSSIAN, 3, 0) if first: difference = cv.CloneImage(color_image) temp = cv.CloneImage(color_image) cv.ConvertScale(color_image, moving_average, 1.0, 0.0) first = False else: cv.RunningAvg(color_image, moving_average, 0.020, None) # Convert the scale of the moving average. cv.ConvertScale(moving_average, temp, 1.0, 0.0) # Minus the current frame from the moving average. cv.AbsDiff(color_image, temp, difference) # Convert the image to grayscale. cv.CvtColor(difference, grey_image, cv.CV_RGB2GRAY) # Convert the image to black and white. cv.Threshold(grey_image, grey_image, 70, 255, cv.CV_THRESH_BINARY) # Dilate and erode to get people blobs cv.Dilate(grey_image, grey_image, None, 18) cv.Erode(grey_image, grey_image, None, 10) storage = cv.CreateMemStorage(0) contour = cv.FindContours(grey_image, storage, cv.CV_RETR_CCOMP, cv.CV_CHAIN_APPROX_SIMPLE) objects = [] while contour: bound_rect = cv.BoundingRect(list(contour)) contour = contour.h_next() pt1 = (bound_rect[0], bound_rect[1]) pt2 = (bound_rect[0] + bound_rect[2], bound_rect[1] + bound_rect[3]) cv.Rectangle(color_image, pt1, pt2, cv.CV_RGB(255,0,0), 1) center_point = ((pt1[0] + pt2[0]) / 2, (pt1[1] + pt2[1]) / 2), points) if len(objects): closest
self.eye_cascade = cv2.CascadeClassifier( '/usr/local/Cellar/opencv/2.4.7.1/share/OpenCV/haarcascades/haarcascade_eye.xml' ) # used for storing and recalling the face image history: self.storage = cv.CreateMemStorage(0) self.last_face_position = None self.detect_times = [] self.eye_pair_history = [] self.xamount_histories = [[], []] self.yamount_histories = [[], []] self.xpos_history = [] self.ypos_history = [] def drawrect(self, image, (x, y, w, h), color=(0, 0, 255)): cv.Rectangle(image, (x, y), (x + w, y + h), cv.RGB(*color), 3, 8, 0) # TODO: put draw pupil, draw_plus, and maybe drawrect in the same function and request parameters when called def draw_pupil(self, face_img, pupcoord_normalized, (ex, ey, ew, eh), rad=5, color=(0, 0, 255)): pupcoord = ((pupcoord_normalized[0] + 1) / 2 * ew + ex, (pupcoord_normalized[1] + 1) / 2 * eh + ey) pupcoord = tuple([int(round(p)) for p in pupcoord]) cv2.circle(numpy.asarray(face_img[:, :]), pupcoord, rad, color) def draw_plus(self, image,
def show(area): cv.Rectangle(img,(area[0][0],area[0][1]), (area[0][0]+area[0][2],area[0][1]+area[0][3]), (255,0,0),2)
storage = cv.CreateMemStorage(0) contour = cv.FindContours(imgyellowthresh, storage, cv.CV_RETR_CCOMP, cv.CV_CHAIN_APPROX_SIMPLE) points = [] # This is the new part here. ie Use of cv.BoundingRect() while contour: # Draw bounding rectangles bound_rect = cv.BoundingRect(list(contour)) contour = contour.h_next() print contour # for more details about cv.BoundingRect,see documentation pt1 = (bound_rect[0], bound_rect[1]) pt2 = (bound_rect[0] + bound_rect[2], bound_rect[1] + bound_rect[3]) points.append(pt1) points.append(pt2) cv.Rectangle(color_image, pt1, pt2, cv.CV_RGB(255,0,0), 1) # Calculating centroids centroidx=cv.Round((pt1[0]+pt2[0])/2) centroidy=cv.Round((pt1[1]+pt2[1])/2) # Identifying if blue or yellow blobs and adding centroids to corresponding lists if (20<cv.Get2D(imghsv,centroidy,centroidx)[0]<30): yellow.append((centroidx,centroidy)) elif (100<cv.Get2D(imghsv,centroidy,centroidx)[0]<120): blue.append((centroidx,centroidy)) # Now drawing part. Exceptional handling is used to avoid IndexError. After drawing is over, centroid from previous part is # removed from list by pop. So in next frame,centroids in this frame become initial points of line to draw. try: cv.Circle(imdraw,yellow[1],5,(0,255,255))
while True: video = kinect.get_video() width = video.width height = video.height rcenter = (width / 4, height / 4, width / 2, height * 3 / 4) bincount = 4 mask = cv.CreateImage((width, height), 8, 1) cv.Zero(mask) cv.SetImageROI(mask, rcenter) cv.Set(mask, 1) cv.ResetImageROI(mask) cv.Rectangle(video, (width / 4, height / 4), (width * 3 / 4, height), 255) # define our 'control region' xleft = int(leftLimit * width) xright = int(rightLimit * width) faces = cv.HaarDetectObjects(video, hc, ms, 1.25, 2, 0, (50, 50)) # move the robot toward the first face it sees for (x, y, w, h), n in faces: cv.Rectangle(video, (x, y), (x + w, y + h), 255) if x + w > xright: bot.moveBot("right", 50) print "right" # sleep(0.2) # bot.stopBot() elif x < xleft: bot.moveBot("left", 50)
boxWidth = box[right][0] - box[left][0] boxHeight = box[bottom][0] - box[top][0] boxAreas.append(boxWidth * boxHeight) averageBoxArea = 0.0 if len(boxAreas): averageBoxArea = float(sum(boxAreas) / len(boxAreas)) trimmedBoxList = [] for box in boundingBoxList: boxWidth = box[right][0] - box[left][0] boxHeight = box[bottom][0] - box[top][0] # remove the box if it's smaller than our noise threshold if (boxWidth * boxHeight) > 0.05 * averageBoxArea: trimmedBoxList.append(box) boundingBoxList = mergeOverlappingBoxes(trimmedBoxList) #Draw the boxes for box in boundingBoxList: cv.Rectangle(displayImage, box[0], box[1], cv.CV_RGB(0, 255, 0), 1) estimatedTargetCount = len(boundingBoxList) cv.ShowImage("Target", displayImage) frameCount += 1 print "FPS: %f" % (frameCount / (time.time() - t0)) cv.DestroyAllWindows()
def draw_from_points(cv_image, points): """Takes the cv_image and points and draws a rectangle based on the points. Returns a cv_image.""" for (x, y, w, h), n in points: cv.Rectangle(cv_image, (x, y), (x + w, y + h), 255) return cv_image
def meet(IP, PORT, fighter): global motionProxy global post global sonarProxy global memoryProxy global cameraProxy global videoClient # work ! set current to servos stiffnesses = 1.0 time.sleep(0.5) # TEMP pNames = "Body" pStiffnessLists = 0.0 pTimeLists = 1.0 motionProxy.stiffnessInterpolation(pNames, pStiffnessLists, pTimeLists) # Get a camera image. # image[6] contains the image data passed as an array of ASCII chars. naoImage = cameraProxy.getImageRemote(videoClient) imageWidth = naoImage[0] imageHeight = naoImage[1] found = True posx = 0 posy = 0 mem = cv.CreateMemStorage(0) i = 0 cv.NamedWindow("Real") cv.MoveWindow("Real", 0, 0) cv.NamedWindow("Threshold") cv.MoveWindow("Real", imageWidth + 100, 0) error = 0.0 nframe = 0.0 closing = 3 tstp, tu = 0, 0 K = 2 try: while found: nframe = nframe + 1 # Get current image (top cam) naoImage = cameraProxy.getImageRemote(videoClient) # Get the image size and pixel array. imageWidth = naoImage[0] imageHeight = naoImage[1] array = naoImage[6] # Create a PIL Image from our pixel array. pilImg = Image.fromstring("RGB", (imageWidth, imageHeight), array) # Convert Image to OpenCV cvImg = cv.CreateImageHeader((imageWidth, imageHeight), cv.IPL_DEPTH_8U, 3) cv.SetData(cvImg, pilImg.tostring()) cvImg2 = cv.CreateImageHeader((imageWidth, imageHeight), cv.IPL_DEPTH_8U, 3) cv.SetData(cvImg2, pilImg.tostring()) cv.CvtColor(cvImg, cvImg, cv.CV_RGB2BGR) hsv_img = cv.CreateImage(cv.GetSize(cvImg), 8, 3) cv.CvtColor(cvImg, hsv_img, cv.CV_BGR2HSV) thresholded_img = cv.CreateImage(cv.GetSize(hsv_img), 8, 1) thresholded_img2 = cv.CreateImage(cv.GetSize(hsv_img), 8, 1) imcv2 = cv2.cvtColor(np.asarray(pilImg), cv2.COLOR_RGB2BGR) # Get the orange on the image cv.InRangeS(hsv_img, (0, 150, 150), (40, 255, 255), thresholded_img) cv.InRangeS(hsv_img, (0, 150, 150), (40, 255, 255), thresholded_img2) storage = cv.CreateMemStorage(0) contour = cv.FindContours(thresholded_img, storage, cv.CV_RETR_CCOMP, cv.CV_CHAIN_APPROX_SIMPLE) cv.Smooth(thresholded_img, thresholded_img, cv.CV_GAUSSIAN, 5, 5) cv.Erode(thresholded_img, thresholded_img, None, closing) cv.Dilate(thresholded_img, thresholded_img, None, closing) storage = cv.CreateMemStorage(0) contour = cv.FindContours(thresholded_img, storage, cv.CV_RETR_CCOMP, cv.CV_CHAIN_APPROX_SIMPLE) points = [] d = [] data = [] while contour: # Draw bounding rectangles bound_rect = cv.BoundingRect(list(contour)) contour = contour.h_next() # for more details about cv.BoundingRect,see documentation pt1 = (bound_rect[0], bound_rect[1]) pt2 = (bound_rect[0] + bound_rect[2], bound_rect[1] + bound_rect[3]) points.append(pt1) points.append(pt2) cv.Rectangle(cvImg2, pt1, pt2, cv.CV_RGB(255, 0, 0), 1) lastx = posx lasty = posy posx = cv.Round((pt1[0] + pt2[0]) / 2) posy = cv.Round((pt1[1] + pt2[1]) / 2) data.append([posx, posy]) d.append(math.sqrt(pt1[0]**2 + pt2[0]**2)) d.append(math.sqrt(pt1[1]**2 + pt2[1]**2)) cvImg2, error, centroid, labels = clustering( data, cvImg2, nframe, error, K) # Update the closing size towards the number of found labels if labels.size < 2: closing = 1 if labels.size < 6: closing = 2 if labels.size > 10: closing = 3 if closing < 1: closing = 0 if centroid.size != 0: uh = 0 try: x = int(centroid[0][0]) y = int(centroid[0][1]) except: print "NaN" l = memoryProxy.getData( "Device/SubDeviceList/US/Left/Sensor/Value") r = memoryProxy.getData( "Device/SubDeviceList/US/Right/Sensor/Value") # Commande proportionnelles pour le cap et la distance kh = 20.0 / (imageWidth / 2) km = 0.25 uh = -kh * (x - imageWidth / 2) um = km * (math.sqrt(l**2 + r**2) - 0.6) if (uh > 4 or uh < -4): motionProxy.moveTo(0.0, 0.0, uh * almath.TO_RAD) tu = time.time() if (um > 0.03 or um < -0.03) and (l > 0.6 and r > 0.6): motionProxy.moveTo(um, 0.0, 0.0) tu = time.time() else: # quand il est proche mettre K=1, on admet qu il n y a plus de parasites, et que les zones oranges sont assez # grosse pour leur appliquer un coef ce fermeture eleve # print "-------------------------" # print "K = 1" K = 1 closing = 5 tstp = time.time() # print "l",l # print "r",r # print "um ",um #Temps de repos du Nao tact = tstp - tu #S'il attend plus de 5sec, il admet qu'il est devant sa cible if tact > 5: found = False if fighter == "dark": #tts.say("I've been waiting for you, Obi-Wan. We meet again, at last. The circle is now complete. When I left you, I was but the learner; now I am the master.") #time.sleep(1.0) tts.say("I see you") if fighter == "obi": #time.sleep(15.0) #tts.say("Only a master of evil, Darth.") tts.say("I see you") cv.ShowImage("Real", cvImg) #cv.ShowImage("Threshold",thresholded_img2) cv.ShowImage("Threshold", thresholded2) cv.WaitKey(1) except KeyboardInterrupt: print print "Interrupted by user, shutting down" end(IP, PORT)
def main(): captura = cv.CreateCameraCapture( 1) ##guardamos la imagen de la camara web usb global arra ##cargamos el arreglo de los objetos font = cv.InitFont(cv.CV_FONT_HERSHEY_SIMPLEX, 1, 1, 0, 3, 3) #creamos el fondo para las letras proses = 0 sumaa = 0 while True: img = cv.QueryFrame(captura) #cv.Resize(img,img,cv.CV_INTER_CUBIC) #tiempoi = time.time() #draw = ImageDraw.Draw(img) anch, alt = cv.GetSize(img) ##obtenemos las dimensiones k = cv.WaitKey(10) ##esperemos para cualquier incombeniente #cv.SaveImage("test.jpg",img) cv.Smooth(img, img, cv.CV_GAUSSIAN, 9, 9) ##aplicamos filtro para reducir el ruido #cv.SaveImage("sruido.jpg",img) grey = cv.CreateImage(cv.GetSize(img), 8, 1) ##creamos una imagen en blanco bn = cv.CreateImage(cv.GetSize(img), 8, 1) ##creamos imagen en blanco cv.CvtColor( img, grey, cv.CV_BGR2GRAY ) ###pasamos la imagen a escala de grises y la guardamos en la imagen ne blanco #cv.SaveImage("gris.jpg",grey) cv.ConvertImage(img, bn, 0) ##convertimos la imagen a blancos threshold = 40 ##umbral 1 para binarizacion colour = 255 ## umbral 2 para binarizacion cv.Threshold(grey, grey, threshold, colour, cv.CV_THRESH_BINARY) ##aplicamos binarizacion cv.Canny( grey, bn, 1, 1, 3 ) ##preparamos para obtener contornos, esto nos muestra la imagen con los contornos #cv.SaveImage("cont.jpg",bn) cv.SaveImage("grey.jpg", grey) ##guardamos la imagen cambio("grey.jpg") ##invertimos la imagen y discretisamos imgg = cv.LoadImage( 'ngrey.jpg', cv.CV_LOAD_IMAGE_GRAYSCALE) ##cargamos nuevamente la imagen storage = cv.CreateMemStorage( 0) ##para guardar los puntos y no saturar la memoria contours = cv.FindContours( imgg, storage, cv.CV_RETR_TREE, cv.CV_CHAIN_APPROX_SIMPLE, (0, 0)) ##obtener los puntos de los contornos puntos = [ ] ##para guardar los diferente centros de los objetos y verificarlas posteriormente while contours: ##leemos los contornos nx, ny = contours[ 0] ##para verificar donde se encuentra los centros de la figura u bojeto mx, my = contours[0] ## ancho, altura = cv.GetSize(img) ##obtenemos el tama de la imagen for i in range(len(contours)): ##verificamos las esquinas xx, yy = contours[i] if xx > mx: mx = xx if xx < nx: nx = xx if yy > my: my = yy if yy < ny: ny = yy a, b, c, d = random.randint(0, 255), random.randint( 0, 255), random.randint(0, 255), random.randint(0, 255) ##para el color if len(contours ) >= 50: ##si son mas de 50 puntos es tomada como figura cv.Rectangle(img, (nx, ny), (mx, my), cv.RGB(0, 255, 0), 1, 8, 0) ##pintamos el rectangulo con las esquinas #are = abs(mx-nx)*abs(my-ny) puntos.append((abs(mx + nx) / 2, abs(my + ny) / 2)) ##agregamos los centros #are = abs(mx-nx)*abs(my-ny) contours = contours.h_next( ) #pasamos con los siguientes puntos unidos nuevo = de( puntos, anch, alt ) ##verificamos los objetos y obtenemos los centros de los mismos for i in range(len(nuevo)): ## pintamos la direccin de los mismos x, y, z = nuevo[i] cv.PutText(img, "" + z + "", (x, y), font, 255) tiempof = time.time() ##verificar rendimiento cv.ShowImage('img', img) #cv.SaveImage("final.jpg",img) #tiempoa = tiempof - tiempoi #proses += 1 #sumaa = sumaa + tiempoa #print float(sumaa)/float(proses) #f.write(""+str(proses)+" "+str(tiempoa)+"\n") ##verificar rendimientp if k == 'f': ##si se preciona f se sale break
for (x, y, w, h), n in detected: faces.append((x, y, w, h)) return faces if __name__ == "__main__": cv.NamedWindow("Video", cv.CV_WINDOW_AUTOSIZE) capture = cv.CaptureFromCAM(CAMERA_INDEX) storage = cv.CreateMemStorage() cascade = cv.Load(HAAR_CASCADE_PATH) faces = [] c = -1 i = 0 while (c == -1): image = cv.QueryFrame(capture) # Only run the Detection algorithm every 5 frames to improve performance if i % 5 == 0: faces = detect_faces(image) for (x, y, w, h) in faces: cv.Rectangle(image, (x, y), (x + w, y + h), 255) cv.PutText(image, "Hello Suresh", (x + 20, y - 20), font, 255) #cv.Rectangle(image, (50,50), (100,100), 255) cv.ShowImage("Video", image) i += 1 c = cv.WaitKey(10)
def run(self): # Capture first frame to get size frame = cv.QueryFrame(self.capture) frame_size = cv.GetSize(frame) new_size = ( frame_size[0] / 2, frame_size[1] / 2) color_image = cv.CreateImage(new_size, 8, 3) grey_image = cv.CreateImage(new_size, cv.IPL_DEPTH_8U, 1) moving_average = cv.CreateImage(new_size, cv.IPL_DEPTH_32F, 3) font = cv.InitFont(cv.CV_FONT_HERSHEY_COMPLEX_SMALL, 1, 1, 0, 1, 1) first = True k = 0 while True: k+=1 captured_image = cv.QueryFrame(self.capture) color_image = cv.CreateImage(new_size, captured_image.depth, captured_image.nChannels) cv.Resize(captured_image, color_image) # Smooth to get rid of false positives cv.Smooth(color_image, color_image, cv.CV_GAUSSIAN, 3, 0) if first: difference = cv.CloneImage(color_image) temp = cv.CloneImage(color_image) cv.ConvertScale(color_image, moving_average, 1.0, 0.0) first = False else: cv.RunningAvg(color_image, moving_average, 0.020, None) # Convert the scale of the moving average. cv.ConvertScale(moving_average, temp, 1.0, 0.0) # Minus the current frame from the moving average. cv.AbsDiff(color_image, temp, difference) # Convert the image to grayscale. cv.CvtColor(difference, grey_image, cv.CV_RGB2GRAY) # Convert the image to black and white. cv.Threshold(grey_image, grey_image, 70, 255, cv.CV_THRESH_BINARY) # Dilate and erode to get people blobs cv.Dilate(grey_image, grey_image, None, 18) cv.Erode(grey_image, grey_image, None, 10) storage = cv.CreateMemStorage(0) contour = cv.FindContours(grey_image, storage, cv.CV_RETR_CCOMP, cv.CV_CHAIN_APPROX_TC89_KCOS) points = [] #cv.DrawContours(color_image, contour, cv.CV_RGB(255,0,0), cv.CV_RGB(255,0,255), 2, 1, 8, (0, 0)) i = 0 while contour: self.observed_occupancy = True bound_rect = cv.BoundingRect(list(contour)) center_x = bound_rect[0] + (bound_rect[2]/2) center_y = bound_rect[1] + (bound_rect[3]/2) #if center_y < 200: # continue i+=1 closest_distance = 10000 closest_object = None for to in self.tracked_objects: current_distance = math.hypot(to.latest_position[0] - center_x, to.latest_position[1] - center_y) closest_distance = min(closest_distance, current_distance) #print "DISTANCES: ", str(closest_distance), str(current_distance) if current_distance == closest_distance: closest_object = to if closest_object is None: #print "OBJECT IS NEW" self.tracked_objects.append(TrackedObject((center_x, center_y), [(center_x, center_y)], "new")) else: #print "CLOSEST OBJECT: ", closest_object.latest_position closest_object.movement_vector.append((center_x, center_y)) closest_object.latest_position = (center_x, center_y) #print "AMOUNT OF OBJECTS: ", str(len(self.tracked_objects)) if closest_object is not None: cv.Line(color_image, closest_object.latest_position, (center_x, center_y), cv.CV_RGB(0,255,0)) #closest_x = min(closest_x, to.latest_position[0]) #closest_y = min(closest_y, to.latest_position[0]) contour = contour.h_next() pt1 = (bound_rect[0], bound_rect[1]) pt2 = (bound_rect[0] + bound_rect[2], bound_rect[1] + bound_rect[3]) points.append(pt1) points.append(pt2) cv.Rectangle(color_image, pt1, pt2, cv.CV_RGB(0,0,255), 1) cv.PutText(color_image, str(i), pt1, font, cv.CV_RGB(255,0,255)) cv.Circle(color_image, (center_x, center_y), 2, cv.CV_RGB(255,0,255), 2, 8, 0) #print "LEN ", len(self.tracked_objects) #if len(self.tracked_objects) > 0 and self.tracked_objects[0] is not None: # #print "ENTRE" # obj_vector = self.tracked_objects[0].movement_vector # print "MVV LEN ", len(obj_vector) # for index in range(0, len(obj_vector)-2): # try: # print "Index ", index, "len(obj_vector) ", len(obj_vector) # cv.Line(color_image, obj_vector[index], obj_vector[index+1], cv.CV_RGB(0,255,0)) # # except: print "oops" #print "Iteration ", k, " Vector: ", vectors["1"] cv.ShowImage("Target", color_image) time_passed = time.time() - self.last_request request_threshold = 60 if time_passed > request_threshold: self.send_occupancy() self.send_image(color_image) #Listen for ESC key c = cv.WaitKey(10) #c = cv.WaitKey(7) % 0x100 if c == 27: break
def segment_rect(image, rect, debug=False, display=None, target_size=None, group_range=(3, 25)): global next skip = False best_chars = [] best_threshold = None thresholded = cv.CloneImage(image) contour_image = cv.CloneImage(image) edges = cv.CloneImage(image) min_x, min_y, width, height = rect # cv.SetImageROI(thresholded, rect) cv.SetImageROI(contour_image, rect) cv.SetImageROI(image, rect) cv.SetImageROI(edges, rect) horizontal = cv.CreateImage(cv.GetSize(image), cv.IPL_DEPTH_16S, 1) magnitude32f = cv.CreateImage(cv.GetSize(image), cv.IPL_DEPTH_32F, 1) vertical = cv.CloneImage(horizontal) magnitude = cv.CloneImage(horizontal) cv.Sobel(image, horizontal, 0, 1, 3) cv.Sobel(image, vertical, 1, 0, 3) cv.Pow(horizontal, horizontal, 2) cv.Pow(vertical, vertical, 2) cv.Add(vertical, horizontal, magnitude) cv.Convert(magnitude, magnitude32f) cv.Pow(magnitude32f, magnitude32f, 0.5) cv.Convert(magnitude32f, edges) original_rect = rect if display: cv.SetImageROI(display, rect) for threshold in range(1, 20, 1): cv.SetImageROI(thresholded, original_rect) #for i in range(30, 60, 1): if display: cv.Merge(image, image, image, None, display) cv.Copy(image, thresholded) #cv.Threshold(thresholded, thresholded, i, 255, cv.CV_THRESH_BINARY_INV) cv.AdaptiveThreshold(thresholded, thresholded, 255, cv.CV_ADAPTIVE_THRESH_MEAN_C, cv.CV_THRESH_BINARY_INV, 17, threshold) #cv.AdaptiveThreshold(thresholded, thresholded, 255, cv.ADAPTIVE_THRESH_GAUSSIAN_C, cv.THRESH_BINARY_INV, 5, i) # skip rects greater than 50% thresholded summed = cv.Norm(thresholded, None, cv.CV_L1, None) / 255 / thresholded.width / thresholded.height if summed > 0.5: continue if debug: cv.ShowImage("edge", thresholded) storage = cv.CreateMemStorage(0) cv.Copy(thresholded, contour_image) contours = cv.FindContours(contour_image, storage, cv.CV_RETR_LIST, cv.CV_CHAIN_APPROX_SIMPLE, (0, 0)) ext.filter_contours(contours, 20, ext.LESSTHAN) groups = [] rects = [] edge_counts = [] overlappings = {} if contours: seq = contours while seq: c = ext.as_contour(ext.wrapped(seq)) r = (c.rect.x, c.rect.y, c.rect.width, c.rect.height) rects.append(r) seq = seq.h_next() similarity = 0.45 #0.3 rects.sort(lambda x, y: cmp(y[2] * y[3], x[2] * x[3])) for rect in rects: if debug: print print "R", rect, len(groups) cv.SetImageROI(edges, (original_rect[0] + rect[0], original_rect[1] + rect[1], rect[2], rect[3])) edge_count = cv.Sum(edges)[0] / 255 / (rect[2] * rect[3]) edge_counts.append(edge_count) # cv.ShowImage("edges", edges) # cv.WaitKey(0) if debug and target_size: print "X", target_size, rect print(target_size[0] - rect[2]) / target_size[0] print(target_size[1] - rect[3]) / target_size[1] if rect[2] > rect[3] or float(rect[3])/rect[2] < 3./3 or edge_count < 0.1\ or (rect[2] == image.width and rect[3] == image.height) \ or (target_size and not 0 < (target_size[0] - rect[2]) / target_size[0] < 0.3 \ and not 0 < (target_size[1] - rect[3]) / target_size[1] < 0.05): if debug: print "rej", rect[2], ">", rect[3], "edge=", edge_count cv.Rectangle(display, (rect[0], rect[1]), (rect[0] + rect[2], rect[1] + rect[3]), (0, 0, 255), 1) cv.ShowImage("main", display) if not skip and not next: c = cv.WaitKey(0) if c == ord("a"): skip = True if c == ord("z"): next = True continue added = False for group_id, group in enumerate(groups): avg_width, avg_height, avg_y = 0, 0, 0 overlap = None c = 0 for r in group: avg_y += r[1] + r[3] / 2.0 avg_width += r[2] avg_height += r[3] irect = intersect(r, rect) if irect[2] * irect[3] > 0.2 * r[2] * r[3]: overlappings.setdefault(group_id, []).append([r, rect]) avg_y /= float(len(group)) avg_width /= float(len(group)) avg_height /= float(len(group)) if debug: print group if (abs(avg_width - rect[2]) / avg_width < similarity or \ (rect[2] < avg_width)) and \ abs(avg_height - rect[3])/ avg_height < similarity and \ abs(avg_y - (rect[1] + rect[3]/2.0)) / avg_y < similarity: group.append(rect) added = True else: pass if not added: # first char in group groups.append([rect]) if debug: print "now:" for g in groups: print g cv.Rectangle(display, (rect[0], rect[1]), (rect[0] + rect[2], rect[1] + rect[3]), (255, 0, 0), 1) cv.ShowImage("main", display) if not skip and not next: c = cv.WaitKey(0) if c == ord("a"): skip = True if c == ord("z"): next = True if groups: #handle overlapping regions, default to average width match for group_id, over in overlappings.items(): group = groups[group_id] avg_width = 0 avg_height = 0 for r in group: avg_width += r[2] avg_height += r[3] avg_width /= float(len(group)) avg_height /= float(len(group)) for r1, r2 in over: if r2 not in group or r1 not in group: continue if debug: print "over", r1, r2, r1[2] * r1[3], r2[2] * r2[ 3], avg_width d1 = abs(r1[2] - avg_width) + abs(r1[3] - avg_height) d2 = abs(r2[2] - avg_width) + abs(r2[3] - avg_height) if d1 < d2: group.remove(r2) else: group.remove(r1) #group = max(groups, key=len) # from longest groups, find largest area groups.sort(key=len) groups.reverse() max_area = 0 mad_index = -1 for i, g in enumerate(groups[:5]): area = 0 for r in g: area += r[2] * r[3] if area > max_area: max_area = area max_index = i group = groups[max_index] # vertical splitting avg_width, avg_height, avg_y = 0, 0, 0 if debug: print "G", group for r in group: avg_y += r[1] + r[3] / 2.0 avg_width += r[2] avg_height += r[3] avg_y /= float(len(group)) avg_width /= float(len(group)) avg_height /= float(len(group)) band_rects = [] bound = bounding_rect(group) for i, rect in enumerate(rects): if edge_counts[i] < 0.1: continue if (abs(avg_width - rect[2]) / avg_width < similarity or \ (rect[2] < avg_width)) and \ (abs(avg_height - rect[3]) / avg_height < similarity or \ (rect[3] < avg_height)) and \ abs(avg_y - (rect[1] + rect[3]/2.0)) < avg_height/2: band_rects.append(rect) band_rects.sort(lambda x, y: cmp(y[2] * y[3], x[2] * x[3])) for i, rect_a in enumerate(band_rects[:-1]): if rect_a[2] * rect_a[3] < 0.2 * avg_width * avg_height: continue merge_rects = [] for rect_b in band_rects[i + 1:]: w = avg_width m1 = rect_a[0] + rect_a[2] / 2 m2 = rect_b[0] + rect_b[2] / 2 if abs(m1 - m2) < w: merge_rects.append(rect_b) if debug: print "M", merge_rects if merge_rects: merge_rects.append(rect_a) rect = bounding_rect(merge_rects) area = 0 for r in merge_rects: area += r[2] * r[3] if (abs(avg_width - rect[2]) / avg_width < similarity or \ (rect[2] < avg_width)) and \ abs(avg_height - rect[3])/ avg_height < similarity and \ area > 0.5*(avg_width*avg_height) and \ abs(avg_y - (rect[1] + rect[3]/2.0)) / avg_y < similarity: for r in merge_rects: if r in group: group.remove(r) # merge into group new_group = [] merged = False for gr in group: area2 = max(gr[2] * gr[3], rect[2] * rect[3]) isect = intersect(gr, rect) if isect[2] * isect[3] > 0.4 * area2: x = min(gr[0], rect[0]) y = min(gr[1], rect[1]) x2 = max(gr[0] + gr[2], rect[0] + rect[2]) y2 = max(gr[1] + gr[3], rect[1] + rect[3]) new_rect = (x, y, x2 - x, y2 - y) new_group.append(new_rect) merged = True else: new_group.append(gr) if not merged: new_group.append(rect) group = new_group cv.Rectangle(display, (rect[0], rect[1]), (rect[0] + rect[2], rect[1] + rect[3]), (255, 0, 255), 2) # avoid splitting split = False # select higher threshold if innovates significantly best_width = 0.0 if best_chars: best_area = 0.0 for rect in best_chars: best_area += rect[2] * rect[3] best_width += rect[2] best_width /= len(best_chars) area = 0.0 overlapped = 0.0 avg_width = 0.0 avg_height = 0.0 for rect in group: area += rect[2] * rect[3] avg_width += rect[2] avg_height += rect[3] for char in best_chars: section = intersect(rect, char) if section[2] * section[3] > 0: overlapped += section[2] * section[3] avg_width /= len(group) avg_height /= len(group) quotient = overlapped / area quotient2 = (area - overlapped) / best_area if debug: print area, overlapped, best_area print group print "QUO", quotient print "QUO2", quotient2 else: quotient = 0 quotient2 = 1 best_area = 0 group.sort(lambda x, y: cmp(x[0] + x[2] / 2, y[0] + y[2] / 2)) best_chars.sort(lambda x, y: cmp(x[0] + x[2] / 2, y[0] + y[2] / 2)) if group_range[0] <= len(group) <= group_range[1] and avg_width > 5 and avg_height > 10 and \ ((quotient2 > 0.05 and (best_area == 0 or abs(area - best_area)/best_area < 0.4)) or (quotient2 > 0.3 and area > best_area)): if debug: print "ASSIGNED", group best_chars = group best_threshold = threshold #get_patch(thresholded, original_rect) else: if debug: print "not", quotient2, len( group), avg_width, avg_height, area, best_area # best_chars = groups if debug: for rect in best_chars: cv.Rectangle(display, (rect[0], rect[1]), (rect[0] + rect[2], rect[1] + rect[3]), (0, 255, 0), 1) cv.ShowImage("main", display) if not skip and not next: c = cv.WaitKey(0) if c == ord("a"): skip = True if c == ord("z"): next = True best_chars.sort(lambda x, y: cmp(x[0], y[0])) cv.ResetImageROI(thresholded) cv.ResetImageROI(contour_image) cv.ResetImageROI(image) cv.ResetImageROI(edges) if display: cv.ResetImageROI(display) return best_chars, best_threshold
def blockfacemask(bgrimg): global lastrects rects = detect_faces(bgrimg) rects = addheights(rects) newimg = im.newgray(bgrimg) cv.Set(newimg, 255) if rects: lastrects = rects else: rects = lastrects if rects: for rect in rects: cv.SetImageROI(newimg, rect) cv.Set(newimg, 0) cv.ResetImageROI(newimg) return newimg if __name__ == '__main__': cam = cv.CaptureFromCAM(0) while True: img = cv.QueryFrame(cam) img = im.resize(img, width=400) faces = detect_faces(img) for f in faces: cv.Rectangle(img, (f[0], f[1]), (f[0] + f[2], f[1] + f[3]), im.color.RED, thickness=3) cv.Flip(img, None, 1) cv.ShowImage('face', img)
def recognize(image, display=None, visualize=None, clean=False): rects = detect(image, debug=0, display=display) candidates = segment(image, rects, debug=0, display=display) # return font = cv.InitFont(cv.CV_FONT_VECTOR0, 0.4, 0.4, 0.0, 0, 0) source = cv.CreateImage((image.width, image.height), 8, 1) source2 = cv.CreateImage((image.width, image.height), 8, 1) mask = cv.CloneImage(source) cv.CvtColor(image, source, cv.CV_BGR2GRAY) for i, c in enumerate(candidates): window_name = "candidate%s" % i rect = c["rect"] invert = c["invert"] if visualize: cv.SetImageROI(source, rect) cv.SetImageROI(source2, rect) cv.SetImageROI(mask, rect) bound_rect = bounding_rect(c["chars"]) rects_to_mask([bound_rect], mask, value=255) cv.Zero(source2) edge = edge_threshold(source) cv.Copy(edge, source2, mask) text1, conf1 = run_tesseract(source) text2, conf2 = run_tesseract(source2) cv.ShowImage("source", source) cv.ShowImage("thresholded", source2) cv.ShowImage("edge", edge) cv.ShowImage("mask", mask) cv.WaitKey(5) gray = (150, 150, 150) col1, col2, col3 = gray, gray, gray k = 70 if np.mean(conf1) >= np.mean(conf2): if any(conf1 > k): if any(conf2 > k) and len(text2) > len(text1) * 2: col2 = (0, 255, 0) else: col1 = (0, 255, 0) col3 = (0, 255, 0) else: if any(conf2 > k): if any(conf1 > k) and len(text1) > len(text2) * 2: col1 = (0, 255, 0) else: col2 = (0, 255, 0) col3 = (0, 255, 0) if clean: if col1 != gray and text1 is not None: cv.PutText(visualize, text1, (rect[0], rect[1] - 5), font, col1) if col2 != gray and text2 is not None: cv.PutText(visualize, text2, (rect[0], rect[1] - 5), font, col2) else: if text1 is not None: cv.PutText(visualize, text1, (rect[0], rect[1] - 5), font, col1) if text2 is not None: cv.PutText(visualize, text2, (rect[0], rect[1] - 15), font, col2) cv.Rectangle(visualize, (rect[0], rect[1]), (rect[0] + rect[2], rect[1] + rect[3]), col3, 1) cv.ResetImageROI(source) cv.ResetImageROI(source2) cv.ResetImageROI(mask)
def DetectRedEyes(image, faceCascade, smileCascade): min_size = (20, 20) image_scale = 2 haar_scale = 1.2 min_neighbors = 2 haar_flags = 0 # Allocate the temporary images gray = cv.CreateImage((image.width, image.height), 8, 1) smallImage = cv.CreateImage((cv.Round( image.width / image_scale), cv.Round(image.height / image_scale)), 8, 1) # Convert color input image to grayscale cv.CvtColor(image, gray, cv.CV_BGR2GRAY) # Scale input image for faster processing cv.Resize(gray, smallImage, cv.CV_INTER_LINEAR) # Equalize the histogram cv.EqualizeHist(smallImage, smallImage) # Detect the faces faces = cv.HaarDetectObjects(smallImage, faceCascade, cv.CreateMemStorage(0), haar_scale, min_neighbors, haar_flags, min_size) # If faces are found if faces: print faces for ((x, y, w, h), n) in faces: # the input to cv.HaarDetectObjects was resized, so scale the # bounding box of each face and convert it to two CvPoints print "face" pt1 = (int(x * image_scale), int(y * image_scale)) pt2 = (int((x + w) * image_scale), int((y + h) * image_scale)) # print pt1 # print pt2 cv.Rectangle(image, pt1, pt2, cv.RGB(255, 0, 0), 1, 8, 0) cv.PutText(image, "face", pt1, font, cv.RGB(255, 0, 0)) face_region = cv.GetSubRect(image, (x, int(y + (h / 4)), w, int(h / 2))) #split face cv.Rectangle(image, (pt1[0], (pt1[1] + (abs(pt1[1] - pt2[1]) / 2))), pt2, cv.RGB(0, 255, 0), 1, 8, 0) cv.PutText(image, "lower", (pt1[0], (pt1[1] + (abs(pt1[1] - pt2[1]) / 2))), font, cv.RGB(0, 255, 0)) cv.SetImageROI( image, (pt1[0], (pt1[1] + (abs(pt1[1] - pt2[1]) / 2)), pt2[0] - pt1[0], int((pt2[1] - (pt1[1] + (abs(pt1[1] - pt2[1]) / 2)))))) smiles = cv.HaarDetectObjects(image, smileCascade, cv.CreateMemStorage(0), 1.1, 5, 0, (15, 15)) if smiles: #print smiles for smile in smiles: cv.Rectangle( image, (smile[0][0], smile[0][1]), (smile[0][0] + smile[0][2], smile[0][1] + smile[0][3]), cv.RGB(0, 0, 255), 1, 8, 0) cv.PutText(image, "smile", (smile[0][0], smile[0][1]), font, cv.RGB(0, 0, 255)) cv.PutText(image, str(smile[1]), (smile[0][0], smile[0][1] + smile[0][3]), font, cv.RGB(0, 0, 255)) #print ((abs(smile[0][1] - smile[0][2]) / abs(pt1[0] - pt2[0])) * 100) global smileness smileness = smile[1] cv.ResetImageROI(image) #if smile[1] > 90: # mqttc.publish("smiles", "got smile", 1) # time.sleep(5) #eyes = cv.HaarDetectObjects(image, eyeCascade, #cv.CreateMemStorage(0), #haar_scale, min_neighbors, #haar_flags, (15,15)) #if eyes: # For each eye found #print eyes #for eye in eyes: # Draw a rectangle around the eye # cv.Rectangle(image, # (eye[0][0], # eye[0][1]), # (eye[0][0] + eye[0][2], # eye[0][1] + eye[0][3]), # cv.RGB(255, 0, 0), 1, 8, 0) cv.ResetImageROI(image) return image