Object tracking using Homography – OpenCV 3.4 with python 3 Tutorial 34
Source code:
import cv2 import numpy as np img = cv2.imread("ultimo_sopravvissuto.jpg", cv2.IMREAD_GRAYSCALE) # queryiamge cap = cv2.VideoCapture(0) # Features sift = cv2.xfeatures2d.SIFT_create() kp_image, desc_image = sift.detectAndCompute(img, None) # Feature matching index_params = dict(algorithm=0, trees=5) search_params = dict() flann = cv2.FlannBasedMatcher(index_params, search_params) while True: _, frame = cap.read() grayframe = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) # trainimage kp_grayframe, desc_grayframe = sift.detectAndCompute(grayframe, None) matches = flann.knnMatch(desc_image, desc_grayframe, k=2) good_points = [] for m, n in matches: if m.distance < 0.6*n.distance: good_points.append(m) #img3 = cv2.drawMatches(img, kp_image, grayframe, kp_grayframe, good_points, grayframe) # Homography if len(good_points) > 10: query_pts = np.float32([kp_image[m.queryIdx].pt for m in good_points]).reshape(-1, 1, 2) train_pts = np.float32([kp_grayframe[m.trainIdx].pt for m in good_points]).reshape(-1, 1, 2) matrix, mask = cv2.findHomography(query_pts, train_pts, cv2.RANSAC, 5.0) matches_mask = mask.ravel().tolist() # Perspective transform h, w = img.shape pts = np.float32([[0, 0], [0, h], [w, h], [w, 0]]).reshape(-1, 1, 2) dst = cv2.perspectiveTransform(pts, matrix) homography = cv2.polylines(frame, [np.int32(dst)], True, (255, 0, 0), 3) cv2.imshow("Homography", homography) else: cv2.imshow("Homography", grayframe) #cv2.imshow("Image", img) #cv2.imshow("grayFrame", grayframe) #cv2.imshow("img3", img3) key = cv2.waitKey(1) if key == 27: break cap.release() cv2.destroyAllWindows()
Files:
Related Posts
2 Comments
Leave a Reply Cancel reply
This site uses Akismet to reduce spam. Learn how your comment data is processed.
Recent Posts
- Detecting colors (Hsv Color Space) – Opencv with Python
- Write using your Eyes – Gaze controlled keyboard with Python and Opencv p.10
- Play sounds – Gaze controlled keyboard with Python and Opencv p.9
- Press a key by blinking eyes – Gaze controlled keyboard with Python and Opencv p.8
- Virtual Keyboard 3 – Gaze controlled keyboard with Python and Opencv p.7
thanks a loooooooot
Hello~ Sir,
I cannot find “cv2.drawKeypoints()”.
I has install opencv-python_3.4.4.19 in PyCharm Community 2018.3
What should i do for this problem,
Thanks for your reply.
BTW,
Thanks a lot for your tutorial