'OpenCV Homography gives error: The input arrays should have at least
Following this https://www.youtube.com/watch?v=I8tHLZDDHr4&list=WL&index=1&ab_channel=Pysource tutorial on YouTube for my own project, answers I found basically had the exact same code, but my code gives me this error:
Traceback (most recent call last): File "c:\Users\NoName69\Desktop\ProJects\PyJects\Homography\test.py", line 31, in <module> matrix, mask = cv2.findHomography(query_p, train_p, cv2.RANSAC, 5.0) cv2.error: OpenCV(4.5.1) C:\Users\appveyor\AppData\Local\Temp\1\pip-req-build 5rb_9df3\opencv\modules\calib3d\src\fundam.cpp:385: error: (-28:Unknown error code -28) The input arrays should have at least 4 corresponding point sets to calculate Homography in function "cv::findHomography"
Code:
from cv2 import cv2
import numpy as np
img1 = cv2.cvtColor(cv2.imread("Aim Circle.png"), cv2.COLOR_RGB2GRAY)
img2 = cv2.cvtColor(cv2.imread("Map.png"), cv2.COLOR_RGB2GRAY)
sift = cv2.xfeatures2d.SIFT_create()
kp_img1, desc_img1 = sift.detectAndCompute(img1, None)
kp_img2, desc_img2 = sift.detectAndCompute(img2, None)
img1 = cv2.drawKeypoints(img1, kp_img1, img1)
img2 = cv2.drawKeypoints(img2, kp_img2, img2)
index_params = dict(algorithm = 0, trees = 5)
search_params = dict()
flann = cv2.FlannBasedMatcher(index_params, search_params)
while True:
matches = flann.knnMatch(desc_img1, desc_img2, k = 2)
good_kp = []
for m, n in matches:
if m.distance < 0.75 * n.distance:
good_kp.append(m)
if len(good_kp) > 2:
query_p = np.float32([kp_img1[m.queryIdx].pt for m in good_kp]).reshape(-1, 1 ,2)
train_p = np.float32([kp_img2[m.trainIdx].pt for m in good_kp]).reshape(-1, 1, 2)
matrix, mask = cv2.findHomography(query_p, train_p, cv2.RANSAC, 5.0)
if cv2.waitKey(25) & 0xFF == ord(" "):
cv2.destroyAllWindows()
break
OpenCv also gives this warning:
[ WARN:0] global c:\users\appveyor\appdata\local\temp\1\pip-req-build-5rb_9df3\opencv_contrib\modules\xfeatures2d\misc\python\shadow_sift.hpp (15) cv::xfeatures2d::SIFT_create DEPRECATED: cv.xfeatures2d.SIFT_create() is deprecated due SIFT tranfer to the main repository. https://github.com/opencv/opencv/issues/16736
Solution 1:[1]
I have found out the problem. OpenCV cannot find enough features in the file "Aim Circle.png", and so gives this error. If anyone knows another way to find a picture that is similar embedded in another picture it would be greatly appreciated.
Solution 2:[2]
i think in
if len(good_kp) > 2:
you have to substiture the value of 2 with a value greater than 4:
example: if len(good_kp) > 10:
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | NoName69 |
| Solution 2 | chiara ricci |
