AddingImagesTrackbar.py 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. from __future__ import print_function
  2. from __future__ import division
  3. import cv2 as cv
  4. import argparse
  5. alpha_slider_max = 100
  6. title_window = 'Linear Blend'
  7. ## [on_trackbar]
  8. def on_trackbar(val):
  9. alpha = val / alpha_slider_max
  10. beta = ( 1.0 - alpha )
  11. dst = cv.addWeighted(src1, alpha, src2, beta, 0.0)
  12. cv.imshow(title_window, dst)
  13. ## [on_trackbar]
  14. parser = argparse.ArgumentParser(description='Code for Adding a Trackbar to our applications tutorial.')
  15. parser.add_argument('--input1', help='Path to the first input image.', default='LinuxLogo.jpg')
  16. parser.add_argument('--input2', help='Path to the second input image.', default='WindowsLogo.jpg')
  17. args = parser.parse_args()
  18. ## [load]
  19. # Read images ( both have to be of the same size and type )
  20. src1 = cv.imread(cv.samples.findFile(args.input1))
  21. src2 = cv.imread(cv.samples.findFile(args.input2))
  22. ## [load]
  23. if src1 is None:
  24. print('Could not open or find the image: ', args.input1)
  25. exit(0)
  26. if src2 is None:
  27. print('Could not open or find the image: ', args.input2)
  28. exit(0)
  29. ## [window]
  30. cv.namedWindow(title_window)
  31. ## [window]
  32. ## [create_trackbar]
  33. trackbar_name = 'Alpha x %d' % alpha_slider_max
  34. cv.createTrackbar(trackbar_name, title_window , 0, alpha_slider_max, on_trackbar)
  35. ## [create_trackbar]
  36. # Show some stuff
  37. on_trackbar(0)
  38. # Wait until user press some key
  39. cv.waitKey()