def scale_32f_image(image): ''' Scales the given cv.IPL_DEPTH_32F type image to an 8 bit image so that the smallest value maps to 0 and the largest maps to 255. Used for displaying debugging images. Processes each channel separately, which can produce some useful, but esoteric results. ''' if image.depth != cv.IPL_DEPTH_32F: return image result = cv.CreateImage(cv.GetSize(image), 8, image.channels) channel_image = cv.CreateImage(cv.GetSize(image), cv.IPL_DEPTH_32F, 1) channel_scaled = cv.CreateImage(cv.GetSize(image), 8, 1) for channel_num in xrange(1, image.channels + 1): cv.SetImageCOI(image, channel_num) cv.Copy(image, channel_image) minmaxloc = cv.MinMaxLoc(channel_image) minimum = minmaxloc[0] maximum = minmaxloc[1] if maximum - minimum > 0: cv.ConvertScale(channel_image, channel_scaled, 255 / (maximum - minimum), -255 / (maximum - minimum) * minimum) else: cv.ConvertScale(channel_image, channel_scaled, 0, -255 / minimum) cv.SetImageCOI(result, channel_num) cv.Copy(channel_scaled, result) cv.SetImageCOI(image, 0) cv.SetImageCOI(result, 0) return result
def get_channel(frame, channel): ''' Returns a single channel image containing the specified channel from frame. The channel given is 0-indexed. ''' result = cv.CreateImage(cv.GetSize(frame), 8, 1) previous_coi = cv.GetImageCOI(frame) cv.SetImageCOI(frame, channel + 1) # COI is 1-indexed cv.Copy(frame, result) cv.SetImageCOI(frame, previous_coi) return result
def do_frame(img, hsv): cv.CvtColor(img, hsv, cv.CV_RGB2HSV) one_ch = cv.CreateImage(cv.GetSize(img), cv.IPL_DEPTH_8U, 1) for channel in (2, ): cv.SetImageCOI(hsv, channel) cv.Copy(hsv, one_ch) do_channel(one_ch)
def extract_v(image): size = cv.GetSize(image) hsv = cv.CreateImage(size, cv.IPL_DEPTH_8U, 3) v = cv.CreateImage(size, cv.IPL_DEPTH_8U, 1) cv.CvtColor(image, hsv, cv.CV_RGB2HSV) cv.SetImageCOI(hsv, 3) cv.Copy(hsv, v) return v
def extract_v(image): """Convert an RGB image to HSV and extract the V component. """ size = cv.GetSize(image) hsv = cv.CreateImage(size, cv.IPL_DEPTH_8U, 3) v = cv.CreateImage(size, cv.IPL_DEPTH_8U, 1) cv.CvtColor(image, hsv, cv.CV_RGB2HSV) cv.SetImageCOI(hsv, 3) cv.Copy(hsv, v) return v
def scale_32f_image(image): result = cv.CreateImage(cv.GetSize(image), 8, image.channels) channel_image = cv.CreateImage(cv.GetSize(image), cv.IPL_DEPTH_32F, 1) channel_scaled = cv.CreateImage(cv.GetSize(image), 8, 1) print "CHANNELS:", image.channels for channel_num in xrange(1, image.channels + 1): cv.SetImageCOI(image, channel_num) cv.Copy(image, channel_image) maximum = cv.MinMaxLoc(channel_image)[1] print "MAXIMUM:", maximum cv.ConvertScale(channel_image, channel_scaled, 255 / maximum) cv.SetImageCOI(result, channel_num) cv.Copy(channel_scaled, result) cv.SetImageCOI(image, 0) cv.SetImageCOI(result, 0) return result
def find_squares4(color_img): """ Finds multiple squares in image Steps: -Use Canny edge to highlight contours, and dilation to connect the edge segments. -Threshold the result to binary edge tokens -Use cv.FindContours: returns a cv.CvSequence of cv.CvContours -Filter each candidate: use Approx poly, keep only contours with 4 vertices, enough area, and ~90deg angles. Return all squares contours in one flat list of arrays, 4 x,y points each. """ #select even sizes only width, height = (color_img.width & -2, color_img.height & -2) timg = cv.CloneImage(color_img) # make a copy of input image gray = cv.CreateImage((width, height), 8, 1) # select the maximum ROI in the image cv.SetImageROI(timg, (0, 0, width, height)) # down-scale and upscale the image to filter out the noise pyr = cv.CreateImage((width / 2, height / 2), 8, 3) cv.PyrDown(timg, pyr, 7) cv.PyrUp(pyr, timg, 7) tgray = cv.CreateImage((width, height), 8, 1) squares = [] # Find squares in every color plane of the image # Two methods, we use both: # 1. Canny to catch squares with gradient shading. Use upper threshold # from slider, set the lower to 0 (which forces edges merging). Then # dilate canny output to remove potential holes between edge segments. # 2. Binary thresholding at multiple levels N = 11 for c in [0, 1, 2]: #extract the c-th color plane cv.SetImageCOI(timg, c + 1) cv.Copy(timg, tgray, None) cv.Canny(tgray, gray, 0, 50, 5) cv.Dilate(gray, gray) squares = squares + find_squares_from_binary(gray) # Look for more squares at several threshold levels for l in range(1, N): cv.Threshold(tgray, gray, (l + 1) * 255 / N, 255, cv.CV_THRESH_BINARY) squares = squares + find_squares_from_binary(gray) return squares
def process_frame(self, frame): self.debug_frame = cv.CreateImage(cv.GetSize(frame), 8, 3) self.test_frame = cv.CreateImage(cv.GetSize(frame), 8, 3) cv.Copy(frame, self.debug_frame) cv.Copy(frame, self.test_frame) cv.Smooth(frame, frame, cv.CV_MEDIAN, 7, 7) # Set binary image to have saturation channel hsv = cv.CreateImage(cv.GetSize(frame), 8, 3) binary = cv.CreateImage(cv.GetSize(frame), 8, 1) cv.CvtColor(frame, hsv, cv.CV_BGR2HSV) cv.SetImageCOI(hsv, 1) cv.Copy(hsv, binary) cv.SetImageCOI(hsv, 0) # Adaptive Threshold cv.AdaptiveThreshold(binary, binary, 255, cv.CV_ADAPTIVE_THRESH_MEAN_C, cv.CV_THRESH_BINARY_INV, self.adaptive_thresh_blocksize, self.adaptive_thresh, ) # Morphology kernel = cv.CreateStructuringElementEx(5, 5, 3, 3, cv.CV_SHAPE_ELLIPSE) cv.Erode(binary, binary, kernel, 1) cv.Dilate(binary, binary, kernel, 1) cv.CvtColor(binary, self.debug_frame, cv.CV_GRAY2RGB) # Find Corners temp1 = cv.CreateImage(cv.GetSize(frame), 8, 1) temp2 = cv.CreateImage(cv.GetSize(frame), 8, 1) self.corners = cv.GoodFeaturesToTrack(binary, temp1, temp2, self.max_corners, self.quality_level, self.min_distance, None, self.good_features_blocksize, 0, 0.4) # Display Corners for corner in self.corners: corner_color = (0, 0, 255) text_color = (0, 255, 0) font = cv.InitFont(cv.CV_FONT_HERSHEY_SIMPLEX, .6, .6, 0, 1, 1) cv.Circle(self.debug_frame, (int(corner[0]), int(corner[1])), 15, corner_color, 2, 8, 0) # Find Candidates for confirmed in self.confirmed: confirmed.corner1_repl_check = 0 confirmed.corner2_repl_check = 0 confirmed.corner3_repl_check = 0 confirmed.corner4_repl_check = 0 for corner in self.corners: if math.fabs(confirmed.corner1[0] - corner[0]) < self.MaxCornerTrans and \ math.fabs(confirmed.corner1[1] - corner[1]) < self.MaxCornerTrans: confirmed.corner1_repl_check = 1 confirmed.corner1_repl = corner elif math.fabs(confirmed.corner2[0] - corner[0]) < self.MaxCornerTrans and \ math.fabs(confirmed.corner2[1] - corner[1]) < self.MaxCornerTrans: confirmed.corner2_repl_check = 1 confirmed.corner2_repl = corner elif math.fabs(confirmed.corner3[0] - corner[0]) < self.MaxCornerTrans and \ math.fabs(confirmed.corner3[1] - corner[1]) < self.MaxCornerTrans: confirmed.corner3_repl_check = 1 confirmed.corner3_repl = corner elif math.fabs(confirmed.corner4[0] - corner[0]) < self.MaxCornerTrans and \ math.fabs(confirmed.corner4[1] - corner[1]) < self.MaxCornerTrans: confirmed.corner4_repl_check = 1 confirmed.corner4_repl = corner if confirmed.corner4_repl_check == 1 and confirmed.corner3_repl_check == 1 and confirmed.corner2_repl_check == 1 and confirmed.corner1_repl_check == 1: confirmed.corner1 = confirmed.corner1_repl confirmed.corner2 = confirmed.corner2_repl confirmed.corner3 = confirmed.corner3_repl confirmed.corner4 = confirmed.corner4_repl confirmed.midx = rect_midpointx(confirmed.corner1, confirmed.corner2, confirmed.corner3, confirmed.corner4) confirmed.midy = rect_midpointy(confirmed.corner1, confirmed.corner2, confirmed.corner3, confirmed.corner4) if confirmed.last_seen < self.last_seen_max: confirmed.last_seen += 5 for corner1 in self.corners: for corner2 in self.corners: for corner3 in self.corners: for corner4 in self.corners: # Checks that corners are not the same and are in the proper orientation if corner4[0] != corner3[0] and corner4[0] != corner2[0] and corner4[0] != corner1[0] and \ corner3[0] != corner2[0] and corner3[0] != corner1[0] and corner2[0] != corner1[0] and \ corner4[1] != corner3[1] and corner4[1] != corner2[1] and corner4[1] != corner1[1] and \ corner3[1] != corner2[1] and corner3[1] != corner1[1] and corner2[1] != corner1[1] and \ corner2[0] >= corner3[0] and corner1[1] >= corner4[1] and corner2[0] >= corner1[0]: # Checks that the side ratios are correct if math.fabs(line_distance(corner1, corner3) - line_distance(corner2, corner4)) < self.size_threshold and \ math.fabs(line_distance(corner1, corner2) - line_distance(corner3, corner4)) < self.size_threshold and \ math.fabs(line_distance(corner1, corner3) / line_distance(corner1, corner2)) < self.ratio_threshold or \ math.fabs(line_distance(corner1, corner2) / line_distance(corner1, corner3)) < self.ratio_threshold: # Checks that angles are roughly 90 degrees angle_cnr_2 = math.fabs(angle_between_lines(line_slope(corner1, corner2), line_slope(corner2, corner4))) if self.angle_min < angle_cnr_2 < self.angle_max: angle_cnr_3 = math.fabs(angle_between_lines(line_slope(corner1, corner3), line_slope(corner3, corner4))) if self.angle_min2 < angle_cnr_3 < self.angle_max2: new_bin = Bin(corner1, corner2, corner3, corner4) self.match_bins(new_bin) self.sort_bins() ''' #START SHAPE PROCESSING #TODO load these ONCE somewhere samples = np.loadtxt('generalsamples.data',np.float32) responses = np.loadtxt('generalresponses.data',np.float32) responses = responses.reshape((responses.size,1)) model = cv2.KNearest() model.train(samples,responses) for bin in self.confirmed: try: bin.speedlimit except: continue transf = cv.CreateMat(3, 3, cv.CV_32FC1) corner_orders = [ [bin.corner1, bin.corner2, bin.corner3, bin.corner4], #0 degrees [bin.corner4, bin.corner3, bin.corner2, bin.corner1], #180 degrees [bin.corner2, bin.corner4, bin.corner1, bin.corner3], #90 degrees [bin.corner3, bin.corner1, bin.corner4, bin.corner2], #270 degrees [bin.corner3, bin.corner4, bin.corner1, bin.corner2], #0 degrees and flipped X [bin.corner2, bin.corner1, bin.corner4, bin.corner3], #180 degrees and flipped X [bin.corner1, bin.corner3, bin.corner2, bin.corner4], #90 degrees and flipped X [bin.corner4, bin.corner2, bin.corner3, bin.corner1]] #270 degrees andf flipped X for i in range(0, 8): cv.GetPerspectiveTransform( corner_orders[i], [(0, 0), (0, 256), (128, 0), (128, 256)], transf ) shape = cv.CreateImage([128, 256], 8, 3) cv.WarpPerspective(frame, shape, transf) shape_thresh = np.zeros((256-104,128,1), np.uint8) j = 104 while j<256: i = 0 while i<128: pixel = cv.Get2D(shape, j, i) if int(pixel[2]) > (int(pixel[1]) + int(pixel[0])) * 0.7: shape_thresh[j-104,i] = 255 else: shape_thresh[j-104,i] = 0 i = i+1 j = j+1 cv2.imshow("Bin " + str(i), shape_thresh) contours,hierarchy = cv2.findContours(thresh,cv2.RETR_LIST,cv2.CHAIN_APPROX_NONE) for cnt in contours: if cv2.contourArea(cnt)>50: [x,y,w,h] = cv2.boundingRect(cnt) if h>54 and w>36: roi = thresh[y:y+h,x:x+w] roismall = cv2.resize(roi,(10,10)) roismall = roismall.reshape((1,100)) roismall = np.float32(roismall) retval, results, neigh_resp, dists = model.find_nearest(roismall, k = 1) digit_tuples.append( (x, int((results[0][0]))) ) if len(digit_tuples) == 2: digit_tuples_sorted = sorted(digit_tuples, key=lambda digit_tuple: digit_tuple[0]) speedlimit = 0 for i in range(0, len(digit_tuples_sorted)): speedlimit = speedlimit * 10 + digit_tuples_sorted[i][1] bin.speedlimit = speedlimit print "Found speed limit: " + str(speedlimit) break else: print "Unable to determine speed limit" #... TODO more #END SHAPE PROCESSING ''' svr.debug("Bins", self.debug_frame) svr.debug("Bins2", self.test_frame) # Output bins self.output.bins = self.confirmed anglesum = 0 for bins in self.output.bins: bins.theta = (bins.midx - frame.width / 2) * 37 / (frame.width / 2) bins.phi = -1 * (bins.midy - frame.height / 2) * 36 / (frame.height / 2) bins.shape = bins.object anglesum += bins.angle # bins.orientation = bins.angle if len(self.output.bins) > 0: self.output.orientation = anglesum / len(self.output.bins) else: self.output.orientation = None self.return_output()
def process_frame(self, frame): ################ #setup CV ###### ################ print "processing frame" (w, h) = cv.GetSize(frame) #generate hue selection frames ones = np.ones((h, w, 1), dtype='uint8') a = ones * (180 - self.target_hue) b = ones * (180 - self.target_hue + 20) a_array = cv.fromarray(a) b_array = cv.fromarray(b) #create locations for the test frame and binary frame frametest = cv.CreateImage(cv.GetSize(frame), 8, 3) binarytest = cv.CreateImage(cv.GetSize(frame), 8, 1) #use the red channel for the binary frame (just for debugging purposes) cv.Copy(frame, frametest) cv.SetImageCOI(frametest, 3) cv.Copy(frametest, binarytest) #reset the COI for test frame to RGB. cv.SetImageCOI(frametest, 0) # Resize image to 320x240 #copy = cv.CreateImage(cv.GetSize(frame), 8, 3) #cv.Copy(frame, copy) #cv.SetImageROI(frame, (0, 0, 320, 240)) #cv.Resize(copy, frame, cv.CV_INTER_NN) found_gate = False #create a new frame for comparison purposes unchanged_frame = cv.CreateImage(cv.GetSize(frame), 8, 3) cv.Copy(frame, unchanged_frame) #apply noise filter #1 cv.Smooth(frame, frame, cv.CV_MEDIAN, 7, 7) # Set binary image to have saturation channel hsv = cv.CreateImage(cv.GetSize(frame), 8, 3) binary = cv.CreateImage(cv.GetSize(frame), 8, 1) cv.CvtColor(frame, hsv, cv.CV_BGR2HSV) cv.SetImageCOI(hsv, 1) cv.Copy(hsv, binary) #spin the color wheel (psuedo-code for later if necessary) # truncate spectrum marked as end # shift all values up based on truncating value (mask out 0 regions) # take truncated bits, and flip them (180->0, 179->1...) # dnow that truncated bits are flipped, add them back in to final image #Reset hsv COI cv.SetImageCOI(hsv, 0) #correct for wraparound on red spectrum cv.InRange(binary, a_array, b_array, binarytest) #generate mask cv.Add(binary, cv.fromarray(ones * 180), binary, mask=binarytest) #use mask to selectively add values #run adaptive threshold for edge detection cv.AdaptiveThreshold( binary, binary, 255, cv.CV_ADAPTIVE_THRESH_MEAN_C, cv.CV_THRESH_BINARY_INV, self.adaptive_thresh_blocksize, self.adaptive_thresh, ) # Morphology kernel = cv.CreateStructuringElementEx(5, 5, 3, 3, cv.CV_SHAPE_ELLIPSE) cv.Erode(binary, binary, kernel, 1) cv.Dilate(binary, binary, kernel, 1) if self.debug: color_filtered = cv.CloneImage(binary) # Get Edges cv.Canny(binary, binary, 30, 40) # Hough Transform line_storage = cv.CreateMemStorage() raw_lines = cv.HoughLines2(binary, line_storage, cv.CV_HOUGH_STANDARD, rho=1, theta=math.pi / 180, threshold=self.hough_threshold, param1=0, param2=0) # Get vertical lines vertical_lines = [] i = 0 for line in raw_lines: if line[1] < self.vertical_threshold or \ line[1] > (math.pi-self.vertical_threshold): #absolute value does better grouping currently vertical_lines.append((abs(line[0]), line[1])) i += 1 # print message to user for performance purposes logging.debug("{} possibilities reduced to {} lines".format( i, len(vertical_lines))) # Group vertical lines vertical_line_groups = [ ] #A list of line groups which are each a line list i = 0 for line in vertical_lines: group_found = False for line_group in vertical_line_groups: i += 1 if line_group_accept_test(line_group, line, self.max_range): line_group.append(line) group_found = True if not group_found: vertical_line_groups.append([line]) #quick debugging statement logging.debug("{} internal iterations for {} groups".format( i, len(vertical_line_groups))) # Average line groups into lines vertical_lines = [] for line_group in vertical_line_groups: rhos = map(lambda line: line[0], line_group) #get rho of each line angles = map(lambda line: line[1], line_group) line = (sum(rhos) / len(rhos), circular_average(angles, math.pi)) vertical_lines.append(line) self.left_pole = None self.right_pole = None self.returning = 0 self.found = False if len(vertical_lines) is 2: roi = cv.GetImageROI(frame) width = roi[2] height = roi[3] self.left_pole = round( min(vertical_lines[0][0], vertical_lines[1][0]), 2) - width / 2 self.right_pole = round( max(vertical_lines[0][0], vertical_lines[1][0]), 2) - width / 2 self.returning = (self.left_pole + self.right_pole) / 2 logging.info("Returning {}".format(self.returning)) #If this is first iteration, count this as seeing the gate if self.last_seen < 0: self.last_center = None self.last_seen = 0 #increment a counter if result is good. if self.last_center is None: self.last_center = self.returning self.seen_count = 1 elif math.fabs(self.last_center - self.returning) < self.center_trans_thresh: self.seen_count += 1 self.last_seen += 2 else: self.last_seen -= 1 #if not convinced, forget left/right pole. Else, proclaim success. if self.seen_count < self.seen_count_thresh: self.left_pole = None self.right_pole = None else: print "FOUND CENTER AND RETURNED IT" self.found = True else: self.returning = 0 if self.last_seen < 0: self.last_center = None self.last_seen = 0 self.last_seen -= 1 self.left_pole = None self.right_POLE = None #extra debugging stuff if self.debug: cv.CvtColor(color_filtered, frame, cv.CV_GRAY2RGB) libvision.misc.draw_lines(frame, vertical_lines) if self.found: cv.Circle(frame, (int(frame.width / 2 + self.returning), int(frame.height / 2)), 15, (0, 255, 0), 2, 8, 0) font = cv.InitFont(cv.CV_FONT_HERSHEY_SIMPLEX, 1, 1, 1, 3) cv.PutText(frame, "Gate Sent to Mission Control", (100, 400), font, (255, 255, 0)) #print frame.width #cv.ShowImage("Gate", cv.CloneImage(frame)) svr.debug("Gate", cv.CloneImage(frame)) svr.debug("Unchanged", cv.CloneImage(unchanged_frame)) self.return_output()
def main(): os.chdir(sys.argv[1]) try: os.mkdir(OUTPUT_DIR_NAME) except OSError: pass tree = et.parse("project.xml") movie = tree.getroot() file_path = movie.attrib["path"] if DEBUG: cv.NamedWindow("win", cv.CV_WINDOW_AUTOSIZE) cv.MoveWindow("win", 200, 200) cap = cv.CreateFileCapture(file_path) skip_frames(cap, movie) pixel_count = None prev_img = None global_frame_counter = 0 file_counter = 0 w = None h = None output_img = cv.CreateImage((WIDTH, MAX_FRAMES), cv.IPL_DEPTH_8U, 3) f = open("shots.txt", "r") lines = [line for line in f if line] # (start_frame, end_frame, duration) f.close() f_frm = open("motion.txt", "w") f_avg = open("motion_shot-avg.txt", "w") motion = [] t = time.time() for nr, line in enumerate(lines): print(nr + 1), "/", len(lines) duration = int(line.split("\t")[2]) for frame_counter in range(duration): img = cv.QueryFrame(cap) if not img: print "error?" print nr, frame_counter #break return if DEBUG: cv.ShowImage("win", img) global_frame_counter += 1 if nr == 0 and frame_counter == 0: # first shot, first frame w = img.width h = img.height pixel_count = float(img.width * img.height) prev_img = cv.CreateImage(cv.GetSize(img), cv.IPL_DEPTH_8U, 3) cv.Zero(prev_img) diff = cv.CreateImage(cv.GetSize(img), cv.IPL_DEPTH_8U, 3) cv.AbsDiff(img, prev_img, diff) cv.Threshold(diff, diff, 10, 255, cv.CV_THRESH_BINARY) d_color = 0 for i in range(1, 4): cv.SetImageCOI(diff, i) d_color += cv.CountNonZero(diff) / pixel_count d_color = d_color / 3 # 0..1 #print "%.1f" % (d_color*100), "%" motion.append(d_color) cv.Copy(img, prev_img) # WRITE TEXT FILE f_frm.write("%f\n" % (d_color)) if frame_counter == duration - 1: # last frame of current shot motion_value = sum(motion) / len(motion) print "average motion:", motion_value f_avg.write("%f\t%d\n" % (motion_value, duration)) motion = [] # WRITE IMAGE if frame_counter == 0: # ignore each first frame -- the diff after a hard cut is meaningless global_frame_counter -= 1 continue else: for i in range(WIDTH): value = d_color * 255 cv.Set2D(output_img, (global_frame_counter - 1) % MAX_FRAMES, i, cv.RGB(value, value, value)) if global_frame_counter % MAX_FRAMES == 0: cv.SaveImage( os.path.join(OUTPUT_DIR_NAME, "motion_%03d.png" % (file_counter)), output_img) file_counter += 1 if DEBUG: if cv.WaitKey(1) == 27: break if global_frame_counter % MAX_FRAMES != 0: #cv.SetImageROI(output_img, (0, 0, WIDTH-1, (global_frame_counter % MAX_FRAMES)-1)) cv.SetImageROI(output_img, (0, 0, WIDTH - 1, (global_frame_counter - 1) % MAX_FRAMES)) cv.SaveImage( os.path.join(OUTPUT_DIR_NAME, "motion_%03d.png" % (file_counter)), output_img) f_frm.close() f_avg.close() if DEBUG: cv.DestroyWindow("win") print "%.2f min" % ((time.time() - t) / 60) #raw_input("- done -") return
def main(): BLACK_AND_WHITE = False THRESHOLD = 0.48 BW_THRESHOLD = 0.4 os.chdir(sys.argv[1]) try: os.mkdir(OUTPUT_DIR_NAME) except: pass if len(sys.argv) > 2: if sys.argv[2] == "bw": BLACK_AND_WHITE = True THRESHOLD = BW_THRESHOLD print "##########" print " B/W MODE" print "##########" tree = et.parse("project.xml") movie = tree.getroot() file_path = movie.attrib["path"] cap = cv.CreateFileCapture(file_path) if DEBUG: cv.NamedWindow("win", cv.CV_WINDOW_AUTOSIZE) cv.MoveWindow("win", 200, 200) hist = None prev_hist = None prev_img = None pixel_count = None frame_counter = 0 last_frame_black = False black_frame_start = -1 t = time.time() while 1: img_orig = cv.QueryFrame(cap) if not img_orig: # eof cv.SaveImage(OUTPUT_DIR_NAME + "\\%06d.png" % (frame_counter - 1), prev_img) """movie.set("frames", str(frame_counter)) tree.write("project.xml")""" break img = cv.CreateImage( (int(img_orig.width / 4), int(img_orig.height / 4)), cv.IPL_DEPTH_8U, 3) cv.Resize(img_orig, img, cv.CV_INTER_AREA) if frame_counter == 0: # erster frame cv.SaveImage(OUTPUT_DIR_NAME + "\\%06d.png" % (0), img) pixel_count = img.width * img.height prev_img = cv.CreateImage(cv.GetSize(img), cv.IPL_DEPTH_8U, 3) cv.Zero(prev_img) if DEBUG and frame_counter % 2 == 1: cv.ShowImage("win", img) img_hsv = cv.CreateImage(cv.GetSize(img), cv.IPL_DEPTH_8U, 3) cv.CvtColor(img, img_hsv, cv.CV_BGR2HSV) # ##################### # METHOD #1: find the number of pixels that have (significantly) changed since the last frame diff = cv.CreateImage(cv.GetSize(img), cv.IPL_DEPTH_8U, 3) cv.AbsDiff(img_hsv, prev_img, diff) cv.Threshold(diff, diff, 10, 255, cv.CV_THRESH_BINARY) d_color = 0 for i in range(1, 4): cv.SetImageCOI(diff, i) d_color += float(cv.CountNonZero(diff)) / float(pixel_count) if not BLACK_AND_WHITE: d_color = float(d_color / 3.0) # 0..1 # ##################### # METHOD #2: calculate the amount of change in the histograms h_plane = cv.CreateMat(img.height, img.width, cv.CV_8UC1) s_plane = cv.CreateMat(img.height, img.width, cv.CV_8UC1) v_plane = cv.CreateMat(img.height, img.width, cv.CV_8UC1) cv.Split(img_hsv, h_plane, s_plane, v_plane, None) planes = [h_plane, s_plane, v_plane] hist_size = [50, 50, 50] hist_range = [[0, 360], [0, 255], [0, 255]] if not hist: hist = cv.CreateHist(hist_size, cv.CV_HIST_ARRAY, hist_range, 1) cv.CalcHist([cv.GetImage(i) for i in planes], hist) cv.NormalizeHist(hist, 1.0) if not prev_hist: prev_hist = cv.CreateHist(hist_size, cv.CV_HIST_ARRAY, hist_range, 1) # wieso gibt es kein cv.CopyHist()?! cv.CalcHist([cv.GetImage(i) for i in planes], prev_hist) cv.NormalizeHist(prev_hist, 1.0) continue d_hist = cv.CompareHist(prev_hist, hist, cv.CV_COMP_INTERSECT) # combine both methods to make a decision if ((0.4 * d_color + 0.6 * (1 - d_hist))) >= THRESHOLD: if DEBUG: if frame_counter % 2 == 0: cv.ShowImage("win", img) winsound.PlaySound(soundfile, winsound.SND_FILENAME | winsound.SND_ASYNC) print "%.3f" % ((0.4 * d_color + 0.6 * (1 - d_hist))), "%.3f" % ( d_color), "%.3f" % (1 - d_hist), frame_counter if DEBUG and DEBUG_INTERACTIVE: if win32api.MessageBox(0, "cut?", "", win32con.MB_YESNO) == 6: #yes cv.SaveImage( OUTPUT_DIR_NAME + "\\%06d.png" % (frame_counter), img) else: cv.SaveImage(OUTPUT_DIR_NAME + "\\%06d.png" % (frame_counter), img) cv.CalcHist([cv.GetImage(i) for i in planes], prev_hist) cv.NormalizeHist(prev_hist, 1.0) # ##################### # METHOD #3: detect series of (almost) black frames as an indicator for "fade to black" average = cv.Avg(v_plane)[0] if average <= 0.6: if not last_frame_black: # possible the start print "start", frame_counter black_frame_start = frame_counter last_frame_black = True else: if last_frame_black: # end of a series of black frames cut_at = black_frame_start + int( (frame_counter - black_frame_start) / 2) print "end", frame_counter, "cut at", cut_at img_black = cv.CreateImage( (img_orig.width / 4, img_orig.height / 4), cv.IPL_DEPTH_8U, 3) cv.Set(img_black, cv.RGB(0, 255, 0)) cv.SaveImage(OUTPUT_DIR_NAME + "\\%06d.png" % (cut_at), img_black) last_frame_black = False cv.Copy(img_hsv, prev_img) frame_counter += 1 if DEBUG: if cv.WaitKey(1) == 27: break if DEBUG: cv.DestroyWindow("win") print "%.2f min" % ((time.time() - t) / 60) #raw_input("- done -") return
def process_frame(self, frame): self.debug_frame = cv.CreateImage(cv.GetSize(frame), 8, 3) og_frame = cv.CreateImage(cv.GetSize(frame), 8, 3) cv.Copy(frame, self.debug_frame) cv.Copy(self.debug_frame, og_frame) cv.Smooth(frame, frame, cv.CV_MEDIAN, 7, 7) # Set binary image to have saturation channel hsv = cv.CreateImage(cv.GetSize(frame), 8, 3) binary = cv.CreateImage(cv.GetSize(frame), 8, 1) cv.CvtColor(frame, hsv, cv.CV_BGR2HSV) cv.SetImageCOI(hsv, 1) #3 before competition #2 at competition cv.Copy(hsv, binary) cv.SetImageCOI(hsv, 0) cv.AdaptiveThreshold(binary, binary, 255, cv.CV_ADAPTIVE_THRESH_MEAN_C, cv.CV_THRESH_BINARY_INV, self.adaptive_thresh_blocksize, self.adaptive_thresh, ) # Morphology kernel = cv.CreateStructuringElementEx(5, 5, 3, 3, cv.CV_SHAPE_ELLIPSE) cv.Erode(binary, binary, kernel, 1) cv.Dilate(binary, binary, kernel, 1) # Get Edges #cv.Canny(binary, binary, 30, 40) cv.CvtColor(binary, self.debug_frame, cv.CV_GRAY2RGB) # Hough Transform line_storage = cv.CreateMemStorage() raw_lines = cv.HoughLines2(binary, line_storage, cv.CV_HOUGH_PROBABILISTIC, rho=1, theta=math.pi / 180, threshold=self.hough_threshold, param1=self.min_length, param2=self.max_gap ) lines = [] for line in raw_lines: lines.append(line) #Grouping lines depending on endpoint similarities for line1 in lines[:]: for line2 in lines[:]: if line1 in lines and line2 in lines and line1 != line2: if math.fabs(line1[0][0] - line2[0][0]) < self.max_corner_range and \ math.fabs(line1[0][1] - line2[0][1]) < self.max_corner_range and \ math.fabs(line1[1][0] - line2[1][0]) < self.max_corner_range and \ math.fabs(line1[1][1] - line2[1][1]) < self.max_corner_range: if line_distance(line1[0], line1[1]) > line_distance(line2[0], line2[1]): lines.remove(line2) else: lines.remove(line1) elif math.fabs(line1[0][0] - line2[1][0]) < self.max_corner_range and \ math.fabs(line1[0][1] - line2[1][1]) < self.max_corner_range and \ math.fabs(line1[1][0] - line2[0][0]) < self.max_corner_range and \ math.fabs(line1[1][1] - line2[0][1]) < self.max_corner_range: if line_distance(line1[0], line1[1]) > line_distance(line2[0], line2[1]): lines.remove(line2) else: lines.remove(line1) self.hough_corners = [] for line in lines: self.hough_corners.append(line[0]) self.hough_corners.append(line[1]) for corner1 in self.hough_corners[:]: for corner2 in self.hough_corners[:]: if corner1 is not corner2 and corner1 in self.hough_corners and corner2 in self.hough_corners: if math.fabs(corner1[0] - corner2[0]) < self.max_corner_range4 and \ math.fabs(corner1[1] - corner2[1]) < self.max_corner_range4: corner1 = [(corner1[0] + corner2[0]) / 2, (corner1[1] + corner2[1]) / 2] self.hough_corners.remove(corner2) for line1 in lines: #cv.Line(self.debug_frame,line1[0],line1[1], (0,0,255), 10, cv.CV_AA, 0) for line2 in lines: if line1 is not line2: self.find_corners(line1, line2) for corner1 in self.corners: for corner2 in self.corners: if math.fabs(corner1[1][0] - corner2[1][0]) < self.max_corner_range2 and \ math.fabs(corner1[1][1] - corner2[1][1]) < self.max_corner_range2 and \ math.fabs(corner1[2][0] - corner2[2][0]) < self.max_corner_range2 and \ math.fabs(corner1[2][1] - corner2[2][1]) < self.max_corner_range2 and \ math.fabs(corner1[0][0] - corner2[0][0]) > self.max_corner_range2 and \ math.fabs(corner1[0][1] - corner2[0][1]) > self.max_corner_range2: pt1 = (int(corner1[0][0]), int(corner1[0][1])) pt4 = (int(corner2[0][0]), int(corner2[0][1])) pt3 = (int(corner1[1][0]), int(corner1[1][1])) pt2 = (int(corner1[2][0]), int(corner1[2][1])) #line_color = (0,255,0)s #cv.Line(self.debug_frame,pt1,pt2, line_color, 10, cv.CV_AA, 0) #cv.Line(self.debug_frame,pt1,pt3, line_color, 10, cv.CV_AA, 0) #cv.Line(self.debug_frame,pt4,pt2, line_color, 10, cv.CV_AA, 0) #cv.Line(self.debug_frame,pt4,pt3, line_color, 10, cv.CV_AA, 0) new_bin = Bin(pt1, pt2, pt3, pt4) new_bin.id = self.bin_id self.bin_id += 1 if math.fabs(line_distance(pt1, pt2) - line_distance(pt3, pt4)) < self.parallel_sides_length_thresh and \ math.fabs(line_distance(pt1, pt3) - line_distance(pt2, pt4)) < self.parallel_sides_length_thresh: self.Bins.append(new_bin) print "new_bin" elif (math.fabs(corner1[1][0] - corner2[2][0]) < self.max_corner_range2 and math.fabs(corner1[1][1] - corner2[2][1]) < self.max_corner_range2 and math.fabs(corner1[2][0] - corner2[1][0]) < self.max_corner_range2 and math.fabs(corner1[2][1] - corner2[1][1]) < self.max_corner_range2 and math.fabs(corner1[0][0] - corner2[0][0]) > self.max_corner_range2 and math.fabs(corner1[0][1] - corner2[0][1]) > self.max_corner_range2): continue self.corners = [] self.final_corners = self.sort_corners() #Results are not used. Experimental corners which have been seen twice, should be only the corners we want, but there were problems self.sort_bins() self.update_bins() self.group_bins() self.draw_bins() for corner in self.hough_corners: line_color = [255, 0, 0] cv.Circle(self.debug_frame, corner, 15, (255, 0, 0), 2, 8, 0) for line in lines: line_color = [255, 0, 0] cv.Line(self.debug_frame, line[0], line[1], line_color, 5, cv.CV_AA, 0) #cv.Circle(self.debug_frame, line[0], 15, (255,0,0), 2,8,0) #cv.Circle(self.debug_frame, line[1], 15, (255,0,0), 2,8,0) #Output bins self.output.bins = self.Bins anglesum = 0 for bins in self.output.bins: bins.theta = (bins.center[0] - frame.width / 2) * 37 / (frame.width / 2) bins.phi = -1 * (bins.center[1] - frame.height / 2) * 36 / (frame.height / 2) anglesum += bins.angle # bins.orientation = bins.angle if len(self.output.bins) > 0: self.output.orientation = anglesum / len(self.output.bins) else: self.output.orientation = None self.return_output() svr.debug("Bins", self.debug_frame) svr.debug("Original", og_frame) #BEGIN SHAPE PROCESSING #constants img_width = 128 img_height = 256 number_x = 23 number_y = 111 number_w = 82 number_h = 90 bin_thresh_blocksize = 11 bin_thresh = 1.9 red_significance_threshold = 0.4 #load templates - run once, accessible to number processor number_templates = [ (10, cv.LoadImage("number_templates/10.png")), (16, cv.LoadImage("number_templates/16.png")), (37, cv.LoadImage("number_templates/37.png")), (98, cv.LoadImage("number_templates/98.png")), ] #Begin Bin Contents Processing for bin in self.Bins: #Take the bin's corners, and get an image containing an img_width x img_height rectangle of it transf = cv.CreateMat(3, 3, cv.CV_32FC1) cv.GetPerspectiveTransform( [bin.corner1, bin.corner2, bin.corner3, bin.corner4], [(0, 0), (0, img_height), (img_width, 0), (img_width, img_height)], transf ) bin_image = cv.CreateImage([img_width, img_height], 8, 3) cv.WarpPerspective(frame, bin_image, transf) #AdaptaveThreshold to get black and white image highlighting the number (still works better than my yellow-vs-red threshold attempt hsv = cv.CreateImage(cv.GetSize(bin_image), 8, 3) bin_thresh_image = cv.CreateImage(cv.GetSize(bin_image), 8, 1) cv.CvtColor(bin_image, hsv, cv.CV_BGR2HSV) cv.SetImageCOI(hsv, 3) cv.Copy(hsv, bin_thresh_image) cv.SetImageCOI(hsv, 0) cv.AdaptiveThreshold(bin_thresh_image, bin_thresh_image, 255, cv.CV_ADAPTIVE_THRESH_MEAN_C, cv.CV_THRESH_BINARY_INV, bin_thresh_blocksize, bin_thresh, ) kernel = cv.CreateStructuringElementEx(5, 5, 3, 3, cv.CV_SHAPE_ELLIPSE) cv.Erode(bin_thresh_image, bin_thresh_image, kernel, 1) cv.Dilate(bin_thresh_image, bin_thresh_image, kernel, 1) #Here, we loop through all four different templates, and figure out which one we think is most likely. #The comparison function counts corresponding pixels that are non-zero in each image, and then corresponding pixels that are different in each image. The ratio of diff_count/both_count is our "unconfidence" ratio. The lower it is, the more confident we are. #There are two nearly identical pieces of code within this loop. One checks the bin right-side-up, and the other one checks it flipped 180. last_thought_number = -1 last_unconfidence_ratio = number_w * number_h + 2 for i in range(0, len(number_templates)): both_count = 0 diff_count = 0 this_number_image = number_templates[i][1] for y in range(0, number_h): for x in range(0, number_w): if (bin_thresh_image[y + number_y, x + number_x] != 0) and (this_number_image[y, x][0] != 0): both_count += 1 elif (bin_thresh_image[y + number_y, x + number_x] != 0) or (this_number_image[y, x][0] != 0): diff_count += 1 if both_count == 0: unconfidence_ratio = number_w * number_h + 1 # max unconfidence else: unconfidence_ratio = 1.0 * diff_count / both_count if unconfidence_ratio < last_unconfidence_ratio: last_thought_number = number_templates[i][0] last_unconfidence_ratio = unconfidence_ratio both_count = 0 diff_count = 0 for y in range(0, number_h): for x in range(0, number_w): if (bin_thresh_image[img_height - number_y - 1 - y, img_width - number_x - 1 - x] != 0) and ( this_number_image[y, x][0] != 0): both_count += 1 elif (bin_thresh_image[img_height - number_y - 1 - y, img_width - number_x - 1 - x] != 0) or ( this_number_image[y, x][0] != 0): diff_count += 1 if both_count == 0: unconfidence_ratio = number_w * number_h + 1 # max unconfidence else: unconfidence_ratio = 1.0 * diff_count / both_count if unconfidence_ratio < last_unconfidence_ratio: last_thought_number = number_templates[i][0] last_unconfidence_ratio = unconfidence_ratio print str(last_thought_number) + " | " + str(last_unconfidence_ratio) try: #check if it's defined bin.number_unconfidence_ratio except: bin.number_unconfidence_ratio = last_unconfidence_ratio bin.number = last_thought_number print "Set Speed Limit Number" else: if last_unconfidence_ratio < bin.number_unconfidence_ratio: bin.number_unconfidence_ratio = last_unconfidence_ratio if bin.number == last_thought_number: print "More Confident on Same Number: Updated" else: print "More Confident on Different Number: Updated" bin.icon = last_thought_number
def process_frame(self, frame): (w, h) = cv.GetSize(frame) #generate hue selection frames #create locations for the a pair of test frames frametest = cv.CreateImage(cv.GetSize(frame), 8, 3) binarytest = cv.CreateImage(cv.GetSize(frame), 8, 1) #use the red channel for the binary frame (just for debugging purposes) cv.Copy(frame, frametest) cv.SetImageCOI(frametest, 3) cv.Copy(frametest, binarytest) cv.SetImageCOI(frametest, 0) #reset COI #svr.debug("R?",binarytest) # Resize image to 320x240 #copy = cv.CreateImage(cv.GetSize(frame), 8, 3) #cv.Copy(frame, copy) #cv.SetImageROI(frame, (0, 0, 320, 240)) #cv.Resize(copy, frame, cv.CV_INTER_NN) found_gate = False #create a new frame just for comparison purposes unchanged_frame = cv.CreateImage(cv.GetSize(frame), 8, 3) cv.Copy(frame, unchanged_frame) #apply a course noise filter cv.Smooth(frame, frame, cv.CV_MEDIAN, 7, 7) # Set binary image to have saturation channel hsv = cv.CreateImage(cv.GetSize(frame), 8, 3) binary = cv.CreateImage(cv.GetSize(frame), 8, 1) cv.CvtColor(frame, hsv, cv.CV_BGR2HSV) cv.SetImageCOI(hsv, 1) cv.Copy(hsv, binary) cv.SetImageCOI(hsv, 0) #reset COI #shift hue of image such that orange->red are at top of spectrum ''' binary = libvision.misc.cv_to_cv2(binary) binary = libvision.misc.shift_hueCV2(binary, self.target_shift) binary = libvision.misc.cv2_to_cv(binary) ''' #correct for wraparound on red spectrum #cv.InRange(binary,a_array,b_array,binarytest) #generate mask #cv.Add(binary,cv.fromarray(ones*180),binary,mask=binarytest) #use mask to selectively add values svr.debug("R2?", binary) svr.debug("R2?", binary) #run adaptive threshold for edge detection and more noise filtering cv.AdaptiveThreshold( binary, binary, 255, cv.CV_ADAPTIVE_THRESH_MEAN_C, cv.CV_THRESH_BINARY_INV, self.adaptive_thresh_blocksize, self.adaptive_thresh, ) # Morphology kernel = cv.CreateStructuringElementEx(5, 5, 3, 3, cv.CV_SHAPE_ELLIPSE) cv.Erode(binary, binary, kernel, 1) cv.Dilate(binary, binary, kernel, 1) if self.debug: color_filtered = cv.CloneImage(binary) # Get Edges cv.Canny(binary, binary, 30, 40) # Hough Transform line_storage = cv.CreateMemStorage() raw_lines = cv.HoughLines2(binary, line_storage, cv.CV_HOUGH_STANDARD, rho=1, theta=math.pi / 180, threshold=self.hough_threshold, param1=0, param2=0) # Get vertical lines vertical_lines = [] for line in raw_lines: if line[1] < self.vertical_threshold or \ line[1] > math.pi-self.vertical_threshold: #absolute value does better grouping currently vertical_lines.append((abs(line[0]), line[1])) #print message to user for performance purposes logging.debug("{} possibilities reduced to {} lines".format( len(raw_lines), len(vertical_lines))) # Group vertical lines vertical_line_groups = [ ] # A list of line groups which are each a line list i = 0 for line in vertical_lines: group_found = False for line_group in vertical_line_groups: i += 1 if line_group_accept_test(line_group, line, self.max_range): line_group.append(line) group_found = True if not group_found: vertical_line_groups.append([line]) #quick debugging statement logging.debug("{} internal iterations for {} groups".format( i, len(vertical_line_groups))) # Average line groups into lines vertical_lines = [] for line_group in vertical_line_groups: rhos = map(lambda line: line[0], line_group) angles = map(lambda line: line[1], line_group) line = (sum(rhos) / len(rhos), circular_average(angles, math.pi)) vertical_lines.append(line) #################################################### #vvvv Horizontal line code isn't used for anything # Get horizontal lines horizontal_lines = [] for line in raw_lines: dist_from_horizontal = (math.pi / 2 + line[1]) % math.pi if dist_from_horizontal < self.horizontal_threshold or \ dist_from_horizontal > math.pi-self.horizontal_threshold: horizontal_lines.append((abs(line[0]), line[1])) # Group horizontal lines horizontal_line_groups = [ ] # A list of line groups which are each a line list for line in horizontal_lines: group_found = False for line_group in horizontal_line_groups: if line_group_accept_test(line_group, line, self.max_range): line_group.append(line) group_found = True if not group_found: horizontal_line_groups.append([line]) if len(horizontal_line_groups) is 1: self.seen_crossbar = True if self.debug: rhos = map(lambda line: line[0], horizontal_line_groups[0]) angles = map(lambda line: line[1], horizontal_line_groups[0]) line = (sum(rhos) / len(rhos), circular_average(angles, math.pi)) horizontal_lines = [line] else: self.seen_crossbar = False horizontal_lines = [] #^^^ Horizontal line code isn't used for anything ################################################### self.left_pole = None self.right_pole = None #print vertical_lines self.returning = 0 self.found = False if len(vertical_lines) is 2: roi = cv.GetImageROI(frame) width = roi[2] height = roi[3] self.left_pole = round( min(vertical_lines[0][0], vertical_lines[1][0]), 2) - width / 2 self.right_pole = round( max(vertical_lines[0][0], vertical_lines[1][0]), 2) - width / 2 self.returning = (self.left_pole + self.right_pole) / 2 logging.info("Returning {} as gate center delta.".format( self.returning)) #initialize first iteration with 2 known poles if self.last_seen < 0: self.last_center = None self.last_seen = 0 #increment a counter if result is good. if self.last_center is None: self.last_center = self.returning self.seen_count = 1 elif math.fabs(self.last_center - self.returning) < self.center_trans_thresh: self.seen_count += 1 self.last_seen += 2 else: self.last_seen -= 1 #if not conviced, forget left/right pole. Else proclaim success. if self.seen_count < self.seen_count_thresh: self.left_pole = None self.right_pole = None else: print "FOUND CENTER AND RETURNED IT" self.found = True else: self.returning = 0 if self.last_seen < 0: self.last_center = None self.last_seen = 0 self.last_seen -= 1 self.left_pole = None self.right_pole = None #TODO: If one pole is seen, is it left or right pole? if self.debug: cv.CvtColor(color_filtered, frame, cv.CV_GRAY2RGB) libvision.misc.draw_lines(frame, vertical_lines) libvision.misc.draw_lines(frame, horizontal_lines) if self.found: cv.Circle(frame, (int(frame.width / 2 + self.returning), int(frame.height / 2)), 15, (0, 255, 0), 2, 8, 0) font = cv.InitFont(cv.CV_FONT_HERSHEY_SIMPLEX, 1, 1, 1, 3) cv.PutText(frame, "Gate Sent to Mission Control", (100, 400), font, (255, 255, 0)) #print frame.width #cv.ShowImage("Gate", cv.CloneImage(frame)) svr.debug("Gate", cv.CloneImage(frame)) svr.debug("Unchanged", cv.CloneImage(unchanged_frame)) #populate self.output with infos self.output.seen_crossbar = self.seen_crossbar self.output.left_pole = self.left_pole self.output.right_pole = self.right_pole self.return_output()
def process_frame(self, frame): # Resize image to 320x240 #copy = cv.CreateImage(cv.GetSize(frame), 8, 3) #cv.Copy(frame, copy) #cv.SetImageROI(frame, (0, 0, 320, 240)) #cv.Resize(copy, frame, cv.CV_INTER_NN) found_hedge = False test_frame = cv.CreateImage(cv.GetSize(frame), 8, 3) cv.Copy(frame, test_frame) cv.Smooth(frame, frame, cv.CV_MEDIAN, 7, 7) # Set binary image to have value channel hsv = cv.CreateImage(cv.GetSize(frame), 8, 3) binary = cv.CreateImage(cv.GetSize(frame), 8, 1) cv.CvtColor(frame, hsv, cv.CV_BGR2HSV) cv.SetImageCOI(hsv, 2) cv.Copy(hsv, binary) cv.SetImageCOI(hsv, 0) cv.AdaptiveThreshold( binary, binary, 255, cv.CV_ADAPTIVE_THRESH_MEAN_C, cv.CV_THRESH_BINARY_INV, self.adaptive_thresh_blocksize, self.adaptive_thresh, ) # Morphology kernel = cv.CreateStructuringElementEx(3, 3, 1, 1, cv.CV_SHAPE_ELLIPSE) #cv.Erode(binary, binary, kernel, 1) cv.Dilate(binary, binary, kernel, 4) if self.debug: color_filtered = cv.CloneImage(binary) # Get Edges #cv.Canny(binary, binary, 30, 40) # Hough Transform ''' line_storage = cv.CreateMemStorage() raw_lines = cv.HoughLines2(binary, line_storage, cv.CV_HOUGH_STANDARD, rho=1, theta=math.pi/180, threshold=self.hough_threshold, param1=0, param2=0 ) ''' # Hough Transform line_storage = cv.CreateMemStorage() raw_lines = cv.HoughLines2(binary, line_storage, cv.CV_HOUGH_PROBABILISTIC, rho=1, theta=math.pi / 180, threshold=self.hough_threshold, param1=self.min_length, param2=self.max_gap) self.hor_lines = [] for line in raw_lines: slope = line_slope(line[0], line[1]) if slope is None: continue if math.fabs(line_slope(line[0], line[1])) < self.hor_threshold: self.hor_lines.append(line) max_length = 0 for line in self.hor_lines: print line if math.fabs(line_distance(line[0], line[1])) > max_length: max_length = math.fabs(line_distance(line[0], line[1])) crossbar_seg = line ''' # Get vertical lines vertical_lines = [] for line in raw_lines: if line[1] < self.vertical_threshold or \ line[1] > math.pi-self.vertical_threshold: vertical_lines.append( (abs(line[0]), line[1]) ) # Group vertical lines vertical_line_groups = [] # A list of line groups which are each a line list for line in vertical_lines: group_found = False for line_group in vertical_line_groups: if line_group_accept_test(line_group, line, self.max_range): line_group.append(line) group_found = True if not group_found: vertical_line_groups.append([line]) # Average line groups into lines vertical_lines = [] for line_group in vertical_line_groups: rhos = map(lambda line: line[0], line_group) angles = map(lambda line: line[1], line_group) line = (sum(rhos)/len(rhos), circular_average(angles, math.pi)) vertical_lines.append(line) # Get horizontal lines horizontal_lines = [] for line in raw_lines: dist_from_horizontal = (math.pi/2 + line[1]) % math.pi if dist_from_horizontal < self.horizontal_threshold or \ dist_from_horizontal > math.pi-self.horizontal_threshold: horizontal_lines.append( (abs(line[0]), line[1]) ) # Group horizontal lines horizontal_line_groups = [] # A list of line groups which are each a line list for line in horizontal_lines: group_found = False for line_group in horizontal_line_groups: if line_group_accept_test(line_group, line, self.max_range): line_group.append(line) group_found = True if not group_found: horizontal_line_groups.append([line]) if len(horizontal_line_groups) is 1: self.seen_crossbar = True rhos = map(lambda line: line[0], horizontal_line_groups[0]) angles = map(lambda line: line[1], horizontal_line_groups[0]) line = (sum(rhos)/len(rhos), circular_average(angles, math.pi)) horizontal_lines = [line] else: self.seen_crossbar = False horizontal_lines = [] self.left_pole = None self.right_pole = None if len(vertical_lines) is 2: roi = cv.GetImageROI(frame) width = roi[2] height = roi[3] self.left_pole = round(min(vertical_lines[0][0], vertical_lines[1][0]), 2) - width/2 self.right_pole = round(max(vertical_lines[0][0], vertical_lines[1][0]), 2) - width/2 #TODO: If one pole is seen, is it left or right pole? # Calculate planar distance r (assuming we are moving perpendicular to # the hedge) if self.left_pole and self.right_pole: theta = abs(self.left_pole - self.right_pole) self.r = 3 / tan(radians(theta/2)) else: self.r = None if self.r and self.seen_crossbar: bar_phi = (-1*horizontal_lines[0][0] + frame.height/2) / (frame.height/2) * 32 self.crossbar_depth = self.r * atan(radians(bar_phi)) else: self.crossbar_depth = None ''' self.left_pole = None self.right_pole = None self.seen_crossbar = False self.crossbar_depth = None if self.debug and max_length != 0: cv.CvtColor(color_filtered, frame, cv.CV_GRAY2RGB) #libvision.misc.draw_lines(frame, vertical_lines) #libvision.misc.draw_lines(frame, horizontal_lines) # for line in raw_lines: # cv.Line(frame,line[0],line[1], (255,255,0), 10, cv.CV_AA, 0) # cv.Circle(frame, line[1], 15, (255,0,0), 2,8,0) # print len(raw_lines) #cv.ShowImage("Hedge", cv.CloneImage(frame)) if (crossbar_seg[0][0] - frame.width / 2) * 37 / ( frame.width / 2) < (crossbar_seg[1][0] - frame.width / 2) * 37 / (frame.width / 2): self.left_pole = round((crossbar_seg[0][0] - frame.width / 2) * 37 / (frame.width / 2)) self.right_pole = round( (crossbar_seg[1][0] - frame.width / 2) * 37 / (frame.width / 2)) else: self.left_pole = round((crossbar_seg[1][0] - frame.width / 2) * 37 / (frame.width / 2)) self.right_pole = round( (crossbar_seg[0][0] - frame.width / 2) * 37 / (frame.width / 2)) self.crossbar_depth = round( -1 * (crossbar_seg[1][1] - frame.height / 2) * 36 / (frame.height / 2)) if math.fabs(self.left_pole) <= 37 and math.fabs( self.left_pole) >= self.frame_boundary_thresh: self.left_pole = None if math.fabs(self.right_pole) <= 37 and math.fabs( self.right_pole) >= self.frame_boundary_thresh: self.right_pole = None self.seen_crossbar = True if self.left_pole and self.right_pole: self.returning = (self.left_pole + self.right_pole) / 2 print "Returning ", self.returning if self.last_seen < 0: self.last_center = None self.last_seen = 0 if self.last_center is None: self.last_center = self.returning self.seen_count = 1 elif math.fabs(self.last_center - self.returning) < self.center_trans_thresh: self.seen_count += 1 self.last_seen += 2 else: self.last_seen -= 1 if self.seen_count < self.seen_count_thresh: self.left_pole = None self.right_pole = None else: print "FOUND CENTER AND RETURNED IT" self.found = True else: self.returning = 0 if self.last_seen < 0: self.last_center = None self.last_seen = 0 self.last_seen -= 1 self.left_pole = None self.right_pole = None cv.Line(frame, crossbar_seg[0], crossbar_seg[1], (255, 255, 0), 10, cv.CV_AA, 0) if self.left_pole and crossbar_seg[0][0] < crossbar_seg[1][0]: cv.Line(frame, crossbar_seg[0], (crossbar_seg[0][0], crossbar_seg[0][0] - 500), (255, 0, 0), 10, cv.CV_AA, 0) elif self.left_pole: cv.Line(frame, crossbar_seg[1], (crossbar_seg[1][0], crossbar_seg[1][1] - 500), (255, 0, 0), 10, cv.CV_AA, 0) if self.right_pole and crossbar_seg[0][0] > crossbar_seg[1][0]: cv.Line(frame, crossbar_seg[0], (crossbar_seg[0][0], crossbar_seg[0][0] - 500), (255, 0, 0), 10, cv.CV_AA, 0) elif self.right_pole: cv.Line(frame, crossbar_seg[1], (crossbar_seg[1][0], crossbar_seg[1][1] - 500), (255, 0, 0), 10, cv.CV_AA, 0) # populate self.output with infos self.output.seen_crossbar = self.seen_crossbar self.output.left_pole = self.left_pole self.output.right_pole = self.right_pole #self.output.r = self.r self.output.crossbar_depth = self.crossbar_depth self.return_output() print self else: cv.CvtColor(color_filtered, frame, cv.CV_GRAY2RGB) svr.debug("Hedge", cv.CloneImage(frame)) svr.debug("Hedge2", test_frame)
def process_frame(self, frame): frametest = cv.CreateImage(cv.GetSize(frame), 8, 3) binarytest = cv.CreateImage(cv.GetSize(frame), 8, 1) cv.Copy(frame, frametest) cv.SetImageCOI(frametest, 3) cv.Copy(frametest, binarytest) cv.SetImageCOI(frametest, 0) svr.debug("R?", binarytest) # Resize image to 320x240 #copy = cv.CreateImage(cv.GetSize(frame), 8, 3) #cv.Copy(frame, copy) #cv.SetImageROI(frame, (0, 0, 320, 240)) #cv.Resize(copy, frame, cv.CV_INTER_NN) found_gate = False unchanged_frame = cv.CreateImage(cv.GetSize(frame), 8, 3) cv.Copy(frame, unchanged_frame) cv.Smooth(frame, frame, cv.CV_MEDIAN, 7, 7) # Set binary image to have saturation channel hsv = cv.CreateImage(cv.GetSize(frame), 8, 3) binary = cv.CreateImage(cv.GetSize(frame), 8, 1) cv.CvtColor(frame, hsv, cv.CV_BGR2HSV) cv.SetImageCOI(hsv, 1) cv.Copy(hsv, binary) cv.SetImageCOI(hsv, 0) cv.AdaptiveThreshold( binary, binary, 255, cv.CV_ADAPTIVE_THRESH_MEAN_C, cv.CV_THRESH_BINARY_INV, self.adaptive_thresh_blocksize, self.adaptive_thresh, ) # Morphology kernel = cv.CreateStructuringElementEx(5, 5, 3, 3, cv.CV_SHAPE_ELLIPSE) cv.Erode(binary, binary, kernel, 1) cv.Dilate(binary, binary, kernel, 1) if self.debug: color_filtered = cv.CloneImage(binary) # Get Edges cv.Canny(binary, binary, 30, 40) # Hough Transform line_storage = cv.CreateMemStorage() raw_lines = cv.HoughLines2(binary, line_storage, cv.CV_HOUGH_STANDARD, rho=1, theta=math.pi / 180, threshold=self.hough_threshold, param1=0, param2=0) # Get vertical lines vertical_lines = [] for line in raw_lines: if line[1] < self.vertical_threshold or \ line[1] > math.pi-self.vertical_threshold: #absolute value does better grouping currently vertical_lines.append((abs(line[0]), line[1])) # Group vertical lines vertical_line_groups = [ ] # A list of line groups which are each a line list for line in vertical_lines: group_found = False for line_group in vertical_line_groups: if line_group_accept_test(line_group, line, self.max_range): line_group.append(line) group_found = True if not group_found: vertical_line_groups.append([line]) # Average line groups into lines vertical_lines = [] for line_group in vertical_line_groups: rhos = map(lambda line: line[0], line_group) angles = map(lambda line: line[1], line_group) line = (sum(rhos) / len(rhos), circular_average(angles, math.pi)) vertical_lines.append(line) # Get horizontal lines horizontal_lines = [] for line in raw_lines: dist_from_horizontal = (math.pi / 2 + line[1]) % math.pi if dist_from_horizontal < self.horizontal_threshold or \ dist_from_horizontal > math.pi-self.horizontal_threshold: horizontal_lines.append((abs(line[0]), line[1])) # Group horizontal lines horizontal_line_groups = [ ] # A list of line groups which are each a line list for line in horizontal_lines: group_found = False for line_group in horizontal_line_groups: if line_group_accept_test(line_group, line, self.max_range): line_group.append(line) group_found = True if not group_found: horizontal_line_groups.append([line]) if len(horizontal_line_groups) is 1: self.seen_crossbar = True if self.debug: rhos = map(lambda line: line[0], horizontal_line_groups[0]) angles = map(lambda line: line[1], horizontal_line_groups[0]) line = (sum(rhos) / len(rhos), circular_average(angles, math.pi)) horizontal_lines = [line] else: self.seen_crossbar = False horizontal_lines = [] self.left_pole = None self.right_pole = None print vertical_lines self.returning = 0 self.found = False if len(vertical_lines) is 2: roi = cv.GetImageROI(frame) width = roi[2] height = roi[3] self.left_pole = round( min(vertical_lines[0][0], vertical_lines[1][0]), 2) - width / 2 self.right_pole = round( max(vertical_lines[0][0], vertical_lines[1][0]), 2) - width / 2 self.returning = (self.left_pole + self.right_pole) / 2 print "Returning ", self.returning if self.last_seen < 0: self.last_center = None self.last_seen = 0 if self.last_center is None: self.last_center = self.returning self.seen_count = 1 elif math.fabs(self.last_center - self.returning) < self.center_trans_thresh: self.seen_count += 1 self.last_seen += 2 else: self.last_seen -= 1 if self.seen_count < self.seen_count_thresh: self.left_pole = None self.right_pole = None else: print "FOUND CENTER AND RETURNED IT" self.found = True else: self.returning = 0 if self.last_seen < 0: self.last_center = None self.last_seen = 0 self.last_seen -= 1 self.left_pole = None self.right_pole = None #TODO: If one pole is seen, is it left or right pole? if self.debug: cv.CvtColor(color_filtered, frame, cv.CV_GRAY2RGB) libvision.misc.draw_lines(frame, vertical_lines) libvision.misc.draw_lines(frame, horizontal_lines) if self.found: cv.Circle(frame, (int(frame.width / 2 + self.returning), int(frame.height / 2)), 15, (0, 255, 0), 2, 8, 0) font = cv.InitFont(cv.CV_FONT_HERSHEY_SIMPLEX, 1, 1, 1, 3) cv.PutText(frame, "Gate Sent to Mission Control", (100, 400), font, (255, 255, 0)) print frame.width #cv.ShowImage("Gate", cv.CloneImage(frame)) svr.debug("Gate", cv.CloneImage(frame)) svr.debug("Unchanged", cv.CloneImage(unchanged_frame)) #populate self.output with infos self.output.seen_crossbar = self.seen_crossbar self.output.left_pole = self.left_pole self.output.right_pole = self.right_pole self.return_output() print self
def process_frame(self, frame): debug_frame = cv.CreateImage(cv.GetSize(frame), 8, 3) cv.Copy(frame, debug_frame) cv.Smooth(frame, frame, cv.CV_MEDIAN, 7, 7) # Set binary image to have saturation channel hsv = cv.CreateImage(cv.GetSize(frame), 8, 3) binary = cv.CreateImage(cv.GetSize(frame), 8, 1) cv.CvtColor(frame, hsv, cv.CV_BGR2HSV) cv.SetImageCOI(hsv, 2) cv.Copy(hsv, binary) cv.SetImageCOI(hsv, 0) cv.AdaptiveThreshold( binary, binary, 255, cv.CV_ADAPTIVE_THRESH_MEAN_C, cv.CV_THRESH_BINARY_INV, self.adaptive_thresh_blocksize, self.adaptive_thresh, ) # Morphology kernel = cv.CreateStructuringElementEx(5, 5, 3, 3, cv.CV_SHAPE_ELLIPSE) cv.Erode(binary, binary, kernel, 1) cv.Dilate(binary, binary, kernel, 1) # Get Edges #cv.Canny(binary, binary, 30, 40) cv.CvtColor(binary, debug_frame, cv.CV_GRAY2RGB) # Hough Transform line_storage = cv.CreateMemStorage() raw_lines = cv.HoughLines2(binary, line_storage, cv.CV_HOUGH_STANDARD, rho=1, theta=math.pi / 180, threshold=self.hough_threshold, param1=0, param2=0) line_groups = [] # A list of line groups which are each a line list for line in raw_lines: group_found = False for line_group in line_groups: if line_group_accept_test(line_group, line, self.max_range): line_group.append(line) group_found = True if not group_found: line_groups.append([line]) # Average line groups into lines lines = [] for line_group in line_groups: rhos = map(lambda line: line[0], line_group) angles = map(lambda line: line[1], line_group) line = (sum(rhos) / len(rhos), circular_average(angles, math.pi)) lines.append(line) libvision.misc.draw_lines(debug_frame, raw_lines) # cv.CvtColor(color_filtered,debug_frame, cv.CV_GRAY2RGB) svr.debug("Bins", debug_frame)
def process_frame(self, frame): self.debug_frame = cv.CreateImage(cv.GetSize(frame), 8, 3) og_frame = cv.CreateImage(cv.GetSize(frame), 8, 3) cv.Copy(frame, self.debug_frame) cv.Copy(self.debug_frame, og_frame) cv.Smooth(frame, frame, cv.CV_MEDIAN, 7, 7) # Set binary image to have saturation channel hsv = cv.CreateImage(cv.GetSize(frame), 8, 3) binary = cv.CreateImage(cv.GetSize(frame), 8, 1) cv.CvtColor(frame, hsv, cv.CV_BGR2HSV) cv.SetImageCOI(hsv, 1) cv.Copy(hsv, binary) cv.SetImageCOI(hsv, 0) cv.AdaptiveThreshold( binary, binary, 255, cv.CV_ADAPTIVE_THRESH_MEAN_C, cv.CV_THRESH_BINARY_INV, self.adaptive_thresh_blocksize, self.adaptive_thresh, ) # Morphology kernel = cv.CreateStructuringElementEx(5, 5, 3, 3, cv.CV_SHAPE_ELLIPSE) cv.Erode(binary, binary, kernel, 1) cv.Dilate(binary, binary, kernel, 1) # Get Edges #cv.Canny(binary, binary, 30, 40) cv.CvtColor(binary, self.debug_frame, cv.CV_GRAY2RGB) # Hough Transform line_storage = cv.CreateMemStorage() raw_lines = cv.HoughLines2(binary, line_storage, cv.CV_HOUGH_PROBABILISTIC, rho=1, theta=math.pi / 180, threshold=self.hough_threshold, param1=self.min_length, param2=self.max_gap) lines = [] corners = [] for line in raw_lines: lines.append(line) # Grouping lines depending on endpoint similarities for line1 in lines[:]: for line2 in lines[:]: if line1 in lines and line2 in lines and line1 != line2: if math.fabs(line1[0][0] - line2[0][0]) < self.max_corner_range and \ math.fabs(line1[0][1] - line2[0][1]) < self.max_corner_range and \ math.fabs(line1[1][0] - line2[1][0]) < self.max_corner_range and \ math.fabs(line1[1][1] - line2[1][1]) < self.max_corner_range: if line_distance(line1[0], line1[1]) > line_distance( line2[0], line2[1]): lines.remove(line2) else: lines.remove(line1) elif math.fabs(line1[0][0] - line2[1][0]) < self.max_corner_range and \ math.fabs(line1[0][1] - line2[1][1]) < self.max_corner_range and \ math.fabs(line1[1][0] - line2[0][0]) < self.max_corner_range and \ math.fabs(line1[1][1] - line2[0][1]) < self.max_corner_range: if line_distance(line1[0], line1[1]) > line_distance( line2[0], line2[1]): lines.remove(line2) else: lines.remove(line1) for line in lines: corners.append(line[0]) corners.append(line[1]) for corner1 in corners: for corner2 in corners: for corner3 in corners: for corner4 in corners: # Checks that corners are not the same and are in the proper orientation if corner4[0] != corner3[0] and corner4[0] != corner2[0] and corner4[0] != corner1[0] and \ corner3[0] != corner2[0] and corner3[0] != corner1[0] and corner2[0] != corner1[0] and \ corner4[1] != corner3[1] and corner4[1] != corner2[1] and corner4[1] != corner1[1] and \ corner3[1] != corner2[1] and corner3[1] != corner1[1] and corner2[1] != corner1[1] and \ corner2[0] >= corner3[0] and corner1[1] >= corner4[1] and corner2[0] >= corner1[0]: # Checks that the side ratios are correct if math.fabs(line_distance(corner1, corner3) - line_distance(corner2, corner4)) < self.size_threshold and \ math.fabs(line_distance(corner1, corner2) - line_distance(corner3, corner4)) < self.size_threshold and \ math.fabs(line_distance(corner1, corner3) / line_distance(corner1, corner2)) < self.ratio_threshold and \ math.fabs(line_distance(corner1, corner2) / line_distance(corner1, corner3)) < self.ratio_threshold: #^^^ CHANGED OR TO AND --> DID MUCH BETTER. CONSIDER CHANGING ON BINSCORNER # Checks that angles are roughly 90 degrees angle_cnr_2 = math.fabs( angle_between_lines( line_slope(corner1, corner2), line_slope(corner2, corner4))) if self.angle_min < angle_cnr_2 < self.angle_max: angle_cnr_3 = math.fabs( angle_between_lines( line_slope(corner1, corner3), line_slope(corner3, corner4))) if self.angle_min2 < angle_cnr_3 < self.angle_max2: new_box = Pizza( corner1, corner2, corner3, corner4) self.match_Boxes(new_box) for Box in self.Boxes[:]: Box.lastseen -= 1 if Box.lastseen < 0: self.Boxes.remove(Box) self.draw_pizza() for line in lines: cv.Line(self.debug_frame, line[0], line[1], (255, 255, 0), 10, cv.CV_AA, 0) cv.Circle(self.debug_frame, line[0], 15, (255, 0, 0), 2, 8, 0) cv.Circle(self.debug_frame, line[1], 15, (255, 0, 0), 2, 8, 0) self.output.pizza = self.Boxes anglesum = 0 for Box in self.Boxes: Box.theta = (Box.center[0] - frame.width / 2) * 37 / (frame.width / 2) Box.phi = -1 * (Box.center[1] - frame.height / 2) * 36 / (frame.height / 2) anglesum += Box.angle if len(self.output.pizza) > 0: self.output.orientation = anglesum / len(self.output.pizza) else: self.output.orientation = None self.return_output() svr.debug("Pizza", self.debug_frame) svr.debug("Original", og_frame)
def process_frame(self, frame): # Resize image to 320x240 #copy = cv.CreateImage(cv.GetSize(frame), 8, 3) #cv.Copy(frame, copy) #cv.SetImageROI(frame, (0, 0, 320, 240)) #cv.Resize(copy, frame, cv.CV_INTER_NN) found_hedge = False cv.Smooth(frame, frame, cv.CV_MEDIAN, 7, 7) # Set binary image to have value channel hsv = cv.CreateImage(cv.GetSize(frame), 8, 3) binary = cv.CreateImage(cv.GetSize(frame), 8, 1) cv.CvtColor(frame, hsv, cv.CV_BGR2HSV) cv.SetImageCOI(hsv, 1) cv.Copy(hsv, binary) cv.SetImageCOI(hsv, 0) cv.AdaptiveThreshold( binary, binary, 255, cv.CV_ADAPTIVE_THRESH_MEAN_C, cv.CV_THRESH_BINARY_INV, self.adaptive_thresh_blocksize, self.adaptive_thresh, ) # Morphology ''' kernel = cv.CreateStructuringElementEx(3, 3, 1, 1, cv.CV_SHAPE_ELLIPSE) cv.Erode(binary, binary, kernel, 1) cv.Dilate(binary, binary, kernel, 1) ''' if self.debug: color_filtered = cv.CloneImage(binary) # Get Edges #cv.Canny(binary, binary, 30, 40) # Hough Transform line_storage = cv.CreateMemStorage() raw_lines = cv.HoughLines2(binary, line_storage, cv.CV_HOUGH_STANDARD, rho=1, theta=math.pi / 180, threshold=self.hough_threshold, param1=0, param2=0) # Get vertical lines vertical_lines = [] for line in raw_lines: if line[1] < self.vertical_threshold or \ line[1] > math.pi - self.vertical_threshold: vertical_lines.append((abs(line[0]), line[1])) # Group vertical lines vertical_line_groups = [ ] # A list of line groups which are each a line list for line in vertical_lines: group_found = False for line_group in vertical_line_groups: if line_group_accept_test(line_group, line, self.max_range): line_group.append(line) group_found = True if not group_found: vertical_line_groups.append([line]) # Average line groups into lines vertical_lines = [] for line_group in vertical_line_groups: rhos = map(lambda line: line[0], line_group) angles = map(lambda line: line[1], line_group) line = (sum(rhos) / len(rhos), circular_average(angles, math.pi)) vertical_lines.append(line) # Get horizontal lines horizontal_lines = [] for line in raw_lines: dist_from_horizontal = (math.pi / 2 + line[1]) % math.pi if dist_from_horizontal < self.horizontal_threshold or \ dist_from_horizontal > math.pi - self.horizontal_threshold: horizontal_lines.append((abs(line[0]), line[1])) # Group horizontal lines horizontal_line_groups = [ ] # A list of line groups which are each a line list for line in horizontal_lines: group_found = False for line_group in horizontal_line_groups: if line_group_accept_test(line_group, line, self.max_range): line_group.append(line) group_found = True if not group_found: horizontal_line_groups.append([line]) if len(horizontal_line_groups) is 1: self.seen_crossbar = True rhos = map(lambda line: line[0], horizontal_line_groups[0]) angles = map(lambda line: line[1], horizontal_line_groups[0]) line = (sum(rhos) / len(rhos), circular_average(angles, math.pi)) horizontal_lines = [line] else: self.seen_crossbar = False horizontal_lines = [] self.left_pole = None self.right_pole = None if len(vertical_lines) is 2: roi = cv.GetImageROI(frame) width = roi[2] height = roi[3] self.left_pole = round( min(vertical_lines[0][0], vertical_lines[1][0]), 2) - width / 2 self.right_pole = round( max(vertical_lines[0][0], vertical_lines[1][0]), 2) - width / 2 # TODO: If one pole is seen, is it left or right pole? # Calculate planar distance r (assuming we are moving perpendicular to # the hedge) if self.left_pole and self.right_pole: theta = abs(self.left_pole - self.right_pole) self.r = 3 / tan(radians(theta / 2)) else: self.r = None if self.r and self.seen_crossbar: bar_phi = (-1 * horizontal_lines[0][0] + frame.height / 2) / (frame.height / 2) * 32 self.crossbar_depth = self.r * atan(radians(bar_phi)) else: self.crossbar_depth = None if self.debug: cv.CvtColor(color_filtered, frame, cv.CV_GRAY2RGB) libvision.misc.draw_lines(frame, vertical_lines) libvision.misc.draw_lines(frame, horizontal_lines) #cv.ShowImage("Hedge", cv.CloneImage(frame)) svr.debug("Hedge", cv.CloneImage(frame)) # populate self.output with infos self.output.seen_crossbar = self.seen_crossbar self.output.left_pole = self.left_pole self.output.right_pole = self.right_pole self.output.r = self.r self.output.crossbar_depth = self.crossbar_depth self.return_output() print self