OpenCV  4.10.0-dev
Open Source Computer Vision
Loading...
Searching...
No Matches
Adding a Trackbar to our applications!

Table of Contents

Next Tutorial: Reading Geospatial Raster files with GDAL

Original author Ana Huamán
Compatibility OpenCV >= 3.0
  • In this tutorial we will just modify our two previous programs so that they get the input information from the trackbar.

Goals

In this tutorial you will learn how to:

Code

Let's modify the program made in the tutorial Adding (blending) two images using OpenCV. We will let the user enter the \(\alpha\) value by using the Trackbar.

Explanation

We only analyze the code that is related to Trackbar:

  • First, we load two images, which are going to be blended.
  • To create a trackbar, first we have to create the window in which it is going to be located. So:
  • Now we can create the Trackbar:

Note the following (C++ code):

  • Our Trackbar has a label TrackbarName
  • The Trackbar is located in the window named Linear Blend
  • The Trackbar values will be in the range from \(0\) to alpha_slider_max (the minimum limit is always zero).
  • The numerical value of Trackbar is stored in alpha_slider
  • Whenever the user moves the Trackbar, the callback function on_trackbar is called

Finally, we have to define the callback function on_trackbar for C++ and Python code, using an anonymous inner class listener in Java

Note that (C++ code):

  • We use the value of alpha_slider (integer) to get a double value for alpha.
  • alpha_slider is updated each time the trackbar is displaced by the user.
  • We define src1, src2, dist, alpha, alpha_slider and beta as global variables, so they can be used everywhere.

Result

  • Our program produces the following output: