The feature matching and findHomography from calib3d module to find known objects in a complex image
import numpy as np
import cv2 as cv
from matplotlib import pyplot as plt
from time import sleep
MIN_MATCH_COUNT = 10
img1 = cv.imread('arduino.jpg',0)
# Initiate SIFT detector
sift = cv.xfeatures2d.SIFT_create()
# find the keypoints and descriptors with SIFT
kp1, des1 = sift.detectAndCompute(img1,None)
cap = cv.VideoCapture(0)
while (True):
ret, frame = cap.read()
img2 = cv.cvtColor(frame, cv.COLOR_BGR2GRAY)
kp2, des2 = sift.detectAndCompute(img2, None)
FLANN_INDEX_KDTREE = 1
index_params = dict(algorithm=FLANN_INDEX_KDTREE, trees=5)
search_params = dict(checks=50)
flann = cv.FlannBasedMatcher(index_params, search_params)
matches = flann.knnMatch(des1, des2, k=2)
# store all the good matches as per Lowe's ratio test.
good = []
for m, n in matches:
if m.distance < 0.7 * n.distance:
good.append(m)
print('good', len(good))
print('kp1', len(kp1))
if len(good) > MIN_MATCH_COUNT:
src_pts = np.float32([kp1[m.queryIdx].pt for m in good]).reshape(-1, 1, 2)
dst_pts = np.float32([kp2[m.trainIdx].pt for m in good]).reshape(-1, 1, 2)
M, mask = cv.findHomography(src_pts, dst_pts, cv.RANSAC, 5.0)
matchesMask = mask.ravel().tolist()
h, w = img1.shape
pts = np.float32([[0, 0], [0, h - 1], [w - 1, h - 1], [w - 1, 0]]).reshape(-1, 1, 2)
dst = cv.perspectiveTransform(pts, M)
img2 = cv.polylines(frame, [np.int32(dst)], True, 255, 5, cv.LINE_AA)
cv.imshow('frame', img2)
else:
cv.imshow('frame', frame)
print("Not enough matches are found - {}/{}".format(len(good), MIN_MATCH_COUNT))
matchesMask = None
if cv.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv.destroyAllWindows()
import cv2 as cv
from matplotlib import pyplot as plt
from time import sleep
MIN_MATCH_COUNT = 10
img1 = cv.imread('arduino.jpg',0)
# Initiate SIFT detector
sift = cv.xfeatures2d.SIFT_create()
# find the keypoints and descriptors with SIFT
kp1, des1 = sift.detectAndCompute(img1,None)
cap = cv.VideoCapture(0)
while (True):
ret, frame = cap.read()
img2 = cv.cvtColor(frame, cv.COLOR_BGR2GRAY)
kp2, des2 = sift.detectAndCompute(img2, None)
FLANN_INDEX_KDTREE = 1
index_params = dict(algorithm=FLANN_INDEX_KDTREE, trees=5)
search_params = dict(checks=50)
flann = cv.FlannBasedMatcher(index_params, search_params)
matches = flann.knnMatch(des1, des2, k=2)
# store all the good matches as per Lowe's ratio test.
good = []
for m, n in matches:
if m.distance < 0.7 * n.distance:
good.append(m)
print('good', len(good))
print('kp1', len(kp1))
if len(good) > MIN_MATCH_COUNT:
src_pts = np.float32([kp1[m.queryIdx].pt for m in good]).reshape(-1, 1, 2)
dst_pts = np.float32([kp2[m.trainIdx].pt for m in good]).reshape(-1, 1, 2)
M, mask = cv.findHomography(src_pts, dst_pts, cv.RANSAC, 5.0)
matchesMask = mask.ravel().tolist()
h, w = img1.shape
pts = np.float32([[0, 0], [0, h - 1], [w - 1, h - 1], [w - 1, 0]]).reshape(-1, 1, 2)
dst = cv.perspectiveTransform(pts, M)
img2 = cv.polylines(frame, [np.int32(dst)], True, 255, 5, cv.LINE_AA)
cv.imshow('frame', img2)
else:
cv.imshow('frame', frame)
print("Not enough matches are found - {}/{}".format(len(good), MIN_MATCH_COUNT))
matchesMask = None
if cv.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv.destroyAllWindows()
Thao khảo tại Banggood:
Arduino UNO R3: http://bit.ly/2DiFCEB
Arduino Nano V3: http://bit.ly/37tvgiP
Arduino MEGA 2560 V3: http://bit.ly/2OQCOnu
------------------------------------------------------
Arduino UNO R3: http://bit.ly/2DiFCEB
Arduino Nano V3: http://bit.ly/37tvgiP
Arduino MEGA 2560 V3: http://bit.ly/2OQCOnu
------------------------------------------------------
Web: http://nguyenvankhoa.com
Facebook: http://www.facebook.com/NguyenVanKhoaCom
Đăng ký kênh youtube: http://goo.gl/rHDTKK
Rất mong được sự ủng hộ của quý vị
Xin trân trọng!
Không có nhận xét nào :
Đăng nhận xét