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.
- OpenCV 3.4
- Python 3.6+
- Image, Webcam or Video input
- Documentation Source: OpenCV Official Documentation
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
In this tutorial, I will show you how to Create Trackbars using OpenCV library and Python coding.
Python: cv2.createTrackbar(trackbarName, windowName, value, count, onChange) → None
Creates a trackbar and attaches it to the specified window.
- 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.
- trackbarname – Name of the trackbar.
- winname – Name of the window that is the parent of the trackbar.
- 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()
import cv2 import numpy as np def passFunction(): pass def main(): img1 = np.zeros((512, 512, 3), np.uint8) windowName = 'OpenCV BGR Color Generation' cv2.namedWindow(windowName) cv2.createTrackbar('B', windowName, 0, 255, passFunction) cv2.createTrackbar('G', windowName, 0, 255, passFunction) cv2.createTrackbar('R', windowName, 0, 255, passFunction) while (True): cv2.imshow(windowName, img1) if cv2.waitKey(1) == 27: break blue = cv2.getTrackbarPos('B', windowName) green = cv2.getTrackbarPos('G', windowName) red = cv2.getTrackbarPos('R', windowName) img1[:] = [blue, green, red] #save the color cv2.imwrite('outColor.jpg',img1) cv2.destroyAllWindows() if __name__ == "__main__": main()
- How to Create a RGB Color Picker for Images using OpenCV Python - 22 April, 2022
- Combine Several Images Vertically with Padding using OpenCV Python - 21 April, 2022
- Combine Several Images Horizontally with Padding using OpenCV Python - 21 April, 2022