def main(camera_toml_path, enable_distortion_correction, scale_val = 0.65): camera_config = get_config(camera_toml_path) camera = Camera(camera_config) print(camera) scaling = partial(scaling_int, scale=scale_val) if camera_config.roi_size != 4: sys.exit('This script is only supported on "camera_config.roi_size == 4" ') if camera_config.auto_exposure != "roi": sys.exit('This script is only supported on "camera_config.auto_exposure == roi" ') image_width = camera.image_width image_height = camera.image_height roi = cvui.Rect(0, 0, 0, 0) WINDOW_NAME = "Capture" cvui.init(WINDOW_NAME) click_pos_x = image_width // 2 click_pos_y = image_height // 2 while True: key = cv2.waitKey(10) frame = np.zeros((scaling(image_height), scaling(image_width), 3), np.uint8) frame[:] = (49, 52, 49) status = camera.update() if status: # WARNING:If distortion correction is enabled, the rectangle on windows doesn't indicate actual RoI area for auto exposure. see3cam_rgb_image = camera.remap_image if enable_distortion_correction else camera.image scaled_width = scaling(image_width) scaled_height = scaling(image_height) see3cam_rgb_image_resized = cv2.resize(see3cam_rgb_image, (scaled_width, scaled_height)) frame[:scaled_height, :scaled_width, :] = see3cam_rgb_image_resized window_w = image_width // 2 window_h = image_height // 2 if cvui.mouse(cvui.DOWN): click_pos_x = int(cvui.mouse().x / scale_val) click_pos_y = int(cvui.mouse().y / scale_val) camera.set_roi_properties(click_pos_x, click_pos_y, win_size=4) roi = cvui.Rect(scaling(click_pos_x - image_width // 4), scaling(click_pos_y - image_height // 4), scaling(window_w), scaling(window_h)) # Ensure ROI is within bounds roi.x = 0 if roi.x < 0 else roi.x roi.y = 0 if roi.y < 0 else roi.y roi.width = roi.width + scaled_width - (roi.x + roi.width) if roi.x + roi.width > scaled_width else roi.width roi.height = roi.height + scaled_height - (roi.y + roi.height) if roi.y + roi.height > scaled_height else roi.height cvui.rect(frame, roi.x, roi.y, roi.width, roi.height, 0xFF0000) if key == 27 or key == ord("q"): break cvui.update() cvui.imshow(WINDOW_NAME, frame) cv2.destroyAllWindows()
def handsign_capture(self, local_image, global_image, x, y, w, h): cvui.text(global_image, 0, 0, f"images: {self.counter}") thresh=cv2.getTrackbarPos("thresh", "Control") local_image = cv2.cvtColor(local_image, cv2.COLOR_BGR2GRAY) local_image= local_image[y:y+h, x:x+w] ret, local_image = cv2.threshold(local_image, thresh, 255, cv2.THRESH_BINARY_INV) cvui.rect(global_image, x, y, w , h, 0xff0000) cv2.imshow("Image", local_image)
def main(): frame = np.zeros((300, 600, 3), np.uint8) # Init cvui and tell it to create a OpenCV window, i.e. cv::namedWindow(WINDOW_NAME). cvui.init(WINDOW_NAME) while (True): # Fill the frame with a nice color frame[:] = (49, 52, 49) # Render a rectangle on the screen. rectangle = cvui.Rect(50, 50, 100, 100) cvui.rect(frame, rectangle.x, rectangle.y, rectangle.width, rectangle.height, 0xff0000) # Check what is the current status of the mouse cursor # regarding the previously rendered rectangle. status = cvui.iarea(rectangle.x, rectangle.y, rectangle.width, rectangle.height) # cvui::iarea() will return the current mouse status: # CLICK: mouse just clicked the interaction are # DOWN: mouse button was pressed on the interaction area, but not released yet. # OVER: mouse cursor is over the interaction area # OUT: mouse cursor is outside the interaction area if status == cvui.CLICK: print('Rectangle was clicked!') if status == cvui.DOWN: cvui.printf(frame, 240, 70, "Mouse is: DOWN") if status == cvui.OVER: cvui.printf(frame, 240, 70, "Mouse is: OVER") if status == cvui.OUT: cvui.printf(frame, 240, 70, "Mouse is: OUT") # Show the coordinates of the mouse pointer on the screen cvui.printf(frame, 240, 50, "Mouse pointer is at (%d,%d)", cvui.mouse().x, cvui.mouse().y) # This function must be called *AFTER* all UI components. It does # all the behind the scenes magic to handle mouse clicks, etc. cvui.update() # Show everything on the screen cv2.imshow(WINDOW_NAME, frame) # Check if ESC key was pressed if cv2.waitKey(20) == 27: break
def main(): frame = np.zeros((300, 600, 3), np.uint8) # Init cvui and tell it to create a OpenCV window, i.e. cv::namedWindow(WINDOW_NAME). cvui.init(WINDOW_NAME) while (True): # Fill the frame with a nice color frame[:] = (49, 52, 49) # Render a rectangle on the screen. rectangle = cvui.Rect(50, 50, 100, 100) cvui.rect(frame, rectangle.x, rectangle.y, rectangle.width, rectangle.height, 0xff0000) # Check what is the current status of the mouse cursor # regarding the previously rendered rectangle. status = cvui.iarea(rectangle.x, rectangle.y, rectangle.width, rectangle.height); # cvui::iarea() will return the current mouse status: # CLICK: mouse just clicked the interaction are # DOWN: mouse button was pressed on the interaction area, but not released yet. # OVER: mouse cursor is over the interaction area # OUT: mouse cursor is outside the interaction area if status == cvui.CLICK: print('Rectangle was clicked!') if status == cvui.DOWN: cvui.printf(frame, 240, 70, "Mouse is: DOWN") if status == cvui.OVER: cvui.printf(frame, 240, 70, "Mouse is: OVER") if status == cvui.OUT: cvui.printf(frame, 240, 70, "Mouse is: OUT") # Show the coordinates of the mouse pointer on the screen cvui.printf(frame, 240, 50, "Mouse pointer is at (%d,%d)", cvui.mouse().x, cvui.mouse().y) # This function must be called *AFTER* all UI components. It does # all the behind the scenes magic to handle mouse clicks, etc. cvui.update() # Show everything on the screen cv2.imshow(WINDOW_NAME, frame) # Check if ESC key was pressed if cv2.waitKey(20) == 27: break
def drawGUI(width, x): global actionValue global moveValue frame[:] = (49, 52, 49) cvui.rect(frame, 2, 2, 355, 120, 0xffaa77, 0x4444aa) cvui.rect(frame, 2, 125, 355, 55, 0xffaa77, 0x4444aa) ######################################################### cvui.text(frame, x, 5, 'repeatValue (0 - 10)') cvui.trackbar(frame, x, 10, width, repeatValue, 0, 10, 1, '%.0Lf') cvui.text(frame, x, 65, 'moveValue (10 - 100)') cvui.trackbar(frame, x, 75, width, moveValue, 1, 100, 1, '%.0Lf') ######################################################### cvui.text(frame, x + (80 * 1.25), 130, "role") if cvui.button(frame, x, 145, "Horizon"): return "Horizon" if cvui.button(frame, x + (80 * 1.25), 145, "Vertical"): return "Vertical" if cvui.button(frame, x + (80 * 2.5), 145, "Stop"): return "Stop" if cvui.button(frame, x + (80 * 3.5), 145, "Exit"): exit() #cvui.update() cv2.imshow(WINDOW_NAME, frame) return ""
def group(frame, x, y, width, height): padding = 5 w = (width - padding) / 4 h = (height - 15 - padding) / 2 pos = cvui.Point(x + padding, y + 5) cvui.text(frame, pos.x, pos.y, 'Group title') pos.y += 15 cvui.window(frame, pos.x, pos.y, width - padding * 2, h - padding, 'Something') cvui.rect(frame, pos.x + 2, pos.y + 20, width - padding * 2 - 5, h - padding - 20, 0xff0000) pos.y += h cvui.window(frame, pos.x, pos.y, w / 3 - padding, h, 'Some') cvui.text(frame, pos.x + 25, pos.y + 60, '65', 1.1) pos.x += w / 3 cvui.window(frame, pos.x, pos.y, w / 3 - padding, h, 'Info') cvui.text(frame, pos.x + 25, pos.y + 60, '30', 1.1) pos.x += w / 3 cvui.window(frame, pos.x, pos.y, w / 3 - padding, h, 'Here') cvui.text(frame, pos.x + 25, pos.y + 60, '70', 1.1) pos.x += w / 3 cvui.window(frame, pos.x, pos.y, w - padding, h, 'And') cvui.rect(frame, pos.x + 2, pos.y + 22, w - padding - 5, h - padding - 20, 0xff0000) pos.x += w cvui.window(frame, pos.x, pos.y, w - padding, h, 'Here') cvui.rect(frame, pos.x + 2, pos.y + 22, w - padding - 5, h - padding - 20, 0xff0000) pos.x += w cvui.window(frame, pos.x, pos.y, w - padding, h, 'More info') cvui.rect(frame, pos.x + 2, pos.y + 22, w - padding - 5, h - padding - 20, 0xff0000) pos.x += w
def onWork(self): self.frame[:] = (49, 52, 49) char = cv.waitKey(1) doOverw = False writeOnName = False writeOnSubject = False writeOnProf = False stringCopy = '' stringCopyColor = 0x10dca1 while True: cursor = cvui.mouse(WINDOW_NAME) click = cvui.mouse(cvui.CLICK) # (self, x, y, w, h, title, char, writeOn, cursor, click, writeOnBool): writeOnName, self.folderName = self.textInput( 5, 5, 305, 40, "Folder Name:", char, self.folderName, cursor, click, writeOnName) writeOnSubject, self.subjectName = self.textInput( 5 + X_SCREEN / 2, 5, 305, 40, "Subject Name:", char, self.subjectName, cursor, click, writeOnSubject) writeOnProf, self.tempName = self.textInput( 5 + X_SCREEN / 2, 105, 305, 40, "Professors:", char, self.tempName, cursor, click, writeOnProf) cvui.rect(self.frame, 6 + X_SCREEN / 2, 155, X_SCREEN / 2 - 63, 235 - 165, 0x8d9797, 0x3c4747) cvui.rect(self.frame, X_SCREEN - 52, 155, 47, 235 - 165, 0x8d9797, 0x293939) if cvui.button( self.frame, X_SCREEN - 49, 157, "+") and not self.tempName == '' and len(self.names) < 4: self.names.append(self.tempName) self.tempName = '' if cvui.button(self.frame, X_SCREEN - 49, 196, "-") and not len(self.names) == 0: del self.names[-1] for i in range(len(self.names)): cvui.printf(self.frame, 10 + X_SCREEN / 2, 160 + 15 * i, 0.4, 0xdd97fb, self.names[i]) xp = int((X_SCREEN - 20) / 4) cvui.window(self.frame, 5, 235, X_SCREEN - 10, Y_SCREEN - 235 - 5, "Premade:") if cvui.button(self.frame, 10 + xp - 10 * 11, 260, "Lab. Micro."): self.subjectName = '22.99 Laboratorio De Microprocesadores' self.names.clear() self.names.append('Jacoby, Daniel Andres') self.names.append('Magliola, Nicolas') self.names.append('Ismirlian, Diego Matias') self.group[0] = 3 if cvui.button(self.frame, 10 + xp * 2 - 10 * 7, 260, "Control"): self.subjectName = '22.85 Sistemas de Control' self.names.clear() self.names.append('Nasini, Victor Gustavo') self.names.append('Zujew, Cristian Alejo') if cvui.button(self.frame, 10 + xp * 3 - 10 * 9, 260, "Transinfo"): self.subjectName = '22.61 Transmision de la Informacion' self.names.clear() self.names.append('Bertucci, Eduardo Adolfo') self.names.append('Vila Krause, Luis Gustavo') if cvui.button(self.frame, 10 + xp * 4 - 10 * 11, 260, "Electromag."): self.subjectName = '22.37 Electromagnetismo' self.names.clear() self.names.append('Munoz, Claudio Marcelo') self.names.append('Dobrusin, Pablo') cvui.printf(self.frame, 5, 55, 0.4, 0xdd97fb, f'N Exercises:') cvui.counter(self.frame, 5, 70, self.numberFolder) if self.numberFolder[0] <= 0: cvui.rect(self.frame, 5 + 25, 72, 40, 17, 0x292929, 0x292929) self.numberFolder[0] = 1 cvui.printf(self.frame, 5 + 41, 76, 0.4, 0x9C9C9C, '1') cvui.printf(self.frame, 5 + X_SCREEN / 6, 55, 0.4, 0xdd97fb, f'Day:') cvui.counter(self.frame, 5 + X_SCREEN / 6, 70, self.day) if self.day[0] <= 0: cvui.rect(self.frame, 5 + X_SCREEN / 6 + 25, 72, 40, 17, 0x292929, 0x292929) self.day[0] = 1 cvui.printf(self.frame, 5 + X_SCREEN / 6 + 41, 76, 0.4, 0x9C9C9C, '1') elif self.day[0] >= 31: self.day[0] = 31 cvui.printf(self.frame, 5 + X_SCREEN * 2 / 6, 55, 0.4, 0xdd97fb, f'Month:') cvui.counter(self.frame, 5 + X_SCREEN * 2 / 6, 70, self.month) if self.month[0] <= 0: cvui.rect(self.frame, 5 + X_SCREEN * 2 / 6 + 25, 72, 40, 17, 0x292929, 0x292929) self.month[0] = 1 cvui.printf(self.frame, 5 + X_SCREEN * 2 / 6 + 41, 76, 0.4, 0x9C9C9C, '1') elif self.month[0] >= 12: self.month[0] = 12 cvui.printf(self.frame, 5 + X_SCREEN * 3 / 6, 55, 0.4, 0xdd97fb, f'Year:') cvui.counter(self.frame, 5 + X_SCREEN * 3 / 6, 70, self.year) if self.year[0] <= 19: cvui.rect(self.frame, 5 + X_SCREEN * 3 / 6 + 25, 72, 40, 17, 0x292929, 0x292929) self.year[0] = 20 cvui.printf(self.frame, 5 + X_SCREEN * 3 / 6 + 37, 76, 0.4, 0x9C9C9C, '20') elif self.year[0] >= 22: self.year[0] = 22 cvui.printf(self.frame, 5 + X_SCREEN * 4 / 6, 55, 0.4, 0xdd97fb, f'N Group:') cvui.counter(self.frame, 5 + X_SCREEN * 4 / 6, 70, self.group) if self.group[0] <= 0: cvui.rect(self.frame, 5 + X_SCREEN * 4 / 6 + 25, 72, 40, 17, 0x292929, 0x292929) self.group[0] = 1 cvui.printf(self.frame, 5 + X_SCREEN * 4 / 6 + 41, 76, 0.4, 0x9C9C9C, '1') elif self.group[0] >= 7: self.group[0] = 7 cvui.printf(self.frame, 5 + X_SCREEN * 5 / 6, 55, 0.4, 0xdd97fb, f'N TP:') cvui.counter(self.frame, 5 + X_SCREEN * 5 / 6, 70, self.tp) if self.tp[0] <= 0: cvui.rect(self.frame, 5 + X_SCREEN * 5 / 6 + 25, 72, 40, 17, 0x292929, 0x292929) self.tp[0] = 1 cvui.printf(self.frame, 5 + X_SCREEN * 5 / 6 + 41, 76, 0.4, 0x9C9C9C, '1') if cvui.button(self.frame, X_SCREEN / 4 - 10 * 16 / 2, 110, "Load Folder Path") and not doOverw: self.folderPath = self.getPath() cvui.window(self.frame, 5, 155, X_SCREEN / 2 - 10, 40, "Folder Path Selected") if len(self.folderPath) < MAXCHAR + 3: cvui.printf(self.frame, 10, 180, 0.4, 0xdd97fb, self.folderPath) else: cvui.printf(self.frame, 10, 180, 0.4, 0xdd97fb, self.folderPath[0:MAXCHAR + 2] + '...') if (not self.folderPath == '') and (not self.folderName == '') and (not self.subjectName == ''): if cvui.button(self.frame, 120, 200, "Start Copy"): self.startCopy = True stringCopy = "Wait While Copying..." stringCopyColor = 0xdc1076 cvui.printf(self.frame, 5, 205, 0.4, stringCopyColor, stringCopy) if self.startCopy: self.startCopy = False while self.folderName[-1] == ' ': #32: self.folderName = self.folderName[:-1] finalPath = self.folderPath + '/' + self.folderName coping = self.copyFolders(self.numberFolder[0], finalPath, True) if not coping == None: if not coping: doOverw = True else: stringCopy = "Copy Done!" stringCopyColor = 0x10dca1 else: stringCopy = "Template Not Found!!" stringCopyColor = 0xdc1076 if doOverw: check = self.overWrite(finalPath) if not check == -1: if check == None: stringCopy = "Template Not Found!!" stringCopyColor = 0xdc1076 else: if check == True: stringCopy = "Copy Done!" stringCopyColor = 0x10dca1 else: stringCopy = "Unable To Copy" stringCopyColor = 0xdc1076 doOverw = False cvui.imshow(WINDOW_NAME, self.frame) char = cv.waitKey(1) self.frame[:] = (49, 52, 49) if (char == 27) or not cv.getWindowProperty( WINDOW_NAME, cv.WND_PROP_VISIBLE): break
def main(): frame = np.zeros((600, 800, 3), np.uint8) # Create variables used by some components values = [] checked = [False] checked2 = [False] value = [1.0] value2 = [1.0] value3 = [1.0] padding = 10 img = cv2.imread('lena-face.jpg', cv2.IMREAD_COLOR) imgRed = cv2.imread('lena-face-red.jpg', cv2.IMREAD_COLOR) imgGray = cv2.imread('lena-face-gray.jpg', cv2.IMREAD_COLOR) # Fill the vector with a few random values for i in range(0, 20): values.append(random.uniform(0., 300.0)) # Init cvui and tell it to create a OpenCV window, i.e. cv::namedWindow(WINDOW_NAME). cvui.init(WINDOW_NAME) while (True): # Fill the frame with a nice color frame[:] = (49, 52, 49) # In a row, all added elements are # horizontally placed, one next the other (from left to right) # # Within the cvui.beginRow() and cvui.endRow(), # all elements will be automatically positioned by cvui. # # Notice that all component calls within the begin/end block # DO NOT have (x,y) coordinates. # # Let's create a row at position (10,20) with width 100 and height 50. cvui.beginRow(frame, 10, 20, 100, 50) cvui.text('This is ') cvui.printf('a row') cvui.checkbox('checkbox', checked) cvui.window(80, 80, 'window') cvui.rect(50, 50, 0x00ff00, 0xff0000) cvui.sparkline(values, 50, 50) cvui.counter(value) cvui.button(100, 30, 'Fixed') cvui.image(img) cvui.button(img, imgGray, imgRed) cvui.endRow() # Here is another row, this time with a padding of 50px among components. padding = 50 cvui.beginRow(frame, 10, 150, 100, 50, padding) cvui.text('This is ') cvui.printf('another row') cvui.checkbox('checkbox', checked2) cvui.window(80, 80, 'window') cvui.button(100, 30, 'Fixed') cvui.printf('with 50px padding.') cvui.endRow() # Another row mixing several components cvui.beginRow(frame, 10, 250, 100, 50) cvui.text('This is ') cvui.printf('another row with a trackbar ') #cvui.trackbar(150, &value2, 0., 5.); cvui.printf(' and a button ') cvui.button(100, 30, 'button') cvui.endRow() # In a column, all added elements are vertically placed, # one below the other, from top to bottom. Let's create # a column at (50, 300) with width 100 and height 200. cvui.beginColumn(frame, 50, 330, 100, 200) cvui.text('Column 1 (no padding)') cvui.button('button1') cvui.button('button2') cvui.text('End of column 1') cvui.endColumn() # Here is another column, using a padding value of 10, # which will add an space of 10px between each component. padding = 10 cvui.beginColumn(frame, 300, 330, 100, 200, padding) cvui.text('Column 2 (padding = 10)') cvui.button('button1') cvui.button('button2') #cvui.trackbar(150, &value3, 0., 5., 1, '%3.2Lf', cvui.TRACKBAR_DISCRETE, 0.25); cvui.text('End of column 2') cvui.endColumn() # You can also add an arbitrary amount of space between # components by calling cvui.space(). # # cvui.space() is aware of context, so if it is used # within a beginColumn()/endColumn() block, the space will # be vertical. If it is used within a beginRow()/endRow() # block, space will be horizontal. cvui.beginColumn(frame, 550, 330, 100, 200) cvui.text('Column 3 (use space)') # Add 5 pixels of (vertical) space. cvui.space(5) cvui.button('button1 5px below') # Add 50 pixels of (vertical) space. cvui.space(50) cvui.text('Text 50px below') # Add 20 pixels of (vertical) space. cvui.space(20) cvui.button('Button 20px below') # Add 40 pixels of (vertical) space. cvui.space(40) cvui.text('End of column 2 (40px below)') cvui.endColumn() # This function must be called *AFTER* all UI components. It does # all the behind the scenes magic to handle mouse clicks, etc. cvui.update() # Show everything on the screen cv2.imshow(WINDOW_NAME, frame) # Check if ESC key was pressed if cv2.waitKey(20) == 27: break
def main(): lena = cv2.imread('lena.jpg') frame = np.zeros(lena.shape, np.uint8) anchors = [cvui.Point() for i in range(3)] # one anchor for each mouse button rois = [cvui.Rect() for i in range(3)] # one ROI for each mouse button colors = [0xff0000, 0x00ff00, 0x0000ff] # Init cvui and tell it to create a OpenCV window, i.e. cv.namedWindow(WINDOW_NAME). cvui.init(WINDOW_NAME) while (True): # Fill the frame with Lena's image frame[:] = lena[:] # Show the coordinates of the mouse pointer on the screen cvui.text(frame, 10, 10, 'Click (any) mouse button then drag the pointer around to select a ROI.') cvui.text(frame, 10, 25, 'Use different mouse buttons (right, middle and left) to select different ROIs.') # Iterate all mouse buttons (left, middle and right button) button = cvui.LEFT_BUTTON while button <= cvui.RIGHT_BUTTON: # Get the anchor, ROI and color associated with the mouse button anchor = anchors[button] roi = rois[button] color = colors[button] # The function 'bool cvui.mouse(int button, int query)' allows you to query a particular mouse button for events. # E.g. cvui.mouse(cvui.RIGHT_BUTTON, cvui.DOWN) # # Available queries: # - cvui.DOWN: mouse button was pressed. cvui.mouse() returns true for single frame only. # - cvui.UP: mouse button was released. cvui.mouse() returns true for single frame only. # - cvui.CLICK: mouse button was clicked (went down then up, no matter the amount of frames in between). cvui.mouse() returns true for single frame only. # - cvui.IS_DOWN: mouse button is currently pressed. cvui.mouse() returns true for as long as the button is down/pressed. # Did the mouse button go down? if cvui.mouse(button, cvui.DOWN): # Position the anchor at the mouse pointer. anchor.x = cvui.mouse().x anchor.y = cvui.mouse().y # Is any mouse button down (pressed)? if cvui.mouse(button, cvui.IS_DOWN): # Adjust roi dimensions according to mouse pointer width = cvui.mouse().x - anchor.x height = cvui.mouse().y - anchor.y roi.x = anchor.x + width if width < 0 else anchor.x roi.y = anchor.y + height if height < 0 else anchor.y roi.width = abs(width) roi.height = abs(height) # Show the roi coordinates and size cvui.printf(frame, roi.x + 5, roi.y + 5, 0.3, color, '(%d,%d)', roi.x, roi.y) cvui.printf(frame, cvui.mouse().x + 5, cvui.mouse().y + 5, 0.3, color, 'w:%d, h:%d', roi.width, roi.height) # Ensure ROI is within bounds lenaRows, lenaCols, lenaChannels = lena.shape roi.x = 0 if roi.x < 0 else roi.x roi.y = 0 if roi.y < 0 else roi.y roi.width = roi.width + lenaCols - (roi.x + roi.width) if roi.x + roi.width > lenaCols else roi.width roi.height = roi.height + lenaRows - (roi.y + roi.height) if roi.y + roi.height > lenaRows else roi.height # If the ROI is valid, render it in the frame and show in a window. if roi.area() > 0: cvui.rect(frame, roi.x, roi.y, roi.width, roi.height, color) cvui.printf(frame, roi.x + 5, roi.y - 10, 0.3, color, 'ROI %d', button) lenaRoi = lena[roi.y : roi.y + roi.height, roi.x : roi.x + roi.width] cv2.imshow('ROI button' + str(button), lenaRoi) button += 1 # This function must be called *AFTER* all UI components. It does # all the behind the scenes magic to handle mouse clicks, etc. cvui.update() # Show everything on the screen cv2.imshow(WINDOW_NAME, frame) # Check if ESC key was pressed if cv2.waitKey(20) == 27: break
def main(): # We have one mat for each window. frame1 = np.zeros((600, 800, 3), np.uint8) frame2 = np.zeros((600, 800, 3), np.uint8) # Create variables used by some components window1_values = [] window2_values = [] window1_checked = [False] window1_checked2 = [False] window2_checked = [False] window2_checked2 = [False] window1_value = [1.0] window1_value2 = [1.0] window1_value3 = [1.0] window2_value = [1.0] window2_value2 = [1.0] window2_value3 = [1.0] img = cv2.imread('lena-face.jpg', cv2.IMREAD_COLOR) imgRed = cv2.imread('lena-face-red.jpg', cv2.IMREAD_COLOR) imgGray = cv2.imread('lena-face-gray.jpg', cv2.IMREAD_COLOR) padding = 10 # Fill the vector with a few random values for i in range(0, 20): window1_values.append(random.uniform(0., 300.0)) window2_values.append(random.uniform(0., 300.0)) # Start two OpenCV windows cv2.namedWindow(WINDOW1_NAME) cv2.namedWindow(WINDOW2_NAME) # Init cvui and inform it to use the first window as the default one. # cvui.init() will automatically watch the informed window. cvui.init(WINDOW1_NAME) # Tell cvui to keep track of mouse events in window2 as well. cvui.watch(WINDOW2_NAME) while (True): # Inform cvui that all subsequent component calls and events are related to window 1. cvui.context(WINDOW1_NAME) # Fill the frame with a nice color frame1[:] = (49, 52, 49) cvui.beginRow(frame1, 10, 20, 100, 50) cvui.text('This is ') cvui.printf('a row') cvui.checkbox('checkbox', window1_checked) cvui.window(80, 80, 'window') cvui.rect(50, 50, 0x00ff00, 0xff0000) cvui.sparkline(window1_values, 50, 50) cvui.counter(window1_value) cvui.button(100, 30, 'Fixed') cvui.image(img) cvui.button(img, imgGray, imgRed) cvui.endRow() padding = 50 cvui.beginRow(frame1, 10, 150, 100, 50, padding) cvui.text('This is ') cvui.printf('another row') cvui.checkbox('checkbox', window1_checked2) cvui.window(80, 80, 'window') cvui.button(100, 30, 'Fixed') cvui.printf('with 50px paddin7hg.') cvui.endRow() cvui.beginRow(frame1, 10, 250, 100, 50) cvui.text('This is ') cvui.printf('another row with a trackbar ') cvui.trackbar(150, window1_value2, 0., 5.) cvui.printf(' and a button ') cvui.button(100, 30, 'button') cvui.endRow() cvui.beginColumn(frame1, 50, 330, 100, 200) cvui.text('Column 1 (no padding)') cvui.button('button1') cvui.button('button2') cvui.text('End of column 1') cvui.endColumn() padding = 10 cvui.beginColumn(frame1, 300, 330, 100, 200, padding) cvui.text('Column 2 (padding = 10)') cvui.button('button1') cvui.button('button2') cvui.trackbar(150, window1_value3, 0., 5., 1, '%3.2Lf', cvui.TRACKBAR_DISCRETE, 0.25) cvui.text('End of column 2') cvui.endColumn() cvui.beginColumn(frame1, 550, 330, 100, 200) cvui.text('Column 3 (use space)') cvui.space(5) cvui.button('button1 5px below') cvui.space(50) cvui.text('Text 50px below') cvui.space(20) cvui.button('Button 20px below') cvui.space(40) cvui.text('End of column 2 (40px below)') cvui.endColumn() # Update all components of window1, e.g. mouse clicks, and show it. cvui.update(WINDOW1_NAME) cv2.imshow(WINDOW1_NAME, frame1) # From this point on, we are going to render the second window. We need to inform cvui # that all updates and components from now on are connected to window 2. # We do that by calling cvui.context(). cvui.context(WINDOW2_NAME) frame2[:] = (49, 52, 49) cvui.beginRow(frame2, 10, 20, 100, 50) cvui.text('This is ') cvui.printf('a row') cvui.checkbox('checkbox', window2_checked) cvui.window(80, 80, 'window') cvui.rect(50, 50, 0x00ff00, 0xff0000) cvui.sparkline(window2_values, 50, 50) cvui.counter(window2_value) cvui.button(100, 30, 'Fixed') cvui.image(img) cvui.button(img, imgGray, imgRed) cvui.endRow() padding = 50 cvui.beginRow(frame2, 10, 150, 100, 50, padding) cvui.text('This is ') cvui.printf('another row') cvui.checkbox('checkbox', window2_checked2) cvui.window(80, 80, 'window') cvui.button(100, 30, 'Fixed') cvui.printf('with 50px paddin7hg.') cvui.endRow() # Another row mixing several components cvui.beginRow(frame2, 10, 250, 100, 50) cvui.text('This is ') cvui.printf('another row with a trackbar ') cvui.trackbar(150, window2_value2, 0., 5.) cvui.printf(' and a button ') cvui.button(100, 30, 'button') cvui.endRow() cvui.beginColumn(frame2, 50, 330, 100, 200) cvui.text('Column 1 (no padding)') cvui.button('button1') cvui.button('button2') cvui.text('End of column 1') cvui.endColumn() padding = 10 cvui.beginColumn(frame2, 300, 330, 100, 200, padding) cvui.text('Column 2 (padding = 10)') cvui.button('button1') cvui.button('button2') cvui.trackbar(150, window2_value3, 0., 5., 1, '%3.2Lf', cvui.TRACKBAR_DISCRETE, 0.25) cvui.text('End of column 2') cvui.endColumn() cvui.beginColumn(frame2, 550, 330, 100, 200) cvui.text('Column 3 (use space)') cvui.space(5) cvui.button('button1 5px below') cvui.space(50) cvui.text('Text 50px below') cvui.space(20) cvui.button('Button 20px below') cvui.space(40) cvui.text('End of column 2 (40px below)') cvui.endColumn() # Update all components of window2, e.g. mouse clicks, and show it. cvui.update(WINDOW2_NAME) cv2.imshow(WINDOW2_NAME, frame2) # Check if ESC key was pressed if cv2.waitKey(20) == 27: break
def fizeau(frame, expr): curr_expr = expr experiments = [[False] for i in range(2)] fizeau_default = cv2.imread(resource_path("images/fizeau_default.png"), 3) fizeau_not_default = cv2.imread(resource_path("images/fizeau_not_default.png"), 3) fizeau_line = cv2.imread(resource_path("images/fizeau_line.png"), 3) fizeau_distance = cv2.imread(resource_path("images/fizeau_distance.png"), 3) fizeau_dot = cv2.imread(resource_path("images/fizeau_dot.png"), cv2.IMREAD_UNCHANGED) fizeau_dot_grey = cv2.imread(resource_path("images/fizeau_dot_grey.png"), cv2.IMREAD_UNCHANGED) fizeau_sprocket = cv2.imread(resource_path("images/fizeau_sprocket.png"), cv2.IMREAD_UNCHANGED) fizeau_metre = cv2.imread(resource_path("images/fizeau_metre.png"), cv2.IMREAD_UNCHANGED) fizeau_blank = cv2.imread(resource_path("images/fizeau_blank.png"), cv2.IMREAD_UNCHANGED) fizeau_darkstripes = cv2.imread(resource_path("images/fizeau_darkstripes.png"), cv2.IMREAD_UNCHANGED) fizeau_lightstripes = cv2.imread(resource_path("images/fizeau_lightstripes.png"), cv2.IMREAD_UNCHANGED) wheel_sprites_pos = get_120(fizeau_default, fizeau_sprocket) wheel_sprites_pos = put_dots_pos(wheel_sprites_pos, fizeau_dot, fizeau_dot_grey) wheel_sprites_pos.rotate(30) wheel_sprites_pos = put_dots_pos(wheel_sprites_pos, fizeau_dot, fizeau_dot_grey) wheel_sprites_pos.rotate(30) wheel_sprites_pos = put_dots_pos(wheel_sprites_pos, fizeau_dot, fizeau_dot_grey) wheel_sprites_pos.rotate(30) wheel_sprites_pos = put_dots_pos(wheel_sprites_pos, fizeau_dot, fizeau_dot_grey) wheel_sprites_neg = get_120(fizeau_not_default, fizeau_sprocket) wheel_sprites_neg = put_dots_neg(wheel_sprites_neg, fizeau_dot, fizeau_dot_grey) wheel_sprites_neg.rotate(30) wheel_sprites_neg = put_dots_neg(wheel_sprites_neg, fizeau_dot, fizeau_dot_grey) wheel_sprites_neg.rotate(30) wheel_sprites_neg = put_dots_neg(wheel_sprites_neg, fizeau_dot, fizeau_dot_grey) wheel_sprites_neg.rotate(30) wheel_sprites_neg = put_dots_neg(wheel_sprites_neg, fizeau_dot, fizeau_dot_grey) animation = 0 frequency_tr = [6.0] while (True): frame[:] = (49,52,49) #Animation window coord = get_coord(frequency_tr[0]) if coord == 23: cvui.image(frame, 0, 0, wheel_sprites_neg[animation]) cvui.text(frame, 33, 200, "Calculation of the speed of light", 0.5) cvui.text(frame, 34, 220, "distance ... s = {:,} m" .format(8633), 0.5) cvui.text(frame, 34, 240, "number of teeth in sprocket ... N = 720", 0.5) cvui.text(frame, 34, 260, "c=4*s*N*f=4*{:,}*{:,}*{:,}={:,} km/s" .format(8633, 7200, round(frequency_tr[0], 2), round((4 * 8633 * 7200 * frequency_tr[0])/1000, 2)), 0.5) else: cvui.image(frame, 0, 0, wheel_sprites_pos[animation]) cvui.text(frame, 33, 200, "Calculation of the speed of light", 0.5) cvui.text(frame, 34, 220, "distance ... s = {:,} m" .format(8633), 0.5) cvui.text(frame, 34, 240, "number of teeth in sprocket ... N = 720", 0.5) cvui.text(frame, 34, 260, "c=4*s*N*f ... proper frequency wasn't found", 0.5) cvui.image(frame, 872, 499, fizeau_line) cvui.image(frame, 874, 687, fizeau_distance) animation += 1 if animation == 120: animation = 0 cvui.text(frame, 58, 660, "Light source") cvui.text(frame, 473, 660, "Semipermeable mirror") cvui.text(frame, 840, 660, "Sprocket") cvui.text(frame, 994, 673, "Distance") cvui.text(frame, 1145, 660, "Mirror") cvui.text(frame, 892, 277, "-> Light behind sprocket") img = get_graph(fizeau_metre, fizeau_blank, fizeau_darkstripes, fizeau_lightstripes, coord=0, crop_light=False) cvui.image(frame, 896, 298, img) cvui.text(frame, 892, 377, "<- Reflected light before sprocket") img = get_graph(fizeau_metre, fizeau_blank, fizeau_darkstripes, fizeau_lightstripes, coord=coord, crop_light=False) cvui.image(frame, 896, 397, img) cvui.text(frame, 434, 100, "Reflected light behind sprocket") img = get_graph(fizeau_metre, fizeau_blank, fizeau_darkstripes, fizeau_lightstripes, coord=coord, crop_light=True) cvui.image(frame, 434, 120, img) #Experiment settings window cvui.window(frame, 1033.5, 2, 243.5, 104, 'Experiment settings') cvui.trackbar(frame, 1030, 39, 249, frequency_tr, 0.01, 12.0577) cvui.rect(frame, 1035, 39, 240, 12, 0x313131, 0x313131) cvui.rect(frame, 1035, 74, 240, 25, 0x313131, 0x313131) cvui.text(frame, 1041, 32, "Frequency") cvui.text(frame, 1042, 82, "{:,} Hz".format(round(frequency_tr[0], 2))) #Experiments window cvui.window(frame, 2, 2, 155, 75, 'Experiments') cvui.checkbox(frame, 10, 30, "1638 - Galileo", experiments[0]) cvui.checkbox(frame, 10, 53, "1849 - Fizeau", experiments[1]) curr_expr = exp_type(curr_expr, experiments) experiments = [[False] for i in range(2)] experiments[curr_expr] = [True] cvui.update() cv2.imshow('Speed of Light Measurement', frame) if cv2.waitKey(20) == 27: return -1 if curr_expr != expr: return curr_expr
def main(): lena = cv2.imread('lena.jpg') frame = np.zeros(lena.shape, np.uint8) anchor = cvui.Point() roi = cvui.Rect(0, 0, 0, 0) working = False # Init cvui and tell it to create a OpenCV window, i.e. cv.namedWindow(WINDOW_NAME). cvui.init(WINDOW_NAME) while (True): # Fill the frame with Lena's image frame[:] = lena[:] # Show the coordinates of the mouse pointer on the screen cvui.text(frame, 10, 10, 'Click (any) mouse button and drag the pointer around to select a ROI.') # The function 'bool cvui.mouse(int query)' allows you to query the mouse for events. # E.g. cvui.mouse(cvui.DOWN) # # Available queries: # - cvui.DOWN: any mouse button was pressed. cvui.mouse() returns true for single frame only. # - cvui.UP: any mouse button was released. cvui.mouse() returns true for single frame only. # - cvui.CLICK: any mouse button was clicked (went down then up, no matter the amount of frames in between). cvui.mouse() returns true for single frame only. # - cvui.IS_DOWN: any mouse button is currently pressed. cvui.mouse() returns true for as long as the button is down/pressed. # Did any mouse button go down? if cvui.mouse(cvui.DOWN): # Position the anchor at the mouse pointer. anchor.x = cvui.mouse().x anchor.y = cvui.mouse().y # Inform we are working, so the ROI window is not updated every frame working = True # Is any mouse button down (pressed)? if cvui.mouse(cvui.IS_DOWN): # Adjust roi dimensions according to mouse pointer width = cvui.mouse().x - anchor.x height = cvui.mouse().y - anchor.y roi.x = anchor.x + width if width < 0 else anchor.x roi.y = anchor.y + height if height < 0 else anchor.y roi.width = abs(width) roi.height = abs(height) # Show the roi coordinates and size cvui.printf(frame, roi.x + 5, roi.y + 5, 0.3, 0xff0000, '(%d,%d)', roi.x, roi.y) cvui.printf(frame, cvui.mouse().x + 5, cvui.mouse().y + 5, 0.3, 0xff0000, 'w:%d, h:%d', roi.width, roi.height) # Was the mouse clicked (any button went down then up)? if cvui.mouse(cvui.UP): # We are done working with the ROI. working = False # Ensure ROI is within bounds lenaRows, lenaCols, lenaChannels = lena.shape roi.x = 0 if roi.x < 0 else roi.x roi.y = 0 if roi.y < 0 else roi.y roi.width = roi.width + lena.cols - (roi.x + roi.width) if roi.x + roi.width > lenaCols else roi.width roi.height = roi.height + lena.rows - (roi.y + roi.height) if roi.y + roi.height > lenaRows else roi.height # Render the roi cvui.rect(frame, roi.x, roi.y, roi.width, roi.height, 0xff0000) # This function must be called *AFTER* all UI components. It does # all the behind the scenes magic to handle mouse clicks, etc. cvui.update() # Show everything on the screen cv2.imshow(WINDOW_NAME, frame) # If the ROI is valid, show it. if roi.area() > 0 and working == False: lenaRoi = lena[roi.y : roi.y + roi.height, roi.x : roi.x + roi.width] cv2.imshow(ROI_WINDOW, lenaRoi) # Check if ESC key was pressed if cv2.waitKey(20) == 27: break
def galileo(frame, expr): curr_expr = expr experiments = [[False] for i in range(2)] restart = True distance_tr = [1.1] galileo_default = cv2.imread(resource_path("images/galileo_default.png"), 3) galileo_yellow_dot = cv2.imread( resource_path("images/galileo_yellow_dot.png"), 3) galileo_fire = cv2.imread(resource_path("images/galileo_fire.png"), 3) while (True): frame[:] = (49, 52, 49) if restart: animation = 0 animate = False galileo_coord_bottom = 385 galileo_coord_top = 985 first_graph_f = False second_graph_f = False first_graph = False second_graph = False first_delay = 0 second_delay = 0 delay = 0 speed_of_light = 299792 half = False first_after_half = 0 first_after_end = 0 restart = False static_distance = False if not first_graph_f: first_graph, lag = get_graph() first_delay = lag first_graph_f = True if not second_graph_f: second_graph, lag = get_graph() second_delay = lag second_graph_f = True #Animation window cvui.image(frame, 0, 0, galileo_default) if animate: galileo_coord_bottom = 368 galileo_coord_top = 968 cvui.image(frame, 314, 393, galileo_fire) if animation < 21: for _ in range(animation + 1): cvui.image(frame, galileo_coord_bottom, 400, galileo_yellow_dot) galileo_coord_bottom = galileo_coord_bottom + 30 else: for _ in range(21): cvui.image(frame, galileo_coord_bottom, 400, galileo_yellow_dot) galileo_coord_bottom = galileo_coord_bottom + 30 if animation >= 21 and animation < 42: cvui.image(frame, 960, 200, first_graph) cvui.text(frame, 1045, 186, "Reaction delay", 0.4) cvui.text(frame, 1079, 278, str(int(first_delay)) + "ms", 0.4) cvui.image(frame, 1036, 390, galileo_fire) for _ in range(animation + 1 - 21): cvui.image(frame, galileo_coord_top, 375, galileo_yellow_dot) galileo_coord_top = galileo_coord_top - 30 elif animation == 42: if first_after_end != 0: if (timer() - first_after_end) > (second_delay / 1000): first_after_end = 0 cvui.image(frame, 131, 205, second_graph) cvui.text(frame, 216, 191, "Reaction delay", 0.4) cvui.text(frame, 248, 285, str(int(second_delay)) + "ms", 0.4) delay = static_distance / speed_of_light + ( first_delay / 1000) + (second_delay / 1000) else: cvui.image(frame, 131, 205, second_graph) cvui.text(frame, 216, 191, "Reaction delay", 0.4) cvui.text(frame, 248, 285, str(int(second_delay)) + "ms", 0.4) cvui.image(frame, 1036, 390, galileo_fire) for _ in range(21): cvui.image(frame, galileo_coord_top, 375, galileo_yellow_dot) galileo_coord_top = galileo_coord_top - 30 if animation < 42: act_time = timer() - start_time whole_distance = static_distance if half: traveled_distance = (act_time - (first_delay / 1000)) * speed_of_light else: traveled_distance = act_time * speed_of_light piece_of_distance = whole_distance / 21 traveled_parts = traveled_distance / piece_of_distance if first_after_half != 0: if (timer() - first_after_half) > (first_delay / 1000): first_after_half = 0 else: if not half: if traveled_parts >= 21: animation = 20 half = True first_after_half = timer() else: animation = int(traveled_parts) else: if traveled_parts >= 42: animation = 42 first_after_end = timer() else: animation = int(traveled_parts) else: cvui.image(frame, 960, 200, first_graph) cvui.text(frame, 1045, 186, "Reaction delay", 0.4) cvui.text(frame, 1079, 278, str(int(first_delay)) + "ms", 0.4) if delay != 0: cvui.text(frame, 640, 320, "Total time", 0.5) cvui.text(frame, 660, 343, str(round(delay, 2)) + "s", 0.5) cvui.text(frame, 345, 60, "Calculation of the speed of light", 0.5) cvui.text( frame, 345, 80, "distance between mountains ... s = {:,} km".format( static_distance / 2), 0.5) cvui.text(frame, 345, 100, "average human reaction time ... d = 250 ms", 0.5) cvui.text( frame, 345, 120, "c=(2*s)/(t-2*d)=(2*{:,})/({}-2*0.25)={:,} km/s". format( static_distance / 2, str(round(delay, 2)), round( static_distance / (round(delay, 2) - 2 * 0.25), 2)), 0.5) if delay == 0: cvui.text(frame, 345, 60, "Calculation of the speed of light", 0.5) if static_distance: cvui.text( frame, 345, 80, "distance between mountains ... s = {:,} km".format( static_distance / 2), 0.5) else: cvui.text( frame, 345, 80, "distance between mountains ... s = {:,} km".format( round((distance_tr[0])**8, 0)), 0.5) cvui.text(frame, 345, 100, "average human reaction time ... d = 250 ms", 0.5) cvui.text(frame, 345, 120, "c=(2*s)/(t-2*d) ... total time wasn't measured", 0.5) #Experiment settings window cvui.window(frame, 1033.5, 2, 243.5, 133, 'Experiment settings') cvui.trackbar(frame, 1030, 39, 249, distance_tr, 1.1, 10.0) cvui.rect(frame, 1035, 39, 240, 12, 0x313131, 0x313131) cvui.rect(frame, 1035, 74, 240, 25, 0x313131, 0x313131) cvui.text(frame, 1041, 32, "Distance") cvui.text(frame, 1042, 82, "{:,} km".format(round((distance_tr[0])**8, 0))) if cvui.button(frame, 1040, 102, "Measure time"): if not animate: static_distance = round((distance_tr[0])**8, 0) * 2 animate = True start_time = timer() if cvui.button(frame, 1161, 102, "Clear"): restart = True #Experiments window cvui.window(frame, 2, 2, 155, 75, 'Experiments') cvui.checkbox(frame, 10, 30, "1638 - Galileo", experiments[0]) cvui.checkbox(frame, 10, 53, "1849 - Fizeau", experiments[1]) curr_expr = exp_type(curr_expr, experiments) experiments = [[False] for i in range(2)] experiments[curr_expr] = [True] cvui.update() cv2.imshow('Speed of Light Measurement', frame) if cv2.waitKey(20) == 27: return -1 if curr_expr != expr: return curr_expr
def main(): lena = cv2.imread('lena.jpg') frame = np.zeros(lena.shape, np.uint8) anchor = cvui.Point() roi = cvui.Rect(0, 0, 0, 0) working = False # Init cvui and tell it to create a OpenCV window, i.e. cv.namedWindow(WINDOW_NAME). cvui.init(WINDOW_NAME) while (True): # Fill the frame with Lena's image frame[:] = lena[:] # Show the coordinates of the mouse pointer on the screen cvui.text( frame, 10, 10, 'Click (any) mouse button and drag the pointer around to select a ROI.' ) # The function 'bool cvui.mouse(int query)' allows you to query the mouse for events. # E.g. cvui.mouse(cvui.DOWN) # # Available queries: # - cvui.DOWN: any mouse button was pressed. cvui.mouse() returns true for single frame only. # - cvui.UP: any mouse button was released. cvui.mouse() returns true for single frame only. # - cvui.CLICK: any mouse button was clicked (went down then up, no matter the amount of frames in between). cvui.mouse() returns true for single frame only. # - cvui.IS_DOWN: any mouse button is currently pressed. cvui.mouse() returns true for as long as the button is down/pressed. # Did any mouse button go down? if cvui.mouse(cvui.DOWN): # Position the anchor at the mouse pointer. anchor.x = cvui.mouse().x anchor.y = cvui.mouse().y # Inform we are working, so the ROI window is not updated every frame working = True # Is any mouse button down (pressed)? if cvui.mouse(cvui.IS_DOWN): # Adjust roi dimensions according to mouse pointer width = cvui.mouse().x - anchor.x height = cvui.mouse().y - anchor.y roi.x = anchor.x + width if width < 0 else anchor.x roi.y = anchor.y + height if height < 0 else anchor.y roi.width = abs(width) roi.height = abs(height) # Show the roi coordinates and size cvui.printf(frame, roi.x + 5, roi.y + 5, 0.3, 0xff0000, '(%d,%d)', roi.x, roi.y) cvui.printf(frame, cvui.mouse().x + 5, cvui.mouse().y + 5, 0.3, 0xff0000, 'w:%d, h:%d', roi.width, roi.height) # Was the mouse clicked (any button went down then up)? if cvui.mouse(cvui.UP): # We are done working with the ROI. working = False # Ensure ROI is within bounds lenaRows, lenaCols, lenaChannels = lena.shape roi.x = 0 if roi.x < 0 else roi.x roi.y = 0 if roi.y < 0 else roi.y roi.width = roi.width + lena.cols - ( roi.x + roi.width) if roi.x + roi.width > lenaCols else roi.width roi.height = roi.height + lena.rows - ( roi.y + roi.height) if roi.y + roi.height > lenaRows else roi.height # Render the roi cvui.rect(frame, roi.x, roi.y, roi.width, roi.height, 0xff0000) # This function must be called *AFTER* all UI components. It does # all the behind the scenes magic to handle mouse clicks, etc. cvui.update() # Show everything on the screen cv2.imshow(WINDOW_NAME, frame) # If the ROI is valid, show it. if roi.area() > 0 and working == False: lenaRoi = lena[roi.y:roi.y + roi.height, roi.x:roi.x + roi.width] cv2.imshow(ROI_WINDOW, lenaRoi) # Check if ESC key was pressed if cv2.waitKey(20) == 27: break
def main(): frame = np.zeros((300, 600, 3), np.uint8) # Init cvui and tell it to create a OpenCV window, i.e. cv::namedWindow(WINDOW_NAME). cvui.init(WINDOW_NAME); # Rectangle to be rendered according to mouse interactions. rectangle = cvui.Rect(0, 0, 0, 0) while (True): # Fill the frame with a nice color frame[:] = (49, 52, 49) # Show the coordinates of the mouse pointer on the screen cvui.text(frame, 10, 30, 'Click (any) mouse button and drag the pointer around to select an area.') cvui.printf(frame, 10, 50, 'Mouse pointer is at (%d,%d)', cvui.mouse().x, cvui.mouse().y) # The function "bool cvui.mouse(int query)" allows you to query the mouse for events. # E.g. cvui.mouse(cvui.DOWN) # # Available queries: # - cvui.DOWN: any mouse button was pressed. cvui.mouse() returns true for a single frame only. # - cvui.UP: any mouse button was released. cvui.mouse() returns true for a single frame only. # - cvui.CLICK: any mouse button was clicked (went down then up, no matter the amount of frames in between). cvui.mouse() returns true for a single frame only. # - cvui.IS_DOWN: any mouse button is currently pressed. cvui.mouse() returns true for as long as the button is down/pressed. # Did any mouse button go down? if cvui.mouse(cvui.DOWN): # Position the rectangle at the mouse pointer. rectangle.x = cvui.mouse().x rectangle.y = cvui.mouse().y # Is any mouse button down (pressed)? if cvui.mouse(cvui.IS_DOWN): # Adjust rectangle dimensions according to mouse pointer rectangle.width = cvui.mouse().x - rectangle.x rectangle.height = cvui.mouse().y - rectangle.y # Show the rectangle coordinates and size cvui.printf(frame, rectangle.x + 5, rectangle.y + 5, 0.3, 0xff0000, '(%d,%d)', rectangle.x, rectangle.y) cvui.printf(frame, cvui.mouse().x + 5, cvui.mouse().y + 5, 0.3, 0xff0000, 'w:%d, h:%d', rectangle.width, rectangle.height) # Did any mouse button go up? if cvui.mouse(cvui.UP): # Hide the rectangle rectangle.x = 0 rectangle.y = 0 rectangle.width = 0 rectangle.height = 0 # Was the mouse clicked (any button went down then up)? if cvui.mouse(cvui.CLICK): cvui.text(frame, 10, 70, 'Mouse was clicked!') # Render the rectangle cvui.rect(frame, rectangle.x, rectangle.y, rectangle.width, rectangle.height, 0xff0000) # This function must be called *AFTER* all UI components. It does # all the behind the scenes magic to handle mouse clicks, etc, then # shows the frame in a window like cv2.imshow() does. cvui.imshow(WINDOW_NAME, frame) # Check if ESC key was pressed if cv2.waitKey(20) == 27: break
def main(): frame = np.zeros((300, 600, 3), np.uint8) # Init cvui and tell it to create a OpenCV window, i.e. cv::namedWindow(WINDOW_NAME). cvui.init(WINDOW_NAME) # Rectangle to be rendered according to mouse interactions. rectangle = cvui.Rect(0, 0, 0, 0) while (True): # Fill the frame with a nice color frame[:] = (49, 52, 49) # Show the coordinates of the mouse pointer on the screen cvui.text( frame, 10, 30, 'Click (any) mouse button and drag the pointer around to select an area.' ) cvui.printf(frame, 10, 50, 'Mouse pointer is at (%d,%d)', cvui.mouse().x, cvui.mouse().y) # The function "bool cvui.mouse(int query)" allows you to query the mouse for events. # E.g. cvui.mouse(cvui.DOWN) # # Available queries: # - cvui.DOWN: any mouse button was pressed. cvui.mouse() returns true for a single frame only. # - cvui.UP: any mouse button was released. cvui.mouse() returns true for a single frame only. # - cvui.CLICK: any mouse button was clicked (went down then up, no matter the amount of frames in between). cvui.mouse() returns true for a single frame only. # - cvui.IS_DOWN: any mouse button is currently pressed. cvui.mouse() returns true for as long as the button is down/pressed. # Did any mouse button go down? if cvui.mouse(cvui.DOWN): # Position the rectangle at the mouse pointer. rectangle.x = cvui.mouse().x rectangle.y = cvui.mouse().y # Is any mouse button down (pressed)? if cvui.mouse(cvui.IS_DOWN): # Adjust rectangle dimensions according to mouse pointer rectangle.width = cvui.mouse().x - rectangle.x rectangle.height = cvui.mouse().y - rectangle.y # Show the rectangle coordinates and size cvui.printf(frame, rectangle.x + 5, rectangle.y + 5, 0.3, 0xff0000, '(%d,%d)', rectangle.x, rectangle.y) cvui.printf(frame, cvui.mouse().x + 5, cvui.mouse().y + 5, 0.3, 0xff0000, 'w:%d, h:%d', rectangle.width, rectangle.height) # Did any mouse button go up? if cvui.mouse(cvui.UP): # Hide the rectangle rectangle.x = 0 rectangle.y = 0 rectangle.width = 0 rectangle.height = 0 # Was the mouse clicked (any button went down then up)? if cvui.mouse(cvui.CLICK): cvui.text(frame, 10, 70, 'Mouse was clicked!') # Render the rectangle cvui.rect(frame, rectangle.x, rectangle.y, rectangle.width, rectangle.height, 0xff0000) # This function must be called *AFTER* all UI components. It does # all the behind the scenes magic to handle mouse clicks, etc, then # shows the frame in a window like cv2.imshow() does. cvui.imshow(WINDOW_NAME, frame) # Check if ESC key was pressed if cv2.waitKey(20) == 27: break
def main(): lena = cv2.imread('lena.jpg') frame = np.zeros(lena.shape, np.uint8) anchors = [cvui.Point() for i in range(3)] # one anchor for each mouse button rois = [cvui.Rect() for i in range(3)] # one ROI for each mouse button colors = [0xff0000, 0x00ff00, 0x0000ff] # Init cvui and tell it to create a OpenCV window, i.e. cv.namedWindow(WINDOW_NAME). cvui.init(WINDOW_NAME) while (True): # Fill the frame with Lena's image frame[:] = lena[:] # Show the coordinates of the mouse pointer on the screen cvui.text( frame, 10, 10, 'Click (any) mouse button then drag the pointer around to select a ROI.' ) cvui.text( frame, 10, 25, 'Use different mouse buttons (right, middle and left) to select different ROIs.' ) # Iterate all mouse buttons (left, middle and right button) button = cvui.LEFT_BUTTON while button <= cvui.RIGHT_BUTTON: # Get the anchor, ROI and color associated with the mouse button anchor = anchors[button] roi = rois[button] color = colors[button] # The function 'bool cvui.mouse(int button, int query)' allows you to query a particular mouse button for events. # E.g. cvui.mouse(cvui.RIGHT_BUTTON, cvui.DOWN) # # Available queries: # - cvui.DOWN: mouse button was pressed. cvui.mouse() returns true for single frame only. # - cvui.UP: mouse button was released. cvui.mouse() returns true for single frame only. # - cvui.CLICK: mouse button was clicked (went down then up, no matter the amount of frames in between). cvui.mouse() returns true for single frame only. # - cvui.IS_DOWN: mouse button is currently pressed. cvui.mouse() returns true for as long as the button is down/pressed. # Did the mouse button go down? if cvui.mouse(button, cvui.DOWN): # Position the anchor at the mouse pointer. anchor.x = cvui.mouse().x anchor.y = cvui.mouse().y # Is any mouse button down (pressed)? if cvui.mouse(button, cvui.IS_DOWN): # Adjust roi dimensions according to mouse pointer width = cvui.mouse().x - anchor.x height = cvui.mouse().y - anchor.y roi.x = anchor.x + width if width < 0 else anchor.x roi.y = anchor.y + height if height < 0 else anchor.y roi.width = abs(width) roi.height = abs(height) # Show the roi coordinates and size cvui.printf(frame, roi.x + 5, roi.y + 5, 0.3, color, '(%d,%d)', roi.x, roi.y) cvui.printf(frame, cvui.mouse().x + 5, cvui.mouse().y + 5, 0.3, color, 'w:%d, h:%d', roi.width, roi.height) # Ensure ROI is within bounds lenaRows, lenaCols, lenaChannels = lena.shape roi.x = 0 if roi.x < 0 else roi.x roi.y = 0 if roi.y < 0 else roi.y roi.width = roi.width + lenaCols - ( roi.x + roi.width) if roi.x + roi.width > lenaCols else roi.width roi.height = roi.height + lenaRows - ( roi.y + roi.height) if roi.y + roi.height > lenaRows else roi.height # If the ROI is valid, render it in the frame and show in a window. if roi.area() > 0: cvui.rect(frame, roi.x, roi.y, roi.width, roi.height, color) cvui.printf(frame, roi.x + 5, roi.y - 10, 0.3, color, 'ROI %d', button) lenaRoi = lena[roi.y:roi.y + roi.height, roi.x:roi.x + roi.width] cv2.imshow('ROI button' + str(button), lenaRoi) button += 1 # This function must be called *AFTER* all UI components. It does # all the behind the scenes magic to handle mouse clicks, etc. cvui.update() # Show everything on the screen cv2.imshow(WINDOW_NAME, frame) # Check if ESC key was pressed if cv2.waitKey(20) == 27: break
def main(): frame = np.zeros((600, 800, 3), np.uint8) # Create variables used by some components values = [] checked = [False] checked2 = [False] value = [1.0] value2 = [1.0] value3 = [1.0] padding = 10 img = cv2.imread('lena-face.jpg', cv2.IMREAD_COLOR) imgRed = cv2.imread('lena-face-red.jpg', cv2.IMREAD_COLOR) imgGray = cv2.imread('lena-face-gray.jpg', cv2.IMREAD_COLOR) # Fill the vector with a few random values for i in range(0, 20): values.append(random.uniform(0., 300.0)) # Init cvui and tell it to create a OpenCV window, i.e. cv::namedWindow(WINDOW_NAME). cvui.init(WINDOW_NAME) while (True): # Fill the frame with a nice color frame[:] = (49, 52, 49) # In a row, all added elements are # horizontally placed, one next the other (from left to right) # # Within the cvui.beginRow() and cvui.endRow(), # all elements will be automatically positioned by cvui. # # Notice that all component calls within the begin/end block # DO NOT have (x,y) coordinates. # # Let's create a row at position (10,20) with width 100 and height 50. cvui.beginRow(frame, 10, 20, 100, 50) cvui.text('This is ') cvui.printf('a row') cvui.checkbox('checkbox', checked) cvui.window(80, 80, 'window') cvui.rect(50, 50, 0x00ff00, 0xff0000); cvui.sparkline(values, 50, 50); cvui.counter(value) cvui.button(100, 30, 'Fixed') cvui.image(img) cvui.button(img, imgGray, imgRed) cvui.endRow() # Here is another row, this time with a padding of 50px among components. padding = 50; cvui.beginRow(frame, 10, 150, 100, 50, padding) cvui.text('This is ') cvui.printf('another row') cvui.checkbox('checkbox', checked2) cvui.window(80, 80, 'window') cvui.button(100, 30, 'Fixed') cvui.printf('with 50px padding.') cvui.endRow() # Another row mixing several components cvui.beginRow(frame, 10, 250, 100, 50) cvui.text('This is ') cvui.printf('another row with a trackbar ') #cvui.trackbar(150, &value2, 0., 5.); cvui.printf(' and a button ') cvui.button(100, 30, 'button') cvui.endRow() # In a column, all added elements are vertically placed, # one below the other, from top to bottom. Let's create # a column at (50, 300) with width 100 and height 200. cvui.beginColumn(frame, 50, 330, 100, 200) cvui.text('Column 1 (no padding)') cvui.button('button1') cvui.button('button2') cvui.text('End of column 1') cvui.endColumn() # Here is another column, using a padding value of 10, # which will add an space of 10px between each component. padding = 10 cvui.beginColumn(frame, 300, 330, 100, 200, padding) cvui.text('Column 2 (padding = 10)') cvui.button('button1') cvui.button('button2') #cvui.trackbar(150, &value3, 0., 5., 1, '%3.2Lf', cvui.TRACKBAR_DISCRETE, 0.25); cvui.text('End of column 2') cvui.endColumn() # You can also add an arbitrary amount of space between # components by calling cvui.space(). # # cvui.space() is aware of context, so if it is used # within a beginColumn()/endColumn() block, the space will # be vertical. If it is used within a beginRow()/endRow() # block, space will be horizontal. cvui.beginColumn(frame, 550, 330, 100, 200) cvui.text('Column 3 (use space)') # Add 5 pixels of (vertical) space. cvui.space(5) cvui.button('button1 5px below') # Add 50 pixels of (vertical) space. cvui.space(50) cvui.text('Text 50px below') # Add 20 pixels of (vertical) space. cvui.space(20) cvui.button('Button 20px below') # Add 40 pixels of (vertical) space. cvui.space(40) cvui.text('End of column 2 (40px below)') cvui.endColumn() # This function must be called *AFTER* all UI components. It does # all the behind the scenes magic to handle mouse clicks, etc. cvui.update() # Show everything on the screen cv2.imshow(WINDOW_NAME, frame) # Check if ESC key was pressed if cv2.waitKey(20) == 27: break