Locked learning resources

Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Adding OpenCV Processing

Here are resources and additional documentation about OpenCV and colorspaces:

00:00 Adding OpenCV Processing. Now let’s start adding features to the program. The first one will be a radio button, which will be used to select no image processing.

00:13 It’s added to the layout in the manner that’s already been seen—adding an element to the list—in this case, a radio button with appropriate settings. While this doesn’t do anything at the moment, running the program to check that it’s in place is always worthwhile.

00:30 Here, you can see the radio button has been added. Next, a Radio() element and a Slider() element are added and their identifier keys are set to "-THRESH-" and "-THRESH SLIDER-", respectively. These will select thresholding to allow control of the threshold levels.

01:05 Before adding code to handle this, let’s just check that the UI elements appear appropriately. They work okay, but there is an issue. The window is now getting a little too big to fit onto the screen comfortably, as it’s sized to the default image size from the webcam. To rectify this, it’s possible to change the capture settings using these two lines, allowing a smaller window to be created onscreen.

01:46 Now that the window size is under control, it’s time to alter the event loop so that threshold control actually works. Here you can see the code which checks for the "-THRESH-" value being present, and if so, it performs threshold processing on the frame using the following lines.

02:18 Note that the OpenCV threshold() function used the value of "-THRESH SLIDER-" to set the thresholding level. Running this allows thresholding to be activated and the level controlled using the slider.

02:36 Now that you have one processing control up and running, let’s add another one. In this case, it will be canny edge detection. While the radio button is familiar from the previous example, the slider code is different. There are two sliders needed, so each is half the width of the threshold slider.

03:27 Next, the event loop image processing if statement is extended with an elif, allowing checking of the "-CANNY-" radio button. If that has been set, then the canny processing is applied to the frame using the following lines of code, taking the slider values and passing them into the function at the appropriate point.

04:02 Next, a radio button and slider are added to control image blurring, which is also known as image smoothing. You can read more about this technique in the OpenCV documentation.

04:25 Hopefully by now, the addition of the two extra areas of code—one to add elements to the UI, one in the event loop to perform the processing—will make sense. And again, you’ll see the new code being tested.

05:02 You will add the hue controls next. These elements allow you to convert between different color spaces. Color spaces are a complex topic that’s outside the scope of this video course, but you can read more about them in the Changing Colorspaces tutorial over on the OpenCV website.

06:33 Following a similar pattern, the last elements to add are for controlling contrast, allowing you to enhance the video stream’s contrast using the Contrast Limited Adaptive Histogram Equalization algorithm.

07:27 Note that in this case, the slider value is being scaled by division to give an appropriate input value to the function, and also that the processing here involves a number of steps.

08:07 And now you can see the full program running onscreen with multiple ways to alter the image stream in real time. Having seen the potential of PySimpleGUI to allow you to incorporate the power of OpenCV, it’s now time to learn how to create an executable of your application for Windows.

Become a Member to join the conversation.