Guest User

Untitled

a guest
Sep 22nd, 2018
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.38 KB | None | 0 0
  1. [[-1 -1 1]
  2. [-1 -1 1]
  3. [ 1 1 1]]
  4.  
  5. mask = cv2.morphologyEx(threshed, cv2.MORPH_HITMISS, kernel, anchor=(-1,-1))
  6.  
  7. import cv2
  8. import numpy as np
  9. from itertools import tee
  10.  
  11. def pairwise(iterable):
  12. a, b = tee(iterable)
  13. next(b, None)
  14. return zip(a, b)
  15.  
  16. # read image as grayscale
  17. img = cv2.imread('example.png', 0)
  18.  
  19. # get corner points, remove duplicate/nearby lines
  20. contours = cv2.findContours(img, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)[1]
  21. contour = contours[0]
  22. pts = np.array([contour[0]] + [pt1 for pt0, pt1 in pairwise(contour) if not (abs(pt0 - pt1) <= 1).all()])
  23. x, y = pts[:, -1, 0], pts[:, -1, 1]
  24.  
  25. # get the kernel that you will sum around your corner points
  26. kernel = np.float64(cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (13, 13)))
  27. kernel /= np.sum(kernel)
  28.  
  29. # convole the image with the kernel, and pull out the sums at the corner points
  30. conv = cv2.filter2D(img/255, cv2.CV_64F, kernel)
  31. neighborhood_sums = conv[y, x]
  32.  
  33. # concave indices have more white than black around them, so convolution will be >= 1/2
  34. concave_indices = neighborhood_sums >= 0.5
  35.  
  36. # draw markers
  37. marked = cv2.merge([img, img, img])
  38. for pt, concave in zip(pts, concave_indices):
  39. color = (255, 0, 255) if concave else (0, 255, 0)
  40. marker = cv2.MARKER_TRIANGLE_UP if concave else cv2.MARKER_TRIANGLE_DOWN
  41. cv2.drawMarker(marked, tuple(pt[0]), color, markerType=marker, markerSize=10, thickness=3)
Add Comment
Please, Sign In to add comment