Created
March 12, 2018 04:18
-
-
Save macrat/02ac167f5fcad3a8e1646724c40d76f8 to your computer and use it in GitHub Desktop.
Python/OpenCVで特徴点マッチングして、DBSCANでクラスタリングしたらサイコロの目の合計とか出せないかと思ったやつ。失敗。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import cv2 | |
import numpy | |
import matplotlib.pyplot as plt | |
from sklearn.cluster import DBSCAN | |
images = [cv2.imread('dataset/samples/' + x) for x in os.listdir('dataset/samples') if x.endswith('.jpg')] # サイコロの特定の面の教師画像 | |
original = cv2.imread('dataset/random/0.jpg') # ランダムにサイコロを転がした画像 | |
for i, img in enumerate(images): | |
cv2.imshow(str(i), img) | |
cv2.waitKey(1) | |
cv2.waitKey(1) | |
akaze = cv2.AKAZE_create() | |
poses = [] | |
kps = [] | |
for img in images: | |
kp1, des1 = akaze.detectAndCompute(original, None) | |
kp2, des2 = akaze.detectAndCompute(img, None) | |
if des1 is not None and des2 is not None: | |
bf = cv2.BFMatcher() | |
matches = bf.knnMatch(des1, des2, k=2) | |
for m, n in matches: | |
if m.distance < n.distance * 0.9: | |
poses.append(kp1[m.queryIdx].pt) | |
kps.append(kp1[m.queryIdx]) | |
# 確認用に描画 | |
kps_image = cv2.drawKeypoints(original, kps, None, flags=cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS) | |
cv2.imshow('original', original) | |
cv2.imshow('kps_image', kps_image) | |
# マッチした特徴点をクラスタリングする | |
db = DBSCAN(eps=50, min_samples=20).fit(poses) | |
# クラスタリングの結果をプロット | |
poses = numpy.array(poses) | |
plt.scatter(poses[:,0], poses[:,1], c=db.labels_) | |
ax = plt.gca() | |
ax.set_ylim(ax.get_ylim()[::-1]) | |
plt.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment