def debug_iter(box, frame, hull, timestamp): if box is not None: cv2.circle(frame, (int(np.round(box[0][0])), int(np.round(box[0][1]))), 10, (0, 255, 0)) cv2.ellipse(frame, box, (0, 255, 0)) cv2.polylines(frame, [hull], 1, (0, 0, 255)) if random.random() < .5: cv2.imshow("Eye", frame) key = cv2.waitKey(20) # No user input if key == -1: return elif key == 27: return 'QUIT' elif key == 97: # a PARAMS['_delta'] += 5 elif key == 122: # z PARAMS['_delta'] -= 5 elif key == 115: # s PARAMS['_max_variation'] += .1 elif key == 120: # x PARAMS['_max_variation'] -= .1 elif key == 100: # d PARAMS['_min_diversity'] += .1 elif key == 99: # c PARAMS['pupil_intensity'] -= .1 elif key == 102: # f PARAMS['pupil_intensity'] += 5 elif key == 118: # v PARAMS['pupil_intensity'] -= 5 if 97 <= key <= 122: print('Got key[%d]' % key) return 'RELOAD'
def drawParticles(image,particles): particles=sortParticles(particles)[:2] drawImage=np.copy(image) for particle in particles: cv2.polylines(drawImage,np.array([particle.points]),True,vv.highlightColor,1,1) #cv2.circle(image,(particle.centerX,particle.centerY),2,vv.highlightColor) #changed return drawImage
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 rgb_analisis(self, img): # TODO what happend if the object is lost? if self.track_window == None: self.detect_object(img) if self.track_window == None: return None else: hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV) dst = cv2.calcBackProject([hsv], [0], self.hist_track, [0, 180], 1) # apply meanshift to get the new location # print dst # print self.track_window ret, self.track_window = cv2.CamShift(dst, self.track_window, self.track_crit) # Draw it on image pts = cv2.cv.BoxPoints(ret) pts = np.int0(pts) cv2.polylines(img, [pts], True, (0, 255, 0), 2) self.img = img # cv2.imshow('track',img) # cv2.waitKey(1) print "Object tracked" return self.track_window[0] + self.track_window[2] / 2
def run(self): while True: playing = not self.paused and not self.rect_sel.dragging if playing or self.frame is None: ret, frame = self.cap.read() if not ret: break self.frame = frame.copy() vis = self.frame.copy() if playing: tracked = self.tracker.track(self.frame) for tr in tracked: cv2.polylines(vis, [np.int32(tr.quad)], True, (255, 255, 255), 2) for (x, y) in np.int32(tr.p1): cv2.circle(vis, (x, y), 2, (255, 255, 255)) self.rect_sel.draw(vis) cv2.imshow('plane', vis) ch = cv2.waitKey(1) if ch == ord(' '): self.paused = not self.paused if ch == ord('c'): self.tracker.clear() if ch == 27: break
def update(self, frame): # print "updating %d " % self.id hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV) back_project = cv2.calcBackProject([hsv],[0], self.roi_hist,[0,180],1) if args.get("algorithm") == "c": ret, self.track_window = cv2.CamShift(back_project, self.track_window, self.term_crit) pts = cv2.boxPoints(ret) pts = np.int0(pts) self.center = center(pts) cv2.polylines(frame,[pts],True, 255,1) if not args.get("algorithm") or args.get("algorithm") == "m": ret, self.track_window = cv2.meanShift(back_project, self.track_window, self.term_crit) x,y,w,h = self.track_window self.center = center([[x,y],[x+w, y],[x,y+h],[x+w, y+h]]) cv2.rectangle(frame, (x,y), (x+w, y+h), (255, 255, 0), 2) self.kalman.correct(self.center) prediction = self.kalman.predict() cv2.circle(frame, (int(prediction[0]), int(prediction[1])), 4, (255, 0, 0), -1) # fake shadow cv2.putText(frame, "ID: %d -> %s" % (self.id, self.center), (11, (self.id + 1) * 25 + 1), font, 0.6, (0, 0, 0), 1, cv2.LINE_AA) # actual info cv2.putText(frame, "ID: %d -> %s" % (self.id, self.center), (10, (self.id + 1) * 25), font, 0.6, (0, 255, 0), 1, cv2.LINE_AA)
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 draw_match(img1, img2, p1, p2, 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(p1), np.bool_) green = (0, 255, 0) red = (0, 0, 255) for (x1, y1), (x2, y2), inlier in zip(np.int32(p1), np.int32(p2), status): col = [red, green][inlier] if inlier: cv2.line(vis, (x1, y1), (x2+w1, y2), col) cv2.circle(vis, (x1, y1), 2, col, -1) cv2.circle(vis, (x2+w1, y2), 2, col, -1) else: 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+w1-r, y2-r), (x2+w1+r, y2+r), col, thickness) cv2.line(vis, (x2+w1-r, y2+r), (x2+w1+r, y2-r), col, thickness) return vis
def process(self): img1 = cv2.cvtColor(self.imgcv1, cv2.COLOR_BGR2GRAY) #queryimage # left image img2 = cv2.cvtColor(self.imgcv2, cv2.COLOR_BGR2GRAY) #trainimage # right image # find the keypoints and descriptors with SIFT kp1, des1 = self.detector.detectAndCompute(img1,None) kp2, des2 = self.detector.detectAndCompute(img2,None) matches = self.flann.knnMatch(des1,des2,k=2) pts1 = [] pts2 = [] # ratio test as per Lowe's paper for i,(m,n) in enumerate(matches): if m.distance < 0.8*n.distance: pts2.append(kp2[m.trainIdx].pt) pts1.append(kp1[m.queryIdx].pt) pts1 = np.float32(pts1) pts2 = np.float32(pts2) M, mask = cv2.findHomography(pts1, pts2, cv2.RANSAC,5.0) h,w = img1.shape h/=2 w/=2 pts = np.float32([ [0,0],[0,h-1],[w-1,h-1],[w-1,0] ]).reshape(-1,1,2) dst = cv2.perspectiveTransform(pts,M) cv2.polylines(img2,[np.int32(dst)],True,255,3) #We select only inlier points pts1 = pts1[mask.ravel()==1] pts2 = pts2[mask.ravel()==1] self.data=(M,pts1,pts2) pts1i=np.int32(pts1) pts2i=np.int32(pts2) return drawpoints(img1,img2,pts1i,pts2i)
def run(self): while True: playing = not self.paused and not self.rect_sel.dragging if playing or self.frame is None: ret, frame = self.cap.read() if not ret: break self.frame = frame.copy() w, h = getsize(self.frame) vis = np.zeros((h, w*2, 3), np.uint8) vis[:h,:w] = self.frame if len(self.tracker.targets) > 0: target = self.tracker.targets[0] vis[:,w:] = target.image draw_keypoints(vis[:,w:], target.keypoints) x0, y0, x1, y1 = target.rect cv2.rectangle(vis, (x0+w, y0), (x1+w, y1), (0, 255, 0), 2) if playing: tracked = self.tracker.track(self.frame) if len(tracked) > 0: tracked = tracked[0] cv2.polylines(vis, [np.int32(tracked.quad)], True, (255, 255, 255), 2) for (x0, y0), (x1, y1) in zip(np.int32(tracked.p0), np.int32(tracked.p1)): cv2.line(vis, (x0+w, y0), (x1, y1), (0, 255, 0)) draw_keypoints(vis, self.tracker.frame_points) self.rect_sel.draw(vis) cv2.imshow('plane', vis) ch = cv2.waitKey(1) if ch == ord(' '): self.paused = not self.paused if ch == 27: break
def draw_flow(img, flow, step=16): h, w = img.shape[:2] y, x = np.mgrid[step/2:h:step, step/2:w:step].reshape(2,-1).astype(int) fx, fy = flow[y,x].T lines = np.vstack([x, y, x+fx, y+fy]).T.reshape(-1, 2, 2) lines = np.int32(lines + 0.5) vis = cv2.cvtColor(img, cv2.COLOR_GRAY2BGR) cv2.polylines(vis, lines, 0, (0, 255, 0)) for (x1, y1), (x2, y2) in lines: cv2.circle(vis, (x1, y1), 1, (0, 255, 0), -1) h, w, _ = flow.shape left_eye = np.apply_along_axis(np.linalg.norm, 0, flow[:,:w/2]).mean() right_eye = np.apply_along_axis(np.linalg.norm, 0, flow[:,w/2:]).mean() print(left_eye - right_eye) # Calculate using angle information left_eye_mean_velocity = flow[:,:w/2,:].sum(axis=0) right_eye_mean_velocity = flow[:,w/2:,:].sum(axis=0) left_eye_mean_velocity_norm = np.linalg.norm(left_eye_mean_velocity) right_eye_mean_velocity_norm = np.linalg.norm(right_eye_mean_velocity) eyes_cosine = left_eye_mean_velocity.dot(right_eye_mean_velocity.T)/left_eye_mean_velocity_norm*right_eye_mean_velocity_norm print(eyes_cosine) return vis
def draw_robot(self, frame, position_dict, color): if position_dict['box']: cv2.polylines(frame, [np.array(position_dict['box'])], True, BGR_COMMON[color], 2) frame = consol.draw_dots(frame) if position_dict['front']: p1 = (position_dict['front'][0][0], position_dict['front'][0][1]) p2 = (position_dict['front'][1][0], position_dict['front'][1][1]) cv2.circle(frame, p1, 3, BGR_COMMON['white'], -1) cv2.circle(frame, p2, 3, BGR_COMMON['white'], -1) cv2.line(frame, p1, p2, BGR_COMMON['red'], 2) if position_dict['dot']: cv2.circle( frame, (int(position_dict['dot'][0]), int(position_dict['dot'][1])), 4, BGR_COMMON['black'], -1) #Draw predicted position ''' cv2.circle( frame, (, ), 4, BGR_COMMON['yellow'], -1) ''' px = self.launch.planner.world.our_attacker.predicted_vector.x py = len(frame) - self.launch.planner.world.our_attacker.predicted_vector.y consol.log_dot([px,py], 'yellow', 'kalman') if position_dict['direction']: cv2.line( frame, position_dict['direction'][0], position_dict['direction'][1], BGR_COMMON['orange'], 2)
def reptfulle(tabc,dx,dy): imgi = np.zeros((dx,dy,3), np.uint8) cv2.polylines(imgi,[tabc],True,(1,1,1)) cv2.fillPoly(imgi,[tabc],(1,1,1)) tabzi = np.array(imgi) tabz = tabzi[:, :,1] return tabz, imgi
def getIcon(self, maxWidth, maxHeight): # Create an icon of a cropped image of the 1st View, and resize it to the parameters. # if drawPickupRect is True, it will also overlay the position of the robots pickup area for the object # Get the full image and crop it fullImage = self.views[0].image.copy() rect = self.views[0].rect image = fullImage[rect[1]:rect[3], rect[0]:rect[2]] # Draw the pickupArea on it before resizing # if drawPickupRect and self.views[0].pickupRect is not None: x0, y0, x1, y1 = self.views[0].pickupRect quad = np.int32([[x0, y0], [x1, y0], [x1, y1], [x0, y1]]) cv2.polylines(image, [quad], True, (255, 255, 255), 2) # Resize it to fit within maxWidth and maxHeight # Keep Width within maxWidth height, width, _ = image.shape if width > maxWidth: image = cv2.resize(image, (maxWidth, int(float(maxWidth) / width * height))) # Keep Height within maxHeight height, width, _ = image.shape if height > maxHeight: image = cv2.resize(image, (int(float(maxHeight) / height * width), maxHeight)) return image.copy()
def detect_possible_buttons(image, source_image): contours, hierarchy = cv2.findContours(image, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) buttons = [] for cnt in contours: if 850 < cv2.contourArea(cnt) < 3000: # remove small and large areas like noise etc hull = cv2.convexHull(cnt) # find the convex hull of contour hull = cv2.approxPolyDP(hull, 0.1 * cv2.arcLength(hull, True), True) min = [10000.0, 10000.0] max = [0.0, 0.0] #print '%d,%d' % (point[0][0], point[0][1]) if len(hull) == 4: margin = 2 for point in hull: x = point[0][0] y = point[0][1] if x < min[0]: min[0] = x - margin if y < min[1]: min[1] = y - margin if x > max[0]: max[0] = x + margin if y > max[1]: max[1] = y + margin points = [[min[0], min[1]], [max[0], min[1]], [max[0], max[1]], [min[0], max[1]]] points = np.array(points,np.int0) button = {'image': source_image[min[0]:max[0], min[1]:max[1]], 'x': 0.5*(max[0]+min[0]), 'y': 0.5*(max[1]+min[1]), 'w': max[0]-min[0], 'h': max[1]-min[1]} buttons.append(button) cv2.polylines(source_image, [points], True, (255, 0, 0), 2) cv2.drawContours(source_image, [hull], 0, (0, 255, 0), 1) return buttons
def draw_markers(img,markers): for m in markers: centroid = np.array(m['centroid'],dtype=np.float32) origin = np.array(m['verts'][0],dtype=np.float32) hat = np.array([[[0,0],[0,1],[.5,1.25],[1,1],[1,0]]],dtype=np.float32) hat = cv2.perspectiveTransform(hat,m_marker_to_screen(m)) if m['id_confidence']>.9: cv2.polylines(img,np.int0(hat),color = (0,0,255),isClosed=True) else: cv2.polylines(img,np.int0(hat),color = (0,255,0),isClosed=True) # cv2.polylines(img,np.int0(centroid),color = (255,255,int(255*m['id_confidence'])),isClosed=True,thickness=2) m_str = 'id: {:d}'.format(m['id']) org = origin.copy() # cv2.rectangle(img, tuple(np.int0(org+(-5,-13))[0,:]), tuple(np.int0(org+(100,30))[0,:]),color=(0,0,0),thickness=-1) cv2.putText(img,m_str,tuple(np.int0(org)[0,:]),fontFace=cv2.FONT_HERSHEY_SIMPLEX, fontScale=0.4, color=(0,0,255)) if 'id_confidence' in m: m_str = 'idc: {:.3f}'.format(m['id_confidence']) org += (0, 12) cv2.putText(img,m_str,tuple(np.int0(org)[0,:]),fontFace=cv2.FONT_HERSHEY_SIMPLEX, fontScale=0.4, color=(0,0,255)) if 'loc_confidence' in m: m_str = 'locc: {:.3f}'.format(m['loc_confidence']) org += (0, 12 ) cv2.putText(img,m_str,tuple(np.int0(org)[0,:]),fontFace=cv2.FONT_HERSHEY_SIMPLEX, fontScale=0.4, color=(0,0,255)) if 'frames_since_true_detection' in m: m_str = 'otf: {}'.format(m['frames_since_true_detection']) org += (0, 12 ) cv2.putText(img,m_str,tuple(np.int0(org)[0,:]),fontFace=cv2.FONT_HERSHEY_SIMPLEX, fontScale=0.4, color=(0,0,255)) if 'opf_vel' in m: m_str = 'otf: {}'.format(m['opf_vel']) org += (0, 12 ) cv2.putText(img,m_str,tuple(np.int0(org)[0,:]),fontFace=cv2.FONT_HERSHEY_SIMPLEX, fontScale=0.4, color=(0,0,255))
def draw_sinusoid(img, thickness, radius, clearance, bgr_color): """ Draws a sinusoid primitive Args: img(:class:`numpy.ndarray(dtype=unit8)`): image to draw on (OpenCV format) thickness(int): line thickness radius(float): approx size of the primitive clearance(float): approx clearance from pic borders """ [b, g, r] = [int(x) for x in bgr_color] h, w, _ = img.shape x_center = randint(low=clearance, high=(w - 1) - clearance) y_center = randint(low=0 + clearance, high=(h - 1) - clearance) start_angle = uniform(low=0, high=2 * numpy.pi) x_points = numpy.linspace(-pi / 2, pi / 2, 100) points = [[x / (pi / 2), sin(4 * x)] for x in x_points] points = warped_polyline(points, [x_center, y_center], start_angle, 40) points = numpy.array(points, numpy.int32) cv2.polylines(img, [points], False, [b, g, r], thickness=thickness)
def _show_image(self): self.subLock.acquire(True) local_image = deepcopy(self._np_image) self.subLock.release() # draw circles for idx, points in enumerate(self._roi_points): cv2.circle(local_image, (points[0], points[1]), 5, (255, 0, 0), 2) # draw green lines cv2.polylines(local_image, np.int32([np.array(self._roi_points)]), 1, (0, 255, 0), 2) cv2.polylines(local_image, np.int32([np.array( self._other_roi_points)]), 1, (0, 255, 0), 2) cv.ShowImage("Learn Play game RGB", cv.fromarray(local_image)) cv.SetMouseCallback("Learn Play game RGB", self._on_mouse_click, 0) cv.CreateTrackbar("Gain", "Learn Play game RGB", self._gain_slider, 100, self._on_gain_slider) cv.CreateTrackbar("Red Threshold", "Learn Play game RGB", self._inrange_colour_thresh, 500, self._on_red_slider) cv.CreateTrackbar("High red", "Learn Play game RGB", self._high_colour_slider, 40, self._on_high_colour_slider) cv.CreateTrackbar("Low red", "Learn Play game RGB", self._low_colour_slider, 40, self._on_low_colour_slider) cv.WaitKey(3)
def make_debug_output(self, image, filename): """ make debug output image """ logging.info('Creating debug output') for burrow in self.burrows: # draw the morphological graph for points in burrow.morphological_graph.get_edge_curves(): cv2.polylines(image, [np.array(points, np.int)], isClosed=False, color=(255, 255, 255), thickness=2) # draw the smooth centerline cline = burrow.centerline cv2.polylines(image, [np.array(cline, np.int)], isClosed=False, color=(255, 0, 0), thickness=3) # mark the end points for e_p in burrow.endpoints: if e_p.is_exit: color = (0, 0, 255) else: color = (0, 255, 0) coords = tuple([int(c) for c in e_p.coords]) cv2.circle(image, coords, 10, color, thickness=-1) cv2.cvtColor(image, cv2.COLOR_RGB2BGR, image) #< convert to BGR cv2.imwrite(filename, image) logging.info('Wrote output file `%s`' % filename)
def visualise_measurements(self): if not self.current_data_visualised: self.current_data_visualised = True cnt = lambda x: np.rint(np.array(x)*self.fusion_model_params.cvscale).astype(int) img = np.zeros((self.particle_filter_model_param.world_dimensions[1]*self.fusion_model_params.cvscale, self.particle_filter_model_param.world_dimensions[0]*self.fusion_model_params.cvscale,3)).astype('uint8') img +=255 im_copies = [] #Draw Usable Area if self.particle_filter_model_param.world_usable_area != [[]]: cv2.polylines(img,[cnt(self.particle_filter_model_param.world_usable_area.exterior.coords[:])], isClosed=True, color=(0,0,0), thickness=2) #Add sensors for k, sensor_prop in self.sensor_properties.items(): img_copy = copy.deepcopy(img) cv2.fillPoly(img_copy, [cnt(self.sensor_properties[k].measurement_boundary_poly.exterior.coords[:])], (128,128,128)) im_copies.append(img_copy) max_w = np.min(np.max([p.w for p in self.pir_model.particles]),0) img_copy = copy.deepcopy(img) for particle in self.pir_model.particles: col = 255.*np.log((particle.w+1)/(max_w+1)) cv2.circle(img_copy, (int(particle.location.x*self.fusion_model_params.cvscale), int(particle.location.y*self.fusion_model_params.cvscale)),5,(0,col,0),-1) im_copies.append(img_copy) for i,overlay in enumerate(im_copies): opacity = 0.4 cv2.addWeighted(overlay, opacity, img, 1-opacity, 0, img) for sensor in self.sensor_properties.values(): cv2.circle(img,(int(sensor.location.x*self.fusion_model_params.cvscale), int(sensor.location.y*self.fusion_model_params.cvscale)),10,(0,0,255),-1) if(self.m_x != -1 and self.m_y != -1) and not (self.m_x is np.inf or self.m_y is np.inf): if self.good: cv2.circle(img,(int(self.m_x*self.fusion_model_params.cvscale), int(self.m_y*self.fusion_model_params.cvscale)), min(12,int(self.fusion_model_params.cvscale/8.)),(255,255,0),1) else: cv2.circle(img,(int(self.m_x*self.fusion_model_params.cvscale), int(self.m_y*self.fusion_model_params.cvscale)), min(12,int(self.fusion_model_params.cvscale/8.)),(64,64,0),1) cv2.imshow('test',cv2.flip(img,0)) cv2.waitKey(1) else: logging.debug('Data unchanged visualisation not updated.')
def draw_matches(self, img, kp_pairs, status, H): '''Derived from find_obj.py''' vis = img h, w = self.feature_shape[:2] if H is not None: corners = np.float32([[0, 0], [w, 0], [w, h], [0, h]]) + self.feature_offset corners = np.int32( cv2.perspectiveTransform(corners.reshape(1, -1, 2), H).reshape(-1, 2)) cv2.polylines(vis, [corners], True, (255, 255, 255)) if status is None: status = np.ones(len(kp_pairs), np.bool_) p2 = np.int32([kpp[1].pt for kpp in kp_pairs]) green = (0, 255, 0) red = (0, 0, 255) for (x2, y2), inlier in zip(p2, status): if inlier: col = green cv2.circle(vis, (x2, y2), 2, col, -1) else: col = red r = 2 thickness = 3 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)
def main(): try: video_src = sys.argv[1] except: video_src = 0 cam = video.create_capture(video_src) mser = cv.MSER_create() while True: ret, img = cam.read() if ret == 0: break gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY) vis = img.copy() regions, _ = mser.detectRegions(gray) hulls = [cv.convexHull(p.reshape(-1, 1, 2)) for p in regions] cv.polylines(vis, hulls, 1, (0, 255, 0)) cv.imshow('img', vis) if cv.waitKey(5) == 27: break print('Done')
def debug(**kw): while True: print(PARAMS) for x, y, frame, region, hull in pupil_iter(debug=True, **PARAMS): if x is not None: cv2.circle(frame, (x, y), 10, (0, 255, 0)) cv2.polylines(frame, [hull], 1, (0, 255, 0)) cv2.imshow("Eye", frame) key = cv2.waitKey(20) if key == -1: continue elif key == 27: return elif key == 97: # a PARAMS['_delta'] += 5 elif key == 122: # z PARAMS['_delta'] -= 5 elif key == 115: # s PARAMS['_max_variation'] += .1 elif key == 120: # x PARAMS['_max_variation'] -= .1 elif key == 100: # d PARAMS['_min_diversity'] += .1 elif key == 99: # c PARAMS['pupil_intensity'] -= .1 elif key == 102: # f PARAMS['pupil_intensity'] += 5 elif key == 118: # v PARAMS['pupil_intensity'] -= 5 if 97 <= key <= 122: print(key) break
def draw_lines(self, frame, color=(255, 0, 255), thickness=1): """ Draw the track to the frame. """ # Track lines. lines = np.array([[tp.x, tp.y] for tp in self.trackpoints]) cv2.polylines(frame, np.int32([lines]), 0, color, thickness=thickness)
def drawBounds(dst, rect, scale): h,w,_ = dst.shape if len(rect) > 1: rect = map(lambda c: projectPoint(c, scale, (w, h)),rect) cv2.polylines(dst, np.array([rect]), True, (0,255,0), 2) return dst
def draw_histogram_hsv(hsv_img, bin_width=2): """ Calculates and plots 2 histograms next to each other: one for hue, and one for saturation and value """ sv_hist_img, h_hist_img = np.zeros((300, 256, 3)), np.zeros((300, 360, 3)) sv_bin_count, h_bin_count = 256 / bin_width , 180 / bin_width sv_bins = np.arange(sv_bin_count).reshape(sv_bin_count, 1) * bin_width h_bins = np.arange(h_bin_count).reshape(h_bin_count, 1) * bin_width * 2 debug_colors = [ (255, 255, 255), (255, 0, 0), (0, 0, 255) ] # Use ternary conditional for outputting to 2 different hists - a bit of a hack for ch, col in enumerate(debug_colors): hist_item = cv2.calcHist([hsv_img], [ch], None, [h_bin_count if ch == 0 else sv_bin_count], [0, 180 if ch == 0 else 255]) cv2.normalize(hist_item, hist_item, 0, 255, cv2.NORM_MINMAX) hist = np.int32(np.around(hist_item)) pts = np.column_stack((h_bins if ch == 0 else sv_bins, hist)) cv2.polylines(h_hist_img if ch == 0 else sv_hist_img, [pts], False, col) sv_hist_img, h_hist_img = np.flipud(sv_hist_img), np.flipud(h_hist_img) h_hist_img[:, 0] = (0, 255, 0) cv2.imshow('sat / val hist | hue hist', np.concatenate([sv_hist_img, h_hist_img], axis=1))
def load_images(landmarks, path="./Project Data(2)/_Data/Radiographs/"): """ shows an img, and its corresponding landmarks """ img_landmark = {} matrix_images = [] if os.path.isdir(path): for filename in os.listdir(path): filepath = path + filename if os.path.isfile(filepath): radiography_nb = int(filename.split(".")[0]) # print radiography_nb landmark = landmarks[str(radiography_nb)] im = cv2.imread(filepath) gray = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY) for l in landmark: pts = np.array(l, np.int32) pts = pts.reshape((-1, 1, 2)) cv2.polylines(im, [pts], True, (0, 255, 255)) img_landmark[str(radiography_nb)] = im # print type(im) # print gray.shape matrix_images.append(gray) # cv2.imshow(filename, im) # cv2.waitKey(0) # cv2.destroyAllWindows() return img_landmark, np.asarray(matrix_images)
def drawHistogram(self,image): '''input of numpy.ndarray type, output is PhotoImage type''' h = numpy.zeros((300,256,3)) b,g,r = image[:,:,0].copy(),image[:,:,1].copy(),image[:,:,2].copy() bins = numpy.arange(257) bin = bins[0:-1] color = [ (255,0,0),(0,255,0),(0,0,255) ] for item,col in zip([b,g,r],color): N,bins = numpy.histogram(item,bins) v=N.max() N = numpy.int32(numpy.around((N*255)/v)) N=N.reshape(256,1) pts = numpy.column_stack((bin,N)) cv2.polylines(h,[pts],False,col,2) h=numpy.flipud(h) #convert numpy.ndarray to iplimage ipl_img = cv2.cv.CreateImageHeader((h.shape[1], h.shape[0]), cv.IPL_DEPTH_8U,3) cv2.cv.SetData(ipl_img, h.tostring(),h.dtype.itemsize * 3 * h.shape[1]) img = self.ipl2tk_image(ipl_img) return img
def trackFace(allRoiPts, allRoiHist): for k in range(0, TRACK): # read the frame and check if the frame has been read properly ret, frame = cap.read() if not ret: return -1; break i=0 # convert the given frame to HSV color space hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV) # for histogram of each window found, back project them on the current frame and track using CAMSHIFT for roiHist in allRoiHist: backProj = cv2.calcBackProject([hsv], [0], roiHist, [0, 180], 1) (r, allRoiPts[i]) = cv2.CamShift(backProj, allRoiPts[i], termination) # error handling for bound exceeding for j in range(0,4): if allRoiPts[i][j] < 0: allRoiPts[i][j] = 0 pts = np.int0(cv2.cv.BoxPoints(r)) # draw bounding box around the new position of the object cv2.polylines(frame, [pts], True, (0, 255, 255), 2) i = i + 1 # show the face on the frame cv2.imshow("Faces", frame) cv2.waitKey(1) return 1;
def renderStarGauss(image, cov, mu, first, scale = 5): num_circles = 3 num_points = 64 cov = sqrtm(cov) num = num_circles * num_points pos = np.ones((num, 2)) for c in range(num_circles): r = c + 1 for p in range(num_points): angle = p / num_points * 2 * np.pi index = c * num_points + p x = r * np.cos(angle) y = r * np.sin(angle) pos[index, 0] = x * cov[0, 0] + y * cov[0, 1] + mu[0] pos[index, 1] = x * cov[1, 0] + y * cov[1, 1] + mu[1] #image = image.copy() #image = cv2.cvtColor(image, cv2.COLOR_GRAY2BGR) if first: image = cv2.resize(image, (0, 0), None, scale, scale, cv2.INTER_NEAREST) for c in range(num_circles): pts = np.array(pos[c * num_points:(c + 1) * num_points, :] * scale + scale / 2, np.int32) pts = pts.reshape((-1,1,2)) cv2.polylines(image, [pts], True, (255, 0, 0)) return image
def draw_img(self) -> None: end = self.index_in_episode + 1 # エピソード内での現在の最新値インデックス+1 img = self.img # 描画先バッファ w = self.w # 画像幅(px) h = self.h # 画像高さ(px) chart_x = 5 # チャート部のX左端(px) chart_y = 0 # チャート部のY上端(px) chart_w = w - 10 # チャート部の幅(px) chart_h = h # チャート部の高さ(px) chart_w_for_scale = chart_w - 1 # チャート部X座標スケーリング用のチャート幅(px) chart_h_for_scale = chart_h - 1 # チャート部Y座標スケーリング用のチャート高さ(px) chart_right = chart_x + chart_w # チャート右端のX左端(px)+1 chart_bottom = chart_y + chart_h # チャート下端のY左端(px)+1 h_max = self.h_max img[:] = 0 position_type = self.position_type position_start_value = self.position_start_value positional_reward = self.calc_positional_reward( ) if position_start_value else 0 if positional_reward < 0: ind_x1 = 0 ind_x2 = 5 elif 0 < positional_reward: ind_x1 = w - 5 ind_x2 = w window_size = self.window_size time = self.episode_time[end - window_size:end] values = self.episode_values[end - window_size:end] ma = [] # 可能なら移動平均を計算 for ki in range(len(ma_kernel_sizes)): size_needed = window_size + ma_kernel_size_halfs[ki] * 2 if size_needed <= end: start = end - size_needed ma.append( np.convolve(self.episode_values[start:end, 1], ma_kernels[ki], mode='valid')) ma.append( np.convolve(self.episode_values[start:end, 2], ma_kernels[ki], mode='valid')) ma.append( np.convolve(self.episode_values[start:end, 3], ma_kernels[ki], mode='valid')) # 表示範囲となる最大最小を探す time_max = time.max() time_min = time_max - window_size * 60 values_min = values.min() values_max = values.max() for y in ma: values_min = min(values_min, y.min()) values_max = max(values_max, y.max()) if position_type: values_min = min(values_min, position_start_value) values_max = max(values_max, position_start_value) # values_min -= values_min % 100 # values_max += 100 - values_max % 100 time_scale = chart_w_for_scale / (time_max - time_min) value_scale = -chart_h_for_scale / (values_max - values_min) value_translate = chart_h_for_scale - values_min * value_scale cur = int( np.rint(values[-1, 3] * value_scale + value_translate).item()) if position_type: pos = int( np.rint(position_start_value * value_scale + value_translate).item()) else: pos = 0 trg = img[0] chart_trg = trg[chart_y:chart_bottom, chart_x:chart_right] # インジケーター描画 # 目盛り描画 for y in np.rint( np.arange(values_min - values_min % 50, values_max + 51 - (values_max % 50), 50) * value_scale + value_translate).astype(np.int32): if 0 <= y and y < chart_trg.shape[0]: chart_trg[y, :] = 0.1 # ポジション持っていたら、ポジった値から現在値まで塗りつぶす if position_type and positional_reward: ind_y1 = max(min(pos, cur), 0) ind_y2 = min(max(pos, cur), h_max) + 1 trg[ind_y1:ind_y2, ind_x1:ind_x2] = 1 # 現在値として水平線を描画 if 0 <= cur and cur < h: cur_y1 = max(cur - 1, 0) cur_y2 = min(cur + 1, h_max) + 1 trg[cur_y1:cur_y2, :] = 1.0 # チャートを描画開始 pts = np.empty((values.shape[0], 1, 2), dtype=np.int32) pts[:, 0, 0] = np.rint((time - time_min) * time_scale) # 可能なら移動平均線を描画 for y in ma: pts[:, 0, 1] = np.rint(y * value_scale + value_translate) cv2.polylines(chart_trg, [pts], False, 0.3) # open, high, low, close を描画 for value_type in range(4): pts[:, 0, 1] = np.rint(values[:, value_type] * value_scale + value_translate) cv2.polylines(chart_trg, [pts], False, 0.7)
def track_vot(model, video, hp=None, mask_enable=False, refine_enable=False): regions = [] # result and states[1 init / 2 lost / 0 skip] image_files, gt = video['image_files'], video['gt'] start_frame, end_frame, lost_times, toc = 0, len(image_files), 0, 0 for f, image_file in enumerate(image_files): im = cv2.imread(image_file) tic = cv2.getTickCount() if f == start_frame: # init cx, cy, w, h = get_axis_aligned_bbox(gt[f]) target_pos = np.array([cx, cy]) target_sz = np.array([w, h]) state = siamese_init(im, target_pos, target_sz, model, hp) # init tracker location = cxy_wh_2_rect(state['target_pos'], state['target_sz']) regions.append(1 if 'VOT' in args.dataset else gt[f]) elif f > start_frame: # tracking state = siamese_track(state, im, mask_enable, refine_enable) # track if mask_enable: location = state['ploygon'].flatten() mask = state['mask'] else: location = cxy_wh_2_rect(state['target_pos'], state['target_sz']) mask = [] if 'VOT' in args.dataset: gt_polygon = ((gt[f][0], gt[f][1]), (gt[f][2], gt[f][3]), (gt[f][4], gt[f][5]), (gt[f][6], gt[f][7])) if mask_enable: pred_polygon = ((location[0], location[1]), (location[2], location[3]), (location[4], location[5]), (location[6], location[7])) else: pred_polygon = ((location[0], location[1]), (location[0] + location[2], location[1]), (location[0] + location[2], location[1] + location[3]), (location[0], location[1] + location[3])) b_overlap = vot_overlap(gt_polygon, pred_polygon, (im.shape[1], im.shape[0])) else: b_overlap = 1 if b_overlap: regions.append(location) else: # lost regions.append(2) lost_times += 1 start_frame = f + 5 # skip 5 frames else: # skip regions.append(0) toc += cv2.getTickCount() - tic if args.visualization and f >= start_frame: # visualization (skip lost frame) im_show = im.copy() if f == 0: cv2.destroyAllWindows() if gt.shape[0] > f: if len(gt[f]) == 8: cv2.polylines( im_show, [np.array(gt[f], np.int).reshape( (-1, 1, 2))], True, (0, 255, 0), 3) else: cv2.rectangle(im_show, (gt[f, 0], gt[f, 1]), (gt[f, 0] + gt[f, 2], gt[f, 1] + gt[f, 3]), (0, 255, 0), 3) if len(location) == 8: if mask_enable: mask = mask > state['p'].seg_thr im_show[:, :, 2] = mask * 255 + (1 - mask) * im_show[:, :, 2] location_int = np.int0(location) cv2.polylines(im_show, [location_int.reshape((-1, 1, 2))], True, (0, 255, 255), 3) else: location = [int(l) for l in location] cv2.rectangle( im_show, (location[0], location[1]), (location[0] + location[2], location[1] + location[3]), (0, 255, 255), 3) cv2.putText(im_show, str(f), (40, 40), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 255), 2) cv2.putText(im_show, str(lost_times), (40, 80), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 2) cv2.imshow(video['name'], im_show) cv2.waitKey(1) toc /= cv2.getTickFrequency() # save result name = args.arch.split('.')[0] + '_' + ('mask_' if mask_enable else '') + ('refine_' if refine_enable else '') +\ args.resume.split('/')[-1].split('.')[0] if 'VOT' in args.dataset: video_path = join('test', args.dataset, name, 'baseline', video['name']) if not isdir(video_path): makedirs(video_path) result_path = join(video_path, '{:s}_001.txt'.format(video['name'])) with open(result_path, "w") as fin: for x in regions: fin.write("{:d}\n".format(x)) if isinstance(x, int) else \ fin.write(','.join([vot_float2str("%.4f", i) for i in x]) + '\n') else: # OTB video_path = join('test', args.dataset, name) if not isdir(video_path): makedirs(video_path) result_path = join(video_path, '{:s}.txt'.format(video['name'])) with open(result_path, "w") as fin: for x in regions: fin.write(','.join([str(i) for i in x]) + '\n') logger.info( '({:d}) Video: {:12s} Time: {:02.1f}s Speed: {:3.1f}fps Lost: {:d}'. format(v_id, video['name'], toc, f / toc, lost_times)) return lost_times, f / toc
def camshift_tracker(v, file_name): # Open output file output_name = sys.argv[3] + file_name output = open(output_name, "w") frameCounter = 0 # read first frame ret, frame = v.read() if ret == False: return # detect face in first frame c, r, w, h = detect_one_face(frame) pt_x, pt_y = c + w / 2, r + h / 2 # Write track point for first frame output.write("%d,%d,%d\n" % (0, pt_x, pt_y)) # Write as 0,pt_x,pt_y frameCounter = frameCounter + 1 # set the initial tracking window track_window = (c, r, w, h) # calculate the HSV histogram in the window # NOTE: you do not need this in the Kalman or OF trackers roi_hist = hsv_histogram_for_window( frame, (c, r, w, h)) # this is provided for you # Setup the termination criteria, either 10 iteration or move by atleast 1 pt term_crit = (cv2.TERM_CRITERIA_EPS | cv2.TERM_CRITERIA_COUNT, 10, 1) while (1): ret, frame = v.read() # read another frame if ret == False: break # perform the tracking # e.g. cv2.meanShift, cv2.CamShift, or kalman.predict(), kalman.correct() # use the tracking result to get the tracking point (pt): # if you track a rect (e.g. face detector) take the mid point, # if you track particles - take the weighted average # the Kalman filter already has the tracking point in the state vector hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV) dst = cv2.calcBackProject([hsv], [0], roi_hist, [0, 180], 1) # apply meanshift to get the new location ret, track_window = cv2.CamShift(dst, track_window, term_crit) # Draw it on image pts = cv2.boxPoints(ret) pt_x = (pts[0][0] + pts[2][0]) / 2 pt_y = (pts[0][1] + pts[2][1]) / 2 # Uncomment to see face tracking using Camshift method. pts = np.int0(pts) img2 = cv2.polylines(frame, [pts], True, 255, 2) cv2.imshow('Camshift', img2) k = cv2.waitKey(60) & 0xff if k == 27: break else: cv2.imwrite(chr(k) + ".jpg", img2) # write the result to the output file output.write( "%d,%d,%d\n" % (frameCounter, pt_x, pt_y)) # Write as frame_index,pt_x,pt_y frameCounter = frameCounter + 1 output.close()
import numpy as np import cv2 img = cv2.imread("games.jpg", cv2.IMREAD_COLOR) # cv2.line(img,(0,0),(150,150),(255,255,255), 15) # cv2.rectangle(img,(15,25),(200,150),(0,255,0),5) # cv2.circle(img,(100,63),55,(0,0,255),-1) pts = np.array([[10, 5], [20, 30], [70, 20], [50, 10]], np.int32) #pts = pts.reshape((-1,1,2)) cv2.polylines(img, [pts], True, (0, 255, 255), 3) font = cv2.FONT_HERSHEY_SIMPLEX cv2.putText(img, "OpenCv Tuts!", (0, 130), font, 1, (200, 255, 255), 2, cv2.LINE_AA) cv2.imshow("image", img) cv2.waitKey(0) cv2.destroyAllWindows()
def polylines(im, pts, color=[0, 200, 0], thickness=5, isClosed=True): return cv2.polylines(im, pts=np.array([pts], dtype=np.int32), isClosed=isClosed, color=color, thickness=thickness)
def main(argv=None): import os os.environ['CUDA_VISIBLE_DEVICES'] = FLAGS.gpu_list try: os.makedirs(FLAGS.output_dir) except OSError as e: if e.errno != 17: raise with tf.get_default_graph().as_default(): input_images = tf.placeholder(tf.float32, shape=[None, None, None, 3], name='input_images') global_step = tf.get_variable('global_step', [], initializer=tf.constant_initializer(0), trainable=False) f_score, f_geometry = model.model(input_images, is_training=False) variable_averages = tf.train.ExponentialMovingAverage( 0.997, global_step) saver = tf.train.Saver(variable_averages.variables_to_restore()) with tf.Session(config=tf.ConfigProto( allow_soft_placement=True)) as sess: ckpt_state = tf.train.get_checkpoint_state(FLAGS.checkpoint_path) model_path = os.path.join( FLAGS.checkpoint_path, os.path.basename(ckpt_state.model_checkpoint_path)) print('Restore from {}'.format(model_path)) saver.restore(sess, model_path) tf.train.write_graph(sess.graph_def, '.', 'expanded_convs.pbtxt') average_time = [[], [], []] im_fn_list = get_images() for im_fn in im_fn_list: im = cv2.imread(im_fn)[:, :, ::-1] start_time = time.time() im_resized, (ratio_h, ratio_w) = resize_image(im) timer = {'net': 0, 'restore': 0, 'nms': 0} start = time.time() score, geometry = sess.run( [f_score, f_geometry], feed_dict={input_images: [im_resized]}) timer['net'] = time.time() - start boxes, timer = detect(score_map=score, geo_map=geometry, timer=timer) print( '{} : net {:.0f}ms, restore {:.0f}ms, nms {:.0f}ms'.format( im_fn, timer['net'] * 1000, timer['restore'] * 1000, timer['nms'] * 1000)) average_time[0].append(timer['net'] * 1000) average_time[1].append(timer['restore'] * 1000) average_time[2].append(timer['nms'] * 1000) if boxes is not None: boxes = boxes[:, :8].reshape((-1, 4, 2)) boxes[:, :, 0] /= ratio_w boxes[:, :, 1] /= ratio_h duration = time.time() - start_time print('[timing] {}'.format(duration)) # save to file if boxes is not None: res_file = os.path.join( FLAGS.output_dir, '{}.txt'.format(os.path.basename(im_fn).split('.')[0])) with open(res_file, 'w') as f: for box in boxes: # to avoid submitting errors box = sort_poly(box.astype(np.int32)) if np.linalg.norm(box[0] - box[1]) < 5 or np.linalg.norm( box[3] - box[0]) < 5: continue f.write('{},{},{},{},{},{},{},{}\r\n'.format( box[0, 0], box[0, 1], box[1, 0], box[1, 1], box[2, 0], box[2, 1], box[3, 0], box[3, 1], )) cv2.polylines( im[:, :, ::-1], [box.astype(np.int32).reshape((-1, 1, 2))], True, color=(255, 255, 0), thickness=1) if not FLAGS.no_write_images: img_path = os.path.join(FLAGS.output_dir, os.path.basename(im_fn)) cv2.imwrite(img_path, im[:, :, ::-1]) avg_time = np.mean(average_time, axis=1) print('average time: net {:.0f}ms, restore {:.0f}ms, nms {:.0f}ms'. format(avg_time[0], avg_time[1], avg_time[2]))
def main(): """ This functions loads the target surface image, """ homography = None camera_parameters = mtx # got after doing the caliberation # camera_parameters = np.array([[800, 0, 320], [0, 800, 240], [0, 0, 1]]) # create ORB keypoint detector sift = cv2.xfeatures2d.SIFT_create() # create BFMatcher object based on hamming distance bf = cv2.BFMatcher() # load the reference surface that will be searched in the video stream dir_name = os.getcwd() marker1 = cv2.imread( os.path.join(dir_name, 'reference/markers/marker1.jpg'), 0) marker2 = cv2.imread( os.path.join(dir_name, 'reference/markers/marker4.jpg'), 0) # Compute marker keypoints and its descriptors kp_marker1 = sift.detect(marker1, None) kp_marker1, des_marker1 = sift.compute(marker1, kp_marker1) kp_marker2 = sift.detect(marker2, None) kp_marker2, des_marker2 = sift.compute(marker2, kp_marker2) # Load 3D model from OBJ file obj = OBJ(os.path.join(dir_name, 'models/fox.obj'), swapyz=True) # init video capture # cap = cv2.VideoCapture(0) cap = cv2.VideoCapture("./reference/videos/video4_1.mp4") start_time = -100 prev5 = np.ones((3, 3)) prev4 = np.ones((3, 3)) prev3 = np.ones((3, 3)) prev2 = np.ones((3, 3)) prev1 = np.ones((3, 3)) homography = np.ones((3, 3)) prev_5 = np.ones((3, 3)) prev_4 = np.ones((3, 3)) prev_3 = np.ones((3, 3)) prev_2 = np.ones((3, 3)) prev_1 = np.ones((3, 3)) homography_2 = np.ones((3, 3)) speed = 5 Identity = np.array([[1, 0, 0], [0, 1, 0], [0, 0, 1]]) unit_translation = np.array([[0, 0, 0], [0, 0, 1], [0, 0, 0]]) prev_trans = np.array([[0, 0, 0], [0, 0, 1], [0, 0, 0]]) inverse = False desMarker1 = des_marker1 desMarker2 = des_marker2 kpMarker1 = kp_marker1 kpMarker2 = kp_marker2 center1 = np.array([0, 0]) center2 = np.array([0, 0]) n_frame = 0 cv2.namedWindow("window", cv2.WND_PROP_FULLSCREEN) cv2.setWindowProperty("window", cv2.WND_PROP_FULLSCREEN, cv2.WINDOW_FULLSCREEN) while True: n_frame += 1 # read the current frame ret, frame = cap.read() if not ret: print("Unable to capture video") return # find and draw the keypoints of the frame kp_frame = sift.detect(frame, None) kp_frame, des_frame = sift.compute(frame, kp_frame) matches1 = bf.knnMatch(desMarker1, des_frame, k=2) matches2 = bf.knnMatch(desMarker2, des_frame, k=2) # match frame descriptors with model descriptors # sort them in the order of their distance # the lower the distance, the better the matc h good = [] for m in matches1: if m[0].distance < 0.75 * m[1].distance: good.append(m) matches1 = np.asarray(good) good = [] for m in matches2: if m[0].distance < 0.75 * m[1].distance: good.append(m) matches2 = np.asarray(good) # print(len(matches)) # compute Homography if enough matches are found if len(matches1) > MIN_MATCHES: # differenciate between source points and destination points src_pts = np.float32([ kpMarker1[m[0].queryIdx].pt for m in matches1 ]).reshape(-1, 1, 2) dst_pts = np.float32([ kp_frame[m[0].trainIdx].pt for m in matches1 ]).reshape(-1, 1, 2) # compute Homography prev5 = prev4 prev4 = prev3 prev3 = prev2 prev2 = prev1 prev1 = homography homography, mask = cv2.findHomography(src_pts, dst_pts, cv2.RANSAC, 5.0) try: avg_homography = (prev1 + prev2 + prev3 + prev4 + prev5 + homography) / 6.0 except: continue # avg_homography = homography if True: # Draw a rectangle that marks the found model in the frame h, w = marker1.shape pts = np.float32([[0, 0], [0, h - 1], [w - 1, h - 1], [w - 1, 0]]).reshape(-1, 1, 2) # project corners into frame dst1 = cv2.perspectiveTransform(pts, avg_homography) # connect them with lines frame = cv2.polylines(frame, [np.int32(dst1)], True, 255, 3, cv2.LINE_AA) # if a valid homography matrix was found render cube on model plane if homography is not None: try: # obtain 3D projection matrix from homography matrix and camera parameters # avg_homography = np.matmul(Identity,avg_homography) avg_homography = np.matmul( avg_homography, Identity + prev_trans + unit_translation * speed) prev_trans = prev_trans + unit_translation * speed dst1 = cv2.perspectiveTransform(pts, avg_homography) center1 = (dst1[0] + dst1[1] + dst1[2] + dst1[3]) / 4 # img coordinates frame = cv2.polylines(frame, [np.int32(dst1)], True, 255, 3, cv2.LINE_AA) # frame = cv2.circle(frame, [np.int32(center)], True, 255, 3, cv2.LINE_AA) projection = projection_matrix(camera_parameters, avg_homography) # project cube or model frame = render(frame, obj, projection, marker1, False) #frame = render(frame, model, projection) except Exception as e: print(e) # draw first 10 matches1. else: print("Not enough matches found - %d/%d" % (len(matches1), MIN_MATCHES)) if len(matches2) > MIN_MATCHES: # differenciate between source points and destination points src_pts = np.float32([ kpMarker2[m[0].queryIdx].pt for m in matches2 ]).reshape(-1, 1, 2) dst_pts = np.float32([ kp_frame[m[0].trainIdx].pt for m in matches2 ]).reshape(-1, 1, 2) # compute Homography prev_5 = prev_4 prev_4 = prev_3 prev_3 = prev_2 prev_2 = prev_1 prev_1 = homography_2 homography_2, mask = cv2.findHomography(src_pts, dst_pts, cv2.RANSAC, 5.0) try: avg_homography_2 = (prev_1 + prev_2 + prev_3 + prev_4 + prev_5 + homography_2) / 6.0 except: continue # avg_homography = homography if True: # Draw a rectangle that markcv2.imshow('frame', frame) h, w = marker2.shape pts = np.float32([[0, 0], [0, h - 1], [w - 1, h - 1], [w - 1, 0]]).reshape(-1, 1, 2) # project corners into frame dst2 = cv2.perspectiveTransform(pts, avg_homography_2) # Printing on Frame if ((time.time() - start_time) < 0.8): cv2.putText(frame, "Reached Destination!", (50, 50), cv2.FONT_HERSHEY_COMPLEX, .7, (200, 255, 0)) elif ((time.time() - start_time) < 1.4): cv2.putText(frame, "What to do ???", (50, 50), cv2.FONT_HERSHEY_COMPLEX, .7, (200, 255, 0)) elif ((time.time() - start_time) < 2.3): cv2.putText(frame, "I better go back!", (50, 50), cv2.FONT_HERSHEY_COMPLEX, .7, (200, 255, 0)) if (point_inside(center1, dst2) and not inverse): print("Reached destination !!") print("What to do ????") print("Better I go back ...") inverse = True prev_trans *= 0 kpMarker1 = kp_marker2 kpMarker2 = kp_marker1 desMarker1 = des_marker2 desMarker2 = des_marker1 start_time = time.time() continue if (point_inside(center1, dst2) and inverse): print("Reached destination !!") # inverse = not inverse print("What to do ????") # prev_trans*=-1 print("Better I go back ...") inverse = False prev_trans *= 0 kpMarker1 = kp_marker1 kpMarker2 = kp_marker2 desMarker1 = des_marker1 desMarker2 = des_marker2 start_time = time.time() continue # connect them with lines frame = cv2.polylines(frame, [np.int32(dst2)], True, 255, 3, cv2.LINE_AA) cv2.imshow("window", frame) if cv2.waitKey(1) & 0xFF == ord('q'): break else: print("Not enough matches found - %d/%d" % (len(matches2), MIN_MATCHES)) cap.release() cv2.destroyAllWindows() return 0
if hist_group: frames = [] for gt in hist_group: frame, instance, left, top, width, height, conf = gt[0:7] frames.append(int(frame)) top_left = [left, top] top_right = [left + width, top] bottom_right = [left + width, top + height] bottom_left = [left, top + height] text_centre = (int(left) + 2, int(top + height) - 2) pts = np.array([top_left, top_right, bottom_right, bottom_left], np.int32) pts = pts.reshape((-1, 1, 2)) cv2.polylines(image, [pts], True, get_colour(instance)) cv2.putText(image, str(int(instance)), text_centre, font, 0.6, get_colour(instance), 1, cv2.LINE_AA) print('Frames plotted: {}'.format(str(frames))) except: print('{} not available, creating blank image w:{} h:{}'.format(history, w, h)) image = np.zeros((w, h, 1), np.uint8) pass images.append(image) image_num -= 1 h0, w0, c0 = images[0].shape h1, w1, c1 = images[1].shape h2, w2, c2 = images[2].shape
def run(self): color = np.random.randint(0,255,(100,3)) out = cv.VideoWriter('result.avi', \ cv.VideoWriter_fourcc('M','J','P','G'), 30, \ (self.frameWidth,self.frameHeight)) while True: ret, frame = self.cam.read() if ret==True: #frameNew = imutils.resize(frame, width=500) vis = frame.copy() layer = np.zeros_like(frame, dtype = "uint8") frame_gray = cv.cvtColor(frame, cv.COLOR_BGR2GRAY) if len(self.tracks) > 0: img0, img1 = self.prev_gray, frame_gray p0 = np.float32([tr[-1] for tr in self.tracks]).reshape\ (-1, 1, 2) p1, _st, _err = cv.calcOpticalFlowPyrLK(img0, img1, p0, \ None, **lk_params) p0r, _st, _err = cv.calcOpticalFlowPyrLK(img1, img0, p1, \ None, **lk_params) d = abs(p0-p0r).reshape(-1, 2).max(-1) good = d < 1 new_tracks = [] for tr, (x, y), good_flag in zip(self.tracks, \ p1.reshape(-1, 2), good): if not good_flag: continue tr.append((x, y)) if len(tr) > self.track_len: del tr[0] new_tracks.append(tr) cv.circle(layer, (x, y), 2, (255,255,255), -1) cv.circle(layer, (x,y), 1, (0, 255, 0), -1) self.tracks = new_tracks for i in range (10): cv.polylines(layer,[np.int32(tr) for tr in self.tracks]\ , False, color[i].tolist()) if self.frame_idx % self.detect_interval == 0: mask = np.zeros_like(frame_gray) mask[:] = 255 for x, y in [np.int32(tr[-1]) for tr in self.tracks]: cv.circle(mask, (x, y), 5, 0, -1) p = cv.goodFeaturesToTrack(frame_gray, mask = mask, \ **feature_params) if p is not None: for x, y in np.float32(p).reshape(-1, 2): self.tracks.append([(x, y)]) self.frame_idx += 1 self.prev_gray = frame_gray #out.write(layer) cv.imshow('Animation', layer) cv.imshow('Original', frame) ch = cv.waitKey(1) if ch == ord('q'): break self.cam.release() cv.destroyAllWindows()
def main(): start_time = time.time() # load config cfg.merge_from_file(args.config) cur_dir = os.path.dirname(os.path.realpath(__file__)) dataset_root = os.path.join(cur_dir, '../testing_dataset', args.dataset) # create model model = ModelBuilder() # load model model = load_pretrain(model, args.snapshot).cuda().eval() # build tracker tracker = build_tracker(model) # create dataset dataset = DatasetFactory.create_dataset(name=args.dataset, dataset_root=dataset_root, load_img=False) model_name = args.snapshot.split('/')[-1].split('.')[0] total_lost = 0 if args.dataset in ['VOT2016', 'VOT2018', 'VOT2019']: # restart tracking for v_idx, video in enumerate(dataset): if args.video != '': # test one special video if video.name != args.video: continue frame_counter = 0 lost_number = 0 toc = 0 pred_bboxes = [] for idx, (img, gt_bbox) in enumerate(video): if len(gt_bbox) == 4: gt_bbox = [ gt_bbox[0], gt_bbox[1], gt_bbox[0], gt_bbox[1] + gt_bbox[3] - 1, gt_bbox[0] + gt_bbox[2] - 1, gt_bbox[1] + gt_bbox[3] - 1, gt_bbox[0] + gt_bbox[2] - 1, gt_bbox[1] ] tic = cv2.getTickCount() if idx == frame_counter: cx, cy, w, h = get_axis_aligned_bbox(np.array(gt_bbox)) gt_bbox_ = [cx - (w - 1) / 2, cy - (h - 1) / 2, w, h] tracker.init(img, gt_bbox_) pred_bbox = gt_bbox_ pred_bboxes.append(1) elif idx > frame_counter: outputs = tracker.track(img) pred_bbox = outputs['bbox'] if cfg.MASK.MASK: pred_bbox = outputs['polygon'] overlap = vot_overlap(pred_bbox, gt_bbox, (img.shape[1], img.shape[0])) if overlap > 0: # not lost pred_bboxes.append(pred_bbox) else: # lost object pred_bboxes.append(2) frame_counter = idx + 5 # skip 5 frames lost_number += 1 else: pred_bboxes.append(0) toc += cv2.getTickCount() - tic if idx == 0: cv2.destroyAllWindows() if args.vis and idx > frame_counter: cv2.polylines( img, [np.array(gt_bbox, np.int).reshape( (-1, 1, 2))], True, (0, 255, 0), 3) if cfg.MASK.MASK: cv2.polylines( img, [np.array(pred_bbox, np.int).reshape( (-1, 1, 2))], True, (0, 255, 255), 3) else: bbox = list(map(int, pred_bbox)) cv2.rectangle(img, (bbox[0], bbox[1]), (bbox[0] + bbox[2], bbox[1] + bbox[3]), (0, 255, 255), 3) cv2.putText(img, str(idx), (40, 40), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 255), 2) cv2.putText(img, str(lost_number), (40, 80), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 2) cv2.imshow(video.name, img) cv2.waitKey(1) toc /= cv2.getTickFrequency() # save results video_path = os.path.join('results', args.dataset, model_name, 'baseline', video.name) if not os.path.isdir(video_path): os.makedirs(video_path) result_path = os.path.join(video_path, '{}_001.txt'.format(video.name)) with open(result_path, 'w') as f: for x in pred_bboxes: if isinstance(x, int): f.write("{:d}\n".format(x)) else: f.write(','.join([vot_float2str("%.4f", i) for i in x]) + '\n') print( '({:3d}) Video: {:12s} Time: {:4.1f}s Speed: {:3.1f}fps Lost: {:d}' .format(v_idx + 1, video.name, toc, idx / toc, lost_number)) total_lost += lost_number print("{:s} total lost: {:d}".format(model_name, total_lost)) else: # OPE tracking for v_idx, video in enumerate(dataset): if args.video != '': # test one special video if video.name != args.video: continue toc = 0 pred_bboxes = [] scores = [] track_times = [] for idx, (img, gt_bbox) in enumerate(video): tic = cv2.getTickCount() if idx == 0: cx, cy, w, h = get_axis_aligned_bbox(np.array(gt_bbox)) gt_bbox_ = [cx - (w - 1) / 2, cy - (h - 1) / 2, w, h] tracker.init(img, gt_bbox_) pred_bbox = gt_bbox_ scores.append(None) if 'VOT2018-LT' == args.dataset: pred_bboxes.append([1]) else: pred_bboxes.append(pred_bbox) else: outputs = tracker.track(img) pred_bbox = outputs['bbox'] pred_bboxes.append(pred_bbox) scores.append(outputs['best_score']) toc += cv2.getTickCount() - tic track_times.append( (cv2.getTickCount() - tic) / cv2.getTickFrequency()) if idx == 0: cv2.destroyAllWindows() if args.vis and idx > 0: gt_bbox = list(map(int, gt_bbox)) pred_bbox = list(map(int, pred_bbox)) cv2.rectangle( img, (gt_bbox[0], gt_bbox[1]), (gt_bbox[0] + gt_bbox[2], gt_bbox[1] + gt_bbox[3]), (0, 255, 0), 3) cv2.rectangle(img, (pred_bbox[0], pred_bbox[1]), (pred_bbox[0] + pred_bbox[2], pred_bbox[1] + pred_bbox[3]), (0, 255, 255), 3) cv2.putText(img, str(idx), (40, 40), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 255), 2) cv2.imshow(video.name, img) cv2.waitKey(1) toc /= cv2.getTickFrequency() # save results if 'VOT2018-LT' == args.dataset: video_path = os.path.join('results', args.dataset, model_name, 'longterm', video.name) if not os.path.isdir(video_path): os.makedirs(video_path) result_path = os.path.join(video_path, '{}_001.txt'.format(video.name)) with open(result_path, 'w') as f: for x in pred_bboxes: f.write(','.join([str(i) for i in x]) + '\n') result_path = os.path.join( video_path, '{}_001_confidence.value'.format(video.name)) with open(result_path, 'w') as f: for x in scores: f.write('\n') if x is None else f.write( "{:.6f}\n".format(x)) result_path = os.path.join(video_path, '{}_time.txt'.format(video.name)) with open(result_path, 'w') as f: for x in track_times: f.write("{:.6f}\n".format(x)) elif 'GOT-10k' == args.dataset: video_path = os.path.join('results', args.dataset, model_name, video.name) if not os.path.isdir(video_path): os.makedirs(video_path) result_path = os.path.join(video_path, '{}_001.txt'.format(video.name)) with open(result_path, 'w') as f: for x in pred_bboxes: f.write(','.join([str(i) for i in x]) + '\n') result_path = os.path.join(video_path, '{}_time.txt'.format(video.name)) with open(result_path, 'w') as f: for x in track_times: f.write("{:.6f}\n".format(x)) else: model_path = os.path.join('results', args.dataset, model_name) if not os.path.isdir(model_path): os.makedirs(model_path) result_path = os.path.join(model_path, '{}.txt'.format(video.name)) with open(result_path, 'w') as f: for x in pred_bboxes: f.write(','.join([str(i) for i in x]) + '\n') print('({:3d}) Video: {:12s} Time: {:5.1f}s Speed: {:3.1f}fps'. format(v_idx + 1, video.name, toc, idx / toc)) elapsed_time = time.time() - start_time print(time.strftime("%H:%M:%S", time.gmtime(elapsed_time)))
print str(i.getX()), ',', str(i.getY()) """ cv2.putText(frame, str(i.getId()), (i.getX(), i.getY()), font, 0.3, i.getRGB(), 1, cv2.LINE_AA) ############### ## IMAGE ## ############### # str_up = 'UP: ' + str(cnt_up) MTR_up = 'Up Motor: ' + str(UpMTR) LV_up = 'Up Mobil: ' + str(UpLV) HV_up = 'Up Truck/Bus: ' + str(UpHV) # str_down = 'DOWN: ' + str(cnt_down) LV_down = 'Down Mobil: ' + str(DownLV) HV_down = 'Down Truck/Bus: ' + str(DownHV) frame = cv2.polylines(frame, [pts_L1], False, line_down_color, thickness=2) frame = cv2.polylines(frame, [pts_L2], False, line_up_color, thickness=2) frame = cv2.polylines(frame, [pts_L3], False, (255, 255, 255), thickness=1) frame = cv2.polylines(frame, [pts_L4], False, (255, 255, 255), thickness=1) # cv2.putText(frame, str_up, (10,40),font,2,(255,255,255),2,cv2.LINE_AA) # cv2.putText(frame, str_up, (10,40),font,2,(0,0,255),1,cv2.LINE_AA) cv2.putText(frame, MTR_up, (10, 40), font, 2, (255, 255, 255), 4, cv2.LINE_AA) cv2.putText(frame, MTR_up, (10, 40), font, 2, (0, 0, 255), 3, cv2.LINE_AA) cv2.putText(frame, LV_up, (10, 90), font, 2, (255, 255, 255), 4, cv2.LINE_AA) cv2.putText(frame, LV_up, (10, 90), font, 2, (0, 0, 255), 3, cv2.LINE_AA) cv2.putText(frame, HV_up, (10, 140), font, 2, (255, 255, 255), 4, cv2.LINE_AA) cv2.putText(frame, HV_up, (10, 140), font, 2, (0, 0, 255), 3, cv2.LINE_AA)
import cv2 import numpy as np from pyzbar.pyzbar import decode img = cv2.imread('qrcode.png') # code = decode(img) # print(code) for barcode in decode(img): print(barcode.data) myData = barcode.data.decode('utf-8') print(myData) pts = np.array([barcode.polygon], np.int32) pts = pts.reshape((-1, 1, 2)) cv2.polylines(img, [pts], True, (255, 0, 255), 5) pts2 = barcode.rect cv2.putText(img, myData, (pts2[0], pts2[1]), cv2.FONT_HERSHEY_SIMPLEX, 0.9, (255, 0, 255), 2) cv2.imshow('Result', img) cv2.waitKey(0) ############################################## ##### Part 1 ###### ############################################## # import cv2 # import numpy as np
def detectPartyVote(self, img, img_0, path, mutex, arr, pref_votes): # Grayscale gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) # Reduce noise gray_erode = cv2.fastNlMeansDenoising(gray, None, 18, 7, 21) # Reduce noise using Gaussian blur gray = cv2.GaussianBlur(gray, (5, 5), 0) # Erode the image gray_erode = cv2.erode(gray_erode.copy(), np.ones((5, 5), np.uint8), iterations=1) # Detect edges cannyout = cv2.Canny(gray_erode, 50, 100, 5) # Find contours im2, contours, hierarchy = cv2.findContours(cannyout.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) # Select contours with convexity and four point contours = [ contour for contour in contours if not cv2.isContourConvex(contour) and len( cv2.approxPolyDP(contour, 0.02 * cv2.arcLength(contour, True), True)) == 4 ] sum_x = 0 count = 0 dst_centroids = [] if len(path) > 0: for file in os.listdir(path): count += 1 if file.endswith('.jpg'): filepath = path + '/' + file if len(filepath) > 0: # ------------------<Party sign>------------------------------- template_ori = cv2.imread(filepath, 0) # template = cv2.bilateralFilter(template_ori,9,75,75) # template = cv2.medianBlur(template_ori,5) # template = cv2.fastNlMeansDenoising(template_ori,None,10,7,21) template = cv2.GaussianBlur(template_ori, (5, 5), 0) # thresh, template = cv2.threshold(template, 200, 255, cv2.THRESH_BINARY) # ------------------</Party sign>------------------------------- # ------------------<BRISK descriptors>--------------------------- detector = cv2.BRISK_create(10, 1) kp1, des1 = detector.detectAndCompute(gray, None) kp2, des2 = detector.detectAndCompute(template, None) bf = cv2.BFMatcher(cv2.NORM_HAMMING, crossCheck=True) matches = bf.match(des1, des2) matches = sorted(matches, key=lambda x: x.distance) distances = [match.distance for match in matches] min_dist = min(distances) avg_dist = sum(distances) / len(distances) min_tolerance = 10 min_dist = min_dist or avg_dist * 1.0 / min_tolerance good_matches = [ match for match in matches if match.distance <= min_dist * min_tolerance ] ballot_matched_points = np.array( [kp1[match.queryIdx].pt for match in good_matches]) party_matched_points = np.array( [kp2[match.trainIdx].pt for match in good_matches]) # ------------------<Find homography>--------------------------- homography, h_mask = cv2.findHomography( party_matched_points, ballot_matched_points, cv2.RANSAC, 2.0) h, w = template_ori.shape[0:2] sh, sw = img.shape[0:2] pts = np.array([(0, 0), (w, 0), (w, h), (0, h)], dtype=np.float32) dst = cv2.perspectiveTransform(pts.reshape(1, -1, 2), homography).reshape( -1, 2) img_0 = cv2.polylines(img_0, [np.int32(dst)], True, (0, 255, 0), 3, cv2.LINE_AA) # ------------------</Find homography>--------------------------- # im3 = cv2.drawMatches(img, kp1, template, kp2, good_matches, None, flags=2) # ------------------</BRISK descriptors>--------------------------- template_ori = Image.fromarray(template_ori) # template_edges = Image.fromarray(template_edges) template_ori = ImageTk.PhotoImage(template_ori) # template_edges = ImageTk.PhotoImage(template_edges) cent_x = ((dst[1][0] + dst[0][0]) / 2 + (dst[2][0] + dst[3][0]) / 2) / 2 cent_y = ((dst[1][1] + dst[0][1]) / 2 + (dst[2][1] + dst[3][1]) / 2) / 2 dst_centroids.append( [template_ori, cent_x, cent_y, str(file)]) sum_x += (dst[1][0] + dst[2][0]) / 2 # print(str(cent_x) + ',' + str(cent_y)) dst_centroids = sorted(dst_centroids, key=lambda x: x[2]) avg_x = sum_x / (count - 1) # print(avg_x) mc = [] rect = [] for contour in contours: M = cv2.moments(contour) if M['m00'] != 0: cx = int(M['m10'] / M['m00']) cy = int(M['m01'] / M['m00']) if cx > avg_x: # print(str((dst[1][1] + dst[2][1]) / 2) + ',' + str(cy)) mc.append(contour) x, y, w, h = cv2.boundingRect(contour) rect.append([x, y, w, h]) cv2.rectangle(img_0, (x, y), (x + w, y + h), (0, 0, 255), 3) count = 0 t_im = None t_vote = None voted = False i = 0 for x, y, w, h in rect: cx = x + w / 2 cy = y + h / 2 rectangle = img[y:y + h, x:x + w] rect_gray = cv2.cvtColor(rectangle, cv2.COLOR_BGR2GRAY) rect_dilate = cv2.fastNlMeansDenoising(rect_gray, None, 18, 7, 21) rect_dilate = cv2.erode(rect_dilate.copy(), np.ones((5, 5), np.uint8), iterations=1) rect_canny = cv2.Canny(rect_dilate, 50, 100, 5) im, cnts, hier = cv2.findContours(rect_canny.copy(), cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) im = rectangle.copy() voted = False for cnt in cnts: hull = cv2.convexHull(cnt, returnPoints=False) defects = cv2.convexityDefects(cnt, hull) if defects is not None and len(defects) > 5: for i in range(defects.shape[0]): s, e, f, d = defects[i, 0] start = tuple(cnt[s][0]) end = tuple(cnt[e][0]) far = tuple(cnt[f][0]) cv2.line(im, start, end, [0, 255, 0], 2) cv2.circle(im, far, 5, [0, 0, 255], -1) voted = True if voted: count += 1 # For Tkinter t_vote = Image.fromarray(rectangle) t_vote = ImageTk.PhotoImage(t_vote) # print(voted) # print(count) # cv2.drawContours(im,cnts,-1,(0,0,255),3) # cv2.imshow('box'+str(i),im) i += 1 if count == 1: print('Voted:' + str(count)) min_cent = dst_centroids[0] t_im = dst_centroids[0][0] minimum_dist = np.sqrt( np.power((dst_centroids[0][1] - cx), 2) + np.power((dst_centroids[0][2] - cy), 2)) i = 0 for temp_image, x, y, file in dst_centroids: d = np.sqrt(np.power((x - cx), 2) + np.power((y - cy), 2)) if d < minimum_dist: minimum_dist = d t_im = temp_image min_cent = dst_centroids[i] i += 1 print(min_cent) arr[min_cent[3]][1] += 1 mutex.acquire() for a in pref_votes: if str(a) in arr[min_cent[3]][0]: arr[min_cent[3]][0][str(a)] += 1 else: arr[min_cent[3]][0][str(a)] = 1 # self.prg_bar.step(increment) mutex.release() print(arr) if self.panelA is None or self.panelB is None: panelA = Label(self.topframe, image=t_im) panelA.image = t_im panelA.pack(padx=10, pady=10) panelA.grid(row=0, column=0, padx=10, pady=10) panelB = Label(self.topframe, image=t_vote) panelB.image = t_vote panelB.pack(padx=10, pady=10) panelB.grid(row=0, column=1, padx=10, pady=10) else: self.panelA.configure(image=t_im) self.panelB.configure(image=t_vote) self.panelA.image = t_im self.panelB.image = t_vote else: print('invalid vote')
_min_area=1000, _max_area=3500, _max_variation=0.1) for i in range(1, 1939): #Your image path i-e receipt path img = cv2.imread('/media/hai/AIDL-USTC/USTCPro/original/' + str(i) + '.jpeg') cropimg = img[300:700, 250:1150] #Convert to gray scale gray = cv2.cvtColor(cropimg, cv2.COLOR_BGR2GRAY) vis = cropimg.copy() #detect regions in gray scale image regions, _ = mser.detectRegions(gray) hulls = [cv2.convexHull(p.reshape(-1, 1, 2)) for p in regions] cv2.polylines(vis, hulls, 1, (0, 0, 0), 3) cv2.imwrite('/media/hai/AIDL-USTC/USTCPro/disposed/' + str(i) + '.jpeg', vis) cv2.imshow('img', vis) cv2.waitKey(1000)
# Run video stream vs = cv2.VideoCapture(2) time.sleep(2) # Take image and setup board outline with mouse cv2.namedWindow('Board Setup') cv2.setMouseCallback('Board Setup', set_points) board_corners = np.array([None, None, None, None]) # tl, tr, br, bl _, img = vs.read() while not all(board_corners): # Draw board so far frame = process_frame(img, camera_parameters) assigned_points = np.array([p for p in board_corners if p is not None]) if len(assigned_points) > 0: frame = cv2.polylines(frame, [assigned_points], True, (255, 0, 0), 2) # Show frame cv2.imshow('Board Setup', frame) # Handle exit k = cv2.waitKey(10) if k & 0xFF == ord("q"): cv2.destroyAllWindows() break cv2.destroyAllWindows() # Finalise board corners board_corners = np.array([p for p in board_corners if p is not None]) # Calculate rest of board spec
def detectPartyImage(self, img, template): # panelA.grid_forget() img_0 = img.copy() # -------------------<Ballot paper>---------------------------- # Grayscale gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) # Apply blur to reduce noise gray = cv2.GaussianBlur(gray, (5, 5), 0) # Detect edges cannyout = cv2.Canny(gray, 50, 100, 5) # Find contours im2, contours, hierarchy = cv2.findContours(cannyout.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) # Select the contours with convexity and four points contours = [ contour for contour in contours if not cv2.isContourConvex(contour) and len( cv2.approxPolyDP(contour, 0.02 * cv2.arcLength(contour, True), True)) == 4 ] # contours = sorted(contours, key=lambda x: (cv2.contourArea(x)), reverse=True) # ------------------</Ballot paper>---------------------------- # ------------------<Party sign>------------------------------- # Read the image # template_ori = cv2.imread(path, 0) template_ori = template # template = cv2.bilateralFilter(template_ori,9,75,75) # template = cv2.medianBlur(template_ori,5) # template = cv2.fastNlMeansDenoising(template_ori,None,10,7,21) # Apply blur to reduce noise template = cv2.GaussianBlur(template_ori, (5, 5), 0) # thresh, template = cv2.threshold(template, 200, 255, cv2.THRESH_BINARY) # Detect edges template_edges = cv2.Canny(template, 100, 200, 10) # ------------------</Party sign>------------------------------- # ------------------<BRISK descriptors>--------------------------- # Create the BRISK descriptor detector detector = cv2.BRISK_create(10, 1) # Compute descriptors and keypoints in image and template kp1, des1 = detector.detectAndCompute(gray, None) kp2, des2 = detector.detectAndCompute(template, None) # Create the matcher bf = cv2.BFMatcher(cv2.NORM_HAMMING, crossCheck=True) # Match the descriptors matches = bf.match(des1, des2) # Sort matches by distances matches = sorted(matches, key=lambda x: x.distance) # Compute the distances for matches distances = [match.distance for match in matches] # Min distance min_dist = min(distances) # Average distance avg_dist = sum(distances) / len(distances) # Define tolerance min_tolerance = 10 # Compute the new min distance min_dist = min_dist or avg_dist * 1.0 / min_tolerance # Select the good matches based on the min distance good_matches = [ match for match in matches if match.distance <= min_dist * min_tolerance ] # Get matched points in the image and the symbol ballot_matched_points = np.array( [kp1[match.queryIdx].pt for match in good_matches]) party_matched_points = np.array( [kp2[match.trainIdx].pt for match in good_matches]) # ------------------<Find homography>--------------------------- # Find homography homography, h_mask = cv2.findHomography(party_matched_points, ballot_matched_points, cv2.RANSAC, 2.0) h, w = template_ori.shape[0:2] sh, sw = img.shape[0:2] pts = np.array([(0, 0), (w, 0), (w, h), (0, h)], dtype=np.float32) # Perspective transformation using homography dst = cv2.perspectiveTransform(pts.reshape(1, -1, 2), homography).reshape(-1, 2) # print(dst) # Draw lines in the image img_0 = cv2.polylines(img_0, [np.int32(dst)], True, (0, 255, 0), 3, cv2.LINE_AA) # ------------------</Find homography>--------------------------- # im3 = cv2.drawMatches(img, kp1, template, kp2, good_matches, None, flags=2) # ------------------</BRISK descriptors>--------------------------- # Resize the image im3 = cv2.resize(img_0, (0, 0), fx=0.25, fy=0.25) # -------------------<Image display processing>------------------- # This is for Tkinter gui template_ori = Image.fromarray(template_ori) template_edges = Image.fromarray(template_edges) template_ori = ImageTk.PhotoImage(template_ori) template_edges = ImageTk.PhotoImage(template_edges) # -------------------</Image display processing>------------------- # Update panels if self.panelA is None or self.panelB is None: self.panelA = Label(self.topframe, image=template_ori) self.panelA.image = template_ori self.panelA.pack(side='left', padx=10, pady=10) self.panelA.grid(row=0, column=0, padx=10, pady=10) self.panelB = Label(self.topframe, image=template_edges) self.panelB.image = template_edges self.panelB.pack(side='left', padx=10, pady=10) self.panelB.grid(row=0, column=1, padx=10, pady=10) else: # panelA.grid_forget() # panelB.grid_forget() self.panelA.configure(image=template_ori) self.panelB.configure(image='') self.panelB.configure(image=template_edges) self.panelA.image = template_ori self.panelB.image = template_edges
xmax = (int(float(x[idx * 2]) + radius)) ymax = (int(float(x[idx * 2 + 1]) + radius)) if xmin < 0: xmin = 0 if ymin < 0: ymin = 0 if xmax > 1300: xmax = 1300 if ymax > 1300: ymax = 1300 pts = np.array([[xmin, ymin], [xmin, ymax], [xmax, ymax], [xmax, ymin]]) pts = pts.reshape((-1, 1, 2)) cv2.polylines(img, [pts], True, (255), thickness=radius + 2) else: cv2.imwrite(image_name + '_nodes.png', img) #cv2.imshow('image',img) #cv2.waitKey(0) #cv2.destroyAllWindows() img = np.zeros((1300, 1300, 1), np.uint8) image_name = row[0] line = row[1].replace('LINESTRING (', '').replace(')', '').replace(',', '') x = line.split() for idx in range((int((len(x) / 2))) - 1): xmin = (int(float(x[idx * 2]) - radius)) ymin = (int(float(x[idx * 2 + 1]) - radius)) xmax = (int(float(x[idx * 2]) + radius))
def main(): fps = FPS().start() #writer = None cap = cv2.VideoCapture(Input_Video) YOLOINIT() ##========================================================= ##View 1 f_num = 0 Detecting_cnt_1 = 0 RED_cnt_1 = 0 BLUE_cnt_1 = 0 initBB_1 = None tracker_1 = None Detecting_cnt_2 = 0 RED_cnt_2 = 0 BLUE_cnt_2 = 0 initBB_2 = None tracker_2 = None while (cap.isOpened()): f_num = f_num + 1 print("F : ", f_num) (grabbed, frame) = cap.read() #cutImg = frame.copy() #cutImg = frame[300:1020, 360:1640] #frame = cutImg if f_num % 2 == 0 and f_num > 0: #====================================================== #Background Substraction #tracker 에 대해서 frame 원본이미지가 아니라, Background Substracted 된 영상에 트래커를 부착하여, 배경에 트래커가 남지 않도록 구현 gray_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) gray_frame = cv2.GaussianBlur(gray_frame, (5, 5), 0) difference = cv2.absdiff(first_gray, gray_frame) _, difference = cv2.threshold(difference, 25, 255, cv2.THRESH_BINARY) mask3 = cv2.cvtColor(difference, cv2.COLOR_GRAY2BGR) # 3 channel mask Substracted = cv2.bitwise_and(frame, mask3) #====================================================== layerOutputs, start, end = YOLO_Detect(frame) # 3.YOLO_BOX_INFO(layerOutputs,BaseConfidence,Base_threshold)) idxs, boxes, classIDs, confidences = YOLO_BOX_INFO( frame, layerOutputs, BaseConfidence, Base_threshold) # 4.검출된 화면의 X,Y 좌표 가져온다. # 검출됨 차량 수 만큼 좌표 가져옴 Vehicle_x = [] Vehicle_y = [] Vehicle_w = [] Vehicle_h = [] #차량 포인트 가져옴 Vehicle_x, Vehicle_y, Vehicle_w, Vehicle_h = Position( idxs, classIDs, boxes, Vehicle_x, Vehicle_y, Vehicle_w, Vehicle_h) #차량 포인트 그리기 Draw_Points(frame, Vehicle_x, Vehicle_y, Vehicle_w, Vehicle_h) #Parking Zone Counter #view1 (인식영역 Y축 +30 ,-30) #4개의 포인트 vertices = [[[600, 350], [600, 650], [1250, 650], [1150, 350]]] tracker_1, initBB_1, RED_cnt_1, BLUE_cnt_1 = Passing_Counter_Zone(Vehicle_x, Vehicle_y, Vehicle_w, Vehicle_h, initBB_1, frame, tracker_1, Substracted,\ RED_cnt_1, BLUE_cnt_1, vertices) # Red_Line cv2.line(frame, (vertices[0][0][0], vertices[0][0][1]), (vertices[0][3][0], vertices[0][3][1]), (0, 0, 255), 2) cv2.putText(frame, "IN Cnt : " + str(RED_cnt_1), (vertices[0][0][0], vertices[0][0][1] - 5), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 3) # Blue_Line #cv2.line(frame, (vertices[0][1][0], vertices[0][1][1]), (vertices[0][2][0], vertices[0][2][1]), (255, 0, 0), 2) #cv2.putText(frame, "IN Cnt : " + str(BLUE_cnt_1), (vertices[0][1][0], vertices[0][1][1] + 25), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 0, 0), 3) # Detecting Zone pts_1 = np.array([[vertices[0][1][0] + int(2 / 3 * (vertices[0][0][0] - vertices[0][1][0])), vertices[0][0][1] + int(1 / 3 * (vertices[0][1][1] - vertices[0][0][1]))], \ [vertices[0][1][0] + int(1 / 3 * (vertices[0][0][0] - vertices[0][1][0])), vertices[0][0][1] + int(2 / 3 * (vertices[0][1][1] - vertices[0][0][1]))], \ [vertices[0][3][0] + int(2 / 3 * (vertices[0][2][0] - vertices[0][3][0])), vertices[0][3][1] + int(2 / 3 * (vertices[0][2][1] - vertices[0][3][1]))], \ [vertices[0][3][0] + int(1 / 3 * (vertices[0][2][0] - vertices[0][3][0])), vertices[0][3][1] + int(1 / 3 * (vertices[0][2][1] - vertices[0][3][1]))]], \ np.int32) cv2.polylines(frame, [pts_1], True, (0, 255, 0), 2) #프레임 레터박스 blank_image = np.zeros((64, 1920, 3), np.uint8) frame[0:64, 0:1920] = blank_image frame = cv2.resize( frame, (1280, 720), interpolation=cv2.INTER_CUBIC) #1920, 1080 -> 1280,720 #Substracted = cv2.resize(Substracted , (1280, 720), interpolation=cv2.INTER_CUBIC) fps.update() fps.stop() cv2.putText(frame, "FPS : " + "{:.2f}".format(fps.fps()), (25, 30), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 255), 2) cv2.imshow("frame", frame) if cv2.waitKey(1) & 0xFF == ord('q'): break return
def calculate_mass_center(detector_matrix, depth_matrix, img, type): dis_list = [] x_list = np.arange(0, depth_matrix.shape[1], 1) y_list = np.arange(0, depth_matrix.shape[0], 1) x_vector = np.asarray(x_list).reshape(1, (len(x_list))) y_vector = np.asarray(y_list).reshape((len(y_list), 1)) x_matrix = np.tile(x_list, (y_vector.shape[0], 1)) y_matrix = np.tile(y_list.T, (x_vector.shape[1],1)).T X_matrix = np.divide(x_matrix * depth_matrix, f) - p_x Y_matrix = np.divide(y_matrix * depth_matrix, f) - p_y # container for mass center return_matrix = np.zeros((detector_matrix.shape[0], 3)) for i in range(detector_matrix.shape[0]): x_left = np.int(detector_matrix[i][0]) y_top = np.int(detector_matrix[i][1]) x_right = np.int(detector_matrix[i][2]) y_bottom = np.int(detector_matrix[i][3]) id = np.int(detector_matrix[i][4]) score = np.int(detector_matrix[i][5]) pts = np.array([[x_left, y_top], [x_right, y_top], [x_right, y_bottom], [x_left, y_bottom]], np.int32) pts = pts.reshape((-1, 1, 2)) if type == "car": img = cv2.polylines(img, [pts], True, (0, 0, 255)) cv2.putText(img, 'Car', (x_left, y_top - 5), font, 0.5, (0, 0, 255), 1, cv2.LINE_AA) if type == "person": img = cv2.polylines(img, [pts], True, (255, 0, 0)) cv2.putText(img, 'Person', (x_left, y_top - 5), font, 0.5, (255, 0, 0), 1, cv2.LINE_AA) if type == "bicycle": img = cv2.polylines(img, [pts], True, (0, 255, 0)) cv2.putText(img, 'Bicycle', (x_left, y_top - 5), font, 0.5, (0, 255, 0), 1, cv2.LINE_AA) Z = depth_matrix[y_top-1:y_bottom-1, x_left-1:x_right-1] X = X_matrix[y_top-1:y_bottom-1, x_left-1:x_right-1] Y = Y_matrix[y_top-1:y_bottom-1, x_left-1:x_right-1] x_median = np.median(X) y_median = np.median(Y) z_median = np.median(Z) how_far = np.power(np.power(x_median,2)+np.power(y_median,2)+np.power(z_median,2), 1/2) dis_list.append(how_far) return_matrix[i,0] = x_median return_matrix[i,1] = y_median return_matrix[i,2] = z_median # print(x_matrix.shape) # print(y_matrix.shape) # print(corresponding_depth_matrix.shape) for m in range(X.shape[0]): for n in range(X.shape[1]): distance = np.power((X[m, n] - x_median), 2) + \ np.power((Y[m, n] - y_median), 2) + \ np.power((Z[m, n] - z_median), 2) if distance < T: if type == "car": img[m + y_top - 1, n + x_left - 1, :] = [0, 0, 255] if type =="person": img[m + y_top - 1, n + x_left - 1, :] = [255, 0, 0] if type == "bicycle": img[m + y_top - 1, n + x_left - 1, :] = [0, 255, 0] return img, return_matrix, dis_list
def draw_one_3d_box_cv2(img, box_3d, obj_id_name_map, score, tlwhy_format=False, calib_cam_to_img_p2=None, force_color=None): """ provide a obj id name map like: {1, 'car'} id to distinguish with previous object type tlwhy means input box are in format: [x, y, z, l, w, h, ry] that means we should convert it first. :param img: :param box_3d: :param obj_id_name_map: :param score: :param tlwhy_format: :param calib_cam_to_img_p2: :param force_color: :return: """ assert isinstance(obj_id_name_map, dict), 'obj_id_name_map must be dict' # color = None if force_color: color = force_color else: color = create_unique_color_uchar(list(obj_id_name_map.keys())[0]) if tlwhy_format: # transform [x, y, z, l, w, h, ry] to normal box assert calib_cam_to_img_p2, 'You should provide calibration matrix, convert camera to image coordinate.' center = box_3d[0:3] dims = box_3d[3:6] rot_y = -box_3d[6] / 180 * np.pi # alpha / 180 * np.pi + np.arctan(center[0] / center[2]) converted_box_3d = [] for i in [1, -1]: for j in [1, -1]: for k in [0, 1]: point = np.copy(center) point[0] = center[0] + i * dims[1] / 2 * np.cos(-rot_y + np.pi / 2) + \ (j * i) * dims[2] / 2 * np.cos(-rot_y) point[2] = center[2] + i * dims[1] / 2 * np.sin(-rot_y + np.pi / 2) + \ (j * i) * dims[2] / 2 * np.sin(-rot_y) point[1] = center[1] - k * dims[0] point = np.append(point, 1) point = np.dot(calib_cam_to_img_p2, point) point = point[:2] / point[2] point = point.astype(np.int16) converted_box_3d.append(point) print('final box: ', converted_box_3d) # box_3d = np.asarray(converted_box_3d) box_3d = converted_box_3d # print(box_3d.shape) for i in range(4): point_1_ = box_3d[2 * i] point_2_ = box_3d[2 * i + 1] cv2.line(img, (point_1_[0], point_1_[1]), (point_2_[0], point_2_[1]), color, 1) for i in range(8): point_1_ = box_3d[i] point_2_ = box_3d[(i + 2) % 8] cv2.line(img, (point_1_[0], point_1_[1]), (point_2_[0], point_2_[1]), color, 1) return img else: # assert len(box_3d) == 8, 'every box 3d should have 8 points. if you got 7, you may want tlwhy=True' face_idx = np.array([ 0, 1, 5, 4, # front face 1, 2, 6, 5, # left face 2, 3, 7, 6, # back face 3, 0, 4, 7 ]).reshape((4, 4)) # print('start draw...') for i in range(4): x = np.append(box_3d[0, face_idx[i, ]], box_3d[0, face_idx[i, 0]]) y = np.append(box_3d[1, face_idx[i, ]], box_3d[1, face_idx[i, 0]]) # print('x: ', x) # print('y: ', y) # cv2.line(img, (point_1_, point_1_), (point_2_, point_2_), color, 1) pts = np.vstack((x, y)).T # filter negative values pts = (pts + abs(pts)) / 2 pts = np.array([pts], dtype=int) # print(pts) cv2.polylines(img, pts, isClosed=True, color=color, thickness=1) if i == 3: # add text ori_txt = pts[0][1] return img
term_crit = (cv2.TERM_CRITERIA_EPS | cv2.TERM_CRITERIA_COUNT, 10, 1) while (1): ret, frame = cap.read() if ret == True: hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV) #Parameters - Target_Image, channels, Histogram RoI, Range of Color, Scale factor. dst = cv2.calcBackProject([hsv], [0], roi_hist, [0, 180], 1) # apply meanshift to get the new location ret, track_window = cv2.CamShift(dst, track_window, term_crit) # Draw it on image pts = cv2.boxPoints(ret) pts = np.int0(pts) img2 = cv2.polylines(frame, [pts], True, 255, 2) cv2.imshow('img2', img2) k = cv2.waitKey(60) & 0xff if k == 27: break else: cv2.imwrite(chr(k) + ".jpg", img2) else: break cv2.destroyAllWindows() cap.release()
def __getitem__(self, idx): if self.phase == 'train' or self.phase == 'val': '''OpenCV''' img_path = os.path.join(self.dataset_dir, self.image_list[idx]) image = cv2.imread(img_path, cv2.IMREAD_COLOR) h, w, c = image.shape image = cv2.resize(image, self.size, interpolation=cv2.INTER_LINEAR) image = image.astype(np.float32) image -= VGG_MEAN image = np.transpose(image, (2, 0, 1)) image = torch.from_numpy(image).float() / 255 bin_seg_label = np.zeros((h, w), dtype=np.uint8) inst_seg_label = np.zeros((h, w), dtype=np.uint8) # inst_seg_label = np.zeros((h, w, 3), dtype=np.uint8) lanes = self.lanes_list[idx] for idx, lane in enumerate(lanes): cv2.polylines(bin_seg_label, [lane], False, 1, 10) cv2.polylines(inst_seg_label, [lane], False, idx + 1, 10) # grey, for training # cv2.polylines(inst_seg_label, [lane], False, utils.get_color(idx), 10) # colored, for visualization bin_seg_label = cv2.resize(bin_seg_label, self.size, interpolation=cv2.INTER_NEAREST) # inst_seg_label = cv2.resize(inst_seg_label, self.size, interpolation=cv2.INTER_NEAREST) bin_seg_label = torch.from_numpy(bin_seg_label).long() inst_seg_label = torch.from_numpy(inst_seg_label).long() sample = { 'input_tensor': image, 'binary_tensor': bin_seg_label, 'instance_tensor': inst_seg_label, 'raw_file': self.image_list[idx] } return sample elif self.phase == 'test' or 'test_extend': '''OpenCV''' img_path = os.path.join(self.dataset_dir, self.image_list[idx]) image = cv2.imread(img_path, cv2.IMREAD_COLOR) image = cv2.resize(image, self.size, interpolation=cv2.INTER_LINEAR) image = image.astype(np.float32) image -= VGG_MEAN image = np.transpose(image, (2, 0, 1)) image = torch.from_numpy(image).float() / 255 clip, seq, frame = self.image_list[idx].split('/')[-3:] path = '/'.join([clip, seq, frame]) sample = { 'input_tensor': image, 'raw_file': self.image_list[idx], 'path': path } return sample else: raise Exception(f"Phase '{self.phase}' cannot be recognize!")
# 외곽선 검출 및 명함 검출 contours, _ = cv2.findContours(src_bin, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE) cpy = src.copy() for pts in contours: # 너무 작은 객체는 무시 if cv2.contourArea(pts) < 1000: continue # 외곽선 근사화 approx = cv2.approxPolyDP(pts, cv2.arcLength(pts, True) * 0.02, True) # 컨벡스가 아니고, 사각형이 아니면 무시 if not cv2.isContourConvex(approx) or len(approx) != 4: continue cv2.polylines(cpy, [approx], True, (0, 255, 0), 2, cv2.LINE_AA) srcQuad = reorderPts(approx.reshape(4, 2).astype(np.float32)) pers = cv2.getPerspectiveTransform(srcQuad, dstQuad) dst = cv2.warpPerspective(src, pers, (dw, dh)) dst_gray = cv2.cvtColor(dst, cv2.COLOR_BGR2GRAY) print(pytesseract.image_to_string(dst_gray, lang='Hangul+eng')) cv2.imshow('src', src) cv2.imshow('dst', dst) cv2.waitKey() cv2.destroyAllWindows()
def main(): # Common arguments: # - points : (0,0) is at the top left # - color : use a tuple for BGR, eg: (255,0,0) for blue. For grayscale, just pass the scalar value. # - thickness : thickness of the line. If -1 is passed for closed figures like circles, it will fill the shape. # - lineType : type of line, whether 8-connected, anti-aliased line etc. By default, it is 8-connected. cv2.LINE_AA gives anti-aliased line which looks great for curves. # CREATE A BLACK IMAGE img_np = np.zeros((512, 512, 3), np.uint8) # DRAW A LINE point_1 = (0, 0) point_2 = (256, 256) color = (0, 0, 255) thickness = 3 cv.line(img_np, point_1, point_2, color, thickness) # DRAW A RECTANGLE point_1 = (300, 100) point_2 = (400, 400) color = (255, 0, 0) thickness = -1 cv.rectangle(img_np, point_1, point_2, color, thickness) # DRAW A CIRCLE center_point = (100, 300) radius = 64 color = (0, 255, 0) thickness = 2 cv.circle(img_np, center_point, radius, color, thickness) line_type = cv.CV_AA # Anti-Aliased cv.circle(img_np, center_point, radius + 20, color, thickness, line_type) # DRAW A ELLIPSE center_point = (200, 400) axes_length = (100, 50) angle = 45 # the angle of rotation of ellipse in anti-clockwise direction start_angle = 0 end_angle = 180 color = (255, 255, 0) thickness = 3 line_type = cv.CV_AA # Anti-Aliased cv.ellipse(img_np, center_point, axes_length, angle, start_angle, end_angle, color, thickness, line_type) # DRAW A POLYGON pts = np.array([[32, 87], [124, 81], [75, 43], [60, 11]], np.int32) pts = pts.reshape((-1, 1, 2)) is_closed = True color = (255, 0, 255) thickness = 3 line_type = cv.CV_AA # Anti-Aliased cv.polylines(img_np, [pts], is_closed, color, thickness, line_type) # ADD TEXT text = "Hello!" start_point = (150, 500) font = cv.FONT_HERSHEY_SIMPLEX font_scale = 4 color = (255, 0, 255) thickness = 3 line_type = cv.CV_AA # Anti-Aliased cv.putText(img_np, text, start_point, font, font_scale, color, thickness, line_type) # SAVE THE IMAGE cv.imwrite("drawing_functions.png", img_np)
import cv2 import numpy as np # Create a black image # img = np.zeros((512, 512, 3), np.uint8) img = cv2.imread("Images\image.jpg", 1) height, width, channels = img.shape pts = np.array([[105, 105], [134, 130], [341, 167], [190, 112], [120, 340]], np.int32) pts.reshape((-1, 1, 2)) font = cv2.FONT_HERSHEY_SIMPLEX cv2.line(img, (0, 0), (width, height), (255, 0, 0), 5) # BGR color mode cv2.rectangle(img, (width, 0), (width / 2, height / 2), (0, 255, 0), 3) cv2.circle(img, (width / 2, height / 2), 100, (0, 0, 255), -1) # -1 will fill the shape with color cv2.ellipse(img, (width / 4, height / 4), (100, 50), 0, 0, 360, (0, 0, 255), -1) cv2.polylines(img, [pts], True, (0, 255, 255)) # Here True gives the closed polygon cv2.putText(img, "ANUJ GUPTA", (width / 2, height / 2), font, 1, (255, 255, 0), 3) cv2.namedWindow("Drawing on images", cv2.WINDOW_NORMAL) cv2.imshow("Drawing on images", img) cv2.waitKey(0) cv2.destroyAllWindows()
def detectPeople(self): _center = [314.67404, 231.52438] #_center = [112.0679, 132.63786] list = [] list_P = [] list_N = [] svm = cv2.ml.NormalBayesClassifier_create() #svm.setKernel(cv2.ml.SVM_LINEAR) #svm.setType(cv2.ml.SVM_C_SVC) #svm.setC(2.67) #svm.setGamma(5.383) #Contadore de entrada e saida cnt_up = 0 cnt_down = 0 #Fonte de video #cap = cv2.VideoCapture(0) # Descomente para usar a camera. #cap = cv2.VideoCapture("C:\\Users\\Bruno\\Documents\\GitHub\\Contador\\peopleCounter.avi") #Captura um video #cap = cv2.VideoCapture("C:\\Users\\Bruno\\Documents\\GitHub\\Contador\\d.mp4") #Captura um video #cap = cv2.VideoCapture("/home/vino/Documents/Contest2018/Cambus/contadorPessoas/src/videos/input2.avi") #Captura um video #cap = cv2.VideoCapture("/home/vino/Documents/Contest2018/Cambus/contadorPessoas/src/videos/cambus.avi") #cap = cv2.VideoCapture("/home/vino/Documents/Contest2018/Cambus/contadorPessoas/src/videos/input.avi") cap = cv2.VideoCapture( "/home/vino/Documents/Contest2018/Cambus/contadorPessoas/src/bus.avi" ) #Captura um video #Descomente para imprimir as propriedades do video """for i in range(19): print (i, cap.get(i))""" #Metodo GET para pegar width e height do frame w = cap.get(3) h = cap.get(4) print("Resolução do video: ", w, "x", h) x_meio = int(w / 2) y_meio = int(h / 2) frameArea = h * w print("Area do Frame:", frameArea) areaTH = frameArea / 100 print('Area Threshold', areaTH) # Area de contorno usada para detectar uma pessoa #Linhas de Entrada/Saida #line_up = int(2*(h/6)) #line_down = int(3*(h/6)) line_up = int( 4.7 * (h / 10)) #deve-se adaptar de acordo com as caracteristicas da camera line_down = int( 5.3 * (h / 10)) #deve-se adaptar de acordo com as caracteristicas da camera print("Line UP:", line_up) print("Line DOW:", line_down) #up_limit = int(1*(h/6)) #down_limit = int(5*(h/6)) up_limit = int(0.1 * (h / 10)) down_limit = int(9.9 * (h / 10)) l1UP = int(4.8 * (h / 10)) l1DOWN = int(5.2 * (h / 10)) l2UP = int(4.9 * (h / 10)) l2DOWN = int(5.1 * (h / 10)) print("Limite superior:", up_limit) print("Limite inferior:", down_limit) #Propriedades das linhas print("Red line y:", str(line_down)) print("Blue line y:", str(line_up)) line_down_color = (0, 0, 255) line_up_color = (255, 0, 0) pt1 = [0, line_down] pt2 = [w, line_down] pts_L1 = np.array([pt1, pt2], np.int32) pts_L1 = pts_L1.reshape((-1, 1, 2)) pt3 = [0, line_up] pt4 = [w, line_up] pts_L2 = np.array([pt3, pt4], np.int32) pts_L2 = pts_L2.reshape((-1, 1, 2)) pt5 = [0, up_limit] pt6 = [w, up_limit] pts_L3 = np.array([pt5, pt6], np.int32) pts_L3 = pts_L3.reshape((-1, 1, 2)) pt7 = [0, down_limit] pt8 = [w, down_limit] pts_L4 = np.array([pt7, pt8], np.int32) pts_L4 = pts_L4.reshape((-1, 1, 2)) pt9 = [0, l1UP] pt10 = [w, l1UP] pts_L5 = np.array([pt9, pt10], np.int32) pts_L5 = pts_L5.reshape((-1, 1, 2)) pt11 = [0, l1DOWN] pt12 = [w, l1DOWN] pts_L6 = np.array([pt11, pt12], np.int32) pts_L6 = pts_L6.reshape((-1, 1, 2)) pt13 = [0, l2UP] pt14 = [w, l2UP] pts_L7 = np.array([pt13, pt14], np.int32) pts_L7 = pts_L7.reshape((-1, 1, 2)) pt15 = [0, l2DOWN] pt16 = [w, l2DOWN] pts_L8 = np.array([pt15, pt16], np.int32) pts_L8 = pts_L8.reshape((-1, 1, 2)) #Substrator de fundo #fgbg = cv2.createBackgroundSubtractorMOG2(detectShadows = False) #fgbg = cv2.createBackgroundSubtractorMOG2(500,detectShadows = True) #fgbg = cv2.createBackgroundSubtractorMOG2() fgbg = cv2.bgsegm.createBackgroundSubtractorMOG() #kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE,(3,3)) #fgbg = cv2.bgsegm.createBackgroundSubtractorGMG() #Elementos estruturantes para filtros morfoogicos kernelOp = np.ones((3, 3), np.uint8) kernelOp2 = np.ones((5, 5), np.uint8) kernelOp3 = np.ones((8, 8), np.uint8) kernelCl = np.ones((11, 11), np.uint8) kernelCl2 = np.ones((8, 8), np.uint8) #Inicializacao de variaveis Globais font = cv2.FONT_HERSHEY_SIMPLEX pessoas = [] max_p_age = 5 pid = 1 while (cap.isOpened()): ##for image in camera.capture_continuous(rawCapture, format="bgr", use_video_port=True): #Le uma imagem de uma fonte de video ret, frame = cap.read() ## frame = image.array for pessoa in pessoas: pessoa.age_one() #age every person one frame ######################### # PRE-PROCESSAMENTO # ######################### #Aplica subtracao de fundo fgmask = fgbg.apply(frame) fgmask2 = fgbg.apply(frame) #Binarizacao para eliminar sombras (color gris) try: fgmask = cv2.GaussianBlur(fgmask, (3, 3), 0) #fgmask2 = cv2.GaussianBlur(fgmask2, (3, 3), 0) ret, imBin = cv2.threshold(fgmask, 128, 255, cv2.THRESH_BINARY) #ret,imBin2 = cv2.threshold(fgmask2,128,255,cv2.THRESH_BINARY) #Opening (erode->dilate) para remover o ruido. mask = cv2.morphologyEx(imBin, cv2.MORPH_OPEN, kernelOp) #mask2 = cv2.morphologyEx(imBin2, cv2.MORPH_OPEN, kernelOp) #Closing (dilate -> erode) para juntar regioes brancas. mask = cv2.morphologyEx(mask, cv2.MORPH_CLOSE, kernelCl) #mask2 = cv2.morphologyEx(mask2, cv2.MORPH_CLOSE, kernelCl) except: print('EOF') print('Entrou:', cnt_up) print('Saiu:', cnt_down) #print(list) #a = np.array(list) Z = np.vstack(list) #Z = np.vstack(list) # convert to np.float32 Z = np.float32(Z) # define criteria and apply kmeans() criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 10, 1.0) ret, label, center = cv2.kmeans(Z, 1, None, criteria, 10, cv2.KMEANS_RANDOM_CENTERS) # Now separate the data, Note the flatten() A = Z[label.ravel() == 0] B = Z[label.ravel() == 1] #print("A") #print(A) #print(len(A)) #print("B") #print(B) #print(len(B)) #print("centre ----") ## print(center) # Plot the data plt.scatter(A[:, 0], A[:, 1]) plt.scatter(B[:, 0], B[:, 1], c='r') plt.scatter(center[:, 0], center[:, 1], s=80, c='y', marker='s') plt.xlabel('Height'), plt.ylabel('Weight') plt.show() a = np.float32(list_P) responses = np.array(list_N) #responses = np.float32(responses) print(len(a)) print(len(responses)) trained = svm.train(a, cv2.ml.ROW_SAMPLE, responses) if (trained): print("trained", trained) print("IsTrained", svm.isTrained()) svm.save('svm_data1.dat') else: print("nao saolvou") #return (cnt_up - cnt_down) #break ################# # CONTORNOS # ################# # RETR_EXTERNAL returns only extreme outer flags. All child contours are left behind. _, contours0, hierarchy = cv2.findContours(mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) for cnt in contours0: #frame = cv2.drawContours(frame, cnt, -1, (0,255,0), 3, 8) area = cv2.contourArea(cnt) peri = cv2.arcLength(cnt, True) M = cv2.moments(cnt) #### #### coloca numa lista para treinamento 1 list_P.append(np.float32(cv2.HuMoments(M))) list_N.append(0) ### #### #### coloca numa lista para treinamento 2 #list_P.append(np.float32(cnt.flatten())) #list_N.append(1) ### shape = cv2.HuMoments(M).flatten() #print(type(cnt[0])) #print(cv2.HuMoments(M).flatten()) #print(cnt.flatten()) #print("-------------------------------------------------------------------------------------------------") #print(decimal.Decimal(shape[6])) #print(format((shape[0]), '20f')) #cv2.drawContours(frame, cnt, -1, (0,0,255), 3, 8) if area > areaTH: #and (peri > 950 and peri < 2500): ##################### # RASTREAMENTO # ##################### #Falta agregar condicoes para multiplas pessoas, saidas e entradas da tela #M = cv2.moments(cnt) #print("Antes dos filtros: ", M) cx = int(M['m10'] / M['m00']) cy = int(M['m01'] / M['m00']) x, y, w, h = cv2.boundingRect(cnt) dist = math.hypot(_center[0] - cx, _center[1] - cy) # tentativa de remover retangulos muito largos #if(x >= 240 or h >= 240): # continue new = True if cy in range(up_limit, down_limit): #print("----------------------------------------------------------------") #print(cnt) #print("----------------------------------------------------------------") #if(len(cnt) < 80): # print("Possivel nao pessoa ................") #continue #print("Shape de nao pessoa: ", cv2.HuMoments(M).flatten()) for pessoa in pessoas: if abs(cx - pessoa.getX()) <= w and abs( cy - pessoa.getY()) <= h: # O objeto esta perto de um que ja foi detectado anteriormente new = False pessoa.updateCoords( cx, cy ) #atualizar coordenadas no objeto e reseta a idade if pessoa.deslocaCima( line_down, line_up ) == True: # and shape[0] < 0.30:# and dist < 170 and dist > 70 : #and (pessoa.getOffset() - time.time() < -0.95): print("Diferenca de tempo: ", (pessoa.getOffset() - time.time())) cnt_up += 1 print("ID: ", pessoa.getId(), 'Entrou as', time.strftime("%c")) print("Area objeto: " + str(area)) print("Distancia do centroide da pessoa: ", dist) print(M) print("Perimetro: ", peri) print("Shape da pessoa: ", shape[0] < 0.30) print("Shape da pessoa: ", shape[0]) list.append((cx, cy)) list_P.pop() list_N.pop() list_P.append(np.float32(cv2.HuMoments(M))) #print(np.float32(cv2.HuMoments(M))) list_N.append(1) #trainingData = np.matrix(cnt, dtype=np.float32) #print("Training data ...... ") #print(trainingData) elif pessoa.deslocaBaixo( line_down, line_up ) == True: # and dist < 170 and dist > 70: # and (pessoa.getOffset() - time.time() < -0.95): print("Diferenca de tempo: ", (pessoa.getOffset() - time.time())) cnt_down += 1 print("ID: ", pessoa.getId(), 'Saiu as', time.strftime("%c")) print("Area objeto: " + str(area)) print("Distancia do centroide da pessoa: ", dist) print(M) print("Perimetro: ", peri) print("Shape da pessoa: ", shape[0] < 0.30) print("Shape da pessoa: ", shape[0]) list.append((cx, cy)) list_P.pop() list_N.pop() list_P.append(np.float32(cv2.HuMoments(M))) #print(np.float32(cv2.HuMoments(M))) list_N.append(1) #trainingData = np.matrix(cnt, dtype=np.float32) #print("Training data ...... ") #print(trainingData) break if pessoa.getState() == '1': if pessoa.getDir( ) == 'Saiu' and pessoa.getY() > down_limit: pessoa.setDone() elif pessoa.getDir( ) == 'Entrou' and pessoa.getY() < up_limit: pessoa.setDone() if pessoa.timedOut(): #remover pessoas da lista index = pessoas.index(pessoa) pessoas.pop(index) del pessoa #libera a memoria de variavel i. if new == True: p = Pessoa.Pessoa(pid, cx, cy, max_p_age, time.time()) pessoas.append(p) pid += 1 ################# # DESENHOS # ################# cv2.circle(frame, (cx, cy), 5, (0, 0, 255), -1) img = cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2) #cv2.drawContours(frame, cnt, -1, (0,255,0), 3) #END for cnt in contours0 ######################### # DESENHAR TRAJETORIAS # ######################### for pessoa in pessoas: if len(pessoa.getTracks()) >= 2: pts = np.array(pessoa.getTracks(), np.int32) pts = pts.reshape((-1, 1, 2)) frame = cv2.polylines(frame, [pts], False, pessoa.getRGB()) #if pessoa.getId() == 9: #print (str(pessoa.getX()), ',', str(pessoa.getY())) cv2.putText(frame, str(pessoa.getId()), (pessoa.getX(), pessoa.getY()), font, 0.3, pessoa.getRGB(), 1, cv2.LINE_AA) ################# # IMAGEM # ################# str_up = 'Entraram ' + str(cnt_up) str_down = 'Sairam ' + str(cnt_down) tituloup = "Entrada " titulodown = "Saida " #dataehora = strftime("%c") dataehora = strftime("%A, %d %b %Y %H:%M:%S", gmtime()) frame = cv2.polylines(frame, [pts_L1], False, line_down_color, thickness=1) frame = cv2.polylines(frame, [pts_L2], False, line_up_color, thickness=1) frame = cv2.polylines(frame, [pts_L3], False, (255, 255, 0), thickness=1) frame = cv2.polylines(frame, [pts_L4], False, (255, 255, 0), thickness=1) frame = cv2.polylines(frame, [pts_L5], False, (line_up_color), thickness=1) frame = cv2.polylines(frame, [pts_L6], False, (line_down_color), thickness=1) frame = cv2.polylines(frame, [pts_L7], False, (line_up_color), thickness=1) frame = cv2.polylines(frame, [pts_L8], False, (line_down_color), thickness=1) self.escreveCabecalho(frame, str_up, str_down, titulodown, tituloup, dataehora, font, x_meio) cv2.imshow('Frame', frame) #cv2.imshow('Debug',mask) #cv2.imshow('Binarizacao', imBin) #preisonar ESC para sair k = cv2.waitKey(30) & 0xff if k == 27: break #END while(cap.isOpened()) ################# # LIMPEZA # ################# cap.release() cv2.destroyAllWindows()
import argparse import glob import cv2 import tqdm # construct the argument parse and parse the arguments ap = argparse.ArgumentParser() ap.add_argument("-i", "--input", required=True, help="path to input dataset of images") ap.add_argument("-o", "--output", required=True, help="path to output folder where to save result") args = vars(ap.parse_args()) # loop over the images for imagePath in tqdm.tqdm(glob.glob(args["input"] + "/*.jpg"), desc=args["input"]): outputPath = args["output"] + "/" + imagePath.split("/")[-1] image = cv2.imread(imagePath) gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) vis = image.copy() mser = cv2.MSER_create() regions = mser.detectRegions(gray)[0] hulls = [cv2.convexHull(p.reshape(-1, 1, 2)) for p in regions] cv2.polylines(vis, hulls, 1, (0, 255, 0)) cv2.imwrite(outputPath, vis)
def embedd_path(self): '''write the polygon path on rasterized array with correct label and width''' coords = self.freehand.data if coords and coords['ys']: pts = np.stack( [np.asarray(coords['xs'][0]), np.asarray(coords['ys'][0])], axis=1) pts, spacing = self._phy_to_px(pts, return_spacing=True) mask = np.zeros_like(self.dataset.img, np.uint8) if mask.ndim > 2: axis = self._get_axis_id(self.slicer.axis) loc = [slice(None) for _ in range(mask.ndim)] loc[axis] = int(self.slicer.slice_id / self.dataset.spacing[axis]) submask = np.zeros(mask[loc].shape, dtype=np.uint8) # handle anisotrpic slice --> draw 1 px line and expand with distance transform cv.polylines( submask, [pts], False, 1, 1, # // 2, cv.LINE_8) min_spacing = min(spacing) if self.draw_in_3D: mask[loc] = submask dist = distance_transform_edt( ~mask.astype(bool), sampling=self.dataset.spacing / min_spacing) mask = dist <= self.tool_width / 2 else: dist = distance_transform_edt( ~submask.astype(bool), sampling=(spacing[0] / min_spacing, spacing[1] / min_spacing)) mask[loc] = dist <= self.tool_width / 2 else: # draw polyline on minimal size crop margin = self.tool_width // 2 + 1 loc = [ slice(max(0, pts[:, ax].min() - margin), pts[:, ax].max() + margin) for ax in range(2) ] offset = np.array([s.start for s in loc])[None] loc = loc[::-1] submask = mask[loc] cv.polylines( submask, [pts - offset], False, 1, self.tool_width, # // 2, cv.LINE_8) mask = mask.astype(bool) self.dataset.write_label(mask) self._clear()
def explore_match(win, img1, img2, kp_pairs, wscale=1.0, hscale=1.0, 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) vis0 = vis.copy() 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) p1 = np.int32([kpp[0].pt for kpp in kp_pairs]) * (wscale, hscale) p2 = np.int32([kpp[1].pt for kpp in kp_pairs]) * (wscale, hscale) + (w1, 0) p1 = np.int32(p1) p2 = np.int32(p2) green = (0, 255, 0) red = (0, 0, 255) white = (255, 255, 255) kp_color = (51, 103, 236) def draw_keypoints(vis): 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) for (x1, y1), (x2, y2), inlier in zip(p1, p2, status): if inlier: cv2.line(vis, (x1, y1), (x2, y2), green) else: cv2.line(vis, (x1, y1), (x2, y2), red) draw_keypoints(vis) cv2.imshow(win, vis) def onmouse(event, x, y, flags, param): if event & cv2.EVENT_LBUTTONDOWN: vis = vis0.copy() r = 8 m = (anorm(p1 - (x, y)) < r) | (anorm(p2 - (x, y)) < r) idxs = np.where(m)[0] for i in idxs: status[i] = not status[i] draw_keypoints(vis) cv2.imshow(win, vis) # what is this supposed to do? Currently it's broken. if event & cv2.EVENT_RBUTTONDOWN: 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(vis, (x1, y1), (x2, y2), col) kp1, kp2 = kp_pairs[i] kp1s.append(kp1) kp2s.append(kp2) vis = cv2.drawKeypoints(vis, kp1s, flags=4, color=kp_color) vis[:, w1:] = cv2.drawKeypoints(vis[:, w1:], kp2s, flags=4, color=kp_color) cv2.imshow(win, vis) cv2.setMouseCallback(win, onmouse) # keyboard input done = False while not done: print('waiting for keyboard input...') key = cv2.waitKey() & 0xff print("received %s (%d)" % (str(key), int(key))) if key == 27: # ESC = set all pairs to True (no delete) and exit for i in range(len(status)): status[i] = True done = True elif key == ord(' ') or key == ord('q'): # accept current selection and exit. Different keystrokes # may have higher level meaning so we return the key as # well. done = True elif key == ord('y'): # set all pairs as valid for i in range(len(status)): status[i] = True elif key == ord('n'): # set all pairs as invalid for i in range(len(status)): status[i] = False # refresh display vis = vis0.copy() draw_keypoints(vis) cv2.imshow(win, vis) return key
kp_grayframe[m.trainIdx].pt for m in good_points ]).reshape(-1, 1, 2) matrix, mask = cv2.findHomography(query_pts, train_pts, cv2.RANSAC, 5.0) matches_mask = mask.ravel().tolist() #print "matrix" #print matrix #print "end" # Perspective transform h, w = img.shape pts = np.float32([[0, 0], [0, h - 1], [w - 1, h - 1], [w - 1, 0]]).reshape(-1, 1, 2) #print pts dst = cv2.perspectiveTransform(pts, matrix) #print dst homography = cv2.polylines(frame, [np.int32(dst)], True, (255, 0, 0), 3) font = cv2.FONT_HERSHEY_SIMPLEX print type(dst) print "-------" print np.int32(dst).shape print "-------" stop = True cv2.putText(homography, 'Stop', (10, 250), font, 1, (255, 255, 255), 2) cv2.imshow("Homography", homography) elif len(good_points_r) > 10: query_pts_r = np.float32([ kp_right[m.queryIdx].pt for m in good_points_r ]).reshape(-1, 1, 2)