| OpenCV 5.0.0alpha Open Source Computer Vision

Loading...
Searching...
No Matches
Adding a Trackbar to our applications!

Next Tutorial: Reading Geospatial Raster files with GDAL

Original author | Ana Huamán
Compatibility | OpenCV >= 3.0

- In the previous tutorials (about Adding (blending) two images using OpenCV and the Changing the contrast and brightness of an image!) you might have noted that we needed to give some input to our programs, such as \(\alpha\) and \(beta\). We accomplished that by entering this data using the Terminal.
- Well, it is time to use some fancy GUI tools. OpenCV provides some GUI utilities (highgui module) for you. An example of this is a Trackbar.

- 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:

- Add a Trackbar in an OpenCV window by using cv::createTrackbar

# 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.

C++

This tutorial code's is shown lines below. You can also download it from here

#include "opencv2/imgcodecs.hpp"
#include "opencv2/highgui.hpp"
#include <iostream>
using namespace cv;
using std::cout;
const int alpha_slider_max = 100;
int alpha_slider;
double alpha;
double beta;
Mat src1;
Mat src2;
Mat dst;
static void on_trackbar( int, void* )
{
alpha = (double) alpha_slider/alpha_slider_max ;
beta = ( 1.0 - alpha );
addWeighted( src1, alpha, src2, beta, 0.0, dst);
imshow( "Linear Blend", dst );
}
int main( void )
{
src1 = imread( samples::findFile("LinuxLogo.jpg") );
src2 = imread( samples::findFile("WindowsLogo.jpg") );
if( src1.empty() ) { cout << "Error loading src1 \n"; return -1; }
if( src2.empty() ) { cout << "Error loading src2 \n"; return -1; }
alpha_slider = 0;
namedWindow("Linear Blend", WINDOW_AUTOSIZE); // Create Window
char TrackbarName[50];
snprintf( TrackbarName, sizeof(TrackbarName), "Alpha x %d", alpha_slider_max );
createTrackbar( TrackbarName, "Linear Blend", &alpha_slider, alpha_slider_max, on_trackbar );
on_trackbar( alpha_slider, 0 );
waitKey(0);
return 0;
}
cv::Mat
n-dimensional dense array class
Definition mat.hpp:951
cv::Mat::empty
bool empty() const
Returns true if the array has no elements.
highgui.hpp
main
int main(int argc, char *argv[])
Definition highgui_qt.cpp:3
imgcodecs.hpp
cv
Definition core.hpp:107
Java

This tutorial code's is shown lines below. You can also download it from here

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Image;
import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.highgui.HighGui;
import org.opencv.imgcodecs.Imgcodecs;
public class AddingImagesTrackbar {
private static final int ALPHA_SLIDER_MAX = 100;
private int alphaVal = 0;
private Mat matImgSrc1;
private Mat matImgSrc2;
private Mat matImgDst = new Mat();
private JFrame frame;
private JLabel imgLabel;
public AddingImagesTrackbar(String[] args) {
String imagePath1 = "../data/LinuxLogo.jpg";
String imagePath2 = "../data/WindowsLogo.jpg";
if (args.length > 1) {
imagePath1 = args[0];
imagePath2 = args[1];
}
matImgSrc1 = Imgcodecs.imread(imagePath1);
matImgSrc2 = Imgcodecs.imread(imagePath2);
if (matImgSrc1.empty()) {
System.out.println("Empty image: " + imagePath1);
System.exit(0);
}
if (matImgSrc2.empty()) {
System.out.println("Empty image: " + imagePath2);
System.exit(0);
}
// Create and set up the window.
frame = new JFrame("Linear Blend");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Set up the content pane.
Image img = HighGui.toBufferedImage(matImgSrc2);
addComponentsToPane(frame.getContentPane(), img);
// Use the content pane's default BorderLayout. No need for
// setLayout(new BorderLayout());
// Display the window.
frame.pack();
frame.setVisible(true);
}
private void addComponentsToPane(Container pane, Image img) {
if (!(pane.getLayout() instanceof BorderLayout)) {
pane.add(new JLabel("Container doesn't use BorderLayout!"));
return;
}
JPanel sliderPanel = new JPanel();
sliderPanel.setLayout(new BoxLayout(sliderPanel, BoxLayout.PAGE_AXIS));
sliderPanel.add(new JLabel(String.format("Alpha x %d", ALPHA_SLIDER_MAX)));
JSlider slider = new JSlider(0, ALPHA_SLIDER_MAX, 0);
slider.setMajorTickSpacing(20);
slider.setMinorTickSpacing(5);
slider.setPaintTicks(true);
slider.setPaintLabels(true);
slider.addChangeListener(new ChangeListener() {
@Override
public void stateChanged(ChangeEvent e) {
JSlider source = (JSlider) e.getSource();
alphaVal = source.getValue();
update();
}
});
sliderPanel.add(slider);
pane.add(sliderPanel, BorderLayout.PAGE_START);
imgLabel = new JLabel(new ImageIcon(img));
pane.add(imgLabel, BorderLayout.CENTER);
}
private void update() {
double alpha = alphaVal / (double) ALPHA_SLIDER_MAX;
double beta = 1.0 - alpha;
Core.addWeighted(matImgSrc1, alpha, matImgSrc2, beta, 0, matImgDst);
Image img = HighGui.toBufferedImage(matImgDst);
imgLabel.setIcon(new ImageIcon(img));
frame.repaint();
}
public static void main(String[] args) {
// Load the native OpenCV library
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
// Schedule a job for the event dispatch thread:
// creating and showing this application's GUI.
javax.swing.SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new AddingImagesTrackbar(args);
}
});
}
}
cv::String
std::string String
Definition cvstd.hpp:151
Python

This tutorial code's is shown lines below. You can also download it from here

from __future__ import print_function
from __future__ import division
import cv2 as cv
import argparse
alpha_slider_max = 100
title_window = 'Linear Blend'
def on_trackbar(val):
alpha = val / alpha_slider_max
beta = ( 1.0 - alpha )
dst = cv.addWeighted(src1, alpha, src2, beta, 0.0)
cv.imshow(title_window, dst)
parser = argparse.ArgumentParser(description='Code for Adding a Trackbar to our applications tutorial.')
parser.add_argument('--input1', help='Path to the first input image.', default='LinuxLogo.jpg')
parser.add_argument('--input2', help='Path to the second input image.', default='WindowsLogo.jpg')
args = parser.parse_args()
src1 = cv.imread(cv.samples.findFile(args.input1))
src2 = cv.imread(cv.samples.findFile(args.input2))
if src1 is None:
print('Could not open or find the image: ', args.input1)
exit(0)
if src2 is None:
print('Could not open or find the image: ', args.input2)
exit(0)
cv.namedWindow(title_window)
trackbar_name = 'Alpha x %d' % alpha_slider_max
cv.createTrackbar(trackbar_name, title_window , 0, alpha_slider_max, on_trackbar)
# Show some stuff
on_trackbar(0)
# Wait until user press some key
cv.waitKey()
cv::addWeighted
void addWeighted(InputArray src1, double alpha, InputArray src2, double beta, double gamma, OutputArray dst, int dtype=-1)
Calculates the weighted sum of two arrays.
cv::samples::findFile
cv::String findFile(const cv::String &relative_path, bool required=true, bool silentMode=false)
Try to find requested data file.
cv::imshow
void imshow(const String &winname, InputArray mat)
Displays an image in the specified window.
cv::waitKey
int waitKey(int delay=0)
Waits for a pressed key.
cv::namedWindow
void namedWindow(const String &winname, int flags=WINDOW_AUTOSIZE)
Creates a window.
cv::createTrackbar
int createTrackbar(const String &trackbarname, const String &winname, int *value, int count, TrackbarCallback onChange=0, void *userdata=0)
Creates a trackbar and attaches it to the specified window.
cv::imread
CV_EXPORTS_W Mat imread(const String &filename, int flags=IMREAD_COLOR_BGR)
Loads an image from a file.

# Explanation

We only analyze the code that is related to Trackbar:

- First, we load two images, which are going to be blended.

C++
src1 = imread( samples::findFile("LinuxLogo.jpg") );
src2 = imread( samples::findFile("WindowsLogo.jpg") );
Java
String imagePath1 = "../data/LinuxLogo.jpg";
String imagePath2 = "../data/WindowsLogo.jpg";
if (args.length > 1) {
imagePath1 = args[0];
imagePath2 = args[1];
}
matImgSrc1 = Imgcodecs.imread(imagePath1);
matImgSrc2 = Imgcodecs.imread(imagePath2);
Python
src1 = cv.imread(cv.samples.findFile(args.input1))
src2 = cv.imread(cv.samples.findFile(args.input2))

- To create a trackbar, first we have to create the window in which it is going to be located. So:

C++
namedWindow("Linear Blend", WINDOW_AUTOSIZE); // Create Window
Java
// Create and set up the window.
frame = new JFrame("Linear Blend");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Set up the content pane.
Image img = HighGui.toBufferedImage(matImgSrc2);
addComponentsToPane(frame.getContentPane(), img);
// Use the content pane's default BorderLayout. No need for
// setLayout(new BorderLayout());
// Display the window.
frame.pack();
frame.setVisible(true);
Python
cv.namedWindow(title_window)

- Now we can create the Trackbar:

C++
char TrackbarName[50];
snprintf( TrackbarName, sizeof(TrackbarName), "Alpha x %d", alpha_slider_max );
createTrackbar( TrackbarName, "Linear Blend", &alpha_slider, alpha_slider_max, on_trackbar );
Java
sliderPanel.add(new JLabel(String.format("Alpha x %d", ALPHA_SLIDER_MAX)));
JSlider slider = new JSlider(0, ALPHA_SLIDER_MAX, 0);
slider.setMajorTickSpacing(20);
slider.setMinorTickSpacing(5);
slider.setPaintTicks(true);
slider.setPaintLabels(true);
Python
trackbar_name = 'Alpha x %d' % alpha_slider_max
cv.createTrackbar(trackbar_name, title_window , 0, alpha_slider_max, on_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

C++
static void on_trackbar( int, void* )
{
alpha = (double) alpha_slider/alpha_slider_max ;
beta = ( 1.0 - alpha );
addWeighted( src1, alpha, src2, beta, 0.0, dst);
imshow( "Linear Blend", dst );
}
Java
slider.addChangeListener(new ChangeListener() {
@Override
public void stateChanged(ChangeEvent e) {
JSlider source = (JSlider) e.getSource();
alphaVal = source.getValue();
update();
}
});
Python
def on_trackbar(val):
alpha = val / alpha_slider_max
beta = ( 1.0 - alpha )
dst = cv.addWeighted(src1, alpha, src2, beta, 0.0)
cv.imshow(title_window, dst)

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:

- As a manner of practice, you can also add two trackbars for the program made in Changing the contrast and brightness of an image!. One trackbar to set \(\alpha\) and another for set \(\beta\). The output might look like: