def setValues(threshold): """Adds trackbars to the main window for adjusting thresholding values Optionally uses some other window (specified by 'window'). """ global current current = threshold for i,v in enumerate(threshold[1]): T[1][i] = v if trackbar_window: cv.SetTrackbarPos("ch%d min"%(i+1), trackbar_window, v) for i,v in enumerate(threshold[2]): T[2][i] = v if trackbar_window: cv.SetTrackbarPos("ch%d max"%(i+1), trackbar_window, v)
def getCornerMap(frame): global corner cornerMap = cv.CreateMat(frame.height, frame.width, cv.CV_32FC1) if (corner < 1): corner = 1 cv.SetTrackbarPos("corner", "RGB module", 1) cv.CornerHarris(frame, cornerMap, corner) return (cornerMap)
def change_object(idx): currentcolors = [] for i in range(6): currentcolors.append(colors[idx * 6 + i]) cv.SetTrackbarPos('object', objects, idx) cv.SetTrackbarPos('hue', objects, int(currentcolors[0])) cv.SetTrackbarPos('saturation', objects, int(currentcolors[1])) cv.SetTrackbarPos('value', objects, int(currentcolors[2])) cv.SetTrackbarPos('maxhue', objects, int(currentcolors[3])) cv.SetTrackbarPos('maxsaturation', objects, int(currentcolors[4])) cv.SetTrackbarPos('maxvalue', objects, int(currentcolors[5]))
def show_images(args): '''show all images''' global image, idx cv.NamedWindow('Bayer') if len(args) > 1: tbar = cv.CreateTrackbar('Image', 'Bayer', 0, len(args) - 1, change_image) cv.SetMouseCallback('Bayer', mouse_event, None) idx = 0 pgm = None while True: print(args[idx]) (image, pgm) = change_image(idx) oldidx = idx newidx = cuav_util.key_menu(oldidx, len(args), image, '%s.png' % args[idx][:-4], pgm=pgm) idx += (newidx - oldidx) cv.SetTrackbarPos('Image', 'Bayer', idx) cv.DestroyWindow('Bayer')
def process_section(self): """ calculates the min/max slider values for a given section and sets the slider values to them """ print "sections:", self.sections if len(self.sections) > 0: # pull out all sections with 'a' in the front adds = [[x[1], x[2]] for x in self.sections if x[0] == 'a'] # pull out all sections with a 's' in the front subs = [[x[1], x[2]] for x in self.sections if x[0] == 's'] print "adds: ", adds print "subs: ", subs # mins default to highest value, maxs default to lowest value minmax = [[255,0] for x in xrange(6)] for i in adds: # get coords for easy access x0 = i[0][0] y0 = i[0][1] x1 = i[1][0] y1 = i[1][1] # Make sure that x0, y0 are smaller than x1, y1 if x0 > x1: x0, x1 = x1, x0 if y0 > y1: y0, y1 = y1, y0 # Go through pixel by pixel for x in xrange(x0, x1): for y in xrange(y0, y1): in_range = True # the point will be out of range if it is in a subtracted section somewhere for p in subs: if x > p[0][0] and y > p[0][1] and x < p[1][0] and y < p[1][1]: in_range = False if in_range: # Now it isn't part of a subtracted region (b,g,r) = self.image[y,x] (h,s,v) = self.hsv[y,x] allv = [r,g,b, h, s, v] for j in xrange(len(allv)): if allv[j] < minmax[j][0]: minmax[j][0] = int(allv[j]) if allv[j] > minmax[j][1]: minmax[j][1] = int(allv[j]) # Now set sliders to those values names = [["low_red", "high_red"], ["low_green", "high_green"],\ ["low_blue", "high_blue"], ["low_hue", "high_hue"],\ ["low_sat", "high_sat"], ["low_val", "high_val"]] for i in xrange(len(names)): for j in xrange(2): self.thresholds[names[i][j]] = minmax[i][j] cv.SetTrackbarPos(names[i][j], 'sliders', minmax[i][j])
def check_key_press(key_press): """ this handler is called when a real key press has been detected, and updates everything appropriately """ # get D so that we can change values in it global D D.last_key_pressed = key_press if key_press == ord('q') or key_press == 27: # if a 'q' or ESC was pressed print "quitting... first, stopping the FSM" D.STOP = True # stop the FSM... time.sleep(2) # give it some time to stop D.running = False # stops the main loop return elif key_press == ord('F'): # toggle our FSM if D.STOP == True: # the FSM is not running print "Starting FSM..." D.STOP = False # so the FSM will run... D.start_fsm_time = time.time() # record our fsm's starting time... transition(0.1, state_start) else: D.STOP = True # will stop one, if it's running print "Stopping FSM..." time.sleep(2.0) # wait a bit so that the FSM thread will stop elif key_press == ord('i'): D.launchcontrol.sendCodeToLauncher("up") elif key_press == ord('k'): D.launchcontrol.sendCodeToLauncher("down") elif key_press == ord('j'): D.launchcontrol.sendCodeToLauncher("left") elif key_press == ord('l'): D.launchcontrol.sendCodeToLauncher("right") elif key_press == ord('\n'): D.launchcontrol.sendCodeToLauncher("fire") elif key_press in [ord('z'), ord(' ')]: D.launchcontrol.sendCodeToLauncher("stop") elif key_press == ord('S'): # save to file x = D.thresholds # the value we will save f = open("./thresh.txt", "w") # open the file "thresh.txt" for writing print >> f, x # print x to the file object f f.close() # it's good to close the file afterwards print "Saving current slider threshold values..." elif key_press == ord('L'): # load from file try: f = open("./thresh.txt", "r") # open the file "thresh.txt" for reading data = f.read() # read everything from f into data x = eval(data) # eval is Python's evaluation function f.close() # it's good to close the file afterwards D.thresholds = x cv.SetTrackbarPos('low_red', 'sliders', D.thresholds['low_red']) cv.SetTrackbarPos('high_red', 'sliders', D.thresholds['high_red']) cv.SetTrackbarPos('low_green', 'sliders', D.thresholds['low_green']) cv.SetTrackbarPos('high_green', 'sliders', D.thresholds['high_green']) cv.SetTrackbarPos('low_blue', 'sliders', D.thresholds['low_blue']) cv.SetTrackbarPos('high_blue', 'sliders', D.thresholds['high_blue']) cv.SetTrackbarPos('low_hue', 'sliders', D.thresholds['low_hue']) cv.SetTrackbarPos('high_hue', 'sliders', D.thresholds['high_hue']) cv.SetTrackbarPos('low_sat', 'sliders', D.thresholds['low_sat']) cv.SetTrackbarPos('high_sat', 'sliders', D.thresholds['high_sat']) cv.SetTrackbarPos('low_val', 'sliders', D.thresholds['low_val']) cv.SetTrackbarPos('high_val', 'sliders', D.thresholds['high_val']) print "Loaded saved slider threshold values..." except: print "No file named thresh.txt found!" else: print "Did not recognize a command for key #", key_press, "(", chr( key_press), ")"
def check_key_press(key_press): """ this handler is called when a real key press has been detected, and updates everything appropriately """ # get D so that we can change values in it global D D.last_key_pressed = key_press # if it was ESC, make it 'q' if key_press == 27: key_press = ord('q') # publish the key press, if it's in range if ord(' ') <= key_press <= ord('z'): D.K_PUB.publish(String(str(chr(key_press)))) # if a 'q' or ESC was pressed, we quit if key_press == ord('q'): print "quitting" rospy.signal_shutdown("Quit requested from keyboard") return # help menu if key_press == ord('h'): print " Keyboard Command Menu" print " ==============================" print " q : quit" print " ESC : quit" print " h : help menu" print " w : show total threshold image in threshold window" print " r : show red image in threshold window" print " t : show green image in threshold window" print " y : show blue image in threshold window" print " f : show thresholded red image in threshold window" print " g : show thresholded blue image in threshold window" print " h : show thresholded green image in threshold window" print " a : show hue image in threshold window" print " s : show saturation image in threshold window" print " d : show value image in threshold window" print " z : show thresholded hue image in threshold window" print " x : show thresholded saturation image in threshold window" print " c : show thresholded value image in threshold window" print " v : saves threshold values to file (overwriting)" print " b : loads threshold values from file" print " u : mousedrags no longer set thresholds" print " i : mousedrag set thresholds to area within drag" elif key_press == ord('v'): x = D.thresholds f = open("./thresh.txt", "w") # open the file "thresh.txt" for writing print >> f, x # print x to the file object f f.close() # it's good to close the file afterwards print "(v) Wrote thresholds to thresh.txt. Use 'b' to load them." elif key_press == ord('b'): # should check if file exists! f = open("./thresh.txt", "r") # open the file "thresh.txt" for reading data = f.read() # read everything from f into data x = eval(data) # eval is Python's evaluation function # eval evaluates strings as if they were at the Python shell f.close() # its good to close the file afterwards print "(b) Loaded thresholds from thresh.txt. Use 'v' to save them." # Set threshold values in D D.thresholds = x # Update threshold values on actual sliders for i in [ 'low_red', 'high_red', 'low_green', 'high_green', 'low_blue', 'high_blue', 'low_hue', 'high_hue', 'low_sat', 'high_sat', 'low_val', 'high_val' ]: cv.SetTrackbarPos(i, 'sliders', D.thresholds[i]) # threshold keypresses: elif key_press in D.key_dictionary.keys(): D.current_threshold = D.key_dictionary[key_press]
f.write('%s %u %u\n' % (args[idx], x, y)) f.close() print("Joe at %u,%u of %s" % (x, y, args[idx])) def change_image(i): '''show image idx''' global idx, pgm idx = i pgm = util.PGM(args[idx]) cv.ShowImage('CanberraUAV', pgm.img) cv.NamedWindow('CanberraUAV') if len(args) > 1: tbar = cv.CreateTrackbar('Image', 'CanberraUAV', 0, len(args) - 1, change_image) cv.SetMouseCallback('CanberraUAV', mouse_event, None) idx = 0 pgm = None while True: print(args[idx]) change_image(idx) oldidx = idx newidx = util.key_menu(oldidx, len(args), None, None) idx += (newidx - oldidx) cv.SetTrackbarPos('Image', 'CanberraUAV', idx) cv.DestroyWindow('CanberraUAV')
cv.GetCaptureProperty(g_capture, cv.CV_CAP_PROP_FRAME_COUNT)) if (frames == 0): print "No video" loop = False else: # create the trackbar and attach the function "onTrackbarSlide" to it loop = True cv.CreateTrackbar("Position", "Example3", g_slider_pos, frames, onTrackbarSlide) while (loop): if (not (useTrackbar and downUpdateTrackbarPos)): frame = cv.QueryFrame(g_capture) if (frame == None): cv.SetTrackbarPos("Position", "Example3", 0) cv.ShowImage("Example3", frame) # wait until a key is pressed, or 33 ms have passed char = cv.WaitKey(33) if (useTrackbar): # if i dont use the listenToSliderVariable, the act of updating the slide makes it # slide, seems like a bug to me. listenToSlider = False curpos = long( cv.GetCaptureProperty(g_capture, cv.CV_CAP_PROP_POS_FRAMES)) cv.SetTrackbarPos("Position", "Example3", curpos) listenToSlider = True # if no key was pressed (timeout) we get -1 if (char != -1): # 27 is esc