def SplitCards(img): global NUM global START_NUM global CARD_NAME global MY_CARDS global SAVE_IMG img_crop = img[605:2105, 60:1830] img_crop = cv2.resize(img_crop, (0, 0), fx=0.5, fy=0.5) #cv2.imshow("crop", img_crop) H = img_crop.shape[0] / 4 W = img_crop.shape[1] / 3 for i in range(3): for j in range(4): D = 5 icard = img_crop[((j * H) + D):(((j + 1) * H) - D), ((i * W) + D):(((i + 1) * W) - D)] cv2.imshow(str(NUM), icard) cv2.moveWindow(str(NUM), 200, 600) CARD_NAME = 'card%03d' % (NUM) if (SAVE_IMG): cv2.imwrite("./DataBase/%s.jpg" % (CARD_NAME), icard) if (NUM >= START_NUM): MY_CARDS[CARD_NAME] = {} UserInput() NUM += 1 print "Next..." cv2.destroyAllWindows()
def main(): img = cv2.imread("text.bmp", cv2.CV_LOAD_IMAGE_COLOR) # Read image file img = cv2.GaussianBlur(img, (3, 3), 0) img = cv2.Laplacian(img, 0) cv2.threshold(img, 70, 255, cv2.THRESH_BINARY, img) kernel = np.ones((4, 6), np.uint8) img = cv2.dilate(img, kernel, iterations = 1) img = cv2.erode(img, kernel, iterations = 1) gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) contours, hierarchy = cv2.findContours(gray, cv2.RETR_LIST, cv2.CHAIN_APPROX_NONE) color = (255, 255, 255) drawing = np.zeros(img.shape, np.uint8) for cnt in contours: x, y, w, h = cv2.boundingRect(cnt) cv2.rectangle(drawing, (x, y), (x + w, y + h), color) drawing = cv2.cvtColor(drawing, cv2.COLOR_RGB2GRAY) h, w = img.shape[:2] outpImg = np.zeros((h-2, w-2), np.uint8) cv2.floodFill(outpImg, drawing, (0, 0), color) cv2.threshold(outpImg, 254, 255, cv2.THRESH_BINARY_INV, outpImg) cv2.imwrite("outputTask3.jpg", outpImg) cv2.namedWindow('Display Window', cv2.WINDOW_NORMAL) # Create window for display cv2.imshow('Display Window', outpImg) # Show image in the window cv2.moveWindow('Display Window', 1, 1) print ("size of image: ", img.shape) # print size of image cv2.waitKey(0) # Wait for keystroke cv2.destroyAllWindows() # Destroy all windows
def main(): files = glob.glob("./scans/*.jpg") files += glob.glob("./scans/*.jpeg") for f in files: reset_stats() print "Processing: " + f.split("/")[len(f.split("/")) - 1] schedule = Schedule() schedule.load_data() if schedule.get_has_schedule(): scan_image(f, schedule) print "Sheet ok? ", while True: cv2.imshow("image", cv2.resize(img, (446, 578))) cv2.moveWindow("image", 0, 0) # user_in = raw_input() key = cv2.waitKey(-1) if key == ord("y"): print "Sheet ok... Dumping data" dump_stats() os.remove(f) break elif key == ord("n"): print "Marking to redo" #os.rename(f, "./scans/redo/" + f.split("/")[len(f.split("/")) - 1]) break elif key == ord("q"): exit(0) else: continue cv2.destroyAllWindows() else: print "Unable to load schedule... Aborting"
def alignCamera(self): # Capture frame _, frame = self.cap.read() # self.findColor(frame) # Prepare frame with Gaussian blurring, HSV conversion, and Canny edge detection preppedFrame = self.prepFrame(frame) # Find the all contours contours, hierarchy = cv2.findContours(preppedFrame.copy(), cv2.RETR_CCOMP, cv2.CHAIN_APPROX_SIMPLE) # If contours exist if contours: # Define bounding box maxContour, maxRectangle, maxContourArea = self.detectBoundary(frame, contours) if not maxRectangle and not self.shapeSets: self.flipColor() if self.mode == 'debug': # Display final image cv2.namedWindow('Shape Detection', cv2.WINDOW_NORMAL) cv2.imshow('Shape Detection', frame) cv2.moveWindow('Shape Detection', 360, 0) cv2.waitKey(1)
def get_info_part(li): li_info = li.get_information_part() if li_info is not None: li_info = cv2.resize(li_info, (info_size, info_size)) cv2.imshow('li_info', li_info) cv2.moveWindow('li_info', int(1280*FRAME_SIZE_FACTOR), 0) return li_info
def show_img(data, suffix, show): global wnd_x global wnd_y if data == None: return image = data.astype(np.uint8) if image.shape[0] == 6: img = image.transpose(1,2,0) img1 = img[:,:,:3] img2 = img[:,:,3:] else: img1 = image[0] img2 = image[1] filename = 'img-1-'+suffix+'.jpg' cv2.imwrite(filename, img1) if show: cv2.imshow("IMG 1", img1) cv2.moveWindow("IMG 1", wnd_x, wnd_y) filename = 'img-2-'+suffix+'.jpg' cv2.imwrite(filename, img2) if show: cv2.imshow("IMG 2", img2) wnd_x+=300 cv2.moveWindow("IMG 2", wnd_x, wnd_y) wnd_x=10 wnd_y+=300
def get_start_points(image): window = cv2.namedWindow(MAZE_NAME, cv2.WINDOW_NORMAL) cv2.resizeWindow(MAZE_NAME, 900,900) cv2.imshow(MAZE_NAME,image) cv2.moveWindow(MAZE_NAME,100,100) imageProcessor = ImageProcessor(image) start_x,start_y = imageProcessor.getDefaultStart(image) end_x, end_y = imageProcessor.getDefaultEnd(image) print("Please \'A\' to use default start and end points, or press \'S\' to choose your own)") key = cv2.waitKey(2000) print key if key == ord('a') or key == -1: print("Using Default Start and End Points") imageProcessor = ImageProcessor(image) start_x,start_y = imageProcessor.getDefaultStart(image) end_x, end_y = imageProcessor.getDefaultEnd(image) print("Start Point: {0}, End Point: {1}".format((start_x,start_y),(end_x,end_y))) elif key == ord ('s'): print("Please select a start point") start_x,start_y = get_user_selected_point(image) print ("Start Point: {0}, please select an end point".format((start_x,start_y))) end_x,end_y = get_user_selected_point(image) print("End Pont: {0}".format((end_x,end_y))) cv2.destroyAllWindows() return start_x,start_y,end_x,end_y
def find_hottest_points(cv_image): clahe = cv2.createCLAHE(clipLimit=2.0, tileGridSize=(3,3)) #gray = clahe.apply(img) gray = clahe.apply(cv_image) gray = cv2.GaussianBlur (gray, (21,21), 0) min_thresh = cv2.threshold(gray, min_th, 255, cv2.THRESH_BINARY)[1] max_thresh = cv2.threshold(gray, max_th, 255, cv2.THRESH_BINARY_INV)[1] thresh = cv2.bitwise_and(min_thresh, max_thresh) thresh = cv2.dilate(thresh, None, iterations = 2) (cnts, _) = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) for c in cnts: if cv2.contourArea(c) > min_area and cv2.contourArea(c) < max_area: (x,y,w,h) = cv2.boundingRect(c) # cv2.rectangle(cv_image, (x, y), (x+w, y+h), (0, 255, 0), 2) cv2.rectangle(cv_image, (x, y), (x+w, y+h), 0, 2) continue cv2.imshow("region_detector", cv_image) cv2.moveWindow("region_detector",900,0) cv2.imshow("band_threshold_image", thresh) cv2.moveWindow("band_threshold_image",900,400) cv2.waitKey(1)
def plot(self): # plot current sdf # curr_obs_inds = self.sdf.to_inds(self.curr_obs[:,0], self.curr_obs[:,1]) # print 'obs inds', curr_obs_inds # print 'sdf\n', self.sdf.data() cmap = np.zeros((256, 3), dtype='uint8') cmap[:128,0] = 255 cmap[:128,1] = np.linspace(0, 255, 128).astype(int) cmap[:128,2] = np.linspace(0, 255, 128).astype(int) cmap[128:,0] = np.linspace(255, 0, 128).astype(int) cmap[128:,1] = np.linspace(255, 0, 128).astype(int) cmap[128:,2] = 255 # cmap[:,0] = range(256) # cmap[:,2] = range(256)[::-1] colors = np.clip((self.sdf.to_image_fmt()*100 + 128), 0, 255).astype(int) flatland.show_2d_image(cmap[colors], "sdf") cv2.moveWindow("sdf", 550, 0) # plot flow field # print 'curr flow\n', self.curr_u.data() self.curr_u.show_as_vector_field("u") cv2.moveWindow("u", 0, 600) total, costs = solvers._eval_cost(PIXEL_AREA, self.curr_obs, self.prev_sdf, self.sdf, self.curr_u, ignore_obs=self.curr_obs is None, return_full=True) print 'total cost', total print 'individual costs', costs
def setupWindow(): filename = getUserSelectedImage() imageProcessor = ImageProcessor(cv2.imread(filename,0)) colourImage = cv2.imread(filename,1) image = imageProcessor.getThresholdedImage(False) granularity = imageProcessor.get_granularity(image, 100) print("Granularity: {0}".format(granularity)) start_x,start_y,end_x,end_y = get_start_points(image) image = imageProcessor.encloseMaze(image) pg = PolicyGenerator(image) rows,cols = pg.get_critical_grid() graph,mapping = pg.get_reduced_graph([rows,cols]) policy = pg.generate_policy((end_x,end_y)) solution = solve_using_policy(policy,(start_x,start_y),(end_x,end_y)) imageProcessor.draw_policy(colourImage,policy) imageProcessor.mark_point((end_x,end_y),3,(255,0,0),colourImage) #cv2.imshow(MAZE_NAME,policy_image) mazerunner = MazeSolver(image,granularity) #solution = mazerunner.solveMaze(start_x,start_y,end_x,end_y) if(not solution): cv2.imshow(MAZE_NAME,image) else: solvedImage = draw_solution(solution, colourImage) solvedImage = imageProcessor.mark_point((start_x,start_y),3,(255,0,0),solvedImage) window = cv2.namedWindow("Solved Image", cv2.WINDOW_NORMAL) cv2.resizeWindow("Solved Image", 900,900) cv2.moveWindow("Solved Image",100,100) cv2.imshow("Solved Image",solvedImage) print("Press any key to exit") cv2.waitKey(0) cv2.destroyAllWindows
def liczenie(crop_img): while(True): #crop_img = cv2.imread("6.png",1) #gray = cv2.cvtColor(crop, cv2.COLOR_BGR2GRAY) lower_white = np.array([0,0,150], dtype=np.uint8) upper_white = np.array([200,100,255], dtype=np.uint8) hsv = cv2.cvtColor(crop_img, cv2.COLOR_BGR2HSV) white = cv2.inRange(hsv, lower_white, upper_white) #img = cv2.erode(white, None, 1) #img1 = cv2.dilate(img,None,1) #mask = cv2.dilate(white,None,1) mask=white # Bitwise-AND mask and original image #res = cv2.bitwise_and(crop, crop, mask= mask) cv2.imshow('Kostka', mask) cv2.moveWindow('Kostka', 2200, 0) #image = cv2.Canny(res,50,50) #ret,thresh = cv2.threshold(imgg,200,200,0) contours , hierarchy = cv2 . findContours ( mask , cv2.RETR_TREE , cv2.CHAIN_APPROX_SIMPLE ) #cv2.drawContours(crop, contours, -1, (0,255,0), 1) liczba=0 for i in contours[1:]: (x,y),radius = cv2.minEnclosingCircle(i) center = (int(x),int(y)) radius = int(radius) #print(radius) #cv2.circle(crop_img,center,radius,(0,255,0),2) liczba=liczba+1 #cv2.imshow("cropped", crop_img) #cv2.moveWindow('cropped', 1500, 0) return liczba
def main(): '''main function''' WIN_NAME = 'foobar' cv2.namedWindow(WIN_NAME) cv2.moveWindow(WIN_NAME, 50, 50) if len(sys.argv) > 1: # has argument for ff in sys.argv[1:]: print('imread {}'.format(ff)) img = cv2.imread(ff) cv2.imshow(WIN_NAME, img) cv2.waitKey(0) else: setting_fn = 'setting.json' if not myutil.isfile(setting_fn): print('[ERROR] cannot find setting: {}'.format(setting_fn)) print('[INFO] may use argument') else: app_name = 'readim.py' data = myutil.read_setting(setting_fn) home = os.environ['HOME'] picpath = home + '/' + data[app_name]['path'] print(picpath) for img_file in data[app_name]['images']: pic1 = picpath + '/' + img_file print(pic1) if os.path.isfile(pic1): cv_test(pic1) else: print("file not found: {}".format(pic1)) #cv_drawline() cv2.destroyAllWindows()
def wykrywanie_kwadratowych_kostek(): video_capture = cv2.VideoCapture(0) while True: ret, frame = video_capture.read() squares = find_squares(frame) x=0 for sq in squares: print(sq) ix= np.min([sq[0][0],sq[1][0],sq[2][0],sq[3][0]]) iy= np.min([sq[0][1],sq[1][1],sq[2][1],sq[3][1]]) ix1=np.max([sq[0][0],sq[1][0],sq[2][0],sq[3][0]]) iy1=np.max([sq[0][1],sq[1][1],sq[2][1],sq[3][1]]) crop_img = frame[iy:iy1, ix:ix1] x = liczenie(crop_img) cv2.drawContours( frame, squares, -1, (0, 255, 0), 2 ) cv2.putText(frame,str(x),(10,50), cv2.FONT_HERSHEY_DUPLEX, 1,(0,255,0),2,) # Display the resulting frame cv2.imshow('Wykrywanie liczby oczek na kostce',frame) cv2.moveWindow('Wykrywanie liczby oczek na kostce', 1500, 200) if cv2.waitKey(1) & 0xFF == ord('q'): break # When everything is done, release the capture video_capture.release() cv2.destroyAllWindows()
def runPdf(path, filename): pdf = PyPDF2.PdfFileReader(file(path+'/'+filename, "rb")) cv2.namedWindow('img') cv2.moveWindow('img', 0, 0) cv2.namedWindow('undistorted') cv2.moveWindow('undistorted', IMAGE_WIDTH, 0) for n in xrange(0, pdf.getNumPages()): img, structure_data = runPage(pdf, n) print '<page>' print structure_data print '</page>' cv2.imwrite(path+'/'+str(n)+'.jpg', img) key = cv2.waitKey(1) if(key == 113): quit() cv2.destroyAllWindows() convert = 'convert' if os.name == 'nt': convert = 'convert2' # avoid collision with windows' convert.exe param = [convert, path+'/*.jpg', path+'/merged.pdf'] ret = subprocess.call(param,stdout=subprocess.PIPE)
def run(self): i = 0 while True: p = self.get_next_image_file() if p is None: break img = cv2.imread(p) if i >= self.skip: rect = self.label_file.readline() try: box = map(lambda x: int(x), rect.split("\t")) B = BoundingRegion(image_shape=img.shape, box=np.array(box)) except: try: box = map(lambda x: int(x), rect.split(",")) B = BoundingRegion(image_shape=img.shape, box=np.array(box)) except: print "No more bounding boxes!" B = BoundingRegion() else: B = BoundingRegion() F = LabeledMovieFrame(internal_storage_method='jpg', compression_level=90) F.set_image(img) F.set_label(B) B.draw_box(img) cv2.imshow("image", img) cv2.moveWindow("image", 50, 50) cv2.waitKey(1) i += 1 self.frame_collection.append(F) self.frame_collection.write_to_file(self.output_file)
def streaming(self): ''' streaming ''' # initialize the camera and grab a reference to the raw camera capture with picamera.PiCamera() as self.camera: self.camera.resolution = (int(640), int(480)) fps = 15 self.camera.framerate = fps # allow the camera to warmup time.sleep(0.1) # use the memory stream, may be faster? stream = io.BytesIO() self.update_camera_setting() ''' camera ''' self.camera.start_recording(stream, format='bgr') # move the imshow window to centre cv2.namedWindow('stream') # Create a named window cv2.moveWindow('stream', 0, 200) # Move it to (40,30) # a default step size #focuser = focuser_class() while True: while True: # reset stream for next frame stream.seek(0) stream.truncate() # this delay has to be faster than generating time.sleep(1/fps*0.1) # return current frame, which is just a string frame = stream.getvalue() ''' ensure the size of package is right''' if len(frame) != 0: break else: pass # convert the stream string into np.arrry ncols, nrows = self.camera.resolution data = np.fromstring(frame, dtype=np.uint8).reshape(nrows, ncols, 3) # no need to decode (it is already bgr) self.image = data # place to run some filters, calculations for library in self.cv_libraries: library() # show the frame cv2.imshow('stream', self.image) key = cv2.waitKey(1) & 0xFF # if the `x` key was pressed, break from the loop if key == ord("x"): break
def init(): rospy.init_node('perspective_correction_test') global CALC_TRANSFORM CALC_TRANSFORM = bool(rospy.get_param('~recalc')) print "recalculating transform?", CALC_TRANSFORM prev_time = rospy.get_time() cv2.namedWindow('Plain') cv2.moveWindow('Plain', 0, WINDOW_HEIGHT) cv2.namedWindow('Transformed') cv2.moveWindow('Transformed', EXPECTED_WIDTH, WINDOW_HEIGHT) rospy.loginfo("Now, move the chessboard into view to get a correct perspective transformation!") sub = rospy.Subscriber('usb_cam/image_raw', Image, callback ) r = rospy.Rate(10) while not rospy.is_shutdown(): global curImg if curImg != None: rospy.loginfo("processing image") dispImages(curImg) cv2.waitKey(30) r.sleep()
def main(): #orig_imgs = ['photoset/cleaned/7,49/IMG_2654.JPG'] # Square, slight angle orig_imgs = glob.glob('photoset/cleaned/*/*.JPG') def img_onchange(x): filename = orig_imgs[cv2.getTrackbarPos('img', 'config')] img = cv2.imread(filename) print filename ret = get_white_rectangles(img, DEBUG=True) for r in ret: print r[1] # just the quads img_windows = ['img', 'blur', 'thresh', 'debug_display'] for i in range(len(img_windows)): w = img_windows[i] cv2.resizeWindow(w, WINDOW_WIDTH, WINDOW_HEIGHT) cv2.moveWindow(w, WINDOW_X_OFFSET + WINDOW_WIDTH * i, WINDOW_Y_OFFSET) cv2.namedWindow('config', cv2.WINDOW_AUTOSIZE) cv2.resizeWindow('config', 600, 60) cv2.createTrackbar('img', 'config', 0, len(orig_imgs), img_onchange) img_onchange(0) cv2.waitKey()
def SelectArea(self, winName="Select an area", winPos=(400, 400)): """This method returns the corners of the selected area as: [(UpLeftcorner), (DownRightCorner)].""" # Reset the selected points. self.__ResetPoints() # Define the window name. self.__winName = winName # Update and show the input image. cv2.namedWindow(winName, cv2.WINDOW_AUTOSIZE) cv2.setMouseCallback(winName, self.__OnMouseOver) cv2.moveWindow(winName, winPos[0], winPos[1]) self.__Update() # Main while condition while True: # Read the keyboard selection. ch = cv2.waitKey(1) if ch is 27 or ch is ord("q"): cv2.destroyWindow(winName) return None, False elif ch is 13 or ch is 32: corners = self.__SetCorners() if corners is None: continue cv2.destroyWindow(winName) return corners, True
def main(): rospy.init_node(node_name) # check to see if launch file remaps this try: cv2.namedWindow(win1_name, flags=cv2.cv.WINDOW_NORMAL) #see if flag is necessary # cv2.NamedWindow(win2_name, flags=WINDOW_NORMAL) cv2.moveWindow(win1_name,200,0) # cv2.MoveWindow(win2_name,200,300) cv2.resizeWindow(win1_name,100,300) # cv2.ResizeWindow(win2_name,100,300) cv2.startWindowThread() rospy.on_shutdown(cleanup) # rate = rospy.Rate(freq) sub_RBG = rospy.Subscriber("/camera/rgb/image_color", Image, callback_rbg) # sub_depth = rospy.Subscriber("/camera/depth/image_raw", Image, callback_depth) print('==========Initializing Subscribers=====') rospy.sleep(1) # while not rospy.is_shutdown(): # rospy.rate=1 # rospy.wait_for_message("/camera/rgb/image_color", Image, timeout=None) # k = cv2.waitKey(0) & 0xFF # if k==27: # cleanup() # # rate.sleep() rospy.spin() except KeyboardInterrupt: print "Shutting down" cleanup()
def capture_video(i11, i12, i13, i21, i22, i23, i31, i32, i33): if SHOW_SCREEN: # shows processed image on screen cv2.imshow('Video Capture', np.vstack((np.hstack((i11, i12, i13)), np.hstack((i21, i22, i23)), np.hstack((i31, i32, i33))))) if CAPTURE_VIDEO: # writes frame to video file vid.write(np.vstack((np.hstack((i11, i12, i13)), np.hstack((i21, i22, i23)), np.hstack((i31, i32, i33))))) if not MOVE_WINDOW: # resets window location to prevent movement cv2.moveWindow('Video Capture', ((1280-(CAPTURE_WIDTH*3))/2), 25) # 1280x1024
def findShapes(self): # Capture frame-by-frame _, frame = self.cap.read() # Prepare frame with Gaussian blurring, HSV conversion, and Canny edge detection preppedFrame = self.prepFrame(frame) # Find the all contours contours, hierarchy = cv2.findContours(preppedFrame.copy(), cv2.RETR_CCOMP, cv2.CHAIN_APPROX_SIMPLE) # If contours exist if contours: # Define bounding box maxContour, maxRectangle, maxContourArea = self.detectBoundary(frame, contours) # If a bounding box has been found if maxRectangle: # Identify all nested shapes self.detected = self.detectNestedShapes(frame, contours, hierarchy, maxContour, maxRectangle, maxContourArea) if self.mode == 'debug': # Display final image cv2.namedWindow('Shape Detection', cv2.WINDOW_NORMAL) cv2.imshow('Shape Detection', frame) cv2.moveWindow('Shape Detection', 360, 0) cv2.waitKey(1)
def Create_Window(self, State): ''' create window with window name and current state. ''' cv2.namedWindow(State, flags=0) cv2.resizeWindow(State, self.VisualParamDict['resolutionWid'], self.VisualParamDict['resolutionHgt']) cv2.moveWindow(State, 50, 50)
def get_state(self,img): img = cv2.GaussianBlur(img, (3, 3), 1) t, img = cv2.threshold(img,0,255,cv2.THRESH_OTSU) cv2.imshow("otsu",img) w = img.shape[1] h = img.shape[0] grids=9 gaph=h//grids gapw=w//grids margin=8 ans="" for i in xrange(9): for j in xrange(9): s=img[i*gapw+margin+5:i*gapw+gapw-margin+5,j*gapw+margin+3:j*gapw+gapw-margin+3] mask = cv2.bitwise_not(s) kernel = np.ones((5,5), np.uint8) mask = cv2.dilate(mask, kernel) mask = cv2.erode(mask, kernel) cnts, _ = cv2.findContours(mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) cnt = sorted(cnts, key=cv2.contourArea, reverse=True) if len(cnt)>0 and cv2.contourArea(cnt[0])>50: x, y, w, h = cv2.boundingRect(cnt[0]) sub = cv2.resize(s[y:y + h,x:x + w],(30,30)) t,sub=cv2.threshold(sub,0,255,cv2.THRESH_OTSU) cv2.imshow("%d"%(i*9+j),sub) cv2.moveWindow("%d"%(i*9+j),j*200,i*100) ans+=find(sub) else: ans+="0" ans+=" " print ans #cv2.imshow("after", img) return ans
def convert_to_pickle(infilename, outfilename, dsize, channel, rotate, format, quality, flip_vert, flip_hor): cam = cv2.VideoCapture(infilename) fc = lm.FrameCollection() cv2.namedWindow("Image") cv2.moveWindow("Image", 50, 50) while True: (ret, im) = cam.read() if not ret: break if dsize is not None: osize = tuple(map(lambda x: int(x), dsize.split("x"))) im=cv2.resize(im, dsize=osize, interpolation=cv2.INTER_CUBIC) if rotate is not None and rotate in ["90", "180", "270"]: im = np.rot90(im) if rotate in ["180", "270"]: im = np.rot90(im) if rotate in ["270"]: im = np.rot90(im) if flip_vert: im = cv2.flip(im, 0) if flip_hor: im = cv2.flip(im, 1) cv2.imshow("Image", im) key = cv2.waitKey(1) if key == 27: quit() f = lm.LabeledMovieFrame(internal_storage_method=format, compression_level=int(quality)) f.create_channel(channel=channel) f.set_image(im, channel=channel) f.set_default_channel(channel=channel) fc.append(f) fc.write_to_file(outfilename)
def find_blob(im): im[:30, :] = 0 im[-30:, :] = 0 im[:, :10] = 0 im[:, -10:] = 0 # thresh, im_bw = cv2.threshold(im, 0, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU) # print thresh thresh, im_bw = cv2.threshold(im, 100, 255, cv2.THRESH_BINARY) contours, hierarchy = cv2.findContours(im_bw.copy(), cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE) # moments = [cv2.moments(i).nu20 for i in contours] # all_bbox = [cv2.boundingRect(i) for i in contours] all_area = np.array([cv2.contourArea(c) for c in contours]) # all_aspect_ratio = np.array([float(b[2]) / b[3] for b in all_bbox]) print all_area big_indices = np.nonzero(all_area > config.AREA_THRESH)[0] print big_indices ellipses = [cv2.fitEllipse(contours[i]) for i in big_indices] color = [0, 255, 0] im_rgb = cv2.cvtColor(im_bw, cv2.COLOR_GRAY2RGB) for ell in ellipses: center, dimension, angle = ell h, w = dimension print h, w cv2.ellipse(im_rgb, ell, color, 2, 8) cv2.namedWindow('threshold') cv2.moveWindow('threshold', 800, 100) cv2.imshow('threshold', im_rgb) cv2.waitKey() cv2.destroyAllWindows()
def displayFeaturePics(self, windowName, startX, startY): """Given a window name and a starting location on the screen, this creates an image that represents the color signature and displays it.""" # Override this in the child class displayPic = self.image.copy() cv2.imshow(windowName, displayPic) cv2.moveWindow(windowName, startX, startY)
def update(self): if self.img is not None: img = self.img.copy() # avoid overwriting original self.detector.calculate_best_position(img, max_iters=1) cv2.imshow('mask', self.detector.mask) cv2.moveWindow('mask', img.shape[1] + 2, 0) cv2.imshow(self.window, img)
def classifyEyes(self,img,bBox): """Run Eyes Cascade Classifier on Image""" EYE_MIN_SIZE = 0.15; bBoxScaled = bBox*EYE_SCALE; eyesROI = img[bBoxScaled[1]:bBoxScaled[3], bBoxScaled[0]:bBoxScaled[2]]; eyesROI = cv2.equalizeHist(eyesROI); # print 'eyesROI dimensions: ',eyesROI.shape; minEyeSize = eyesROI.shape[1]*EYE_MIN_SIZE; # print 'minEyeSize:',minEyeSize; cv2.imshow("eyesROI",eyesROI); cv2.moveWindow("eyeROI",150,300); rectsScaled = self.classify(eyesROI, self.eyeClassifier, minEyeSize); # print rectsScaled; # Scale back to full size rects = rectsScaled / EYE_SCALE; # Loop over each eye for eye in rects: # Adjust coordinates to be in faceRect's coordinate space eye += numpy.array([bBox[0],bBox[1],bBox[0],bBox[1]]); return rects;
def run(self): i = 0 cv2.namedWindow("image") cv2.moveWindow("image", 50, 50) while True: p = self.get_next_image_file() if p is None: break img = np.memmap(p, dtype=np.uint8, mode="r", shape=self.shape) F = LabeledMovieFrame(internal_storage_method=self.format, compression_level=int(self.quality)) if self.rotate is not None and self.rotate in ["90", "180", "270"]: img = np.rot90(img) if self.rotate in ["180", "270"]: img = np.rot90(img) if self.rotate in ["270"]: img = np.rot90(img) if self.flip_vert: img = cv2.flip(img, 0) if self.flip_hor: img = cv2.flip(img, 1) if self.dsize.count("x") == 1: osize = tuple(map(lambda x: int(x), self.dsize.split("x"))) else: factor = float(self.dsize) osize = int(img.shape[1]*factor), int(img.shape[0]*factor) img = cv2.resize(img, dsize=osize, interpolation=cv2.INTER_CUBIC) F.set_image(img) cv2.imshow("image", img) cv2.waitKey(40) i += 1 self.frame_collection.append(F) self.frame_collection.write_to_file(self.output_file)
cv2.rectangle(frame, (0, 500), (650, 750), (0, 0, 0), cv2.FILLED) ## add text to frame font = cv2.FONT_HERSHEY_SIMPLEX cv2.putText(frame, "User Chose: ", (0, 600), font, 1, (0, 0, 255), 2, cv2.LINE_AA) cv2.putText(frame, user_choice, (200, 600), font, 1, (0, 0, 255), 2, cv2.LINE_AA) cv2.putText(frame, "Computer Chose: ", (0, 650), font, 1, (0, 0, 255), 2, cv2.LINE_AA) cv2.putText(frame, cpu_choice, (300, 650), font, 1, (0, 0, 255), 2, cv2.LINE_AA) cv2.putText(frame, text_result, (0, 700), font, 1, (0, 0, 255), 2, cv2.LINE_AA) cv2.putText(frame, "Show Rock, Paper or Scissors in Box", (0, 40), font, 1, (0, 0, 255), 2, cv2.LINE_AA) cv2.putText(frame, "PRESS 'c' TO PLAY", (0, 90), font, 1, (0, 0, 255), 2, cv2.LINE_AA) ## show frame and mask optimazed for 13" macbook screen ## suggested to put terminal window in bottom left corner of screen cv2.namedWindow('frame', cv2.WINDOW_NORMAL) cv2.resizeWindow('frame', (800, 1200)) cv2.imshow('mask', mask) cv2.imshow('frame', frame) cv2.moveWindow("mask", 20, 20) cv2.moveWindow("frame", 500, 0) ## release and destroy windows cap.release() cv2.destroyAllWindows()
cv2.drawContours(frame, contours, index, lightblue, 8) table.putValue('rectFound', isRect) table.putValue('crossFound', isCross) if (isRect): print("rectangle") proc.calculate(focalLength, rectActualWidth, Imagewidth, Xmid - imgXcenter, imgYcenter - Ymid) table.putValue('rectAzi', proc.getAzimuth()) else: table.putValue('rectAzi', -1.0) if (isCross): print("cross") proc.calculate(focalLength, crossActualWidth, Imagewidth, Xmid - imgXcenter, imgYcenter - Ymid) table.putValue('crossAzi', proc.getAzimuth()) else: table.putValue('crossAzi', -1.0) contoured = cv2.resize(frame, None, fx=0.5, fy=0.5) threshed = cv2.resize(threshold, None, fx=0.5, fy=0.5) displayValues() # method displays values in terminal cv2.imshow("contoured", contoured) cv2.imshow("threshed", threshed) cv2.moveWindow("contoured", 0, 20) cv2.moveWindow("threshed", 650, 20) key = cv2.waitKey(10) if key == 27: cv2.destroyAllWindows() break
# define range of yellow color in HSV lower_color = np.array([20, 65, 80]) upper_color = np.array([65, 255, 255]) # Threshold the HSV image to get only yellow colors binary_mask = cv2.inRange(hsvImageInput, lower_color, upper_color) # mask the image to only show yellow or green images # Bitwise-AND mask and original image yellow_mask = cv2.bitwise_and(imgImageInput, imgImageInput, mask=binary_mask) # display the masked images to screen cv2.imshow('hsvImageInput', hsvImageInput) cv2.moveWindow('hsvImageInput', 100, 50) #cv2.imshow('binary_mask',binary_mask) cv2.imshow('yellow_masked', yellow_mask) cv2.moveWindow('yellow_masked', 200, 650) ### wait for user to press any key #while (True): # k = cv2.waitKey(0) # break # generate the contours and display imgFindContourReturn, contours, hierarchy = cv2.findContours( binary_mask, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) imgContours = yellow_mask.copy()
def showPositionedWindow(window_name, img_name, coords): cv2.namedWindow(window_name) cv2.moveWindow(window_name, coords[0], coords[1]) cv2.imshow(window_name, img_name)
import numpy as np import sys import cv2 as cv cam = cv.VideoCapture(0) while True: if cv.waitKey(1) == ord('q'): break ret, frame = cam.read() cv.imshow("SOURCE", frame) cv.moveWindow('SOURCE', 0, 0) #imScale = 50 # percent of original size #width = int(frame.shape[1]*imScale/100) #height = int(frame.shape[0]*imScale/100) #dim = (width,height) #resizedFrame = cv.resize(frame,dim,interpolation = cv.INTER_AREA) ##print("Resized Dimansions: ", resizedFrame.shape) ##cv.imshow("Resized", resizedFrame) roi = frame[0:50, 0:300] roiGray = cv.cvtColor(roi, cv.COLOR_BGR2GRAY) #gray = cv.cvtColor(Frame, cv.COLOR_BGR2GRAY) #gray = cv.cvtColor(resizedFrame, cv.COLOR_BGR2GRAY) # Apply adaptiveThreshold at the bitwise_not of gray, notice the ~ symbol # gray = cv.bitwise_not(gray) gray = cv.bitwise_not(roiGray)
import numpy as np import cv2 src = cv2.imread( '/Users/hyunsul/Desktop/ai-room/OpenCV2_python/ch03/candies.png') #src = cv2.imread('candies2.png') if src is None: print('Image load failed!') sys.exit() src_hsv = cv2.cvtColor(src, cv2.COLOR_BGR2HSV) # (B,R,G) dst1 = cv2.inRange(src, (0, 128, 0), (100, 255, 100)) dst2 = cv2.inRange(src_hsv, (50, 150, 0), (80, 255, 255)) cv2.imshow('src', src) cv2.moveWindow('src', 20, 50) #RGB 밝은 영상이 들어왔을떄는 좋고 cv2.imshow('dst1', dst1) cv2.moveWindow('dst1', 700, 30) #HSV 흑백영상을 받았을때는 밝기정보를 함께받는 HSV가 좋음. cv2.imshow('dst2', dst2) cv2.moveWindow('dst2', 700, 500) cv2.waitKey() cv2.destroyAllWindows()
predictor = create_vgg_ssd_predictor(net, candidate_size=200) cap = cv2.VideoCapture(args.input) if (cap.isOpened() == False): print("Unable to read camera feed") frame_width = int(cap.get(3)) frame_height = int(cap.get(4)) frameCount = 0 frameWait = 1400 # Wait this many frames before looking for next truck frameFound = -frameWait if args.show: cv2.namedWindow('Result', cv2.WINDOW_AUTOSIZE) cv2.moveWindow('Result', 0, 0) # Output of recording file out = None fourcc = cv2.VideoWriter_fourcc(*'MPEG') fgbg = cv2.createBackgroundSubtractorMOG2(300, 400, False) recording = False startedRecordingFrame = 0 MIN_BG_COUNT = 3000 # Minimum amount of pixels that need to be found in order to start recording FRAMES_TO_RECORD = frameWait # Will record the next frames until triggered = False # Allow only one trigger per application
if matches[best_match_index]: name = Names[best_match_index] top = int(top / scaleFactor) right = int(right / scaleFactor) bottom = int(bottom / scaleFactor) left = int(left / scaleFactor) cv2.rectangle(frame, (left, top), (right, bottom), (0, 0, 255), 2) cv2.putText(frame, name, (left, top - 6), font, .75, (0, 0, 255), 2) print("Day la mat cua: ", name) recogName = name data = (1, recogName) db.updateStudentsStatus(myDB, cursor, data) dt = time.time() - timeStamp fps = 1 / dt fpsReport = .90 * fpsReport + .1 * fps print('fps is:', round(fpsReport, 1)) timeStamp = time.time() cv2.rectangle(frame, (0, 0), (100, 40), (0, 0, 255), -1) cv2.putText(frame, str(round(fpsReport, 1)) + 'fps', (0, 25), font, .75, (0, 255, 255, 2)) cv2.imshow('Picture', frame) cv2.moveWindow('Picture', 0, 0) if cv2.waitKey(1) == ord('q'): break cam.release() cv2.destroyAllWindows()
print("Diretório de trabalho: ", os.getcwd()) def substitui_x_por_branco(gray): """ Localiza todas as posições em que há um X 3x3 na imagem de entrada e pinta estas posições com tom de cinza 127. Na saída deve ficar um quadrado cinza onde havia X """ res = gray.copy() return res if __name__ == "__main__": img = cv2.imread("black_white_dots_cross_80_60.png") gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) # Faz o processamento saida = substitui_x_por_branco(gray) cv2.imwrite("ex6_troca_x.png", saida) # NOTE que a OpenCV terminal trabalha com BGR cv2.imshow('entrada', img) cv2.moveWindow('entrada',200,200) cv2.imshow('saida', saida) cv2.moveWindow('saida',800,200) cv2.waitKey() cv2.destroyAllWindows()
line = file.readlines() prerect = line[0].split(' ') prerect = list(map(int, prerect)) print(prerect) else: filename = sys.argv[1] img = cv.imread(filename) img2 = img.copy() mask = np.zeros(img.shape[:2], dtype=np.uint8) output = np.zeros(img.shape, np.uint8) cv.namedWindow('output') cv.namedWindow('input') cv.setMouseCallback('input', onmouse) cv.moveWindow('input', img.shape[1] + 10, 90) def reset(): print("resetting \n") rect = (0, 0, 1, 1) drawing = False rectangle = False rect_or_mask = 100 rect_over = False value = DRAW_FG while (1): cv.imshow('output', output) cv.imshow('input', img) k = cv.waitKey(1) if k == 27:
def calibrate(self, calibImagePathsList, showDebugImages=False, nx=9, ny=6): """ `calibImagePathsList` Input vector of paths to calibration files `showDebugImages` Input to show debug images while calibration if True `nx` Input number of corners in X direction `ny` Input number of corners in Y direction Calibrates the camera based on the given calibration filename list and the number of chessboard corners in x and y direction returns if the calibration has been successful """ print("Camera Calibrating...") # start uncalibrated when running calibration routine self.calibrated = False if calibImagePathsList and (nx > 0) and (ny > 0): # build vectors for imagepoints and objectpoints imgpoints = [] # 2D points in image plane objpoints = [] # 3D points in real world # prepare the object points according to the parameters (z stays 0 # as the chessboards are on a flat surface) objp = np.zeros((nx * ny, 3), np.float32) objp[:, :2] = np.mgrid[0:nx, 0:ny].T.reshape(-1, 2) # x, y coordinates if showDebugImages == True: cv2.namedWindow("Calibration Debug", cv2.WINDOW_AUTOSIZE) cv2.moveWindow("Calibration Debug", 0, 0) for path in calibImagePathsList: # load the image image = mpimg.imread(path) # convert image to grayscale gray = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY) # find chessboard corners for further detection ret, corners = cv2.findChessboardCorners(gray, (nx, ny), None) if ret == True: imgpoints.append(corners) objpoints.append(objp) # draw the corners on debug if showDebugImages == True: cv2.drawChessboardCorners(image, (nx, ny), corners, ret) cv2.imshow("Calibration Debug", image) cv2.waitKey(1000) # calibrate the camera if valid points have been found if len(objpoints) > 0: ret, self.mtx, self.distCoeffs, rvecs, tvecs = \ cv2.calibrateCamera(objpoints, imgpoints, gray.shape[::-1], \ None, None) self.calibrated = True if showDebugImages == True: cv2.destroyWindow("Calibration Debug") return self.calibrated
print(len(example_contour)) elif key & 0xFF == ord('1'): example_contour.append(hand) images.append(temp) labels.append(1) print(len(example_contour)) elif key & 0xFF == ord('2'): example_contour.append(hand) images.append(temp) labels.append(2) print(len(example_contour)) elif key & 0xFF == ord('3'): example_contour.append(hand) images.append(temp) labels.append(3) print(len(example_contour)) elif key & 0xFF == ord('4'): example_contour.append(hand) images.append(temp) labels.append(4) print(len(example_contour)) elif key & 0xFF == ord('5'): example_contour.append(hand) images.append(temp) labels.append(5) print(len(example_contour)) cv2.imshow('Place your hand in the rectangle', img) cv2.imshow('Contour', temp) cv2.moveWindow('Contour', 600, 0)
def showResults(data_path, anno_path, results=None): # img = cv2.imread(data_path) data_expr = os.path.join( data_path, "*.jpg" ) data_paths = glob.glob(data_expr) image_ids = [str(i) for i in range(len(data_paths))] maxArea = 50 for ids, path in enumerate(data_paths): imgname = os.path.basename(os.path.splitext(path)[0]) annotation_dir = os.path.join(anno_path, "annotation_%s" % imgname) # information output dir os.makedirs(annotation_dir, exist_ok=True) # txt file output annotation_path = os.path.join(annotation_dir, "%s.txt" % imgname) img = cv2.imread(path) img_nxt = img.copy() # the copy of the original img for processing img_now = img.copy() # the copy of the original img for processing print('The image is %s' % imgname) cv2.namedWindow(window_name, 0) cv2.imshow(window_name, img) cv2.moveWindow(window_name, move_x, move_y) print('press any key to start, \'p\' to pass : '******'press any key to start, p to pass : '******'p': continue img_bor = cv2.imread(path, cv2.IMREAD_GRAYSCALE) img_bor = accessBinary(img_bor) _, contours, _ = cv2.findContours(img_bor, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE) borders = [] for contour in contours: # 将边缘拟合成一个边框 x, y, w, h = cv2.boundingRect(contour) if w * h > maxArea: border = [(x, y), (x + w, y + h)] borders.append(border) new_str = '' for i, border in enumerate(borders): cv2.rectangle(img_nxt, border[0], border[1], (255, 0, 0)) # box in blue to label cv2.namedWindow(window_name, 0) cv2.imshow(window_name, img_nxt) cv2.moveWindow(window_name, move_x, move_y) label = chr(cv2.waitKey(0)) while label != 'u' and label_class.get(label, None) is None: print('The input is invalide, re-print the label:') label = chr(cv2.waitKey(0)) # if the label is 'u', then drop the box if label == 'u': img_nxt = img_now.copy() cv2.namedWindow(window_name, 0) cv2.imshow(window_name, img_nxt) cv2.moveWindow(window_name, move_x, move_y) print("drop") continue label_tran = label_transfer(label) print('The %d rectangle labeld: %s [%s]' % (i + 1, label, label_tran)) cv2.rectangle(img_now, border[0], border[1], (0, 0, 255)) # save the labeled box in red img_nxt = img_now.copy() cv2.namedWindow(window_name, 0) cv2.imshow(window_name, img_nxt) cv2.moveWindow(window_name, move_x, move_y) xmin = str(border[0][0]) ymin = str(border[0][1]) xmax = str(border[1][0]) ymax = str(border[1][1]) new_str += " " + ",".join( [xmin, ymin, xmax, ymax, str(label_tran)] ) if os.path.exists(annotation_path): os.remove(annotation_path) # print(new_str) with open(annotation_path, "a") as f: annotation = path # path of the img annotation += new_str # label info annotation += "\n" # next line print(annotation) f.write(annotation)