def choose(image): # load the image, clone it, and setup the mouse callback function clone = image.copy() cv2.namedWindow("Select Markers") cv2.setMouseCallback("Select Markers", select_reference, clone) selected_refs = list() # keep looping until the 'q' key is pressed while True: # display the image and wait for a keypress cv2.imshow("Select Markers", image) key = cv2.waitKey(1) & 0xFF # if the 'r' key is pressed, reset the points if key == ord("r"): image = clone.copy() #need to reset the callback as the pointer to image has changed cv2.setMouseCallback("Select Markers", select_reference, image) # if the 's' key is pressed, save the image as one of the reference elif key == ord("s"): selected_refs = refPt break # close all open windows cv2.destroyAllWindows() return selected_refs
def colour_picker(colourSTR ="(unspecified)", colourGrabWindowSize = 5, colourHueWindowSize = 40, colourSaturationWindowSize= 40, colourValueWindowSize =40): global cam global hsv print "Right click the ",colourSTR," blob. Hit escape when done." cv2.namedWindow("image") cv2.setMouseCallback("image", mouseCallBack, param=None) try: while True: #Get image from webcam and convert to greyscale ret, img = cam.read() hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV) cv2.imshow("image", img) cMinY = max(0, rightButtonY - colourGrabWindowSize) cMaxY = min(len(hsv) - 1, rightButtonY + colourGrabWindowSize) cMinX = max(0, rightButtonX - colourGrabWindowSize) cMaxX = min(len(hsv[0]) - 1, rightButtonX + colourGrabWindowSize) cHue = int(npy.mean(hsv[redMinY:redMaxY, redMinX:redMaxX, 0])) cSaturation = int(npy.mean(hsv[redMinY:redMaxY, redMinX:redMaxX, 1])) cValue = int(npy.mean(hsv[redMinY:redMaxY, redMinX:redMaxX, 2])) #Sleep infinite loop for ~10ms #Exit if user presses <Esc> if cv2.waitKey(10) == 27: break finally: cv2.destroyWindow("image") return np.array(cHue, cSaturation, cValue)
def __init__(self, window_name): self.handlers = dict() self.window_name = window_name cv2.namedWindow(self.window_name) cv2.setMouseCallback(self.window_name, self.central_handler)
def __init__(self, w=640, h=480, resizable=False): super(OpencvFramework, self).__init__() if fwSettings.onlyInit: # testing mode doesn't initialize pygame return self._viewZoom = 10.0 self._viewCenter = None self._viewOffset = None self.screenSize = None self.fps = 0 self.screen = np.zeros((h, w, 3), np.uint8) if resizable: cv2.namedWindow(self.name, getattr(cv2,'WINDOW_NORMAL',0)) cv2.resizeWindow(self.name, w, h) else: cv2.namedWindow(self.name, getattr(cv2, 'WINDOW_AUTOSIZE', 1)) cv2.setMouseCallback(self.name, self._on_mouse) self._t0 = time.time() self.textLine = 30 self._font_name, self._font_scale, self._font_thickness = cv2.FONT_HERSHEY_SIMPLEX, 0.5, 1 (_,self._font_h),_ = cv2.getTextSize("X",self._font_name,self._font_scale,self._font_thickness) self.screenSize = b2Vec2(w,h) self.renderer = OpencvDraw(surface=self.screen, test=self) self.world.renderer=self.renderer self.viewCenter = (0,20.0) self.groundbody = self.world.CreateBody()
def position_interpolator(background): global positions if not isfile(POSITIONS_DUMP_FILENAME): def callback(event, x, y, flags, parameters): if event == 1: #cv2.EVENT_RBUTTONDOWN: positions.append(Coordinate(x, y)) cv2.namedWindow("Interpolator") cv2.setMouseCallback("Interpolator", callback) while True: cv2.imshow("Interpolator", background.array) if cv2.waitKey() & 0xFF == 27: break cv2.destroyWindow("Interpolator") with open(POSITIONS_DUMP_FILENAME, "w") as positions_dump_file: pickle.dump(positions, positions_dump_file) else: with open(POSITIONS_DUMP_FILENAME, "r") as positions_dump_file: positions = pickle.load(positions_dump_file) t = map(lambda i: i * STEP, range(len(positions))) x = map(lambda p: p.x, positions) y = map(lambda p: p.y, positions) f_x = interpolate.interp1d(t, x, kind = "quadratic") f_y = interpolate.interp1d(t, y, kind = "quadratic") return PositionInterpolator(f_x, f_y)
def run(self): rate = rospy.Rate(10) done = False cv2.namedWindow("kinect_view") cv2.setMouseCallback("kinect_view", self.mouse_call) while (not rospy.is_shutdown() and not done): if self.image is None: continue image = np.copy(self.image) state = self.states[self.state].replace('_', ' ') cv2.putText(image, 'Click the {}'.format(self.target_object), (10, self.image.shape[1] - 100), self.font, 1, (255, 100, 80), 2) self.draw_corners(image) if self.is_done: cv2.polylines(image, np.int32([self.corners]), True, (0, 255, 0), 6) done = True print 'DONE' cv2.imshow("kinect_view", image) key = cv2.waitKey(1) & 0xFF if key == ord('q'): break rate.sleep() if done: cv2.destroyWindow("kinect_view")
def get_polyline(image,window_name): cv2.namedWindow(window_name) class GetPoly: xys = [] done = False def callback(self,event, x, y, flags, param): if self.done == True: pass elif event == cv2.EVENT_LBUTTONDOWN: self.xys.append((x,y)) elif event == cv2.EVENT_MBUTTONDOWN: self.done = True gp = GetPoly() cv2.setMouseCallback(window_name,gp.callback) print "press middle mouse button or 'c' key to complete the polygon" while not gp.done: im_copy = image.copy() for (x,y) in gp.xys: cv2.circle(im_copy,(x,y),2,(0,255,0)) if len(gp.xys) > 1 and not gp.done: cv2.polylines(im_copy,[np.array(gp.xys).astype('int32')],False,(0,255,0),1) cv2.imshow(window_name,im_copy) key = cv2.waitKey(50) if key == ord('c'): gp.done = True #cv2.destroyWindow(window_name) return gp.xys
def main(): img = None main_win = Windows_handler.WinHandler(title='Nox',class_name='Qt5QWindowIcon') main_box = main_win.get_bbox() px_handler = Pixel_handler.PixelSearch(win_handler=main_win) mouse = Mouse_handler.MouseMovement(window_handler=main_win) main_win.init_window() cv2.namedWindow('image') cv2.namedWindow('config') # create trackbars for color change cv2.createTrackbar('R', 'config', 0, 255, nothing) cv2.createTrackbar('G', 'config', 0, 255, nothing) cv2.createTrackbar('B', 'config', 0, 255, nothing) cv2.createTrackbar('Offset', 'config', 0, 255, nothing) while True: img = px_handler.grab_window(bbox=main_box) img = px_handler.img_to_numpy(img,compound=False) img = cv2.cvtColor(img,cv2.COLOR_RGB2BGR) cv2.imshow('image',img) cv2.setMouseCallback('image', mouse_event, param=img) k = cv2.waitKey(1) if k == ord('q'): # wait for ESC key to exit cv2.destroyAllWindows() quit(0)
def run(self, camera=False): frame = cv2.namedWindow(FRAME_NAME) # Set callback cv2.setMouseCallback(FRAME_NAME, self.draw) if camera: cap = cv2.VideoCapture(0) for i in range(10): status, image = cap.read() else: image = cv2.imread('00000001.jpg') self.image = cv2.undistort(image, CMATRIX, DIST, None, NCMATRIX) # Get various data about the image from the user self.get_pitch_outline() self.get_zone('Zone_0', 'draw LEFT Defender') self.get_zone('Zone_1', 'draw LEFT Attacker') self.get_zone('Zone_2', 'draw RIGHT Attacker') self.get_zone('Zone_3', 'draw RIGHT Defender') self.get_goal('Zone_0') self.get_goal('Zone_3') print 'Press any key to finish.' cv2.waitKey(0) cv2.destroyAllWindows() # Write out the data # self.dump('calibrations/calibrate.json', self.data) tools.save_croppings(pitch=self.pitch, data=self.data)
def __init__(self): """Initialize""" """set the number of torpedo's that should be on the board""" self.numTopedos = 5 """set the number of mines""" self.numMines = 3 """Initialize PyGame""" pygame.init() """Set the window Size""" infoObject = pygame.display.Info() self.width = infoObject.current_w self.height = infoObject.current_h """self.width = 1600 self.height = 900""" """Create the Screen""" self.screen = pygame.display.set_mode((self.width, self.height))#, pygame.FULLSCREEN #camera rectangle values self.rectangleReady= False self.corner1Selected= False self.rectangle= None self.cap = cv2.VideoCapture(0) cv2.namedWindow('frame',1) cv2.setMouseCallback('frame', self.onmouse)
def get_cams(self, image): cv2.namedWindow("Screw") cv2.startWindowThread() cv2.setMouseCallback("Screw", self._get_screw) self.screw_location = Point(0,0) while True: img = copy.copy(image) cv2.circle(img, self.screw_location.to_image(), 3, (0,0,255), 3) cv2.imshow('Screw', img) k = cv2.waitKey(33) if k==10: # enter pressed break elif k==-1: pass else: print k print 'Press Enter to continue..' cv2.destroyWindow('Screw') arc = self.screw_location - self.board_center cam_angle = arc.angle() self.cam_angle = cam_angle if len(self.base_cams) != 0: self.cams = self.rotate(self.base_cams, cam_angle) # self.cams = [c for c in self.cams if c.y <= 0] self.locate_cams(image)
def run(self): cv2.namedWindow('ANALOG', flags=cv2.WINDOW_KEEPRATIO) cv2.setMouseCallback('ANALOG', self.on_mouse) while True: image = self.img.copy() text = 'Press "e" to exit and save, "r" to reset current data' cv2.putText(img=image, org=(100, 100), text=text, fontFace=cv2.FONT_HERSHEY_SIMPLEX, fontScale=1, color=(255, 255, 255), thickness=2, lineType=cv2.LINE_AA) h = 150 for t in self.training_data: txt = "deg {0}, value: {1}".format(t['degree'], t['value']) cv2.putText(img=image, org=(100, h), text=txt, fontFace=cv2.FONT_HERSHEY_SIMPLEX, fontScale=1, color=(255, 255, 255), thickness=2, lineType=cv2.LINE_AA) h += 30 cv2.imshow('ANALOG', image) key = cv2.waitKey(33) & 0xFF if key == ord('e'): break elif key == ord('r'): self.training_data = [] cv2.destroyWindow('ANALOG') return self.training_data
def get_img_reference_points(): """ This function get 4 points of reference from the image from the right hand of baxter. Returns an array of size 4, with 4 coordinates: [[x1,y1], [x2,y2], [x3,y3], [x4,y4]]. TODO: implement this. We have to define a color we will mark the table and get 4 points of that color from the image. """ # The following line is just for test. raw_input('Enter to capture image.') image = baxter.getImageFromRightHandCamera() cvimage = baxter.getLastCvImage() while n_clicks <= tot_clicks-1: # displays the image cv2.imshow("Click", cvimage) #cv.ShowImage("Click", cvimage) #calls the callback function "on_mouse_click'when mouse is clicked inside window cv2.setMouseCallback("Click", on_mouse_click, param=1) #cv.SetMouseCallback("Click", on_mouse_click, param=1) #cv.WaitKey(1000) cv2.waitKey(1000) #print points cv2.destroyAllWindows() return points
def GUI(): global points done = False while(True): points = refinePoints(gray, points) imgD = draw_points(imgO) cv2.imshow(WINDOW_NAME, imgD) cv2.setMouseCallback(WINDOW_NAME, onMouse) pressedChar = cv2.waitKey(0) if pressedChar == 27: # pressed ESC break elif pressedChar == 13: # pressed ENTER done = True break elif pressedChar == 117: # pressed U if len(points) > 0: points.pop() else: print 'Press one of these:' print '\tESC:\t\t to exit' print '\tENTER:\t\t to proceed' print '\tU:\t\t to remove poins' return done
def __init__(self, win, callback): self.win = win self.callback = callback cv2.setMouseCallback(win, self.onmouse) self.drag_start = None self.drag_rect = None self.dragging = False
def handler(img, flag): cv2.namedWindow("image") cv2.setMouseCallback("image", crop) while 1: cv2.imshow("image", img) key = cv2.waitKey(0) & 0xFF # To select ROI again in case of wrong selection if key == ord("r"): print "To select the roi of the player " + flag + " again press : " + flag break # To save the cordinates and the cropped image to a folder elif key == ord("s"): local_roi = {flag: {"x1": crop_pts[0][0], "y1": crop_pts[0][1], "x2": crop_pts[1][0], "y2": crop_pts[1][1]}} roi_json["rois"].update(local_roi) print roi_json clone = img.copy() roi = clone[crop_pts[0][1] : crop_pts[1][1], crop_pts[0][0] : crop_pts[1][0]] image_path = "cropped_images/test_crop_" + flag + ".jpg" cv2.imwrite(image_path, roi) print "saved" break
def process_video(self): cv2.namedWindow('Webcam') cv2.setMouseCallback('Webcam', self.onmouse) while cv2.waitKey(30) <= 0: success, img = self.vc.read() if not success: break if self.keypoints is None: # wait for the user to define the keypoints if self.click1 is not None: if self.click2 is not None: # extract rectangle, start finding matches self.define_interesting_features(img, (self.click1, self.click2)) elif self.drag is not None: cv2.rectangle(img, self.click1, self.drag, (255, 0 ,0) ) kp,desc = self.detector.detectAndCompute(img, None) img = cv2.drawKeypoints(img, kp) else: # do matching in realtime and display the results # - might need to do some buffering to make this work self.match(img) cv2.imshow("Webcam", img)
def show_hls(img, winname=None): if winname is None: winname = 'Choose HLS bounds' h, w, _ = img.shape f = 720.0 / h img_720p = cv2.resize(img, dsize=None, fx=f, fy=f) def onmouse(event, x, y, flags, param): if event == cv2.EVENT_LBUTTONDOWN: bgr = img[y, x] hls = cv2.cvtColor(np.asarray([[bgr]], dtype=img.dtype), cv2.COLOR_BGR2HLS)[0, 0] out = img_720p.copy() cv2.putText(out, text='BGR={}, HLS={}'.format(bgr, hls), org=(0, 300), fontFace=cv2.FONT_HERSHEY_SIMPLEX, fontScale=0.5, color=(255, 255, 255)) cv2.imshow(winname, out) cv2.imshow(winname, img_720p) cv2.setMouseCallback(winname, onmouse) while True: k = cv2.waitKey(1) & 0xFF if k == 27: cv2.destroyWindow(winname) return
def get_coords(img, winname=None): out = img.copy() coords = [] if winname is None: winname = 'choose coords' def onmouse(event, x, y, flags, param): if event == cv2.EVENT_LBUTTONDOWN: coords.append((x, y)) cv2.circle(out, (x, y), radius=10, color=(0, 0, 255), thickness=-1) cv2.imshow(winname, out) cv2.imshow(winname, out) cv2.setMouseCallback(winname, onmouse) while True: k = cv2.waitKey(1) & 0xFF if k == 27: out = img.copy() coords = [] if k == ord(' '): cv2.destroyWindow(winname) return coords
def label_face(self, image): self.image = image image_temp = cv2.pyrUp(image) name = 'Current image -- see terminal window for instructions.' msgs = ['Click the top of the forehead!', 'Click the bottom of the chin!', 'Click the leftmost part of the head! (usually an ear)', 'Click the rightmost part of the head! (usually an ear)', 'How does this look?'] self.clicked = [] self.face = [image_temp.shape[1], image_temp.shape[0], -1, -1] for msg in msgs: cv2.namedWindow(name) cv2.rectangle(image_temp, tuple(self.face[0:2]), tuple(self.face[2:4]), (0,0,255), 1) cv2.imshow(name, image_temp) cv2.setMouseCallback(name, self.mouse_callback) self.need_click = True rospy.loginfo(msg) key = cv2.waitKey(0) if key == 1048696: # the letter "x", lowercase self.keep_going = False rospy.loginfo('Quitting!') break else: self.face[0] = min(self.face[0], self.clicked[-1][0]) # left boundary self.face[1] = min(self.face[1], self.clicked[-1][1]) # top boundary self.face[2] = max(self.face[2], self.clicked[-1][0]) # right boundary self.face[3] = max(self.face[3], self.clicked[-1][1]) # bottom boundary mask_temp = self.make_mask(image_temp) self.mask = cv2.pyrDown(mask_temp)
def __init__(self): ##### Edit These Lines ######################################################## # Real-world coordinates of clicked homography points self.world_points = [[0,0],[0,1],[1,1],[1,0]] # Default values of sliders. Edit this once you know what values to usef # for filtering COLOR_MIN = (0,0,0) # HSV COLOR_MAX = (179,255,255) #HSV BLUR_RADIUS = 0 BLUR_SIGMA = 0 ################################################################################ # Homography class variables self.homography_state = 'Prompt' self.homography_image = None self.image_points = [] self.H = None self.hue_old = [] # Kalman filter class variables self.obs = [] self.traj = [np.array([0.,0.,0.,0.])] self.P = np.eye(4) self.last_time = time.time() # ROS to CV image bridge self.bridge = CvBridge() #Publishes an image after image processing self.pub = rospy.Publisher('processed_image', Image) #Initialize the node rospy.init_node('object_tracker') #Subscribe to the image topic rospy.Subscriber("/usb_cam/image_raw", Image, self.img_received, queue_size=1) # Setup OpenCV windows and sliders def nothing(x): pass cv2.namedWindow("Thresh", cv.CV_WINDOW_AUTOSIZE) cv2.createTrackbar('blur_radius', "Thresh", BLUR_RADIUS, 50, nothing) cv2.createTrackbar('blur_sigma', "Thresh", BLUR_SIGMA, 10, nothing) cv2.createTrackbar('H_min', "Thresh", COLOR_MIN[0], 179, nothing) cv2.createTrackbar('S_min', "Thresh", COLOR_MIN[1], 255, nothing) cv2.createTrackbar('V_min', "Thresh", COLOR_MIN[2], 255, nothing) cv2.createTrackbar('H_max', "Thresh", COLOR_MAX[0], 179, nothing) cv2.createTrackbar('S_max', "Thresh", COLOR_MAX[1], 255, nothing) cv2.createTrackbar('V_max', "Thresh", COLOR_MAX[2], 255, nothing) cv2.namedWindow("Trajectory vs. Observations", cv.CV_WINDOW_AUTOSIZE) cv2.namedWindow("Homography", cv.CV_WINDOW_AUTOSIZE) cv2.setMouseCallback("Homography", self.on_mouse_click, param=1)
def interactive_imshow(img, lclick_cb=None, rclick_cb=None, **kwargs): """ Args: img (np.ndarray): an image (expect BGR) to show. lclick_cb, rclick_cb: a callback ``func(img, x, y)`` for left/right click event. kwargs: can be {key_cb_a: callback_img, key_cb_b: callback_img}, to specify a callback ``func(img)`` for keypress. Some existing keypress event handler: * q: destroy the current window * x: execute ``sys.exit()`` * s: save image to "out.png" """ name = 'tensorpack_viz_window' cv2.imshow(name, img) def mouse_cb(event, x, y, *args): if event == cv2.EVENT_LBUTTONUP and lclick_cb is not None: lclick_cb(img, x, y) elif event == cv2.EVENT_RBUTTONUP and rclick_cb is not None: rclick_cb(img, x, y) cv2.setMouseCallback(name, mouse_cb) key = chr(cv2.waitKey(-1) & 0xff) cb_name = 'key_cb_' + key if cb_name in kwargs: kwargs[cb_name](img) elif key == 'q': cv2.destroyWindow(name) elif key == 'x': sys.exit() elif key == 's': cv2.imwrite('out.png', img)
def mouseCallback(event, x, y, flags, data): pos, adj, controlPointsIndices, controlPointDesirePositions, newPos, selectedInd = data if event == cv2.EVENT_LBUTTONDOWN: nearestInd, nearestDst = nearestPoint(newPos, x, y) print flags, nearestInd if flags & cv2.EVENT_FLAG_SHIFTKEY : toFind = controlPointDesirePositions nearestInd,nearestDst = nearestPoint(toFind, x, y) print nearestInd, 'selected' selectedInd = nearestInd #print newPos elif flags & cv2.EVENT_FLAG_CTRLKEY: if nearestInd not in controlPointsIndices and nearestDst < 50.1: controlPointsIndices.append(nearestInd) print nearestInd, 'added' selectedInd = len(controlPointDesirePositions) controlPointDesirePositions.append(np.array([x, y])) else: controlPointDesirePositions[(selectedInd)] = [x, y] newPos, gs, cells = computeARAPImageWarpStage1(pos, adj, controlPointsIndices, controlPointDesirePositions, 100000) newPos = computeARAPImageWarpStage2(pos, newPos, adj, gs, cells, controlPointsIndices, controlPointDesirePositions, 100000) img = drawPoints(newPos, adj, 1200, 1200) cv2.imshow('', img) cv2.setMouseCallback('', mouseCallback, [pos, adj, controlPointsIndices, controlPointDesirePositions, newPos, selectedInd])
def initScreen(self): if self.toggle % 3 == 1: cv2.imshow('Original', self.nupeimg) try: cv2.destroyWindow("Edge Trace") except: pass try: cv2.destroyWindow("Outline") except: pass elif self.toggle % 3 == 2: cv2.imshow('Edge Trace', self.flingedges) try: cv2.destroyWindow("Original") except: pass try: cv2.destroyWindow("Outline") except: pass elif self.toggle % 3 == 0: cv2.imshow('Outline',self.nuimg) try: cv2.destroyWindow("Edge Trace") except: pass try: cv2.destroyWindow("Original") except: pass cv2.setMouseCallback("Outline",draw)
def show(self, verbose=False): # create window, event callbacks cv2.namedWindow(self.name) cv2.setMouseCallback(self.name, self._onclick) cv2.createTrackbar('Tolerance', self.name, self._tolerance[0], 255, self._onchange) self.verbose = verbose if verbose: print('Click anywhere to select a region of similar colors.') print('Move the slider to include a wider range of colors.\n') print(('Press [m] to switch between displaying the selection, ' 'mask, or applied mask')) print('Press [p] to print color statistics of current selection') print('Press [s] to save the currently displayed image') print('Press [q] or [esc] to close the window') print('------------------------------------------------------------\n') # display the image and wait for a keypress or trackbar change cv2.imshow(self.name, self._image) while(True): k = cv2.waitKey() & 0xFF if k == ord('q') or k == 27: # 27 is [esc] self._close() break elif k == ord('m'): self._flip_displays() elif k == ord('p'): self._print_stats() elif k == ord('s'): self._save()
def mouse_response(self): self.image=self.frame self.clone=self.image self.refPt = [] cv2.namedWindow("Target") cv2.setMouseCallback("Target", self.click_and_crop) while True: self.ret,self.frame = self.cap.read() self.image=self.frame if len(self.refPt) == 2: cv2.rectangle(self.image, self.refPt[0], self.refPt[1], (0, 255, 0), 2) cv2.imshow("Target", self.image) key = cv2.waitKey(1) & 0xFF if key == ord("r"): self.image = self.clone cv2.imshow("Target", self.image) self.refPt=[] cv2.destroyAllWindows() self.image_cropping(self.image) if key == ord("d"): if len(self.refPt) == 2: roi = self.clone[self.refPt[0][1]:self.refPt[1][1], self.refPt[0][0]:self.refPt[1][0]] cv2.imwrite('roi.png',roi) cv2.destroyAllWindows() return self.refPt elif key == ord("q"): cv2.destroyAllWindows() break
def run_on_image(image_filename): ''' open and run on image_filename ''' global IMAGE, IMAGE_HSV, IMAGE_HLS, HEIGHT, WIDTH IMAGE = cv2.imread(image_filename) HEIGHT, WIDTH = IMAGE.shape[:2] # also convert the image to other color spaces IMAGE_HSV = cv2.cvtColor(IMAGE, cv2.COLOR_BGR2HSV) IMAGE_HLS = cv2.cvtColor(IMAGE, cv2.COLOR_BGR2HLS) print("Left-click image regions to evaluate color") print("Press escape to exit") cv2.namedWindow('img') cv2.setMouseCallback('img', on_event) key = None while key != 27: # run until user presses escape cv2.imshow('img', IMAGE) key = cv2.waitKey(0) print("Exiting")
def Urect(img,title): def onmouse(event,x,y,flags,param): global ix,iy,roi,drawing # Draw Rectangle if event == cv2.EVENT_RBUTTONDOWN: drawing = True ix,iy = x,y elif event == cv2.EVENT_MOUSEMOVE: if drawing == True: cv2.rectangle(img,(ix,iy),(x,y),BLUE,-1) rect = (ix,iy,abs(ix-x),abs(iy-y)) elif event == cv2.EVENT_RBUTTONUP: drawing = False cv2.rectangle(img,(ix,iy),(x,y),BLUE,-1) rect = (ix,iy,x,y) roi.extend(rect) cv2.namedWindow(title,cv2.WINDOW_NORMAL) cv2.setMouseCallback(title,onmouse) print ("Right click and hold to draw a single rectangle ROI, beginning at the top left corner of the desired area. A blue box should appear. Hit esc to exit screen. Window can be resized by selecting borders.") while(1): cv2.namedWindow(title,cv2.WINDOW_NORMAL) cv2.imshow(title,img) k = cv2.waitKey(1) & 0xFF if k == 27: break cv2.destroyAllWindows() return(roi)
def userSelectBed(self, img): """ Opens window and prompts user to select BED Input: Filtered image. Output: None """ # Set up window & call back windowName = 'Left click base of bubble. Press Esc to exit.' cv2.namedWindow(windowName,cv2.WINDOW_NORMAL) # Can be resized cv2.resizeWindow(windowName, self.w*3, self.h*3) # Resize window cv2.setMouseCallback(windowName, self.mouseCallback) # Set callback imgDefault = img # Ideally this resets the image later while True: # Plot cursor click if self.bed: h = self.bed else: h = 0 cv2.line(img, (-1000,h), (1000,h), (0,0,0)) cv2.imshow(windowName, img) img = imgDefault k = cv2.waitKey(4) & 0xFF # Basically delay 1000 ms #print 'BED is currently: ', self.bed if k == 27: # Press escape to exit break cv2.destroyAllWindows()
def checkOpt(img): i=0 blankImage1=np.zeros(img.shape,np.uint8) #cv2.line(blankImage1,(0,seed_pt[1]),(img.shape[1],seed_pt[1]),255,4) cv2.line(blankImage1,(0,seed_pt[1]-50),(img.shape[1],(seed_pt[1]-50)),25,1) blankImage1 &=img _,img1 = cap.read() gray = cv2.cvtColor(blankImage1 ,cv2.COLOR_BGR2GRAY) #img = cv2.Canny(gray,10,80,apertureSize = 3) blankImage1 = gray.copy() contours,hierarchy=cv2.findContours(blankImage1,cv2.RETR_LIST,cv2.CHAIN_APPROX_NONE) for cnt in contours: i=i+1 cv2.drawContours(blankImage1,cnt,-1,(255,255,255),2) cv2.imshow("Obstacle Line",blankImage1) cv2.setMouseCallback("Obstacle Line",onmouse) cv2.waitKey(0) #cv2.waitKey(0) if i >1 : return 1 return 0
def explore_match(win, img1, img2, kp_pairs, status = None, H = None): h1, w1 = img1.shape[:2] h2, w2 = img2.shape[:2] vis = np.zeros((max(h1, h2), w1+w2), np.uint8) vis[:h1, :w1] = img1 vis[:h2, w1:w1+w2] = img2 vis = cv2.cvtColor(vis, cv2.COLOR_GRAY2BGR) if H is not None: corners = np.float32([[0, 0], [w1, 0], [w1, h1], [0, h1]]) corners = np.int32( cv2.perspectiveTransform(corners.reshape(1, -1, 2), H).reshape(-1, 2) + (w1, 0) ) cv2.polylines(vis, [corners], True, (255, 255, 255)) if status is None: status = np.ones(len(kp_pairs), np.bool_) p1 = np.int32([kpp[0].pt for kpp in kp_pairs]) p2 = np.int32([kpp[1].pt for kpp in kp_pairs]) + (w1, 0) green = (0, 255, 0) red = (0, 0, 255) white = (255, 255, 255) kp_color = (51, 103, 236) for (x1, y1), (x2, y2), inlier in zip(p1, p2, status): if inlier: col = green cv2.circle(vis, (x1, y1), 2, col, -1) cv2.circle(vis, (x2, y2), 2, col, -1) else: col = red r = 2 thickness = 3 cv2.line(vis, (x1-r, y1-r), (x1+r, y1+r), col, thickness) cv2.line(vis, (x1-r, y1+r), (x1+r, y1-r), col, thickness) cv2.line(vis, (x2-r, y2-r), (x2+r, y2+r), col, thickness) cv2.line(vis, (x2-r, y2+r), (x2+r, y2-r), col, thickness) vis0 = vis.copy() for (x1, y1), (x2, y2), inlier in zip(p1, p2, status): if inlier: cv2.line(vis, (x1, y1), (x2, y2), green) cv2.imshow(win, vis) def onmouse(event, x, y, flags, param): cur_vis = vis if flags & cv2.EVENT_FLAG_LBUTTON: cur_vis = vis0.copy() r = 8 m = (anorm(p1 - (x, y)) < r) | (anorm(p2 - (x, y)) < r) idxs = np.where(m)[0] kp1s, kp2s = [], [] for i in idxs: (x1, y1), (x2, y2) = p1[i], p2[i] col = (red, green)[status[i]] cv2.line(cur_vis, (x1, y1), (x2, y2), col) kp1, kp2 = kp_pairs[i] kp1s.append(kp1) kp2s.append(kp2) cur_vis = cv2.drawKeypoints(cur_vis, kp1s, flags=4, color=kp_color) cur_vis[:,w1:] = cv2.drawKeypoints(cur_vis[:,w1:], kp2s, flags=4, color=kp_color) cv2.imshow(win, cur_vis) cv2.setMouseCallback(win, onmouse) return vis
while (cap.isOpened() and video_flag): (grabbed, frame) = cap.read() # print('frame', type(frame)) new_frame = mx.nd.array(cv2.cvtColor( frame, cv2.COLOR_BGR2RGB)).astype('uint8') x, orig_img = data.transforms.presets.yolo.transform_test( new_frame, short=512, max_size=2000) frameI = orig_img # print('frameI', type(frameI)) cv2.namedWindow("frame", cv2.WINDOW_NORMAL) # cv2.namedWindow('frame') cv2.setMouseCallback('frame', on_mouse) #drawing rectangle if startPoint == True and endPoint == True: cv2.rectangle(frameI, (rect[0], rect[1]), (rect[2], rect[3]), (255, 0, 255), 2) # cv2.rectangle(frame, (rect[0], rect[1]), (rect[2], rect[3]), (255, 0, 255), 2) print(' 正方形位置:', (rect[0], rect[1]), (rect[2], rect[3])) # cv2.imshow('frame',frame) cv2.imshow('frame', frameI[..., ::-1]) key = cv2.waitKey(waitTime) # escape 键 if key == 27: # break print('setting ', video_url, ' square:', (rect[0], rect[1]),
# get coordinates of mouse cursor after clicking def get_coords(event, x, y, flags, param): # grab references to the global variables global refPt, img, st_model if event == cv2.EVENT_LBUTTONDOWN: refPt.append([x, y]) cv2.rectangle(copy_img, (x - 2, y - 2), (x + 2, y + 2), 255, 3) # lighting next point st_model = st_model.replace(" ", "*", 1) print(st_model) copy_img = np.copy(img) cv2.namedWindow("img", cv2.WINDOW_NORMAL) cv2.setMouseCallback("img", get_coords) # beginning process of choosing points on img # lighting first point st_model = st_model.replace(" ", "*", 1) print(st_model) while True: cv2.imshow("img", copy_img) if cv2.waitKey(1) & 0xFF == ord('q'): break if len(refPt) != 24: raise BaseException("Wrong number of points(should be 24)") refPt = np.asarray(refPt, dtype=np.float32) refPt = np.expand_dims(refPt, axis=1) print(refPt)
def main(): global target_x global target_y global target_angle global turning_radius # ROS Publisher Init pub = rospy.Publisher('watch_tower_control', watch_tower_control, queue_size=1) rospy.init_node('custom_talker', anonymous=True) control_signal = watch_tower_control() DIM = (800, 600) # USB Camera Parameters K = np.array([[646.986342788419, 0.0, 383.9135552285603], [0.0, 647.4588452248628, 315.82293891405163], [0.0, 0.0, 1.0]]) D = np.array([[0.40302428743352114], [0.10116712172043976], [0.6206370706341585], [-4.18627051202362]]) camera_params = (646.986342788419, 647.4588452248628, 383.9135552285603, 315.82293891405163) video_capture = cv2.VideoCapture(0) video_capture.set(cv2.CAP_PROP_FRAME_HEIGHT, 600) video_capture.set(cv2.CAP_PROP_FRAME_WIDTH, 800) map1, map2 = cv2.fisheye.initUndistortRectifyMap(K, D, np.eye(3), K, DIM, cv2.CV_16SC2) detector = apriltag.Detector(searchpath=apriltag._get_demo_searchpath()) tag_size = 160 target_x = 300 target_y = 200 target_angle = 0 turning_radius = 30 print("target point is " + "(" + str(target_x) + "," + str(target_y) + ")") print("traget angle is " + str(target_angle)) print("turning radius is " + str(turning_radius)) cv2.namedWindow('image') cv2.setMouseCallback('image', set_target) while (True): _, frame = video_capture.read() gray_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) gray_frame = cv2.remap(gray_frame, map1, map2, interpolation=cv2.INTER_LINEAR, borderMode=cv2.BORDER_CONSTANT) result = detector.detect(gray_frame) target_right = ( np.int(target_x + math.cos(math.radians(target_angle)) * turning_radius), np.int(target_y + math.sin(math.radians(target_angle)) * turning_radius)) target_left = ( np.int(target_x - math.cos(math.radians(target_angle)) * turning_radius), np.int(target_y - math.sin(math.radians(target_angle)) * turning_radius)) cv2.circle(gray_frame, target_right, turning_radius, (255, 255, 255), -1) cv2.circle(gray_frame, target_left, turning_radius, (0, 0, 0), -1) if (result): start_point = result[0].center.astype(int) start_angle = math.degrees( math.atan2(result[0].corners[1][1] - result[0].corners[0][1], result[0].corners[1][0] - result[0].corners[0][0])) curr_right = ( np.int(start_point[0] + math.cos(math.radians(start_angle)) * turning_radius), np.int(start_point[1] + math.sin(math.radians(start_angle)) * turning_radius)) curr_left = ( np.int(start_point[0] - math.cos(math.radians(start_angle)) * turning_radius), np.int(start_point[1] - math.sin(math.radians(start_angle)) * turning_radius)) cv2.circle(gray_frame, curr_right, turning_radius, (255, 255, 255), -1) cv2.circle(gray_frame, curr_left, turning_radius, (0, 0, 0), -1) dist = np.array([ np.linalg.norm(np.array(target_right) - np.array(curr_right)), np.linalg.norm(np.array(target_right) - np.array(curr_left)), np.linalg.norm(np.array(target_left) - np.array(curr_right)), np.linalg.norm(np.array(target_left) - np.array(curr_left)) ]) dist[dist <= 2 * turning_radius] = 100000 index = np.argmin(dist) if np.linalg.norm( np.array([target_x, target_y]) - np.array(start_point)) < 10: control_signal.control = [0, speed_center] else: control_signal.control = [0, max_speed] time.sleep(0.01) if index == 0: dest_angle = math.degrees( math.atan2(target_right[0] - curr_right[0], target_right[1] - curr_right[1])) dest_angle = ( 180 - dest_angle) if dest_angle > 0 else -180 - dest_angle pt1 = (np.int(curr_right[0] - math.cos(math.radians(dest_angle)) * turning_radius), np.int(curr_right[1] - math.sin(math.radians(dest_angle)) * turning_radius)) pt2 = (np.int(target_right[0] - math.cos(math.radians(dest_angle)) * turning_radius), np.int(target_right[1] - math.sin(math.radians(dest_angle)) * turning_radius)) temp_angle = -math.degrees( math.atan2(pt1[0] - pt2[0], pt1[1] - pt2[1])) cv2.line(gray_frame, pt1, pt2, (0, 0, 0), 5) cv2.line(gray_frame, target_right, curr_right, (0, 0, 0), 5) if (abs(start_angle - temp_angle) < 10): control_signal.control[0] = center #pwm.set_pwm(1, 0, center) else: control_signal.control[0] = left_max #pwm.set_pwm(1, 0, left_max) time.sleep(0.01) elif index == 1: dest_angle = math.degrees( math.atan2(target_right[0] - curr_left[0], target_right[1] - curr_left[1])) dest_angle = ( 180 - dest_angle) if dest_angle > 0 else -180 - dest_angle dest_angle = dest_angle + math.degrees( math.acos(turning_radius / (dist[1] / 2))) + 90 pt1 = (np.int(curr_left[0] - math.cos(math.radians(dest_angle)) * turning_radius), np.int(curr_left[1] - math.sin(math.radians(dest_angle)) * turning_radius)) pt2 = (np.int(target_right[0] + math.cos(math.radians(dest_angle)) * turning_radius), np.int(target_right[1] + math.sin(math.radians(dest_angle)) * turning_radius)) temp_angle = -math.degrees( math.atan2(pt1[0] - pt2[0], pt1[1] - pt2[1])) cv2.line(gray_frame, pt1, pt2, (255, 255, 255), 5) cv2.line(gray_frame, target_right, curr_left, (0, 0, 0), 5) if (abs(start_angle - temp_angle) < 10): control_signal.control[0] = center #pwm.set_pwm(1, 0, center) else: control_signal.control[0] = right_max #pwm.set_pwm(1, 0, right_max) time.sleep(0.01) #print(start_angle,temp_angle) elif index == 2: dest_angle = math.degrees( math.atan2(target_left[0] - curr_right[0], target_left[1] - curr_right[1])) dest_angle = ( 180 - dest_angle) if dest_angle > 0 else -180 - dest_angle dest_angle = dest_angle - math.degrees( math.acos(turning_radius / (dist[2] / 2))) + 90 pt1 = (np.int(curr_right[0] - math.cos(math.radians(dest_angle)) * turning_radius), np.int(curr_right[1] - math.sin(math.radians(dest_angle)) * turning_radius)) pt2 = (np.int(target_left[0] + math.cos(math.radians(dest_angle)) * turning_radius), np.int(target_left[1] + math.sin(math.radians(dest_angle)) * turning_radius)) temp_angle = -math.degrees( math.atan2(pt1[0] - pt2[0], pt1[1] - pt2[1])) cv2.line(gray_frame, pt1, pt2, (255, 255, 255), 5) cv2.line(gray_frame, target_left, curr_right, (0, 0, 0), 5) if (abs(start_angle - temp_angle) < 10): control_signal.control[0] = center #pwm.set_pwm(1, 0, center) else: control_signal.control[0] = left_max #pwm.set_pwm(1, 0, left_max) time.sleep(0.01) #print(start_angle,temp_angle) else: dest_angle = math.degrees( math.atan2(target_left[0] - curr_left[0], target_left[1] - curr_left[1])) dest_angle = ( 180 - dest_angle) if dest_angle > 0 else -180 - dest_angle pt1 = (np.int(curr_left[0] + math.cos(math.radians(dest_angle)) * turning_radius), np.int(curr_left[1] + math.sin(math.radians(dest_angle)) * turning_radius)) pt2 = (np.int(target_left[0] + math.cos(math.radians(dest_angle)) * turning_radius), np.int(target_left[1] + math.sin(math.radians(dest_angle)) * turning_radius)) temp_angle = -math.degrees( math.atan2(pt1[0] - pt2[0], pt1[1] - pt2[1])) cv2.line(gray_frame, pt1, pt2, (0, 0, 0), 5) cv2.line(gray_frame, target_left, curr_left, (0, 0, 0), 5) if (abs(start_angle - temp_angle) < 10): control_signal.control[0] = center #pwm.set_pwm(1, 0, center) else: control_signal.control[0] = right_max #pwm.set_pwm(1, 0, right_max) time.sleep(0.01) #print(start_angle,temp_angle) else: control_signal.control = [center, 400] #pwm.set_pwm(2, 0, 400) pub.publish(control_signal) cv2.imshow("image", gray_frame) key = cv2.waitKey(1) & 0xFF if key == ord('q'): break elif key == ord('w'): if turning_radius < 100: turning_radius += 1 elif key == ord('s'): if turning_radius > 10: turning_radius -= 1 elif key == ord('a'): target_angle -= 5 if target_angle <= -180: target_angle = 180 elif key == ord('d'): target_angle += 5 if target_angle >= 180: target_angle = -180 video_capture.release() cv2.destroyAllWindows() control_signal.control = [center, 400] pub.publish(control_signal)
def __setup_window__(self): ### Set up window if it does not exist if not self._window_set_up: cv2.namedWindow(self.WINDOWS_CONTROLS) cv2.setMouseCallback(self.WINDOWS_CONTROLS, self.__mouse__) # Create trackbars if self.patches.contains_locations: cv2.createTrackbar("show_grid", self.WINDOWS_CONTROLS, int(self.show_grid), 1, self.__draw__) cv2.createTrackbar("show_map", self.WINDOWS_CONTROLS, int(self.show_map), 1, self.__draw__) if self.patch_to_text_func is not None or self.patches.contains_mahalanobis_distances: cv2.createTrackbar("show_values", self.WINDOWS_CONTROLS, int(self.show_values), 1, self.__draw__) if self.patch_to_text_func[self.model_index] is not None or \ self.patch_to_color_func[self.model_index] is not None or \ self.patches.contains_locations or self.patches.contains_mahalanobis_distances: cv2.createTrackbar("overlay", self.WINDOWS_CONTROLS, 40, 100, self.__draw__) if self.patches.contains_mahalanobis_distances: cv2.namedWindow(self.WINDOWS_MAHA) for n in sorted(self.patches.mahalanobis_distances.dtype.names): self.patch_to_color_func.append(self.__default_patch_to_color__) self.patch_to_text_func.append(self.__default_patch_to_text__) self.pause_func.append(None) cv2.createTrackbar("threshold", self.WINDOWS_MAHA, 5000, 10000, lambda x: self.__maha__(only_refresh_image=True)) cv2.createTrackbar("show_thresh", self.WINDOWS_MAHA, 1, 1, self.__draw__) cv2.createTrackbar("0_gaussian_0", self.WINDOWS_MAHA, 0, 10, self.__maha__) cv2.createTrackbar("0_gaussian_1", self.WINDOWS_MAHA, 0, 10, self.__maha__) cv2.createTrackbar("0_gaussian_2", self.WINDOWS_MAHA, 0, 10, self.__maha__) cv2.createTrackbar("1_erosion_dilation", self.WINDOWS_MAHA, 0, 2, self.__maha__) cv2.createTrackbar("1_erosion_dilation_structure_rank", self.WINDOWS_MAHA, 2, 3, self.__maha__) cv2.setTrackbarMin("1_erosion_dilation_structure_rank", self.WINDOWS_MAHA, 2) cv2.createTrackbar("1_erosion_dilation_structure_connectivity", self.WINDOWS_MAHA, 1, 3, self.__maha__) cv2.setTrackbarMin("1_erosion_dilation_structure_connectivity", self.WINDOWS_MAHA, 1) cv2.createTrackbar("2_gaussian_0", self.WINDOWS_MAHA, 0, 10, self.__maha__) cv2.createTrackbar("2_gaussian_1", self.WINDOWS_MAHA, 0, 10, self.__maha__) cv2.createTrackbar("2_gaussian_2", self.WINDOWS_MAHA, 0, 10, self.__maha__) cv2.createTrackbar("metric", self.WINDOWS_MAHA, 0, len(PatchArray.METRICS) - 1, lambda x: self.__maha__(only_refresh_image=True)) cv2.createTrackbar("model", self.WINDOWS_MAHA, 0, len(self.patches.mahalanobis_distances.dtype.names), self.__model__) cv2.createTrackbar("optical_flow", self.WINDOWS_CONTROLS, 0, 1, self.__draw__) cv2.createTrackbar("label", self.WINDOWS_CONTROLS, -1, 2, self.__change_frames__) cv2.setTrackbarMin("label", self.WINDOWS_CONTROLS, -1) cv2.setTrackbarPos("label", self.WINDOWS_CONTROLS, -1) cv2.createTrackbar("stop_label", self.WINDOWS_CONTROLS, -1, 2, self.__change_frames__) cv2.setTrackbarMin("stop_label", self.WINDOWS_CONTROLS, -1) cv2.setTrackbarPos("stop_label", self.WINDOWS_CONTROLS, -1) cv2.createTrackbar("direction", self.WINDOWS_CONTROLS, -1, 2, self.__change_frames__) cv2.setTrackbarMin("direction", self.WINDOWS_CONTROLS, -1) cv2.setTrackbarPos("direction", self.WINDOWS_CONTROLS, -1) cv2.createTrackbar("round_number", self.WINDOWS_CONTROLS, -1, 30, self.__change_frames__) cv2.setTrackbarMin("round_number", self.WINDOWS_CONTROLS, -1) cv2.setTrackbarPos("round_number", self.WINDOWS_CONTROLS, -1) cv2.createTrackbar("error", self.WINDOWS_CONTROLS, 0, 2, self.__change_frames__) cv2.createTrackbar("delay", self.WINDOWS_CONTROLS, 1, 1000, self.__draw__) cv2.createTrackbar("skip", self.WINDOWS_CONTROLS, 1, 1000, lambda x: None) cv2.createTrackbar("index", self.WINDOWS_CONTROLS, 0, self.patches.shape[0] - 1, self.__index_update__) cv2.namedWindow(self.WINDOWS_IMAGE) cv2.setMouseCallback(self.WINDOWS_IMAGE, self.__mouse_image__) self._window_set_up = True
def main(): global IntParam, ExtParam, TranParam, DistParam if(str(sys.argv[1])) == '-r1': video = cv2.VideoCapture(0) flag, frame = video.read() if (not (flag)): exit("Error while reading the video, try again.") cv2.namedWindow('Webcam') cv2.setMouseCallback('Webcam', Draw_line) while(True): flag, frame = video.read() if (not (flag)): exit("Error while reading the video, try again.") cv2.imshow('Webcam', frame) if(nova_imagem): resultado = cv2.line(frame, pixel_inicial, pixel_final, (0,255,0), 2, 8) cv2.namedWindow('Resultado') cv2.imshow('Resultado', resultado) if cv2.waitKey(0) == 27: cv2.destroyAllWindows() break video.release() if (str(sys.argv[1]) == '-r2'): WebCam = cv2.VideoCapture(0) PI_11 = np.empty(0) PI_13 = np.empty(0) PI_22 = np.empty(0) PI_23 = np.empty(0) PD_1 = np.empty(0) PD_2 = np.empty(0) PD_3 = np.empty(0) PD_4 = np.empty(0) PD_5 = np.empty(0) # contador para o numero de imagens onde o xadrez foi detectado detected_images = 0 #numero de imagens que queremos detectar o tabuleiro de xadrez para #calcular os parametros intrinsecos da camera max_images = 2 iteracoes = 2 #Numero de bordas (com 4 quadrados) na vertical e na horizontal do tabuleiro board_w = 8 board_h = 6 #tamanho em mm do quadrado tam_quad = 29 #determina o tempo (s) de espera para mudar o tabuleiro de posicao apos uma deteccao time_step = 2 for i in range(iteracoes): mtx, dist, R, T = calibration(WebCam, tam_quad, board_h, board_w, time_step, max_images) PI_11 = np.append(PI_11, mtx[0][0]) PI_13 = np.append(PI_13, mtx[0][2]) PI_22 = np.append(PI_22, mtx[1][1]) PI_23 = np.append(PI_23, mtx[1][2]) PD_1 = np.append(PD_1, dist[0][0]) PD_2 = np.append(PD_2, dist[0][1]) PD_3 = np.append(PD_3, dist[0][2]) PD_4 = np.append(PD_4, dist[0][3]) PD_5 = np.append(PD_5, dist[0][4]) IntParam[0][0] = PI_11.mean() IntParam[0][2] = PI_13.mean() IntParam[1][1] = PI_22.mean() IntParam[1][2] = PI_23.mean() DistParam[0] = PD_1.mean() DistParam[1] = PD_2.mean() DistParam[2] = PD_3.mean() DistParam[3] = PD_4.mean() DistParam[4] = PD_5.mean() strvalue = "" for i in range (3): for j in range (3): strvalue += (str(IntParam[i][j])) strvalue += ("\n") strvalue += ("\n") for i in range(5): strvalue += str(DistParam[i]) strvalue += ("\n") file = open("parametros.txt", "w") file.writelines(strvalue) correct_distortion(WebCam, IntParam, DistParam, ExtParam, 2) print('Media dos parametros intrisecos e distorcao:') print("Intrinsecos:") print(IntParam) print("\nDistorcao: ") print(DistParam) IntParam[0][0] = PI_11.std(ddof=1) IntParam[0][2] = PI_13.std(ddof=1) IntParam[1][1] = PI_22.std(ddof=1) IntParam[1][2] = PI_23.std(ddof=1) print('Desvio Padrao :') print("Intrinsecos:") print(IntParam) print("Distorcao: ") print('[', PD_1.std(ddof=1), end = ' ') print(PD_2.std(ddof=1), end = ' ') print(PD_3.std(ddof=1), end = ' ') print(PD_4.std(ddof=1), end = ' ') print(PD_5.std(ddof=1), end = ']\n') cv2.destroyAllWindows() if (str(sys.argv[1]) == '-r3'): WebCam = cv2.VideoCapture(0) PI_11 = np.empty(0) PI_13 = np.empty(0) PI_22 = np.empty(0) PI_23 = np.empty(0) PD_1 = np.empty(0) PD_2 = np.empty(0) PD_3 = np.empty(0) PD_4 = np.empty(0) PD_5 = np.empty(0) R11 = np.empty(0) R12 = np.empty(0) R13 = np.empty(0) R21 = np.empty(0) R22 = np.empty(0) R23 = np.empty(0) R31 = np.empty(0) R32 = np.empty(0) R33 = np.empty(0) T1 = np.empty(0) T2 = np.empty(0) T3 = np.empty(0) #numero de imagens que queremos detectar o tabuleiro de xadrez para #calcular os parametros intrinsecos da camera max_images = 3 #Numero de bordas (com 4 quadrados) na vertical e na horizontal do tabuleiro board_w = 8 board_h = 6 #tamanho em mm do quadrado tam_quad = 29 #determina o tempo (s) de espera para mudar o tabuleiro de posicao apos uma deteccao time_step = 2 iteracoes = 3 for i in range(iteracoes): mtx, dist, R, T = calibration(WebCam, tam_quad, board_h, board_w, time_step, max_images) print("Iteracao ", i+1, " feita", end='') if(i != (iteracoes - 1)): print(", preparando proxima iteracao\n") start_time = time.time() rotation_matrix = np.zeros(shape=(3,3)) cv2.Rodrigues(R[0], rotation_matrix) R11 = np.append(R11, rotation_matrix[0][0]) R12 = np.append(R12, rotation_matrix[0][1]) R13 = np.append(R13, rotation_matrix[0][2]) R21 = np.append(R21, rotation_matrix[1][0]) R22 = np.append(R22, rotation_matrix[1][1]) R23 = np.append(R23, rotation_matrix[1][2]) R31 = np.append(R31, rotation_matrix[2][0]) R32 = np.append(R32, rotation_matrix[2][1]) R33 = np.append(R33, rotation_matrix[2][2]) PI_11 = np.append(PI_11, mtx[0][0]) PI_13 = np.append(PI_13, mtx[0][2]) PI_22 = np.append(PI_22, mtx[1][1]) PI_23 = np.append(PI_23, mtx[1][2]) PD_1 = np.append(PD_1, dist[0][0]) PD_2 = np.append(PD_2, dist[0][1]) PD_3 = np.append(PD_3, dist[0][2]) PD_4 = np.append(PD_4, dist[0][3]) PD_5 = np.append(PD_5, dist[0][4]) T1 = np.append(T1, T[0][0]) T2 = np.append(T2, T[0][1]) T3 = np.append(T3, T[0][2]) if(i != (iteracoes - 1)): while(time.time() - start_time < 2): pass print('\n') TranParam = T1.mean(), T2.mean(), T3.mean() ExtParam[0][0] = R11.mean() ExtParam[0][1] = R12.mean() ExtParam[0][2] = R13.mean() ExtParam[0][3] = TranParam[0] ExtParam[1][0] = R21.mean() ExtParam[1][1] = R22.mean() ExtParam[1][2] = R23.mean() ExtParam[1][3] = TranParam[1] ExtParam[2][0] = R31.mean() ExtParam[2][1] = R32.mean() ExtParam[2][2] = R33.mean() ExtParam[2][3] = TranParam[2] IntParam[0][0] = PI_11.mean() IntParam[0][2] = PI_13.mean() IntParam[1][1] = PI_22.mean() IntParam[1][2] = PI_23.mean() DistParam = PD_1.mean(), PD_2.mean(), PD_3.mean(), PD_4.mean(), PD_5.mean() DesvPadExt[0][0] = R11.std(ddof=1) DesvPadExt[0][1] = R12.std(ddof=1) DesvPadExt[0][2] = R13.std(ddof=1) DesvPadExt[0][3] = T1.std(ddof=1) DesvPadExt[1][0] = R21.std(ddof=1) DesvPadExt[1][1] = R22.std(ddof=1) DesvPadExt[1][2] = R23.std(ddof=1) DesvPadExt[1][3] = T2.std(ddof=1) DesvPadExt[2][0] = R31.std(ddof=1) DesvPadExt[2][1] = R32.std(ddof=1) DesvPadExt[2][2] = R33.std(ddof=1) DesvPadExt[2][3] = T3.std(ddof=1) DesvPadInt[0][0] = PI_11.std(ddof=1) DesvPadInt[0][2] = PI_13.std(ddof=1) DesvPadInt[1][1] = PI_22.std(ddof=1) DesvPadInt[1][2] = PI_23.std(ddof=1) DesvPadDist = PD_1.std(ddof=1), PD_2.std(ddof=1), PD_3.std(ddof=1), PD_4.std(ddof=1), PD_5.std(ddof=1) print("Matriz Intrinsecos: ") print("Media") print(IntParam) print("Desvio Padrao") print(DesvPadInt) print() print("Matriz Extrinsecos: ") print("Media") print(ExtParam) print("Desvio Padrao") print(DesvPadExt) print() print("Distorcao: ") print("Media") print(DistParam) print("Desvio Padrao") print(DesvPadDist) PI_11 = np.empty(0) PI_13 = np.empty(0) PI_22 = np.empty(0) PI_23 = np.empty(0) PD_1 = np.empty(0) PD_2 = np.empty(0) PD_3 = np.empty(0) PD_4 = np.empty(0) PD_5 = np.empty(0) R11 = np.empty(0) R12 = np.empty(0) R13 = np.empty(0) R21 = np.empty(0) R22 = np.empty(0) R23 = np.empty(0) R31 = np.empty(0) R32 = np.empty(0) R33 = np.empty(0) T1 = np.empty(0) T2 = np.empty(0) T3 = np.empty(0) cv2.destroyAllWindows() if (str(sys.argv[1]) == '-r4'): WebCam = cv2.VideoCapture(0) PI_11 = np.empty(0) PI_13 = np.empty(0) PI_22 = np.empty(0) PI_23 = np.empty(0) PD_1 = np.empty(0) PD_2 = np.empty(0) PD_3 = np.empty(0) PD_4 = np.empty(0) PD_5 = np.empty(0) R11 = np.empty(0) R12 = np.empty(0) R13 = np.empty(0) R21 = np.empty(0) R22 = np.empty(0) R23 = np.empty(0) R31 = np.empty(0) R32 = np.empty(0) R33 = np.empty(0) T1 = np.empty(0) T2 = np.empty(0) T3 = np.empty(0) #numero de imagens que queremos detectar o tabuleiro de xadrez para #calcular os parametros intrinsecos da camera max_images = 2 iteracoes = 2 #Numero de bordas (com 4 quadrados) na vertical e na horizontal do tabuleiro board_w = 8 board_h = 6 #tamanho em mm do quadrado tam_quad = 29 #determina o tempo (s) de espera para mudar o tabuleiro de posicao apos uma deteccao time_step = 2 for i in range(iteracoes): mtx, dist, R, T = calibration(WebCam, tam_quad, board_h, board_w, time_step, max_images) print("Iteracao ", i+1, " feita", end='') if(i != (iteracoes - 1)): print(", preparando proxima iteracao\n") start_time = time.time() rotation_matrix = np.zeros(shape=(3,3)) cv2.Rodrigues(R[0], rotation_matrix) R11 = np.append(R11, rotation_matrix[0][0]) R12 = np.append(R12, rotation_matrix[0][1]) R13 = np.append(R13, rotation_matrix[0][2]) R21 = np.append(R21, rotation_matrix[1][0]) R22 = np.append(R22, rotation_matrix[1][1]) R23 = np.append(R23, rotation_matrix[1][2]) R31 = np.append(R31, rotation_matrix[2][0]) R32 = np.append(R32, rotation_matrix[2][1]) R33 = np.append(R33, rotation_matrix[2][2]) PI_11 = np.append(PI_11, mtx[0][0]) PI_13 = np.append(PI_13, mtx[0][2]) PI_22 = np.append(PI_22, mtx[1][1]) PI_23 = np.append(PI_23, mtx[1][2]) PD_1 = np.append(PD_1, dist[0][0]) PD_2 = np.append(PD_2, dist[0][1]) PD_3 = np.append(PD_3, dist[0][2]) PD_4 = np.append(PD_4, dist[0][3]) PD_5 = np.append(PD_5, dist[0][4]) T1 = np.append(T1, T[0][0]) T2 = np.append(T2, T[0][1]) T3 = np.append(T3, T[0][2]) if(i != (iteracoes - 1)): while(time.time() - start_time < 2): pass TranParam = T1.mean(), T2.mean(), T3.mean() ExtParam[0][0] = R11.mean() ExtParam[0][1] = R12.mean() ExtParam[0][2] = R13.mean() ExtParam[0][3] = TranParam[0] ExtParam[1][0] = R21.mean() ExtParam[1][1] = R22.mean() ExtParam[1][2] = R23.mean() ExtParam[1][3] = TranParam[1] ExtParam[2][0] = R31.mean() ExtParam[2][1] = R32.mean() ExtParam[2][2] = R33.mean() ExtParam[2][3] = TranParam[2] IntParam[0][0] = PI_11.mean() IntParam[0][2] = PI_13.mean() IntParam[1][1] = PI_22.mean() IntParam[1][2] = PI_23.mean() DistParam = PD_1.mean(), PD_2.mean(), PD_3.mean(), PD_4.mean(), PD_5.mean() DesvPadExt[0][0] = R11.std(ddof=1) DesvPadExt[0][1] = R12.std(ddof=1) DesvPadExt[0][2] = R13.std(ddof=1) DesvPadExt[0][3] = T1.std(ddof=1) DesvPadExt[1][0] = R21.std(ddof=1) DesvPadExt[1][1] = R22.std(ddof=1) DesvPadExt[1][2] = R23.std(ddof=1) DesvPadExt[1][3] = T2.std(ddof=1) DesvPadExt[2][0] = R31.std(ddof=1) DesvPadExt[2][1] = R32.std(ddof=1) DesvPadExt[2][2] = R33.std(ddof=1) DesvPadExt[2][3] = T3.std(ddof=1) DesvPadInt[0][0] = PI_11.std(ddof=1) DesvPadInt[0][2] = PI_13.std(ddof=1) DesvPadInt[1][1] = PI_22.std(ddof=1) DesvPadInt[1][2] = PI_23.std(ddof=1) DesvPadDist = PD_1.std(ddof=1), PD_2.std(ddof=1), PD_3.std(ddof=1), PD_4.std(ddof=1), PD_5.std(ddof=1) print("Matriz Intrinsecos: ") print("Media") print(IntParam) print("Desvio Padrao") print(DesvPadInt) print() print("Matriz Extrinsecos: ") print("Media") print(ExtParam) print("Desvio Padrao") print(DesvPadExt) print() print("Distorcao: ") print("Media") print(DistParam) print("Desvio Padrao") print(DesvPadDist) PI_11 = np.empty(0) PI_13 = np.empty(0) PI_22 = np.empty(0) PI_23 = np.empty(0) PD_1 = np.empty(0) PD_2 = np.empty(0) PD_3 = np.empty(0) PD_4 = np.empty(0) PD_5 = np.empty(0) R11 = np.empty(0) R12 = np.empty(0) R13 = np.empty(0) R21 = np.empty(0) R22 = np.empty(0) R23 = np.empty(0) R31 = np.empty(0) R32 = np.empty(0) R33 = np.empty(0) T1 = np.empty(0) T2 = np.empty(0) T3 = np.empty(0) print("Clique em 2 pontos para medir a distancia") correct_distortion(WebCam, IntParam, DistParam, ExtParam, 4)
camera.exposure_mode = 'off' kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (3, 3)) #mqtt and restart the counters send_message() # Camera warmup time.sleep(0.1) # Capture frames from the camera for frame in camera.capture_continuous(rawCapture, format="bgr", use_video_port=True): # Grab the raw NumPy array representing the image image = frame.array image = imutils.resize(image, width=400, height=400) #get the point cv2.setMouseCallback('Frame',obtener_punto) #draw entry and exit areas draw(image) #image processing blobs = cv2.GaussianBlur(image, (5,5), 0) # Blur (noise reduction) blobs = cv2.cvtColor(blobs, cv2.COLOR_BGR2GRAY) # Convert to grayscale fgmask = fgbg.apply(blobs) fgmask = cv2.morphologyEx(fgmask, cv2.MORPH_CLOSE, kernel) fgmask = cv2.morphologyEx(fgmask, cv2.MORPH_OPEN, kernel) fgmask = cv2.dilate(fgmask,None,iterations=2) img, contours,hierarchy = cv2.findContours(fgmask.copy(), cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE) for cnt in contours:
def label_imgs_1by1(args): # this function reads the recorded video back then label the frames one by one, # we manually gives a label to each frame by clicking the central point of the lane # in every driving case (fast moving, straight, curve, oscillate) # in dataset this has to be determined by human video_in_path = str(args.input_file) LabelOutFileName = str(args.output_file) labelf = open(LabelOutFileName, "a+") # 0-based index of the frame to be decoded/captured next img0idx_prop = cv2.CAP_PROP_POS_FRAMES numFrame_prop = cv2.CAP_PROP_FRAME_COUNT cap = cv2.VideoCapture(video_in_path) curr_idx = cap.get(img0idx_prop) numFrame = cap.get(numFrame_prop) numLabeled = 0 print("[INFO] {} frames, we'll start from index {}".format( numFrame, curr_idx)) pbar = tqdm(total=numFrame) param = [] # check # frames that are already saved in the output folder. filelist = os.listdir(args.saved_img_path) existing_img_list = [s for s in filelist if '_flip.jpg' in s] img_file_id_offset = len(existing_img_list) + 1 # start grabbing frames to see if user want to label it & save to image file. while curr_idx < numFrame: while True: flag, frame = cap.read() if flag: break else: # the next frame isn't ready, we try to read it again later cap.set(img0idx_prop, curr_idx - 1) cv2.waitKey(1000) # wait 1000 ms then read again continue try: cropped = frame.copy() height, width, channels = frame.shape # supposed to be 320 x 240 in our USB webcam #### print ("[DBG] curr_idx : "+ str(curr_idx)) #### if (curr_idx < 3): #### print ("[DBG] (width, height) = ("+ str(width) +", "+ str(height) +") ") cropped = cropped[height / 2:height, :] cropped = cv2.resize(cropped, (width / 2, height / 4)) cv2.imshow("current image", cropped) # setMouseCallback () must be set after imshow () # if you want your mouse event detector work cleanupLabelBuf() cv2.setMouseCallback("current image", evtAddNewPointOnImage, param) key = cv2.waitKey(0) & 0xff # check if you label exactly 1 point for each frame, after key event is caught if (len(labelBuffer) == (2 * 1)): addNewLabelExmaple(args.saved_img_path, img_file_id_offset + numLabeled, labelf, labelBuffer, cropped) numLabeled += 1 if key in EXIT_KEYS: break #### elif key in [RIGHT_KEY, ENTER_KEY]: else: curr_idx += 1 except cv2.error as e: print("[ERROR] caught exception when processing image " + str(numLabeled) + ". \r\n") pbar.update(1) labelf.close() pbar.close() cap.release() cv2.destroyAllWindows() print("[INFO] total number of images labeled : " + str(numLabeled))
pts = np.array(pts, np.int) min_hsv = [1.0e6, 1.0e6, 1.0e6] max_hsv = [-1, -1, -1] for ((x, y, z), val) in np.ndenumerate(hsv_img): if cv2.pointPolygonTest(pts, (y, x), False) <= 0: continue min_hsv[z] = min_hsv[z] if min_hsv[z] < val else val max_hsv[z] = max_hsv[z] if max_hsv[z] > val else val print("min_hsv:{}, max_hsv:{}".format(min_hsv, max_hsv)) def on_mouse_cb(event, x, y, flags, userdata): global img, original_img, ply_pts if event == cv2.EVENT_LBUTTONDOWN: ply_pts.append((x, y)) dump_info(original_img, ply_pts) elif event == cv2.EVENT_RBUTTONDOWN: ply_pts.clear() img = plot(img, ply_pts) cv2.namedWindow("test") cv2.setMouseCallback("test", on_mouse_cb) while True: cv2.imshow("test", img) key = cv2.waitKey(1) if key > 0: if chr(key) == 'q': break
def main(): parser = argparse.ArgumentParser(description='Whiteboard inpainting demo') parser.add_argument( '-i', '--input', required=True, help='Required. Path to a video file or a device node of a web-camera.' ) parser.add_argument('--loop', default=False, action='store_true', help='Optional. Enable reading the input in a loop.') parser.add_argument('-o', '--output', required=False, help='Optional. Name of the output file(s) to save.') parser.add_argument('-limit', '--output_limit', required=False, default=1000, type=int, help='Optional. Number of frames to store in output. ' 'If 0 is set, all frames are stored.') parser.add_argument( '-m_i', '--m_instance_segmentation', type=str, required=False, help='Required. Path to the instance segmentation model.') parser.add_argument( '-m_s', '--m_semantic_segmentation', type=str, required=False, help='Required. Path to the semantic segmentation model.') parser.add_argument( '-t', '--threshold', type=float, default=0.6, help='Optional. Threshold for person instance segmentation model.') parser.add_argument('--no_show', help="Optional. Don't show output.", action='store_true') parser.add_argument( '-d', '--device', type=str, default='CPU', help= 'Optional. Specify a target device to infer on. CPU, GPU, HDDL or MYRIAD is ' 'acceptable. The demo will look for a suitable plugin for the device specified.' ) parser.add_argument('-u', '--utilization_monitors', default='', type=str, help='Optional. List of monitors to show initially.') args = parser.parse_args() cap = open_images_capture(args.input, args.loop) if cap.get_type() not in ('VIDEO', 'CAMERA'): raise RuntimeError( "The input should be a video file or a numeric camera ID") if bool(args.m_instance_segmentation) == bool( args.m_semantic_segmentation): raise ValueError( 'Set up exactly one of segmentation models: ' '--m_instance_segmentation or --m_semantic_segmentation') labels_dir = Path(__file__).resolve().parents[3] / 'data/dataset_classes' mouse = MouseClick() if not args.no_show: cv2.namedWindow(WINNAME) cv2.setMouseCallback(WINNAME, mouse.get_points) log.info('OpenVINO Runtime') log.info('\tbuild: {}'.format(get_version())) core = Core() model_path = args.m_instance_segmentation if args.m_instance_segmentation else args.m_semantic_segmentation log.info('Reading model {}'.format(model_path)) if args.m_instance_segmentation: labels_file = str(labels_dir / 'coco_80cl_bkgr.txt') segmentation = MaskRCNN(core, args.m_instance_segmentation, labels_file, args.threshold, args.device) elif args.m_semantic_segmentation: labels_file = str(labels_dir / 'cityscapes_19cl_bkgr.txt') segmentation = SemanticSegmentation(core, args.m_semantic_segmentation, labels_file, args.threshold, args.device) log.info('The model {} is loaded to {}'.format(model_path, args.device)) metrics = PerformanceMetrics() video_writer = cv2.VideoWriter() black_board = False frame_number = 0 key = -1 start_time = perf_counter() frame = cap.read() if frame is None: raise RuntimeError("Can't read an image from the input") out_frame_size = (frame.shape[1], frame.shape[0] * 2) output_frame = np.full((frame.shape[0], frame.shape[1], 3), 255, dtype='uint8') presenter = monitors.Presenter( args.utilization_monitors, 20, (out_frame_size[0] // 4, out_frame_size[1] // 16)) if args.output and not video_writer.open(args.output, cv2.VideoWriter_fourcc(*'MJPG'), cap.fps(), out_frame_size): raise RuntimeError("Can't open video writer") while frame is not None: mask = None detections = segmentation.get_detections([frame]) expand_mask(detections, frame.shape[1] // 27) if len(detections[0]) > 0: mask = detections[0][0][2] for i in range(1, len(detections[0])): mask = cv2.bitwise_or(mask, detections[0][i][2]) if mask is not None: mask = np.stack([mask, mask, mask], axis=-1) else: mask = np.zeros(frame.shape, dtype='uint8') clear_frame = remove_background(frame, invert_colors=not black_board) output_frame = np.where(mask, output_frame, clear_frame) merged_frame = np.vstack([frame, output_frame]) merged_frame = cv2.resize(merged_frame, out_frame_size) metrics.update(start_time, merged_frame) if video_writer.isOpened() and (args.output_limit <= 0 or frame_number <= args.output_limit - 1): video_writer.write(merged_frame) presenter.drawGraphs(merged_frame) if not args.no_show: cv2.imshow(WINNAME, merged_frame) key = check_pressed_keys(key) if key == 27: # 'Esc' break if key == ord('i'): # catch pressing of key 'i' black_board = not black_board output_frame = 255 - output_frame else: presenter.handleKey(key) if mouse.crop_available: x0, x1 = min(mouse.points[0][0], mouse.points[1][0]), \ max(mouse.points[0][0], mouse.points[1][0]) y0, y1 = min(mouse.points[0][1], mouse.points[1][1]), \ max(mouse.points[0][1], mouse.points[1][1]) x1, y1 = min(x1, output_frame.shape[1] - 1), min( y1, output_frame.shape[0] - 1) board = output_frame[y0:y1, x0:x1, :] if board.shape[0] > 0 and board.shape[1] > 0: cv2.namedWindow('Board', cv2.WINDOW_KEEPRATIO) cv2.imshow('Board', board) frame_number += 1 start_time = perf_counter() frame = cap.read() metrics.log_total() for rep in presenter.reportMeans(): log.info(rep)
import cv2 import numpy as np def click_event(event, x, y, flags, params): if event == cv2.EVENT_LBUTTONDOWN: cv2.circle(img, (x, y), 3, (0, 0, 255), 5) points.append((x, y)) if len(points) >= 2: cv2.line(img, points[-1], points[-2], (255, 0, 0), 3) cv2.imshow('image', img) img = cv2.imread('lena.jpg') points = [] # img = np.zeros((512,512,3), np.uint8) # black image cv2.imshow('image', img) cv2.setMouseCallback('image', click_event) cv2.waitKey(0) cv2.destroyAllWindows()
import cv2 import numpy as np cv2.namedWindow('mouseRGB') param1 = 10 capture = cv2.VideoCapture(0) while (True): ret, frame = capture.read() cv2.imshow('mouseRGB', frame) def mouseRGB(event, x, y, flags, param): if event == cv2.EVENT_LBUTTONDOWN: #checks mouse left button down condition colorsB = frame[y, x, 0] colorsG = frame[y, x, 1] colorsR = frame[y, x, 2] colors = frame[y, x] print(param1) cv2.setMouseCallback('mouseRGB', mouseRGB) if cv2.waitKey(1) == 27: break capture.release() cv2.destroyAllWindows()
import numpy as np bridge = CvBridge() roi_pub = None def img_cb(msg): cv2.imshow('win', np.asarray(bridge.imgmsg_to_cv(msg))) cv2.waitKey(10) def click(s, x, y, click, u): if click: roi = RegionOfInterest() #r = 10 roi.x_offset = x #max(x-r,0) roi.y_offset = y #max(y-r,0) #roi.width = 2*r #roi.height= 2*r roi_pub.publish(roi) print roi.x_offset, roi.y_offset if __name__ == '__main__': cv2.namedWindow('win') cv2.setMouseCallback('win', click) rospy.init_node('projector_test') rospy.Subscriber('image', Image, img_cb) roi_pub = rospy.Publisher('roi', RegionOfInterest) rospy.spin() cv2.destroyAllWindows()
# Create a black image and a window windowName = 'MouseCallback' cv2.namedWindow(windowName) # Load the image img_path = "../img/static_frame_from_video.jpg" img = cv2.imread(img_path) # Get the size of the image for the calibration width, height, _ = img.shape # Create an empty list of points for the coordinates list_points = list() # bind the callback function to window cv2.setMouseCallback(windowName, CallBackFunc) if __name__ == "__main__": # Check if the 4 points have been saved while (True): cv2.imshow(windowName, img) if len(list_points) == 4: # Return a dict to the YAML file config_data = dict(image_parameters=dict( p2=list_points[3], p1=list_points[2], p4=list_points[0], p3=list_points[1], width_og=width, height_og=height, img_path=img_path,
if cnt == 4: w = 200 h = 300 dst_pts = np.array([[0, 0], [w - 1, 0], [w - 1, h - 1], [0, h - 1]]).astype(np.float32) pers_mat = cv2.getPerspectiveTransform(src_pts, dst_pts) dst = cv2.warpPerspective(src, pers_mat, (w, h)) cv2.imshow('dst', dst) cnt = 0 src_pts = np.zeros([4, 2], dtype=np.float32) src = cv2.imread('card.bmp') if src is None: print('Image load failed!') sys.exit() cv2.namedWindow('src') cv2.setMouseCallback('src', on_mouse) cv2.imshow('src', src) cv2.waitKey(0) cv2.destroyAllWindows if __name__ == '__main__': on_mouse(5, 1, 1, flags, param)
def main(): args = parseargs() page_img = cv2.imread(args.input_img) page_img_orig = page_img.copy() page_img_rendered = page_img.copy() print('\nLoading engines...') if args.parse_config is not None: config = configparser.ConfigParser() config.read(args.parse_config) # convert relative paths to absolute for section, key in [['LINE_PARSER', 'MODEL_PATH'], ['OCR', 'OCR_JSON']]: if not os.path.isabs(config[section][key]): config[section][key] = os.path.realpath( os.path.join(os.path.dirname(args.parse_config), config[section][key])) parser = page_parser.PageParser(config) else: parser = None enhancer = repair_engine.EngineRepairCNN(args.repair_json) print('Loading page layout...') if os.path.exists(args.input_page): page_layout = layout.PageLayout(file=args.input_page) elif not os.path.exists(args.input_page) and parser is not None: print('Page xml file not found, running automatic parser...') page_layout = layout.PageLayout(id='id_placeholder', page_size=(page_img_orig.shape[0], page_img_orig.shape[1])) page_layout = parser.process_page(page_img_orig, page_layout) if not os.path.exists('./output_pages'): os.makedirs('./output_pages') file_name = os.path.splitext(os.path.split(args.input_img)[1])[0] page_layout.to_pagexml( os.path.join('./output_pages', '{}.xml'.format(file_name))) else: raise Exception( 'Page xml file not found and automatic page parser config not specified.' ) print('\n\n Welcome to the page enhancement interactive demo.') print( 'After choosing a line by double-clicking, you can enhance it by pressing r key. After that, you can change individual parts of the text by selecting the area and pressing e key.' ) cv2.namedWindow("Page Editor", cv2.WINDOW_NORMAL) cv2.resizeWindow('Page Editor', 1024, 1024) layout_clicker = LayoutClicker(page_layout) cv2.setMouseCallback("Page Editor", layout_clicker.callback) while True: page_img_rendered = page_img.copy() if layout_clicker.chosen_line: page_img_rendered = layout.draw_lines( page_img_rendered, [layout_clicker.chosen_line.polygon], color=(0, 255, 0), close=True) if layout_clicker.points: page_img_rendered = layout.draw_lines(page_img_rendered, [layout_clicker.points], color=(0, 0, 255)) cv2.imshow('Page Editor', page_img_rendered) key = cv2.waitKey(1) if key == ord('q'): break elif key == ord('r'): text_input = TextInputRepair( layout_clicker.chosen_line.transcription) action, new_transcription = text_input.run() layout_clicker.chosen_line.transcription = new_transcription if action == 'repair': page_img = enhancer.enhance_line_in_page( page_img, layout_clicker.chosen_line) page_img_rendered = page_img.copy() elif action == 'revert': line_crop, line_mapping, offset = enhancer.cropper.crop( page_img_orig, layout_clicker.chosen_line.baseline, layout_clicker.chosen_line.heights, return_mapping=True) page_img = enhancer.cropper.blend_in(page_img, line_crop, line_mapping, offset) page_img_rendered = page_img.copy() elif key == ord('e') and len(layout_clicker.points) == 2: line_crop, line_mapping, offset = enhancer.cropper.crop( page_img, layout_clicker.chosen_line.baseline, layout_clicker.chosen_line.heights, return_mapping=True) y1 = np.round(line_mapping[line_mapping.shape[0] // 2, layout_clicker.points[0][0] - offset[1], 0]).astype(np.uint16) y2 = np.round( line_mapping[line_mapping.shape[0] // 2, np.clip(layout_clicker.points[1][0] - offset[1], 0, line_mapping.shape[1] - 2), 0]).astype(np.uint16) transcriptions, _ = parser.ocr.ocr_engine.process_lines([ line_crop[:, :np.minimum(y1, y2), :], line_crop[:, np.maximum(y1, y2):, :] ]) line_crop[:, np.minimum(y1, y2):np.maximum(y1, y2), :] = 0 text_input = TextInputInpaint(transcriptions[0], transcriptions[1]) action, new_transcription = text_input.run() if action == 'inpaint': layout_clicker.chosen_line.transcription = new_transcription line_crop = enhancer.inpaint_line( line_crop, layout_clicker.chosen_line.transcription) page_img = enhancer.cropper.blend_in(page_img, line_crop, line_mapping, offset) line_crop, line_mapping, offset = enhancer.cropper.crop( page_img, layout_clicker.chosen_line.baseline, layout_clicker.chosen_line.heights, return_mapping=True) layout_clicker.points = []
point_list = [] def mouse_callback(event, x, y, flags, param): # 누를때마다 좌표값 저장 if event == cv.EVENT_LBUTTONDOWN: print("(%d, %d)" % (x, y)) point_list.append((x, y)) cv.circle(img_color, (x, y), 3, (0, 0, 255), -1) cv.namedWindow('source') cv.setMouseCallback('source', mouse_callback) img_color = cv.imread('../sample/affine.png') while (True): cv.imshow('source', img_color) if cv.waitKey(1) == 32: break height, weight = img_color.shape[:2] pts1 = np.float32([point_list[0], point_list[1], point_list[2]]) pts2 = np.float32([point_list[0], point_list[1], point_list[2]]) pts2[1][1] += 100
counter = 0 def mousePoints(event, x, y, flags, params): global counter if event == cv2.EVENT_LBUTTONDOWN: #print(x, y) circles[counter] = x, y counter = counter + 1 print(circles) img = cv2.imread('Resources/cards0.jpg') while True: if counter == 4: width, height = 250, 350 pts1 = np.float32([circles[0], circles[1], circles[2], circles[3]]) pts2 = np.float32([[0, 0], [width, 0], [0, height], [width, height]]) matrix = cv2.getPerspectiveTransform(pts1, pts2) imgOutput = cv2.warpPerspective(img, matrix, (width, height)) cv2.imshow("Output image ", imgOutput) for x in range(0, 4): cv2.circle(img, (circles[x][0], circles[x][1]), 3, (0, 255, 0), cv2.FILLED) cv2.imshow("Original Image", img) cv2.setMouseCallback("Original Image", mousePoints) cv2.waitKey(1)
import numpy as np import cv2 as cv def click_event(event, x, y, flags, param): if event == cv.EVENT_LBUTTONDOWN: cv.circle(img, (x, y), 10, (0, 0, 255), -1) points.append((x, y)) print(points) print(points[-1]) print(points[-2]) if len(points) >= 2: cv.line(img, points[-1], points[-2], (255, 0, 0), 5) cv.imshow("Image", img) img = np.zeros((500, 500, 3), np.uint8) cv.imshow("Image", img) points = [ ] cv.setMouseCallback("Image", click_event) cv.waitKey(0) cv.destroyAllWindows()
# mouse callback function def draw_circle(event,x,y,flags,param):#创建一个回调函数 global ix,iy,drawing,mode if event == cv.EVENT_LBUTTONDOWN:#左键按下返回起始位置坐标 drawing = True ix,iy = x,y elif event == cv.EVENT_MOUSEMOVE:#左键按下画图, if drawing == True: if mode == True: cv.rectangle(img,(ix,iy),(x,y),(0,255,0),-1) else: cv.circle(img,(x,y),5,(0,0,255),-1) elif event == cv.EVENT_LBUTTONUP:#左键松开不画图, drawing = False if mode == True: cv.rectangle(img,(ix,iy),(x,y),(0,255,0),-1) else: cv.circle(img,(x,y),5,(0,0,255),-1) #和opencv主窗口绑定 #connect with the main opencv windows img = np.zeros((512,512,3), np.uint8) cv.namedWindow('image') cv.setMouseCallback('image',draw_circle) while(1): cv.imshow('image',img) k=cv.waitKey(1) & 0xFF if k ==ord('m'):# m button mode=not mode elif k==27:#esc button break cv.destroyAllWindows()
def main(): global getROI, range_min, range_max args = parse_args() cv2.namedWindow("Input") cv2.namedWindow("HSV") cv2.namedWindow("Mask") cv2.namedWindow("Erosion") cv2.setMouseCallback("Input", select_region) capture = cv2.VideoCapture(0) capture.set(cv2.CAP_PROP_FRAME_WIDTH, args.width) capture.set(cv2.CAP_PROP_FRAME_HEIGHT, args.height) while True: grabbed, frame = capture.read() if not grabbed or frame is None: continue if args.flip: frame = np.fliplr(frame).copy() img_HSV = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV) img_mask = cv2.inRange(img_HSV, range_min, range_max) img_erode = cv2.erode(img_mask, None, iterations=3) moments = cv2.moments(img_erode, True) if moments['m00'] >= MIN_AREA: x = moments['m10'] / moments['m00'] y = moments['m01'] / moments['m00'] print(x, ", ", y) cv2.circle(frame, (int(x), int(y)), 5, (0, 255, 0), -1) if cropping and not getROI: cv2.rectangle(frame, (x_start, y_start), (x_end, y_end), (0, 255, 0), 2) elif not cropping and getROI: cv2.rectangle(frame, (x_start, y_start), (x_end, y_end), (0, 255, 0), 2) if getROI: roi = frame[y_start:y_end, x_start:x_end] hsvROI = cv2.cvtColor(roi, cv2.COLOR_BGR2HSV) print( 'min H = {}, min S = {}, min V = {}; max H = {}, max S = {}, max V = {}' .format(hsvROI[:, :, 0].min(), hsvROI[:, :, 1].min(), hsvROI[:, :, 2].min(), hsvROI[:, :, 0].max(), hsvROI[:, :, 1].max(), hsvROI[:, :, 2].max())) range_min = np.array([ hsvROI[:, :, 0].min(), hsvROI[:, :, 1].min(), hsvROI[:, :, 2].min() ]) range_max = np.array([ hsvROI[:, :, 0].max(), hsvROI[:, :, 1].max(), hsvROI[:, :, 2].max() ]) getROI = False cv2.imshow("Input", frame) cv2.imshow("HSV", img_HSV) cv2.imshow("Mask", img_mask) cv2.imshow("Erosion", img_erode) key = cv2.waitKey(10) if key == ord('q'): # quit break elif key == ord('r'): # reset selection getROI = False cv2.destroyAllWindows()
lines = fin.readlines() for y in range(len(lines)): nums = [int(strip_punc(temp)) for temp in lines[y].split(' ')] for x in range(len(nums)): if nums[x] == 1: highlight_square(x, y) cv2.namedWindow('image', cv2.WINDOW_NORMAL) if args.scale: scaled_width = int(im.shape[1] * args.scale) scaled_height = int(im.shape[0] * args.scale) print('scale: {}, (w, h): {}, {}'.format(args.scale, scaled_width, scaled_height)) cv2.resizeWindow('image', (scaled_width, scaled_height)) mouseDown = False cv2.setMouseCallback('image', mouse_callback) while True: cv2.imshow('image', im) k = cv2.waitKey(10) & 0xFF if k == ord('a'): with open(args.terrainfile, 'w') as fout: to_write = [[ 0 for _ in range(math.ceil(im.shape[1] / objectsize)) ] for _ in range(math.ceil(im.shape[0] / objectsize))] for (temp_x, temp_y) in terrain_coors: print('{} {} terrain_coors'.format(temp_x, temp_y)) to_write[temp_x][temp_y] = 1 for temp_x in range(len(to_write)): row = to_write[temp_x] fout.write('[' + str(row[0])) for temp_y in range(1, len(row)):
elif event == cv2.EVENT_MOUSEMOVE: if cropping == True: x_end, y_end = x, y # check to see if the left mouse button was released elif event == cv2.EVENT_LBUTTONUP: # record the ending (x, y) coordinates and indicate that # the cropping operation is finished x_end, y_end = x, y cropping = False getROI = True cv2.namedWindow("image") cv2.setMouseCallback("image", click_and_crop) # keep looping while True: if not getROI: # get one of the following tags: "blue", "yellow", "red" print( "[+] Enter the tag for the color you are going to select (blue, yellow, red):" ) tag = str(input()) # check if the entered tag is spelled right while (tag != "blue") and (tag != "yellow") and (tag != "red"): print( "[+] ERROR: The entered tag does not match blue, yellow or red! Try again:"
# Initialize camera cam.Init() cam.AcquisitionMode.SetValue(PySpin.AcquisitionMode_SingleFrame) cam.BeginAcquisition() #k=np.array(image_primary) #cap = cv2.VideoCapture(0) #ret,frame=cap.read() image = cam.GetNextImage() print "1" # load the image, clone it, and setup the mouse callback function #image = cv2.imread(args["image"]) image = pyflycapture2.convert(image) cv2.namedWindow("Camera View, Press 'r' to refresh & 'c' to exit.") cv2.setMouseCallback("Camera View, Press 'r' to refresh & 'c' to exit.", click_and_crop) print "2" # keep looping until the 'q' key is pressed while True: # display the image and wait for a keypress print "3" print cv_image cv2.imshow("Camera View, Press 'r' to refresh & 'c' to exit.", cv_image) print "4" key = cv2.waitKey(1) & 0xFF if key == ord("r"): image = cam.GetNextImage() row_bytes = float(len(image.getData())) / float(image.getRows()) cv_image = np.array(image.getData(), dtype="uint8").reshape( (image.getrows(), image.getCols()))
# -*- coding: utf-8 -*- """ Created on Sat Nov 16 23:48:41 2019 @author: Zain Mansoor """ import numpy as np import cv2 def click_event(event, x, y, flags, param): if event == cv2.EVENT_LBUTTONDOWN: blue = img[x, y, 0] green = img[x, y, 1] red = img[x, y, 2] cv2.circle(img, (x, y), 10, (0, 0, 255), -1) mycolorImage = np.zeros([512, 512, 3], dtype=np.uint8) mycolorImage[:] = [blue, green, red] cv2.imshow("color", mycolorImage) img = cv2.imread("lena.jpg", 1) cv2.imshow("image", img) cv2.setMouseCallback("image", click_event) cv2.waitKey(0) cv2.destroyAllWindows()
global canvas global is_drawing if event == cv2.EVENT_LBUTTONDOWN: if is_drawing: start_point = (x, y) elif event == cv2.EVENT_MOUSEMOVE: if is_drawing: end_point = (x, y) draw_line(canvas, start_point, end_point) start_point = end_point elif event == cv2.EVENT_LBUTTONUP: is_drawing = False cv2.namedWindow("Test Canvas") cv2.setMouseCallback("Test Canvas", on_mouse_events) while (True): cv2.imshow("Test Canvas", canvas) key = cv2.waitKey(1) & 0xFF if key == ord('q'): break elif key == ord('s'): is_drawing = True elif key == ord('c'): canvas[:, :] = 0 elif key == ord('p'): image = canvas[:, :] dimension = image.shape print(dimension)
width, height = 107, 48 try: with open('CarParkPos', 'rb') as f: posList = pickle.load(f) except: posList = [] def mouseClick(events, x, y, flags, params): if events == cv2.EVENT_LBUTTONDOWN: posList.append((x, y)) if events == cv2.EVENT_RBUTTONDOWN: for i, pos in enumerate(posList): x1, y1 = pos if x1 < x < x1 + width and y1 < y < y1 + height: posList.pop(i) with open('CarParkPos', 'wb') as f: pickle.dump(posList, f) while True: img = cv2.imread('carParkImg.png') for pos in posList: cv2.rectangle(img, pos, (pos[0] + width, pos[1] + height), (255, 0, 255), 2) cv2.imshow("Image", img) cv2.setMouseCallback("Image", mouseClick) cv2.waitKey(1)
def __setupMouseControl(self): self.mouse_prev_x = 0 self.mouse_prev_y = 0 cv.setMouseCallback(NovaConfig.NOVA_WINDOW_NAME, self.__onMouse) self.mouse_timer = FrequencyTimer( NovaConfig.EXTERNAL_INPUT_MOUSE_FREQUENCY_MS)
root = Tk() filename = filedialog.askopenfilename( initialdir="/", title="Select file", filetypes=(("PNG files", "*.PNG"), ("png files", "*.png"), ("All files", "*.*")), ) root.destroy() filename_split = os.path.split(filename) folder = filename_split[0] file = filename_split[1] file_split = file.split(".") new_filename = folder + "/" + file_split[0] + "_marked." + file_split[-1] mask_im = cv2.imread(filename) cv2.namedWindow("Mask") cv2.setMouseCallback("Mask", get_mouse_points) while True: cv2.imshow("Mask", mask_im) cv2.waitKey(1) if len(mouse_pts) == 6: cv2.destroyWindow("Mask") break first_frame_display = False points = mouse_pts print(points) print("----------------------------------------------------------------") print("Copy the following code and paste it in masks.cfg") print("----------------------------------------------------------------") name_points = ["a", "b", "c", "d", "e", "f"]
import cv2 clicked = False def onMouse(event, x, y, flags, param): global clicked if event == cv2.EVENT_LBUTTONUP: clicked = True cameraCapture = cv2.VideoCapture(0) cv2.namedWindow('MyWindow') cv2.setMouseCallback('MyWindow', onMouse) print('Showing camera feed. Click window or press any key to stop.') success, frame = cameraCapture.read() while cv2.waitKey(1) == -1 and not clicked: if frame is not None: cv2.imshow('MyWindow', frame) success, frame = cameraCapture.read() cv2.destroyWindow('MyWindow')