Skip to content

Instantly share code, notes, and snippets.

@payne911
Last active November 25, 2018 06:37
Show Gist options
  • Save payne911/37c218d163c33b37d9a2abd4023c9060 to your computer and use it in GitHub Desktop.
Save payne911/37c218d163c33b37d9a2abd4023c9060 to your computer and use it in GitHub Desktop.
Pattern matching
# defining/generating all the patterns
pattern1 = np.asarray([[-1, -1, -1, 1],
[ 1, 1, 1, 1]])
pattern2 = np.rot90(pattern1)
pattern3 = np.rot90(pattern2)
pattern4 = np.rot90(pattern3)
pattern5 = np.fliplr(pattern1)
pattern6 = np.rot90(pattern5)
pattern7 = np.rot90(pattern6)
pattern8 = np.rot90(pattern7)
plt.imshow(pattern1, cmap='gray')
plt.title("One of the patterns")
plt.show()
all_patterns = [pattern1, pattern2, pattern3, pattern4, pattern5, pattern6, pattern7, pattern8]
# using a fake image to see if the pattern extraction works
fake_img = [[0,0,0,0,0,0],
[0,1,1,1,1,0],
[0,0,0,0,1,0],
[0,0,0,0,0,0],
[0,0,0,0,0,0],
[0,0,0,0,0,0]]
test_img = np.array(fake_img).astype(np.uint8)
plt.imshow(test_img, cmap='gray')
plt.show()
# showing the results
tmp = 1
plt.figure(1, figsize=(15, 7))
plt.suptitle("Patterns found")
for pattern in all_patterns:
morph = cv.morphologyEx(test_img, cv.MORPH_HITMISS, pattern)
plt.subplot(2, 4, tmp)
plt.imshow(morph, cmap='gray')
tmp = tmp + 1
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment