def trackbars_create_monitor(self): # canny trackbar cvui.trackbar(self.mainframe, 0, 650, 400, self.canny_value, 0, self.canny_trackbar_max_value, 5, '%.0Lf', cvui.TRACKBAR_HIDE_STEP_SCALE) # blur trackbar cvui.trackbar(self.mainframe, 0, 700, 400, self.blur_value, 1, self.blur_trackbar_max_value, 5, '%.0Lf', cvui.TRACKBAR_DISCRETE, 2)
def main(): thresholdValue = [108] chooseInt = [0] # Size of trackbars trackbar_width = 400 #init cvui cvui.init(WINDOW_NAME, 20) ui_frame = np.zeros((480, 640, 3), np.uint8) #camera setting cap = cv2.VideoCapture(0) cap.set(cv2.cv.CV_CAP_PROP_FRAME_WIDTH, 640) cap.set(cv2.cv.CV_CAP_PROP_FRAME_HEIGHT, 480) #main loop while (True): ret, frame = cap.read() #print frame.dtype #to gray gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) ui_frame[:] = (49, 52, 49) cvui.beginColumn(ui_frame, 20, 20, -1, -1, 6) cvui.text('Threshold Adject') cvui.trackbar(trackbar_width, thresholdValue, 0, 255) cvui.counter(chooseInt) cvui.space(5) cvui.text(strArr[chooseInt[0]]) if cvui.button('&Quit'): break cvui.space(5) cvui.endColumn() th1, gray = cv2.threshold(gray, thresholdValue[0], 255, chooseArr[chooseInt[0]]) merged = cv2.merge([gray, gray, gray]) dst = cv2.addWeighted(ui_frame, 1.0, merged, 0.4, 0.0) cvui.update() cv2.imshow(WINDOW_NAME, dst) cap.release() cv2.destroyAllWindows()
def main(): frame = np.zeros((600, 800, 3), np.uint8) value = [50] settings = EnhancedWindow(20, 80, 200, 120, 'Settings') info = EnhancedWindow(250, 80, 330, 60, 'Info') # Init cvui and tell it to create a OpenCV window, i.e. cv2.namedWindow(WINDOW_NAME). cvui.init(WINDOW_NAME) while (True): # Fill the frame with a nice color frame[:] = (49, 52, 49) # Place some instructions on the screen regarding the # settings window cvui.text( frame, 20, 20, 'The Settings and the Info windows below are movable and minimizable.' ) cvui.text(frame, 20, 40, 'Click and drag any window\'s title bar to move it around.') # Render a movable and minimizable settings window using # the EnhancedWindow class. settings.begin(frame) if settings.isMinimized() == False: cvui.text('Adjust something') cvui.space(10) # add 10px of space between UI components cvui.trackbar(settings.width() - 20, value, 5, 150) settings.end() # Render a movable and minimizable settings window using # the EnhancedWindow class. info.begin(frame) if info.isMinimized() == False: cvui.text('Moving and minimizable windows are awesome!') info.end() # Update all cvui internal stuff, e.g. handle mouse clicks, and show # everything on the screen. cvui.imshow(WINDOW_NAME, frame) # Check if ESC key was pressed if cv2.waitKey(20) == 27: break
def printStuffOnCtrlImg(frameCtrl, frameNum, x, y, l, minn, maxx, name, hint): cvui.text(frameCtrl, x, y, name) status = cvui.iarea(x, y, 350, 60) if status == cvui.OVER: cvui.text(frameCtrl, 4, 15, hint) if name != "Frame number": if (frameNum[0] - minn) > (maxx - minn) * 0.9: maxx = minn + (frameNum[0] - minn) * 1.25 if maxx - minn > 255: if frameNum[0] < minn + (maxx - minn) * 0.1: maxx = minn + (maxx - minn) * 0.9 cvui.trackbar(frameCtrl, x, y+10, l, frameNum, minn, maxx) cvui.counter(frameCtrl, x+l+10, y+20, frameNum) return [minn, maxx]
def main(): fruits = cv2.imread('fruits.jpg', cv2.IMREAD_COLOR) frame = np.zeros(fruits.shape, np.uint8) low_threshold = [50] high_threshold = [150] use_canny = [False] # Create a settings window using the EnhancedWindow class. settings = EnhancedWindow(10, 50, 270, 180, 'Settings') # Init cvui and tell it to create a OpenCV window, i.e. cv2.namedWindow(WINDOW_NAME). cvui.init(WINDOW_NAME) while (True): # Should we apply Canny edge? if use_canny[0]: # Yes, we should apply it. frame = cv2.cvtColor(fruits, cv2.COLOR_BGR2GRAY) frame = cv2.Canny(frame, low_threshold[0], high_threshold[0], 3) frame = cv2.cvtColor(frame, cv2.COLOR_GRAY2BGR) else: # No, so just copy the original image to the displaying frame. frame[:] = fruits[:] # Render the settings window and its content, if it is not minimized. settings.begin(frame) if settings.isMinimized() == False: cvui.checkbox('Use Canny Edge', use_canny) cvui.trackbar(settings.width() - 20, low_threshold, 5, 150) cvui.trackbar(settings.width() - 20, high_threshold, 80, 300) cvui.space(20); # add 20px of empty space cvui.text('Drag and minimize this settings window', 0.4, 0xff0000) settings.end() # 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', cv2.IMREAD_COLOR) frame = np.zeros(lena.shape, np.uint8) low_threshold = [50] high_threshold = [150] use_canny = [False] # Init cvui and tell it to create a OpenCV window, i.e. cv2.namedWindow(WINDOW_NAME). cvui.init(WINDOW_NAME) while (True): # Should we apply Canny edge? if use_canny[0]: # Yes, we should apply it. frame = cv2.cvtColor(lena, cv2.COLOR_BGR2GRAY) frame = cv2.Canny(frame, low_threshold[0], high_threshold[0], 3) frame = cv2.cvtColor(frame, cv2.COLOR_GRAY2BGR) else: # No, so just copy the original image to the displaying frame. frame[:] = lena[:] # Render the settings window to house the checkbox # and the trackbars below. cvui.window(frame, 10, 50, 180, 180, 'Settings') # Checkbox to enable/disable the use of Canny edge cvui.checkbox(frame, 15, 80, 'Use Canny Edge', use_canny) # Two trackbars to control the low and high threshold values # for the Canny edge algorithm. cvui.trackbar(frame, 15, 110, 165, low_threshold, 5, 150) cvui.trackbar(frame, 15, 180, 165, high_threshold, 80, 300) # 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, 800, 3), np.uint8) value = [2.25] values = [] # Init cvui and tell it to create a OpenCV window, i.e. cv.namedWindow(WINDOW_NAME). cvui.init(WINDOW_NAME, 20) 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 (20,80) with automatic width and height, and a padding of 10 cvui.beginRow(frame, 20, 80, -1, -1, 10); # trackbar accepts a pointer to a variable that controls their value. # Here we define a double trackbar between 0. and 5. if cvui.trackbar(150, value, 0., 5.): print('Trackbar was modified, value : ', value[0]) values.append(value[0]) if len(values) > 5: cvui.text('Your edits on a sparkline ->') cvui.sparkline(values, 240, 60) if cvui.button('Clear sparkline'): values = [] else: cvui.text('<- Move the trackbar') cvui.endRow(); # 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 ""
(int(800 * image.shape[1] / image.shape[0]), 800), interpolation=cv2.INTER_CUBIC) else: imag = cv2.resize( image, (800, int(800 * image.shape[0] / image.shape[1])), interpolation=cv2.INTER_CUBIC) except: file_path = "cannot open the image" image = None cvui.space(10) cvui.text(file_path, 0.4) cvui.space(10) cvui.text("Kernal Size Gauss", 0.4) cvui.space(5) cvui.trackbar(250, kernalSize, 1, 11, 5, '%d', cvui.TRACKBAR_DISCRETE, 2) cvui.space(10) cvui.text("Kernal Size Sobel", 0.4) cvui.space(5) cvui.trackbar(250, ksize, 1, 11, 5, '%d', cvui.TRACKBAR_DISCRETE, 2) cvui.space(10) cvui.text("Grey or Bright", 0.4) cvui.space(5) cvui.trackbar(250, alpha, 0, 1) cvui.space(10) cvui.text("Gradient Therehold Value", 0.4) cvui.space(5) cvui.trackbar(250, gradientTV, 0, 50, 5) cvui.space(10) cvui.text("Anchor Therehold Value", 0.4) cvui.space(5)
def run(self): self.main_frame = np.zeros((380, 400, 3), np.uint8) cv.namedWindow(self.main_window_name) cvui.init(self.main_window_name) while True: cvui.context(self.main_window_name) self.main_frame[:] = (49, 52, 49) cvui.beginColumn(self.main_frame, 50, 20, -1, -1, 10) cvui.text('Click to open an image') if cvui.button('Select Image'): self.load_image() cvui.text('Load a previously saved txt file to current image') if cvui.button("Read Txt file"): self.read_txt() cvui.text('Save to txt file') if cvui.button('Save'): self.save_to_txt() if cvui.button("Show/Hide Index"): self.has_index = not self.has_index if cvui.button("Clear All Points"): self.points.clear() self.high_light_point = None cvui.text('Max click distance to select one point') cvui.text('adjust smaller if you want to click two close points') cvui.trackbar(200, self.threshold, 0.000, 0.02, 2, '%.4Lf') cvui.endColumn() cvui.update(self.main_window_name) cv.imshow(self.main_window_name, self.main_frame) key = cv.waitKey(20) if key == 27 or not self.is_window_open(self.main_window_name): self.tkinter_root.destroy() break if self.is_window_open(self.image_window_name): cvui.context(self.image_window_name) self.show_image = cv.resize( self.origin_image, (self.actual_width, self.actual_height)) for i, point in enumerate(self.points): if i not in self.deleted_list: self.draw_intersection(self.show_image, point, i) if self.high_light_point is not None: point = self.points[self.high_light_point] cv.circle(self.show_image, (int(point[0] * self.actual_width), int(point[1] * self.actual_height)), 5, (0, 255, 255), 1) # Fill the error window if a vibrant color # if self.image_window_flag: cvui.update(self.image_window_name) cv.imshow(self.image_window_name, self.show_image) self.keyboard(key)
def main(): intValue = [30] ucharValue = [30] charValue = [30] floatValue = [12.] doubleValue1 = [15.] doubleValue2 = [10.3] doubleValue3 = [2.25] frame = np.zeros((650, 450, 3), np.uint8) # Size of trackbars width = 400 # Init cvui and tell it to use a value of 20 for cv2.waitKey() # because we want to enable keyboard shortcut for # all components, e.g. button with label '&Quit'. # If cvui has a value for waitKey, it will call # waitKey() automatically for us within cvui.update(). cvui.init(WINDOW_NAME, 20) while (True): # Fill the frame with a nice color frame[:] = (49, 52, 49) cvui.beginColumn(frame, 20, 20, -1, -1, 6) cvui.text('int trackbar, no customization') cvui.trackbar(width, intValue, 0, 100) cvui.space(5) cvui.text('uchar trackbar, no customization') cvui.trackbar(width, ucharValue, 0, 255) cvui.space(5) cvui.text('signed char trackbar, no customization') cvui.trackbar(width, charValue, -128, 127) cvui.space(5) cvui.text('float trackbar, no customization') cvui.trackbar(width, floatValue, 10., 15.) cvui.space(5) cvui.text('float trackbar, 4 segments') cvui.trackbar(width, doubleValue1, 10., 20., 4) cvui.space(5) cvui.text('double trackbar, label %.1Lf, TRACKBAR_DISCRETE') cvui.trackbar(width, doubleValue2, 10., 10.5, 1, '%.1Lf', cvui.TRACKBAR_DISCRETE, 0.1) cvui.space(5) cvui.text('double trackbar, label %.2Lf, 2 segments, TRACKBAR_DISCRETE') cvui.trackbar(width, doubleValue3, 0., 4., 2, '%.2Lf', cvui.TRACKBAR_DISCRETE, 0.25) cvui.space(10) # Exit the application if the quit button was pressed. # It can be pressed because of a mouse click or because # the user pressed the 'q' key on the keyboard, which is # marked as a shortcut in the button label ('&Quit'). if cvui.button('&Quit'): break cvui.endColumn() # Since cvui.init() received a param regarding waitKey, # there is no need to call cv.waitKey() anymore. cvui.update() # will do it automatically. cvui.update() cv2.imshow(WINDOW_NAME, frame)
def printStuffOnCtrlImg(frameCtrl, frameNum, x, y, l, minn, maxx, name): cvui.text(frameCtrl, x, y, name) cvui.trackbar(frameCtrl, x, y + 10, l, frameNum, minn, maxx) cvui.counter(frameCtrl, x + l + 10, y + 20, frameNum)
frame[:] = result[:] ########################################################################### # GUI ########################################################################### # Render the settings window to house the checkbox and the trackbars below cvui.window(frame, 10, 10, 200, 250, 'Settings') options = cvui.TRACKBAR_DISCRETE | cvui.TRACKBAR_HIDE_SEGMENT_LABELS cvui.beginColumn(frame, 10, 40, -1, -1, 6) if cs == 'hsv': cvui.text('Hue low') elif cs == 'lab': cvui.text('L low') cvui.trackbar(width, lowCh0, limitsCh0[0], limitsCh0[1], 10, '%.0Lf', options, 1) cvui.space(space) if cs == 'hsv': cvui.text('Sat low') elif cs == 'lab': cvui.text('a* low') cvui.trackbar(width, lowCh1, limitsCh1[0], limitsCh1[1], 10, '%.0Lf', options, 1) cvui.space(space) if cs == 'hsv': cvui.text('Val low') elif cs == 'lab': cvui.text('b* low') cvui.trackbar(width, lowCh2, limitsCh2[0], limitsCh2[1], 10, '%.0Lf',
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 main(): intValue = [30] ucharValue = [30] charValue = [30] floatValue = [12.] doubleValue = [45.] doubleValue2 = [15.] doubleValue3 = [10.3] frame = np.zeros((770, 350, 3), np.uint8) # The width of all trackbars used in this example. width = 300 # The x position of all trackbars used in this example x = 10 # 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) # The trackbar component uses templates to guess the type of its arguments. # You have to be very explicit about the type of the value, the min and # the max params. For instance, if they are double, use 100.0 instead of 100. cvui.text(frame, x, 10, 'double, step 1.0 (default)') cvui.trackbar(frame, x, 40, width, doubleValue, 0., 100.) cvui.text(frame, x, 120, 'float, step 1.0 (default)') cvui.trackbar(frame, x, 150, width, floatValue, 10., 15.) # You can specify segments and custom labels. Segments are visual marks in # the trackbar scale. Internally the value for the trackbar is stored as # long double, so the custom labels must always format long double numbers, no # matter the type of the numbers being used for the trackbar. E.g. %.2Lf cvui.text(frame, x, 230, 'double, 4 segments, custom label %.2Lf') cvui.trackbar(frame, x, 260, width, doubleValue2, 0., 20., 4, '%.2Lf') # Again: you have to be very explicit about the value, the min and the max params. # Below is a uchar trackbar. Observe the uchar cast for the min, the max and # the step parameters. cvui.text(frame, x, 340, 'uchar, custom label %.0Lf') cvui.trackbar(frame, x, 370, width, ucharValue, 0, 255, 0, '%.0Lf') # You can change the behavior of any tracker by using the options parameter. # Options are defined as a bitfield, so you can combine them. # E.g. # TRACKBAR_DISCRETE # value changes are discrete # TRACKBAR_DISCRETE | TRACKBAR_HIDE_LABELS # discrete changes and no labels cvui.text(frame, x, 450, 'double, step 0.1, option TRACKBAR_DISCRETE') cvui.trackbar(frame, x, 480, width, doubleValue3, 10., 10.5, 1, '%.1Lf', cvui.TRACKBAR_DISCRETE, 0.1) # More customizations using options. options = cvui.TRACKBAR_DISCRETE | cvui.TRACKBAR_HIDE_SEGMENT_LABELS cvui.text(frame, x, 560, 'int, 3 segments, DISCRETE | HIDE_SEGMENT_LABELS') cvui.trackbar(frame, x, 590, width, intValue, 10, 50, 3, '%.0Lf', options, 2) # Trackbar using char type. cvui.text(frame, x, 670, 'char, 2 segments, custom label %.0Lf') cvui.trackbar(frame, x, 700, width, charValue, -128, 127, 2, '%.0Lf') # 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 chooseBeginningAndEndOfVideo(self, controller): cap = cv2.VideoCapture(self.videoToCreateConfigFileFor) max_l = int(cap.get(7)) - 2 cap.set(1, 1) ret, frame = cap.read() WINDOW_NAME = "Choose where the analysis of your video should start." WINDOW_NAME_CTRL = "Control" cvui.init(WINDOW_NAME) cv2.moveWindow(WINDOW_NAME, 0, 0) cvui.init(WINDOW_NAME_CTRL) cv2.moveWindow(WINDOW_NAME_CTRL, 0, 300) value = [1] curValue = value[0] buttonclicked = False buttonEntireVideo = False widgetX = 40 widgetY = 20 widgetL = 300 while not (buttonclicked) and not (buttonEntireVideo): value[0] = int(value[0]) if curValue != value[0]: cap.set(1, value[0]) frameOld = frame ret, frame = cap.read() if not (ret): frame = frameOld curValue = value[0] frameCtrl = np.full((200, 750), 100).astype('uint8') frameCtrl[widgetY:widgetY + 60, widgetX:widgetX + widgetL] = 0 cvui.text(frameCtrl, widgetX, widgetY, 'Frame') cvui.trackbar(frameCtrl, widgetX, widgetY + 10, widgetL, value, 0, max_l) cvui.counter(frameCtrl, widgetX, widgetY + 60, value) buttonclicked = cvui.button( frameCtrl, widgetX, widgetY + 90, "Ok, I want the tracking to start at this frame!") cvui.text(frameCtrl, widgetX + 350, widgetY + 1, 'No, this is unecessary:') buttonEntireVideo = cvui.button( frameCtrl, widgetX + 350, widgetY + 40, "I want the tracking to run on the entire video!") cvui.text(frameCtrl, widgetX, widgetY + 130, 'Keys: 4 or a: move backwards; 6 or d: move forward') cvui.text(frameCtrl, widgetX, widgetY + 160, 'Keys: g or f: fast backwards; h or j: fast forward') cvui.imshow(WINDOW_NAME, frame) cvui.imshow(WINDOW_NAME_CTRL, frameCtrl) r = cv2.waitKey(20) if (r == 54) or (r == 100) or (r == 0): value[0] = value[0] + 1 elif (r == 52) or (r == 97) or (r == 113): value[0] = value[0] - 1 elif (r == 103): value[0] = value[0] - 30 elif (r == 104): value[0] = value[0] + 30 elif (r == 102): value[0] = value[0] - 100 elif (r == 106): value[0] = value[0] + 100 cv2.destroyAllWindows() if not (buttonEntireVideo): self.configFile["firstFrame"] = int(value[0]) cap.set(1, max_l) ret, frame = cap.read() while not (ret): max_l = max_l - 1 cap.set(1, max_l) ret, frame = cap.read() WINDOW_NAME = "Choose where the analysis of your video should end." WINDOW_NAME_CTRL = "Control" cvui.init(WINDOW_NAME) cv2.moveWindow(WINDOW_NAME, 0, 0) cvui.init(WINDOW_NAME_CTRL) cv2.moveWindow(WINDOW_NAME_CTRL, 0, 300) value = [max_l] curValue = value[0] buttonclicked = False widgetX = 40 widgetY = 20 widgetL = 300 while not (buttonclicked): value[0] = int(value[0]) if curValue != value[0]: cap.set(1, value[0]) frameOld = frame ret, frame = cap.read() if not (ret): frame = frameOld curValue = value[0] frameCtrl = np.full((200, 400), 100).astype('uint8') frameCtrl[widgetY:widgetY + 60, widgetX:widgetX + widgetL] = 0 cvui.text(frameCtrl, widgetX, widgetY, 'Frame') cvui.trackbar(frameCtrl, widgetX, widgetY + 10, widgetL, value, 0, max_l - 1) cvui.counter(frameCtrl, widgetX, widgetY + 60, value) buttonclicked = cvui.button( frameCtrl, widgetX, widgetY + 90, "Ok, I want the tracking to end at this frame!") cvui.text(frameCtrl, widgetX, widgetY + 130, 'Keys: 4 or a: move backwards; 6 or d: move forward') cvui.text(frameCtrl, widgetX, widgetY + 160, 'Keys: g or f: fast backwards; h or j: fast forward') cvui.imshow(WINDOW_NAME, frame) cvui.imshow(WINDOW_NAME_CTRL, frameCtrl) r = cv2.waitKey(20) if (r == 54) or (r == 100) or (r == 0): value[0] = value[0] + 1 elif (r == 52) or (r == 97) or (r == 113): value[0] = value[0] - 1 elif (r == 103): value[0] = value[0] - 30 elif (r == 104): value[0] = value[0] + 30 elif (r == 102): value[0] = value[0] - 100 elif (r == 106): value[0] = value[0] + 100 self.configFile["lastFrame"] = int(value[0]) cv2.destroyAllWindows() if int(self.configFile["headEmbeded"]) == 1: controller.show_frame("HeadEmbeded") else: if self.organism == 'zebrafishNew': controller.show_frame("NumberOfAnimals2") elif self.organism == 'zebrafish': controller.show_frame("NumberOfAnimals") else: controller.show_frame("NumberOfAnimalsCenterOfMass")
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', cv2.IMREAD_COLOR) frame = np.zeros(lena.shape, np.uint8) doubleBuffer = np.zeros(lena.shape, np.uint8) trackbarWidth = 130 # Adjustments values for RGB and HSV rgb = [[1.], [1.], [1]] hsv = [[1.], [1.], [1]] # Copy the loaded image to the buffer doubleBuffer[:] = lena[:] # Init cvui and tell it to use a value of 20 for cv2.waitKey() # because we want to enable keyboard shortcut for # all components, e.g. button with label '&Quit'. # If cvui has a value for waitKey, it will call # waitKey() automatically for us within cvui.update(). cvui.init(WINDOW_NAME, 20) while (True): frame[:] = doubleBuffer[:] frameRows, frameCols, frameChannels = frame.shape # Exit the application if the quit button was pressed. # It can be pressed because of a mouse click or because # the user pressed the 'q' key on the keyboard, which is # marked as a shortcut in the button label ('&Quit'). if cvui.button(frame, frameCols - 100, frameRows - 30, '&Quit'): break # RGB HUD cvui.window(frame, 20, 50, 180, 240, 'RGB adjust') # Within the cvui.beginColumns() and cvui.endColumn(), # all elements will be automatically positioned by cvui. # In a columns, all added elements are vertically placed, # one under the other (from top to bottom). # # Notice that all component calls within the begin/end block # below DO NOT have (x,y) coordinates. # # Let's create a row at position (35,80) with automatic # width and height, and a padding of 10 cvui.beginColumn(frame, 35, 80, -1, -1, 10) rgbModified = False # Trackbar accept a pointer to a variable that controls their value # They return true upon edition if cvui.trackbar(trackbarWidth, rgb[0], 0., 2., 2, '%3.02Lf'): rgbModified = True if cvui.trackbar(trackbarWidth, rgb[1], 0., 2., 2, '%3.02Lf'): rgbModified = True if cvui.trackbar(trackbarWidth, rgb[2], 0., 2., 2, '%3.02Lf'): rgbModified = True cvui.space(2) cvui.printf(0.35, 0xcccccc, ' RGB: %3.02lf,%3.02lf,%3.02lf', rgb[0][0], rgb[1][0], rgb[2][0]) if (rgbModified): b, g, r = cv2.split(lena) b = b * rgb[2][0] g = g * rgb[1][0] r = r * rgb[0][0] cv2.merge((b, g, r), doubleBuffer) cvui.endColumn() # HSV lenaRows, lenaCols, lenaChannels = lena.shape cvui.window(frame, lenaCols - 200, 50, 180, 240, 'HSV adjust') cvui.beginColumn(frame, lenaCols - 180, 80, -1, -1, 10) hsvModified = False if cvui.trackbar(trackbarWidth, hsv[0], 0., 2., 2, '%3.02Lf'): hsvModified = True if cvui.trackbar(trackbarWidth, hsv[1], 0., 2., 2, '%3.02Lf'): hsvModified = True if cvui.trackbar(trackbarWidth, hsv[2], 0., 2., 2, '%3.02Lf'): hsvModified = True cvui.space(2) cvui.printf(0.35, 0xcccccc, ' HSV: %3.02lf,%3.02lf,%3.02lf', hsv[0][0], hsv[1][0], hsv[2][0]) if hsvModified: hsvMat = cv2.cvtColor(lena, cv2.COLOR_BGR2HSV) h, s, v = cv2.split(hsvMat) h = h * hsv[0][0] s = s * hsv[1][0] v = v * hsv[2][0] cv2.merge((h, s, v), hsvMat) doubleBuffer = cv2.cvtColor(hsvMat, cv2.COLOR_HSV2BGR) cvui.endColumn() # Display the lib version at the bottom of the screen cvui.printf(frame, frameCols - 300, frameRows - 20, 0.4, 0xCECECE, 'cvui v.%s', cvui.VERSION) # This function must be called *AFTER* all UI components. It does # all the behind the scenes magic to handle mouse clicks, etc. # # Since cvui.init() received a param regarding waitKey, # there is no need to call cv2.waitKey() anymore. cvui.update() # will do it automatically. cvui.update() # Show everything on the screen cv2.imshow(WINDOW_NAME, frame)
def main(): frame = np.zeros((300, 600, 3), np.uint8) checked = [False] checked2 = [True] count = [0] countFloat = [0.0] trackbarValue = [0.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) # Show some pieces of text. cvui.text(frame, 50, 30, 'Hey there!') # You can also specify the size of the text and its color # using hex 0xRRGGBB CSS-like style. cvui.text(frame, 200, 30, 'Use hex 0xRRGGBB colors easily', 0.4, 0xff0000) # Sometimes you want to show text that is not that simple, e.g. strings + numbers. # You can use cvui.printf for that. It accepts a variable number of parameter, pretty # much like printf does. cvui.printf(frame, 200, 50, 0.4, 0x00ff00, 'Use printf formatting: %d + %.2f = %f', 2, 3.2, 5.2) # Buttons will return true if they were clicked, which makes # handling clicks a breeze. if cvui.button(frame, 50, 60, 'Button'): print('Button clicked') # If you do not specify the button width/height, the size will be # automatically adjusted to properly house the label. cvui.button(frame, 200, 70, 'Button with large label') # You can tell the width and height you want cvui.button(frame, 410, 70, 15, 15, 'x') # Window components are useful to create HUDs and similars. At the # moment, there is no implementation to constraint content within a # a window. cvui.window(frame, 50, 120, 120, 100, 'Window') # The counter component can be used to alter int variables. Use # the 4th parameter of the function to point it to the variable # to be changed. cvui.counter(frame, 200, 120, count) # Counter can be used with doubles too. You can also specify # the counter's step (how much it should change # its value after each button press), as well as the format # used to print the value. cvui.counter(frame, 320, 120, countFloat, 0.1, '%.1f') # The trackbar component can be used to create scales. # It works with all numerical types (including chars). cvui.trackbar(frame, 420, 110, 150, trackbarValue, 0., 50.) # Checkboxes also accept a pointer to a variable that controls # the state of the checkbox (checked or not). cvui.checkbox() will # automatically update the value of the boolean after all # interactions, but you can also change it by yourself. Just # do "checked = [True]" somewhere and the checkbox will change # its appearance. cvui.checkbox(frame, 200, 160, 'Checkbox', checked) cvui.checkbox(frame, 200, 190, 'A checked checkbox', checked2) # Display the lib version at the bottom of the screen cvui.printf(frame, 600 - 80, 300 - 20, 0.4, 0xCECECE, 'cvui v.%s', cvui.VERSION) # 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 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
displ = left_matcher.compute(grayLeft, grayRight) #.astype(np.float32)/16 displ = np.int16(displ) filteredImg = cv2.normalize(src=displ, dst=None, beta=0, alpha=255, norm_type=cv2.NORM_MINMAX, dtype=cv2.CV_8UC1) filteredImg = cv2.applyColorMap(filteredImg, cv2.COLORMAP_BONE) # Fill the frame with a nice color frame[:] = (49, 52, 49) cvui.text(frame, 50, 20, 'Block Size: ') cvui.trackbar(frame, 25, 40, 600, blockSize, 5, 101, 2, '%.1Lf', cvui.TRACKBAR_DISCRETE, 2) cvui.text(frame, 50, 90, 'numDisparites') cvui.trackbar(frame, 25, 110, 600, numDisparities, 16, 198, 16, '%.1Lf', cvui.TRACKBAR_DISCRETE, 16) cvui.text(frame, 50, 160, 'P1: ') cvui.trackbar(frame, 25, 180, 600, P1, 0, 100, 1, '%.1Lf', cvui.TRACKBAR_DISCRETE, 1) cvui.text(frame, 50, 230, 'P2 (should be larger than p2: ') cvui.trackbar(frame, 25, 250, 600, P2, 0, 3000, 1, '%.1Lf', cvui.TRACKBAR_DISCRETE, 1) cvui.text(frame, 50, 300, 'disp12MaxDiff: ') cvui.trackbar(frame, 25, 320, 600, disp12MaxDiff, 0, 100, 1, '%.1Lf',
def prepareConfigFileForParamsAdjustements(configFile, wellNumber, firstFrameParamAdjust, videoToCreateConfigFileFor, adjustOnWholeVideo): initialFirstFrameValue = -1 initialLastFrameValue = -1 if "firstFrame" in configFile: initialFirstFrameValue = configFile["firstFrame"] if "lastFrame" in configFile: initialLastFrameValue = configFile["lastFrame"] cap = cv2.VideoCapture(videoToCreateConfigFileFor) max_l = int(cap.get(7)) if int(firstFrameParamAdjust): cap.set(1, 1) ret, frame = cap.read() WINDOW_NAME = "Choose where you want to start the procedure to adjust parameters." WINDOW_NAME_CTRL = "Control" cvui.init(WINDOW_NAME) cv2.moveWindow(WINDOW_NAME, 0,0) cvui.init(WINDOW_NAME_CTRL) cv2.moveWindow(WINDOW_NAME_CTRL, 0, 300) value = [1] curValue = value[0] buttonclicked = False widgetX = 40 widgetY = 50 widgetL = 300 while not(buttonclicked): value[0] = int(value[0]) if curValue != value[0]: cap.set(1, value[0]) frameOld = frame ret, frame = cap.read() if not(ret): frame = frameOld curValue = value[0] frameCtrl = np.full((200, 400), 100).astype('uint8') frameCtrl[widgetY:widgetY+60, widgetX:widgetX+widgetL] = 0 cvui.text(frameCtrl, widgetX, widgetY, 'Frame') cvui.trackbar(frameCtrl, widgetX, widgetY+10, widgetL, value, 0, max_l-1) cvui.counter(frameCtrl, widgetX, widgetY+60, value) buttonclicked = cvui.button(frameCtrl, widgetX, widgetY+90, "Ok, I want the procedure to start at this frame.") cvui.text(frameCtrl, widgetX, widgetY+130, 'Keys: 4 or a: move backwards; 6 or d: move forward') cvui.text(frameCtrl, widgetX, widgetY+160, 'Keys: g or f: fast backwards; h or j: fast forward') cvui.imshow(WINDOW_NAME, frame) cvui.imshow(WINDOW_NAME_CTRL, frameCtrl) r = cv2.waitKey(20) if (r == 54) or (r == 100) or (r == 0): value[0] = value[0] + 1 elif (r == 52) or (r == 97) or (r == 113): value[0] = value[0] - 1 elif (r == 103): value[0] = value[0] - 30 elif (r == 104): value[0] = value[0] + 30 elif (r == 102): value[0] = value[0] - 100 elif (r == 106): value[0] = value[0] + 100 configFile["firstFrame"] = int(value[0]) cv2.destroyAllWindows() if not(int(adjustOnWholeVideo)): if ("lastFrame" in configFile): if (configFile["lastFrame"] - configFile["firstFrame"] > 500): configFile["lastFrame"] = configFile["firstFrame"] + 500 else: configFile["lastFrame"] = min(configFile["firstFrame"] + 500, max_l-10) else: if not(int(adjustOnWholeVideo)): if ("firstFrame" in configFile) and ("lastFrame" in configFile): if configFile["lastFrame"] - configFile["firstFrame"] > 500: configFile["lastFrame"] = configFile["firstFrame"] + 500 else: configFile["firstFrame"] = 1 configFile["lastFrame"] = min(max_l-10, 500) if "lastFrame" in configFile: if configFile["lastFrame"] > initialLastFrameValue and initialLastFrameValue != -1: configFile["lastFrame"] = initialLastFrameValue if len(wellNumber) != 0: configFile["onlyTrackThisOneWell"] = int(wellNumber) else: configFile["onlyTrackThisOneWell"] = 0 configFile["reloadBackground"] = 1 return [configFile, initialFirstFrameValue, initialLastFrameValue]
def main(): lena = cv2.imread('lena.jpg', cv2.IMREAD_COLOR) frame = np.zeros(lena.shape, np.uint8) doubleBuffer = np.zeros(lena.shape, np.uint8) trackbarWidth = 130 # Adjustments values for RGB and HSV rgb = [[1.], [1.], [1]] hsv = [[1.], [1.], [1]] # Copy the loaded image to the buffer doubleBuffer[:] = lena[:] # Init cvui and tell it to use a value of 20 for cv2.waitKey() # because we want to enable keyboard shortcut for # all components, e.g. button with label '&Quit'. # If cvui has a value for waitKey, it will call # waitKey() automatically for us within cvui.update(). cvui.init(WINDOW_NAME, 20) while (True): frame[:] = doubleBuffer[:] frameRows,frameCols,frameChannels = frame.shape # Exit the application if the quit button was pressed. # It can be pressed because of a mouse click or because # the user pressed the 'q' key on the keyboard, which is # marked as a shortcut in the button label ('&Quit'). if cvui.button(frame, frameCols - 100, frameRows - 30, '&Quit'): break # RGB HUD cvui.window(frame, 20, 50, 180, 240, 'RGB adjust') # Within the cvui.beginColumns() and cvui.endColumn(), # all elements will be automatically positioned by cvui. # In a columns, all added elements are vertically placed, # one under the other (from top to bottom). # # Notice that all component calls within the begin/end block # below DO NOT have (x,y) coordinates. # # Let's create a row at position (35,80) with automatic # width and height, and a padding of 10 cvui.beginColumn(frame, 35, 80, -1, -1, 10) rgbModified = False # Trackbar accept a pointer to a variable that controls their value # They return true upon edition if cvui.trackbar(trackbarWidth, rgb[0], 0., 2., 2, '%3.02Lf'): rgbModified = True if cvui.trackbar(trackbarWidth, rgb[1], 0., 2., 2, '%3.02Lf'): rgbModified = True if cvui.trackbar(trackbarWidth, rgb[2], 0., 2., 2, '%3.02Lf'): rgbModified = True cvui.space(2) cvui.printf(0.35, 0xcccccc, ' RGB: %3.02lf,%3.02lf,%3.02lf', rgb[0][0], rgb[1][0], rgb[2][0]) if (rgbModified): b,g,r = cv2.split(lena) b = b * rgb[2][0] g = g * rgb[1][0] r = r * rgb[0][0] cv2.merge((b,g,r), doubleBuffer) cvui.endColumn() # HSV lenaRows,lenaCols,lenaChannels = lena.shape cvui.window(frame, lenaCols - 200, 50, 180, 240, 'HSV adjust') cvui.beginColumn(frame, lenaCols - 180, 80, -1, -1, 10) hsvModified = False if cvui.trackbar(trackbarWidth, hsv[0], 0., 2., 2, '%3.02Lf'): hsvModified = True if cvui.trackbar(trackbarWidth, hsv[1], 0., 2., 2, '%3.02Lf'): hsvModified = True if cvui.trackbar(trackbarWidth, hsv[2], 0., 2., 2, '%3.02Lf'): hsvModified = True cvui.space(2) cvui.printf(0.35, 0xcccccc, ' HSV: %3.02lf,%3.02lf,%3.02lf', hsv[0][0], hsv[1][0], hsv[2][0]) if hsvModified: hsvMat = cv2.cvtColor(lena, cv2.COLOR_BGR2HSV) h,s,v = cv2.split(hsvMat) h = h * hsv[0][0] s = s * hsv[1][0] v = v * hsv[2][0] cv2.merge((h,s,v), hsvMat) doubleBuffer = cv2.cvtColor(hsvMat, cv2.COLOR_HSV2BGR) cvui.endColumn() # Display the lib version at the bottom of the screen cvui.printf(frame, frameCols - 300, frameRows - 20, 0.4, 0xCECECE, 'cvui v.%s', cvui.VERSION) # This function must be called *AFTER* all UI components. It does # all the behind the scenes magic to handle mouse clicks, etc. # # Since cvui.init() received a param regarding waitKey, # there is no need to call cv2.waitKey() anymore. cvui.update() # will do it automatically. cvui.update() # Show everything on the screen cv2.imshow(WINDOW_NAME, frame)
def chooseVideoToTroubleshootSplitVideo(self, controller): # Choosing video to split if globalVariables["mac"]: self.videoToTroubleshootSplitVideo = filedialog.askopenfilename(initialdir = os.path.expanduser("~"),title = "Select video") else: self.videoToTroubleshootSplitVideo = filedialog.askopenfilename(initialdir = os.path.expanduser("~"),title = "Select video",filetypes = (("video","*.*"),("all files","*.*"))) # User input of beginning and end of subvideo firstFrame = 1 lastFrame = 1000 cap = cv2.VideoCapture(self.videoToTroubleshootSplitVideo) max_l = int(cap.get(7)) - 2 cap.set(1, 1) ret, frame = cap.read() WINDOW_NAME = "Choose where the beginning of your sub-video should be." WINDOW_NAME_CTRL = "Control" cvui.init(WINDOW_NAME) cv2.moveWindow(WINDOW_NAME, 0,0) cvui.init(WINDOW_NAME_CTRL) cv2.moveWindow(WINDOW_NAME_CTRL, 0, 300) value = [1] curValue = value[0] buttonclicked = False widgetX = 40 widgetY = 20 widgetL = 300 while not(buttonclicked): value[0] = int(value[0]) if curValue != value[0]: cap.set(1, value[0]) frameOld = frame ret, frame = cap.read() if not(ret): frame = frameOld curValue = value[0] frameCtrl = np.full((200, 750), 100).astype('uint8') frameCtrl[widgetY:widgetY+60, widgetX:widgetX+widgetL] = 0 cvui.text(frameCtrl, widgetX, widgetY, 'Frame') cvui.trackbar(frameCtrl, widgetX, widgetY+10, widgetL, value, 0, max_l) cvui.counter(frameCtrl, widgetX, widgetY+60, value) buttonclicked = cvui.button(frameCtrl, widgetX, widgetY+90, "Ok, I want the sub-video to start at this frame!") cvui.text(frameCtrl, widgetX, widgetY+130, 'Keys: 4 or a: move backwards; 6 or d: move forward') cvui.text(frameCtrl, widgetX, widgetY+160, 'Keys: g or f: fast backwards; h or j: fast forward') cvui.imshow(WINDOW_NAME, frame) cvui.imshow(WINDOW_NAME_CTRL, frameCtrl) r = cv2.waitKey(20) if (r == 54) or (r == 100) or (r == 0): value[0] = value[0] + 1 elif (r == 52) or (r == 97) or (r == 113): value[0] = value[0] - 1 elif (r == 103): value[0] = value[0] - 30 elif (r == 104): value[0] = value[0] + 30 elif (r == 102): value[0] = value[0] - 100 elif (r == 106): value[0] = value[0] + 100 cv2.destroyAllWindows() firstFrame = int(value[0]) cap.set(1, max_l) ret, frame = cap.read() while not(ret): max_l = max_l - 1 cap.set(1, max_l) ret, frame = cap.read() WINDOW_NAME = "Choose where the sub-video should end." WINDOW_NAME_CTRL = "Control" cvui.init(WINDOW_NAME) cv2.moveWindow(WINDOW_NAME, 0,0) cvui.init(WINDOW_NAME_CTRL) cv2.moveWindow(WINDOW_NAME_CTRL, 0, 300) value = [max_l] curValue = value[0] buttonclicked = False widgetX = 40 widgetY = 20 widgetL = 300 while not(buttonclicked): value[0] = int(value[0]) if curValue != value[0]: cap.set(1, value[0]) frameOld = frame ret, frame = cap.read() if not(ret): frame = frameOld curValue = value[0] frameCtrl = np.full((200, 400), 100).astype('uint8') frameCtrl[widgetY:widgetY+60, widgetX:widgetX+widgetL] = 0 cvui.text(frameCtrl, widgetX, widgetY, 'Frame') cvui.trackbar(frameCtrl, widgetX, widgetY+10, widgetL, value, firstFrame + 1, max_l-1) cvui.counter(frameCtrl, widgetX, widgetY+60, value) buttonclicked = cvui.button(frameCtrl, widgetX, widgetY+90, "Ok, I want the sub-video to end at this frame!") cvui.text(frameCtrl, widgetX, widgetY+130, 'Keys: 4 or a: move backwards; 6 or d: move forward') cvui.text(frameCtrl, widgetX, widgetY+160, 'Keys: g or f: fast backwards; h or j: fast forward') cvui.imshow(WINDOW_NAME, frame) cvui.imshow(WINDOW_NAME_CTRL, frameCtrl) r = cv2.waitKey(20) if (r == 54) or (r == 100) or (r == 0): value[0] = value[0] + 1 elif (r == 52) or (r == 97) or (r == 113): value[0] = value[0] - 1 elif (r == 103): value[0] = value[0] - 30 elif (r == 104): value[0] = value[0] + 30 elif (r == 102): value[0] = value[0] - 100 elif (r == 106): value[0] = value[0] + 100 lastFrame = int(value[0]) cv2.destroyAllWindows() cap.release() # Choosing directory to save sub-video directoryChosen = filedialog.askdirectory(title='Choose in which folder you want to save the sub-video.') # Extracting sub-video cap = cv2.VideoCapture(self.videoToTroubleshootSplitVideo) if (cap.isOpened()== False): print("Error opening video stream or file") frame_width = int(cap.get(3)) frame_height = int(cap.get(4)) xmin = 0 xmax = frame_width ymin = 0 ymax = frame_height out = cv2.VideoWriter(os.path.join(directoryChosen, 'subvideo.avi'), cv2.VideoWriter_fourcc('M','J','P','G'), 10, (xmax-xmin,ymax-ymin)) i = firstFrame maxx = lastFrame cap.set(1, i) while(cap.isOpened() and (i<maxx)): i = i + 1 ret, frame = cap.read() if ret == True: frame2 = frame[ymin:ymax,xmin:xmax] if False: frame2 = cv2.cvtColor(frame2, cv2.COLOR_BGR2GRAY) frame2 = cv2.cvtColor(frame2, cv2.COLOR_GRAY2BGR) out.write(frame2) else: break cap.release() self.show_frame("VideoToTroubleshootSplitVideo")
def main(): intValue = [30] ucharValue = [30] charValue = [30] floatValue = [12.] doubleValue1 = [15.] doubleValue2 = [10.3] doubleValue3 = [2.25] frame = np.zeros((650, 450, 3), np.uint8) # Size of trackbars width = 400 # Init cvui and tell it to use a value of 20 for cv2.waitKey() # because we want to enable keyboard shortcut for # all components, e.g. button with label '&Quit'. # If cvui has a value for waitKey, it will call # waitKey() automatically for us within cvui.update(). cvui.init(WINDOW_NAME, 20) while (True): # Fill the frame with a nice color frame[:] = (49, 52, 49) cvui.beginColumn(frame, 20, 20, -1, -1, 6) cvui.text('int trackbar, no customization') cvui.trackbar(width, intValue, 0, 100) cvui.space(5) cvui.text('uchar trackbar, no customization') cvui.trackbar(width, ucharValue, 0, 255) cvui.space(5) cvui.text('signed char trackbar, no customization') cvui.trackbar(width, charValue, -128, 127) cvui.space(5) cvui.text('float trackbar, no customization') cvui.trackbar(width, floatValue, 10., 15.) cvui.space(5) cvui.text('float trackbar, 4 segments') cvui.trackbar(width, doubleValue1, 10., 20., 4) cvui.space(5) cvui.text('double trackbar, label %.1Lf, TRACKBAR_DISCRETE') cvui.trackbar(width, doubleValue2, 10., 10.5, 1, '%.1Lf', cvui.TRACKBAR_DISCRETE, 0.1) cvui.space(5) cvui.text( 'double trackbar, label %.2Lf, 2 segments, TRACKBAR_DISCRETE') cvui.trackbar(width, doubleValue3, 0., 4., 2, '%.2Lf', cvui.TRACKBAR_DISCRETE, 0.25) cvui.space(10) # Exit the application if the quit button was pressed. # It can be pressed because of a mouse click or because # the user pressed the 'q' key on the keyboard, which is # marked as a shortcut in the button label ('&Quit'). if cvui.button('&Quit'): break cvui.endColumn() # Since cvui.init() received a param regarding waitKey, # there is no need to call cv.waitKey() anymore. cvui.update() # will do it automatically. cvui.update() cv2.imshow(WINDOW_NAME, frame)
filteredImg = wls_filter.filter(displ, fixedLeft, None, dispr) # important to put "imgL" here!!! filteredImg = cv2.normalize(src=filteredImg, dst=filteredImg, beta=0, alpha=255, norm_type=cv2.NORM_MINMAX) filteredImg = np.uint8(filteredImg) filteredImg = cv2.applyColorMap(filteredImg, cv2.COLORMAP_BONE) # Fill the frame with a nice color frame[:] = (49, 52, 49) cvui.text(frame, 50, 20, 'Block Size: ') cvui.trackbar(frame, 25, 40, 600, blockSize, 5, 101, 2, '%.1Lf', cvui.TRACKBAR_DISCRETE, 2) cvui.text(frame, 50, 120, 'minDisparites from -100 to ') cvui.trackbar(frame, 25, 140, 600, numDisparities, 0, 198, 16, '%.1Lf', cvui.TRACKBAR_DISCRETE, 16) cvui.text(frame, 50, 220, 'Uniqueness Ratio: ') cvui.trackbar(frame, 25, 240, 600, uniquessRatio, 0, 100, 1, '%.1Lf', cvui.TRACKBAR_DISCRETE, 1) cvui.text(frame, 50, 320, 'speckleRange: ') cvui.trackbar(frame, 25, 340, 600, speckleRange, 0, 100, 1, '%.1Lf', cvui.TRACKBAR_DISCRETE, 1) cvui.text(frame, 50, 420, 'speckleWindow: ') cvui.trackbar(frame, 25, 440, 600, speckleWindow, 0, 100, 1, '%.1Lf',
def draw_settings(ctrl_frame, control_panes, canny_state, threshold_state, stage_mode, focus_mode, tracker_mode): # define the mutually-exclusive mode toggles stage_manual = [stage_mode == 'MANUAL'] stage_auto = [stage_mode == 'AUTO'] stage_paused = [stage_mode == 'PAUSED'] focus_manual = [focus_mode == 'MANUAL'] focus_sharpness = [focus_mode == 'SHARPNESS'] focus_depth = [focus_mode == 'DEPTH'] tracker_box_kcf = [tracker_mode == 'KCF'] tracker_box_canny = [tracker_mode == 'CANNY'] tracker_box_threshold = [tracker_mode == 'THRESHOLD'] macro_resweep = False ll_resweep = False control_panes.stage_control_pane.begin(ctrl_frame) if not control_panes.stage_control_pane.isMinimized(): cvui.space(100) # add 20px of empty space cvui.checkbox('Stage Paused', stage_paused) cvui.checkbox('Stage Manual', stage_manual) cvui.checkbox('Stage Auto', stage_auto) control_panes.stage_control_pane.end() control_panes.focus_control_pane.begin(ctrl_frame) if not control_panes.focus_control_pane.isMinimized(): cvui.space(80) # add 20px of empty space cvui.checkbox('Focus Manual', focus_manual) cvui.checkbox('Focus Sharpness', focus_sharpness) cvui.checkbox('Focus Depth', focus_depth) control_panes.focus_control_pane.end() control_panes.tracker_select_pane.begin(ctrl_frame) if not control_panes.tracker_select_pane.isMinimized(): cvui.space(60) # add 20px of empty space cvui.checkbox('KCF Tracker', tracker_box_kcf) cvui.checkbox('Canny Tracker', tracker_box_canny) cvui.checkbox('Threshold Tracker', tracker_box_threshold) control_panes.tracker_select_pane.end() control_panes.canny_settings_pane.begin(ctrl_frame) if not control_panes.canny_settings_pane.isMinimized(): cvui.space(40) # add 20px of empty space cvui.text('Canny Low Threshold') cvui.trackbar(control_panes.canny_settings_pane.width() - 20, canny_state.canny_low, 5, 150) cvui.text('Canny High Threshold') cvui.trackbar(control_panes.canny_settings_pane.width() - 20, canny_state.canny_high, 80, 300) control_panes.canny_settings_pane.end() control_panes.threshold_setting_pane.begin(ctrl_frame) if not control_panes.threshold_setting_pane.isMinimized(): cvui.space(20) # add 20px of empty space cvui.text('Binarization Threshold') cvui.trackbar(control_panes.threshold_setting_pane.width() - 20, threshold_state.threshold, 0, 255) cvui.checkbox('Show Binary Image', threshold_state.show_binary) cvui.text('MOVE THESE!!!!') if cvui.button('Force Macro Focus Sweep'): macro_resweep = True else: macro_resweep = False if cvui.button('Liquid Lens Focus Sweep'): ll_resweep = True else: ll_resweep = False control_panes.threshold_setting_pane.end() if stage_manual[0] and not stage_mode == 'MANUAL': stage_mode = 'MANUAL' elif stage_auto[0] and not stage_mode == 'AUTO': stage_mode = 'AUTO' elif stage_paused[0] and not stage_mode == 'PAUSED': stage_mode = 'PAUSED' if focus_manual[0] and not focus_mode == 'MANUAL': focus_mode = 'MANUAL' elif focus_sharpness[0] and not focus_mode == 'SHARPNESS': focus_mode = 'SHARPNESS' elif focus_depth[0] and not focus_mode == 'DEPTH': focus_mode = 'DEPTH' if tracker_box_kcf[0] and not tracker_mode == 'KCF': tracker_mode = 'KCF' elif tracker_box_canny[0] and not tracker_mode == 'CANNY': tracker_mode = 'CANNY' elif tracker_box_threshold and not tracker_mode == 'THRESHOLD': tracker_mode = 'THRESHOLD' return stage_mode, focus_mode, tracker_mode, macro_resweep, ll_resweep
import cvui import os path = os.path.abspath(os.path.dirname(__file__)) src = cv2.imread(path + "/image/1.jpg") hsv = cv2.cvtColor(src, cv2.COLOR_BGR2HSV) cvui.init("opencv") frame = np.zeros((800, 800, 3), np.uint8) floatValue_min = [20.] floatValue_max = [30.] h_min = [20.] h_max = [140.] while True: frame[:] = (49, 52, 49) cvui.trackbar(frame, 10, 60, 300, h_min, 0, 179) cvui.trackbar(frame, 320, 60, 300, h_max, 0, 179) lower_blue = np.array([h_min[0], 90, 90]) upper_blue = np.array([h_max[0], 255, 255]) mask = cv2.inRange(hsv, lower_blue, upper_blue) mask = ~mask dest = cv2.bitwise_and(src, src, mask=mask) cvui.image(frame, 20, 120, dest) cvui.update() cv2.imshow("opencv", frame) cv2.waitKey(20)
def param(frame, x, y, width, value, min, max, name, step=1): cvui.text(frame, x, y, name) cvui.counter(frame, x, y + 13, value, step, "%.0Lf") cvui.trackbar(frame, x + 100, y, width, value, min, max, 0, '%.0Lf', cvui.TRACKBAR_DISCRETE, step) value[0] = np.clip(value[0], min, max).astype(type(value[0]))