OpenCV Python Sliders — Visual Threshold Tuning

Flipflo Dev
10 min readJun 22, 2024
Color filter based on HSV color space

If you’ve ever worked with Computer Vision or image processing in general you will likely have stumbled upon the problem of choosing a threshold. Let’s say you want to extract contours in an image. Most of the times you would follow a pipeline like this: first you blur the image to reduce the noise in the image, then use a Canny Edge edge detection to get a black and white image which you can then pass to a contour detection algorithm and finally use an approximator for these contours. Now we’re already at a lot of parameters for the blur type, kernel size, edge thresholds, non maximum suppression, contour lengths etc. Usually some generally good starting values are used and then adjusted for the task. However, before I knew that OpenCV has a really simple way to create interfaces with sliders to tune these values, this took a substantial amount of time to tweak and compare results.

In this post, I will show you how easy it is to create sliders in OpenCV and tune different image processing parameters in real-time while seeing the influence of that parameter change. For this we will create a simple tool that allows you to mask an image based on thresholds in the color space. More specifically, we are going to define lower and upper thresholds in the HSV color space to segment a flower image into its different parts. Let’s get started!

--

--

Flipflo Dev

A hobby game developer coming from a robotics and electrical engineering background, hoping to provide fun and interesting content for like minded people.