Life2Coding
Generate Any Colored Background with Trackbars using OpenCV

This post will be helpful in learning OpenCV using Python programming. Here I will show how to implement OpenCV functions and apply it in various aspects using some examples. Then the output will be shown with some comparisons as well.

Requirements:

First, you need to setup your Python Environment with OpenCV. You can easily do it by following Life2Coding’s tutorial on YouTube: Linking OpenCV 3 with Python 3

Goals:

In this tutorial, I will show you how to Create Trackbars using OpenCV library and Python coding.

Documentation:

Python: cv2.createTrackbar(trackbarName, windowName, value, count, onChange) → None

Creates a trackbar and attaches it to the specified window.

Parameters:          

  • trackbarname – Name of the created trackbar.
  • winname – Name of the window that will be used as a parent of the created trackbar.
  • value – Optional pointer to an integer variable whose value reflects the position of the slider. Upon creation, the slider position is defined by this variable.
  • count – Maximal position of the slider. The minimal position is always 0.
  • onChange – Pointer to the function to be called every time the slider changes position. This function should be prototyped as void Foo(int,void*); , where the first parameter is the trackbar position and the second parameter is the user data (see the next parameter). If the callback is the NULL pointer, no callbacks are called, but only value is updated.
  • userdata – User data that is passed as is to the callback. It can be used to handle trackbar events without using global variables.

Python: cv2.getTrackbarPos(trackbarname, winname) → retval

Returns the trackbar position.

Parameters:          

  • trackbarname – Name of the trackbar.
  • winname – Name of the window that is the parent of the trackbar.

Steps:

  • Create window using cv2.namedWindow()
  • Create trackbars using cv2.createTrackbar()
  • Display Image using cv2.imshow()
  • Get trackbar scale position using cv2.getTrackbarpos()
  • Wait for keyboard button press using cv2.waitKey()
  • Save the output in an image file using cv2.imwrite()
  • Exit window and destroy all windows using cv2.destroyAllWindows()

Example Code:

Output:

Capture-1 Generate Any Colored Background with Trackbars using OpenCV

life2coding_icon [] Generate Any Colored Background with Trackbars using OpenCV

Life2Coding

Technology Related Blog at Life2Coding
Feel free to contact us for your any kind of technical problems. We are here to help you.
life2coding_icon [] Generate Any Colored Background with Trackbars using OpenCV

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.