[PYTHON] OpenCV feature detection with Google Colaboratory

Some OpenCV tutorials have old specifications, so I will post the code for those who want to move it for the time being.

import numpy as np
import cv2
from google.colab.patches import cv2_imshow

img1 = cv2.imread('box.png', 0)
img2 = cv2.imread('box_in_scene.png', 0)
akaze = cv2.AKAZE_create()

kp1, des1 = akaze.detectAndCompute(img1, None)
kp2, des2 = akaze.detectAndCompute(img2, None)
# create BFMatcher object
bf = cv2.BFMatcher(cv2.NORM_HAMMING, crossCheck=False)
#bf = cv2.BFMatcher()
matches = bf.knnMatch(des1, des2, k=2)

# Need to draw only good matches, so create a mask
matchesMask = [[0,0] for i in range(len(matches))]

# Apply ratio test
good = []
good2 = []
for m,n in matches:
    if m.distance < 0.75*n.distance:
        good.append([m])
        good2.append(m)
# cv2.drawMatchesKnn expects list of lists as matches.
img3 = cv2.drawMatchesKnn(img1,kp1,img2,kp2,good,None,flags=2)

cv2_imshow(img3)

MIN_MATCH_COUNT  =10
if len(good)>MIN_MATCH_COUNT:
    src_pts = np.float32([ kp1[m.queryIdx].pt for m in good2 ]).reshape(-1,1,2)
    dst_pts = np.float32([ kp2[m.trainIdx].pt for m in good2 ]).reshape(-1,1,2)

    M, mask = cv2.findHomography(src_pts, dst_pts, cv2.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 = cv2.perspectiveTransform(pts,M)

    img3 = cv2.polylines(img2,[np.int32(dst)],True,255,3, cv2.LINE_AA)
    cv2_imshow(img3)
    img4 = cv2.drawMatchesKnn(img1,kp1,img2,kp2,good,None,flags=2)
    cv2_imshow(img4)

Recommended Posts

OpenCV feature detection with Google Colaboratory
Try OpenCV with Google Colaboratory
Try edge detection with OpenCV
Feature detection using opencv (corner detection)
Study Python with Google Colaboratory
Real-time edge detection with OpenCV
Face detection with Python + OpenCV
Anime face detection with OpenCV
Google colaboratory
[Python] Using OpenCV with Python (Edge Detection)
Cat detection with OpenCV (model distribution)
Face detection with Python + OpenCV (rotation invariant)
How to search Google Drive with Google Colaboratory
Make a cascade classifier with google colaboratory
Manage deals with Trello + Google Colaboratory (Part 1)
Using Java's Jupyter Kernel with Google Colaboratory
Use TPU and Keras with Google Colaboratory
Hello World and face detection with OpenCV 4.3 + Python
Resize, mosaic, face detection with OpenCV, sometimes Zojirushi
Feature matching with OpenCV 3 and Python 3 (A-KAZE, KNN)
I tried simple image processing with Google Colaboratory.
Cheat sheet when scraping with Google Colaboratory (Colab)
Feature engineering for machine learning starting with the 4th Google Colaboratory --Interaction features
Authenticate Google with Django
Google Colaboratory setup summary
Detect stoop with OpenCV
Binarization with OpenCV / Python
Is it Google Colaboratory?
How to load files in Google Drive with Google Colaboratory
End-to-End single channel sound source separation with Google Colaboratory
Rotate sprites with OpenCV
How to analyze with Google Colaboratory using Kaggle API
Data Augmentation with openCV
Easy learning of 100 language processing knock 2020 with "Google Colaboratory"
Easy TopView with OpenCV
Building an environment to use CaboCha with google colaboratory
Stumble with homebrew opencv3
Feature Engineering for Machine Learning Beginning with Part 3 Google Colaboratory-Scaling
Try to implement linear regression using Pytorch with Google Colaboratory
Face recognition with Python's OpenCV
Test embedded software with Google Test
"Apple processing" with OpenCV3 + Python3
Try Google Mock with C
Try StyleGAN on Google Colaboratory
Image editing with python OpenCV
Camera capture with Python + OpenCV
[Python] Using OpenCV with Python (Basic)
About learning with google colab
Face detection with Python + dlib
Binarize photo data with OpenCV
Loop video loading with opencv
Get image features with OpenCV
Face recognition / cutting with OpenCV
Mount google drive with google-drive-ocamlfuse
Blockchain tampering detection with Python
Access Google Drive with Python
Face detection with Haar Cascades
Translate PHP_UML --help with google translate
Cascade classifier creation with opencv
Tool organization: Google Colaboratory (updated 2020.2.24)
Pandas 100 knocks on Google Colaboratory