Advertisement
Guest User

Untitled

a guest
Mar 18th, 2019
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.21 KB | None | 0 0
  1. import glob
  2. images = glob.glob("*.jpg")
  3. import re
  4. import os
  5. import matplotlib.pyplot as plt
  6.  
  7. import numpy as np
  8. import cv2
  9. from PIL import Image
  10.  
  11. def natural_sort(l):
  12. convert = lambda text: int(text) if text.isdigit() else text.lower()
  13. alphanum_key = lambda key: [ convert(c) for c in re.split('([0-9]+)', key) ]
  14. return sorted(l, key = alphanum_key)
  15.  
  16. images = natural_sort(images)
  17.  
  18. im = cv2.imread(images[0])
  19. #im = cv2.cvtColor(np.array(Image.open(images[0])), cv2.COLOR_BGR2GRAY)
  20. # selective search
  21. #im = cv2.resize(im, (newWidth, newHeight))
  22. # create Selective Search Segmentation Object using default parameters
  23. ss = cv2.ximgproc.segmentation.createSelectiveSearchSegmentation()
  24.  
  25. # set input image on which we will run segmentation
  26. ss.setBaseImage(im)
  27.  
  28. # Switch to fast but low recall Selective Search method
  29. #if (sys.argv[2] == 'f'):
  30. ss.switchToSelectiveSearchFast()
  31.  
  32. # Switch to high recall but slow Selective Search method
  33. #elif (sys.argv[2] == 'q'):
  34. #ss.switchToSelectiveSearchQuality()
  35. # if argument is neither f nor q print help message
  36.  
  37.  
  38. # run selective search segmentation on input image
  39. rects = ss.process()
  40. print('Total Number of Region Proposals: {}'.format(len(rects)))
  41.  
  42. # number of region proposals to show
  43. numShowRects = 100
  44. # increment to increase/decrease total number
  45. # of reason proposals to be shown
  46. increment = 50
  47.  
  48. while True:
  49. # create a copy of original image
  50. imOut = im.copy()
  51.  
  52. # itereate over all the region proposals
  53. for i, rect in enumerate(rects):
  54. # draw rectangle for region proposal till numShowRects
  55. if (i < numShowRects):
  56. x, y, w, h = rect
  57. cv2.rectangle(imOut, (x, y), (x+w, y+h), (0, 255, 0), 1, cv2.LINE_AA)
  58. else:
  59. break
  60.  
  61. # show output
  62. cv2.imshow("Output", imOut)
  63.  
  64. # record key press
  65. k = cv2.waitKey(0) & 0xFF
  66.  
  67. # m is pressed
  68. if k == 109:
  69. # increase total number of rectangles to show by increment
  70. numShowRects += increment
  71. # l is pressed
  72. elif k == 108 and numShowRects > increment:
  73. # decrease total number of rectangles to show by increment
  74. numShowRects -= increment
  75. # q is pressed
  76. elif k == 113:
  77. break
  78. # close image show window
  79. cv2.destroyAllWindows()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement