Advertisement
Guest User

Untitled

a guest
Feb 15th, 2019
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.74 KB | None | 0 0
  1. #!/usr/bin/env python
  2.  
  3. '''
  4. This program illustrates the use of findContours and drawContours.
  5. The original image is put up along with the image of drawn contours.
  6.  
  7. Usage:
  8. contours.py
  9. A trackbar is put up which controls the contour level from -3 to 3
  10. '''
  11.  
  12. # Python 2/3 compatibility
  13. from __future__ import print_function
  14. import sys
  15. PY3 = sys.version_info[0] == 3
  16.  
  17. if PY3:
  18. xrange = range
  19.  
  20. import numpy as np
  21. import cv2 as cv
  22.  
  23. def make_image():
  24. img = np.zeros((500, 500), np.uint8)
  25. black, white = 0, 255
  26. for i in xrange(6):
  27. dx = int((i%2)*250 - 30)
  28. dy = int((i/2.)*150)
  29.  
  30. if i == 0:
  31. for j in xrange(11):
  32. angle = (j+5)*np.pi/21
  33. c, s = np.cos(angle), np.sin(angle)
  34. x1, y1 = np.int32([dx+100+j*10-80*c, dy+100-90*s])
  35. x2, y2 = np.int32([dx+100+j*10-30*c, dy+100-30*s])
  36. cv.line(img, (x1, y1), (x2, y2), white)
  37.  
  38. cv.ellipse( img, (dx+150, dy+100), (100,70), 0, 0, 360, white, -1 )
  39. cv.ellipse( img, (dx+115, dy+70), (30,20), 0, 0, 360, black, -1 )
  40. cv.ellipse( img, (dx+185, dy+70), (30,20), 0, 0, 360, black, -1 )
  41. cv.ellipse( img, (dx+115, dy+70), (15,15), 0, 0, 360, white, -1 )
  42. cv.ellipse( img, (dx+185, dy+70), (15,15), 0, 0, 360, white, -1 )
  43. cv.ellipse( img, (dx+115, dy+70), (5,5), 0, 0, 360, black, -1 )
  44. cv.ellipse( img, (dx+185, dy+70), (5,5), 0, 0, 360, black, -1 )
  45. cv.ellipse( img, (dx+150, dy+100), (10,5), 0, 0, 360, black, -1 )
  46. cv.ellipse( img, (dx+150, dy+150), (40,10), 0, 0, 360, black, -1 )
  47. cv.ellipse( img, (dx+27, dy+100), (20,35), 0, 0, 360, white, -1 )
  48. cv.ellipse( img, (dx+273, dy+100), (20,35), 0, 0, 360, white, -1 )
  49. return img
  50.  
  51. if __name__ == '__main__':
  52. print(__doc__)
  53.  
  54. img = make_image()
  55. h, w = img.shape[:2]
  56.  
  57. contours0, hierarchy = cv.findContours( img.copy(), cv.RETR_TREE, cv.CHAIN_APPROX_SIMPLE)
  58. contours = [cv.approxPolyDP(cnt, 2, True) for cnt in contours0] #ver https://docs.opencv.org/4.0.1/d3/dc0/group__imgproc__shape.html#ga0012a5fdaea70b8a9970165d98722b4c
  59.  
  60. def update(levels):
  61. vis = np.zeros((h, w, 3), np.uint8)
  62. levels = levels - 3
  63. cv.drawContours( vis, contours, (-1, 2)[levels <= 0], (128,255,255),
  64. 3, cv.LINE_AA, hierarchy, abs(levels) )
  65. cv.imshow('contours', vis)
  66. update(3)
  67. cv.createTrackbar( "levels+3", "contours", 3, 7, update )
  68. cv.imshow('image', img)
  69. cv.waitKey()
  70. cv.destroyAllWindows()
  71.  
  72. """
  73. para correte en python3
  74. >>exec(open('contours.py').read())
  75. ver por ejemplo que es
  76. >>>contours0[0][0]
  77. >>>contours0[0][0][0]
  78. >>>contours0[0][0][0]
  79. >>>contours0[0][0][0][0]
  80.  
  81.  
  82.  
  83. """
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement