Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
OpenCV 3 Computer Vision with Python Cookbook

You're reading from   OpenCV 3 Computer Vision with Python Cookbook Leverage the power of OpenCV 3 and Python to build computer vision applications

Arrow left icon
Product type Paperback
Published in Mar 2018
Publisher Packt
ISBN-13 9781788474443
Length 306 pages
Edition 1st Edition
Languages
Tools
Arrow right icon
Authors (2):
Arrow left icon
Aleksandr Rybnikov Aleksandr Rybnikov
Author Profile Icon Aleksandr Rybnikov
Aleksandr Rybnikov
Aleksei Spizhevoi Aleksei Spizhevoi
Author Profile Icon Aleksei Spizhevoi
Aleksei Spizhevoi
Arrow right icon
View More author details
Toc

Table of Contents (11) Chapters Close

Preface 1. I/O and GUI FREE CHAPTER 2. Matrices, Colors, and Filters 3. Contours and Segmentation 4. Object Detection and Machine Learning 5. Deep Learning 6. Linear Algebra 7. Detectors and Descriptors 8. Image and Video Processing 9. Multiple View Geometry 10. Other Books You May Enjoy

Working with UI elements, such as buttons and trackbars, in an OpenCV window

In this recipe, we will learn how to add UI elements, such as buttons and trackbars, into OpenCV windows and work with them. Trackbars are useful UI elements that:

  • Show the value of an integer variable, assuming the value is within a predefined range
  • Allow us to change the value interactively through changing the trackbar position

Let's create a program that allows users to specify the fill color for an image by interactively changing each Red, Green, Blue (RGB) channel value.

Getting ready

You need to have OpenCV 3.x installed with Python API support.

How to do it...

To complete this recipe, the steps are as follows:

  1. First create an OpenCV window named window:
import cv2, numpy as np

cv2.namedWindow('window')
  1. Create a variable that will contain the fill color value for the image. The variable is a NumPy array with three values that will be interpreted as blue, green, and red color components (in that order) from the [0, 255] range:
fill_val = np.array([255, 255, 255], np.uint8)
  1. Add an auxiliary function to call from each trackbar_callback function. The function takes the color component index and new value as settings:
def trackbar_callback(idx, value):
fill_val[idx] = value
  1. Add three trackbars into window and bind each trackbar callback to a specific color component using the Python lambda function:
cv2.createTrackbar('R', 'window', 255, 255, lambda v: trackbar_callback(2, v))
cv2.createTrackbar('G', 'window', 255, 255, lambda v: trackbar_callback(1, v))
cv2.createTrackbar('B', 'window', 255, 255, lambda v: trackbar_callback(0, v))
  1. In a loop, show the image in a window with three trackbars and process keyboard input as well:
while True:
image = np.full((500, 500, 3), fill_val)
cv2.imshow('window', image)
key = cv2.waitKey(3)
if key == 27:
break
cv2.destroyAllWindows()

How it works...

A window like the one following is expected to be shown, though it might vary slightly depending on the version of OpenCV and how it was built:

You have been reading a chapter from
OpenCV 3 Computer Vision with Python Cookbook
Published in: Mar 2018
Publisher: Packt
ISBN-13: 9781788474443
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image