Package: cvui
cvuiA (very) simple UI lib built on top of OpenCV drawing primitives. Other UI libs, such asimgui, require a graphical backend (e.g. OpenGL) to work, so if you want to use imgui in a OpenCV app, you must make it OpenGL enabled, for instance. It is not the case with cvui, which usesonlyOpenCV drawing primitives to do all the rendering (no OpenGL or Qt required).FeaturesLightweight and simple to use user interface;Header-only with no external dependencies (except OpenCV);Based on OpenCV drawing primitives only (OpenGL or Qt are not required);Friendly and C-like API (no classes/objects, etc);Easily render components without worrying about their position (using rows/columns);Simple (yet powerful) mouse API;Modest number of UI components (11 in total);Available in C++ and Python (pure implementation, no bindings).UsageUse of cvui revolves around callingcvui.init()to initialize the lib, rendering cvui components to anp.ndarray(that you handle yourself) and finally showing thatnp.ndarrayon the screen usingcvui.imshow(), which is cvui's version ofcv2.imshow(). Alternatively you can usecv2.imshow()to show things, but in such case you must callcvui.update()yourself before callingcv.imshow().Below is an example:importnumpyasnpimportcv2importcvuiWINDOW_NAME='CVUI Test'# Initialize cvui and create/open a OpenCV window.cvui.init(WINDOW_NAME)# Create a frame to render components to.frame=np.zeros((200,400,3),np.uint8)whileTrue:# Clear the frame.frame[:]=(49,52,49)# Render a message in the frame at position (10, 15)cvui.text(frame,10,15,'Hello world!')# Show frame in a window.cvui.imshow(WINDOW1_NAME,frame)ifcv2.waitKey(20)==27:breakThe following sections explain in detail each one of the steps required to use cvui.1. Import and initialize cvuiBefore using cvui, you need to callcvui.init()to initialize it. The easiest way is to initialize cvui and tell it to create any OpenCV window that will be used, e.g.:importnumpyasnpimportcv2importcvuiWINDOW_NAME='CVUI Test'# Tell cvui to init and create a windowcvui.init(WINDOW_NAME)whileTrue:# your app logic hereifcv2.waitKey(20)==27:breakTip:if you need to use cvui with multiple windows, or you want more control over the process of creating windows, check theMultiple OpenCV windowspage and themultiple-windowsandmultiple-windows-complexexamples.2. Rendering and using cvui componentsAll cvui components are rendered to anp.ndarray. Below is an example showing how to render a'Hello world'message on anp.ndarraynamedframe:importnumpyasnpimportcv2importcvuiWINDOW_NAME='CVUI Test'cvui.init(WINDOW_NAME)# Create a frameframe=np.zeros((200,400,3),np.uint8)whileTrue:# clear the frameframe[:]=(49,52,49)# render a message in the frame at position (10, 15)cvui.text(frame,10,15,'Hello world!')ifcv2.waitKey(20)==27:breakSome cvui components, i.e.counter,trackbarandcheckbox., use an external variable that they need to modify to control their internal state. Since there are no pointers to built-in types in Python, cvui components that need to change an external variable must receive such variable as an array/list with a single element.Below is an example of a checkbox whose state is stored in the variablecheckboxState:importnumpyasnpimportcv2importcvuiWINDOW_NAME='CVUI Test'cvui.init(WINDOW_NAME)frame=np.zeros((200,400,3),np.uint8)# use an array/list because this variable will be changed by cvuicheckboxState=[False]whileTrue:frame[:]=(49,52,49)# Render the checkbox. Notice that checkboxState is used AS IS,# e.g. simply "checkboxState" instead of "checkboxState[0]".# Only internally that cvui will use checkboxState[0].cvui.checkbox(frame,10,15,'My checkbox',checkboxState)# Check the state of the checkbox. Here you need to remember to# use the first position of the array/list because that's the# one being used by all cvui components that perform changes# to external variables.ifcheckboxState[0]==True:print('Checkbox is checked')ifcv2.waitKey(20)==27:breakTip:see theonline documentationto learn more about all available cvui components.3. Show (window) contentAfter rendering your components, show the final result usingcvui.imshow(), which is cvui's improved version of OpenCV'scv2.imshow():importnumpyasnpimportcv2importcvuiWINDOW_NAME='CVUI Test'cvui.init(WINDOW_NAME)frame=np.zeros((200,400,3),np.uint8)whileTrue:frame[:]=(49,52,49)cvui.text(frame,10,15,'Hello world!')# Show window contentcvui.imshow(WINDOW1_NAME,frame)ifcv2.waitKey(20)==27:breakWhen you usecvui.imshow()instead ofcv2.imshow(), cvui will not only show the content, but update its internal structures to ensure all UI interactions work.If you want to usecv2.imshow(), you must callcvui.update()beforecv2.imshow()and after you are finished invoking cvui components, so cvui can perform its internal processing to handle mouse interactions. E.g.importnumpyasnpimportcv2importcvuiWINDOW_NAME='CVUI Test'cvui.init(WINDOW_NAME)frame=np.zeros((200,400,3),np.uint8)whileTrue:frame[:]=(49,52,49)cvui.text(frame,10,15,'Hello world!')# Update cvui internal stuffcvui.update()# Show window contentcv2.imshow(WINDOW1_NAME,frame)ifcv2.waitKey(20)==27:break