edgeboxes_demo.py 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. '''
  4. This sample demonstrates structured edge detection and edgeboxes.
  5. Usage:
  6. edgeboxes_demo.py [<model>] [<input_image>]
  7. '''
  8. import cv2 as cv
  9. import numpy as np
  10. import sys
  11. if __name__ == '__main__':
  12. print(__doc__)
  13. model = sys.argv[1]
  14. im = cv.imread(sys.argv[2])
  15. edge_detection = cv.ximgproc.createStructuredEdgeDetection(model)
  16. rgb_im = cv.cvtColor(im, cv.COLOR_BGR2RGB)
  17. edges = edge_detection.detectEdges(np.float32(rgb_im) / 255.0)
  18. orimap = edge_detection.computeOrientation(edges)
  19. edges = edge_detection.edgesNms(edges, orimap)
  20. edge_boxes = cv.ximgproc.createEdgeBoxes()
  21. edge_boxes.setMaxBoxes(30)
  22. boxes = edge_boxes.getBoundingBoxes(edges, orimap)
  23. boxes, scores = edge_boxes.getBoundingBoxes(edges, orimap)
  24. if len(boxes) > 0:
  25. boxes_scores = zip(boxes, scores)
  26. for b_s in boxes_scores:
  27. box = b_s[0]
  28. x, y, w, h = box
  29. cv.rectangle(im, (x, y), (x+w, y+h), (0, 255, 0), 1, cv.LINE_AA)
  30. score = b_s[1][0]
  31. cv.putText(im, "{:.2f}".format(score), (x, y), cv.FONT_HERSHEY_PLAIN, 0.8, (255, 255, 255), 1, cv.LINE_AA)
  32. print("Box at (x,y)=({:d},{:d}); score={:f}".format(x, y, score))
  33. cv.imshow("edges", edges)
  34. cv.imshow("edgeboxes", im)
  35. cv.waitKey(0)
  36. cv.destroyAllWindows()