Skip to content

Interactive Trackbar to edge detect Sentinel 2 data

  • blog
Earth Observation Sentinel2

Earth Observation Sentinel2

The user interface in OpenCV has a Trackbar. This is a really useful feature for interacting with images. By adjusting the slider the user can manipulate the image allowing for the adjustment of threshold values until the image display is optimised. When I wrote about interactive image segmentation I noted one of the critical things was getting the threshold correct; one of the ways of doing this was just to code in the thresholds and see what you got, then adjust. With a Trackbar this makes this step much more interactive.

OpenCV supplies a nice tutorial on building a colour palette. This allows the user to slide the red, green and blue bars between 0-255. This is a nice demonstration of the capabilities of the Trackbar – note the need to create a switch to turn on the functionality.

# create switch for ON/OFF functionality
switch = '0 : OFF \n1 : ON'
cv2.createTrackbar(switch, 'image',0,1,nothing)

With this switch set to ‘off’ nothing will happen (default it is set off).

Interactive Edge Detection

I have written previously on edge detecting and how fast and simple this is in OpenCV

At the time I noted that all we needed was 3 lines of code

frame = cv2.imread(".../data.tif")
edges = cv2.Canny(frame,100,200)
cv2.imwrite("../out.jpg", edges)

The second line contains the threshold values 100 (upper) and 200 (lower) in both this case and in the online tutorial. It is these values that I want to control with the Trackbar to get fast feedback, enabling me to get the best result. Luckily this code has already been written on github and the code is here.

This produces an interactive display, that the user can manipulate.

Edge Detection

Saving the image

If I want to save the image I know I need to call

cv2.imwrite("imageout.jpg", edges)

If I slightly adjust the script to include an event listener I can call cv2.imwrite in a function. How to do this is in the documentation. Firstly, I need to build a click event function and parse in the 5 arguments it needs by default. Then I need to save the image on the event of a left mouse click.

def click_event(event, x, y, flags, param):
    if event == cv2.EVENT_LBUTTONDOWN:
        cv2.imwrite("imageout2.png", edges)

It is a relatively simple function and then once I have done all my adjustments in the Trackbar window I need to left click on my image. To call the listener I put this line into the while loop.

cv2.setMouseCallback("canny", click_event)   

And that is it; I have my image saved. The Trackbar and the event listeners in OpenCV allow for a rich user interaction that is incredibly fast. I’ll explore the event listeners in a future blog. Now I have interactive control over edge detection, I can get the best threshold values much faster.

Earth Observation Sentinel2



I am a freelancer able to help you with your projects. I offer consultancy, training and writing. I’d be delighted to hear from you. Please check out the books I have written on QGIS 3.4


I have grouped all my previous blogs (technical stuff / tutorials / opinions / ideas) are here

I am @map_andrew on twitter