selectivesearchsegmentation_demo.py 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. #!/usr/bin/env python
  2. '''
  3. A program demonstrating the use and capabilities of a particular image segmentation algorithm described
  4. in Jasper R. R. Uijlings, Koen E. A. van de Sande, Theo Gevers, Arnold W. M. Smeulders:
  5. "Selective Search for Object Recognition"
  6. International Journal of Computer Vision, Volume 104 (2), page 154-171, 2013
  7. Usage:
  8. ./selectivesearchsegmentation_demo.py input_image (single|fast|quality)
  9. Use "a" to display less rects, 'd' to display more rects, "q" to quit.
  10. '''
  11. import cv2 as cv
  12. import sys
  13. if __name__ == '__main__':
  14. img = cv.imread(sys.argv[1])
  15. cv.setUseOptimized(True)
  16. cv.setNumThreads(8)
  17. gs = cv.ximgproc.segmentation.createSelectiveSearchSegmentation()
  18. gs.setBaseImage(img)
  19. if (sys.argv[2][0] == 's'):
  20. gs.switchToSingleStrategy()
  21. elif (sys.argv[2][0] == 'f'):
  22. gs.switchToSelectiveSearchFast()
  23. elif (sys.argv[2][0] == 'q'):
  24. gs.switchToSelectiveSearchQuality()
  25. else:
  26. print(__doc__)
  27. sys.exit(1)
  28. rects = gs.process()
  29. nb_rects = 10
  30. while True:
  31. wimg = img.copy()
  32. for i in range(len(rects)):
  33. if (i < nb_rects):
  34. x, y, w, h = rects[i]
  35. cv.rectangle(wimg, (x, y), (x+w, y+h), (0, 255, 0), 1, cv.LINE_AA)
  36. cv.imshow("Output", wimg);
  37. c = cv.waitKey()
  38. if (c == 100):
  39. nb_rects += 10
  40. elif (c == 97 and nb_rects > 10):
  41. nb_rects -= 10
  42. elif (c == 113):
  43. break
  44. cv.destroyAllWindows()