Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- [[-1 -1 1]
- [-1 -1 1]
- [ 1 1 1]]
- mask = cv2.morphologyEx(threshed, cv2.MORPH_HITMISS, kernel, anchor=(-1,-1))
- import cv2
- import numpy as np
- from itertools import tee
- def pairwise(iterable):
- a, b = tee(iterable)
- next(b, None)
- return zip(a, b)
- # read image as grayscale
- img = cv2.imread('example.png', 0)
- # get corner points, remove duplicate/nearby lines
- contours = cv2.findContours(img, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)[1]
- contour = contours[0]
- pts = np.array([contour[0]] + [pt1 for pt0, pt1 in pairwise(contour) if not (abs(pt0 - pt1) <= 1).all()])
- x, y = pts[:, -1, 0], pts[:, -1, 1]
- # get the kernel that you will sum around your corner points
- kernel = np.float64(cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (13, 13)))
- kernel /= np.sum(kernel)
- # convole the image with the kernel, and pull out the sums at the corner points
- conv = cv2.filter2D(img/255, cv2.CV_64F, kernel)
- neighborhood_sums = conv[y, x]
- # concave indices have more white than black around them, so convolution will be >= 1/2
- concave_indices = neighborhood_sums >= 0.5
- # draw markers
- marked = cv2.merge([img, img, img])
- for pt, concave in zip(pts, concave_indices):
- color = (255, 0, 255) if concave else (0, 255, 0)
- marker = cv2.MARKER_TRIANGLE_UP if concave else cv2.MARKER_TRIANGLE_DOWN
- cv2.drawMarker(marked, tuple(pt[0]), color, markerType=marker, markerSize=10, thickness=3)
Add Comment
Please, Sign In to add comment