I want to make a simple project about logo detecting. So I tried to follow OpenCV-Python tutorial about feature detection. OpenCV: Feature Matching
I wrote my code like below.
ratio = 0.8
logo = cv.imread("T01/CocaCola_logo2.png", cv.IMREAD_GRAYSCALE)
img = cv.imread("T01/CocaCola.png", cv.IMREAD_GRAYSCALE)
orb = cv.ORB_create()
kp_logo, des_logo = orb.detectAndCompute(logo, None)
kp_img, des_img = orb.detectAndCompute(img, None)
FLANN_INDEX_LSH = 6
index_params = dict(algorithm=FLANN_INDEX_LSH,
table_number=6,
key_size=12,
multi_probe_level=1)
search_params = dict(checks=50)
flann = cv.FlannBasedMatcher(index_params, search_params)
match_flann = flann.knnMatch(des_logo, des_img, k=2)
good = []
for p, q in match_flann:
if p.distance > q.distance*ratio:
good.append(p)
try:
img_match = np.empty((max(logo.shape[0], img.shape[0]), logo.shape[1] + img.shape[1], 3), dtype=np.uint8)
cv.drawMatchesKnn(logo, kp_logo, img, kp_img, good,
outImg=img_match, matchColor=None, singlePointColor=(255, 255, 255), flags=2)
cv.imshow("flann matching", img_match)
cv.waitKey(0)
except:
print("...")
This code does not work properly. Process always got handled by error with drawMatchesKnn(). Before I add try-except to that function, process made a system error : SystemError: returned NULL without setting an error
What is the cause of the problem? I tried to search for a long time but it's not easy to me.
stack trace : Traceback (most recent call last): File "C:/Users/choib/Desktop/openCVtest3/T01_ORBtest.py", line 73, in cv.drawMatchesKnn(logo, kp_logo, img, kp_img, good, outImg=img_match, matchColor=None, singlePointColor=(255, 255, 255), flags=2) SystemError: returned NULL without setting an error
I misunderstood about drawMatches() and drawMatchesKnn(). reference
drawMatchesKnn() needs to get vector which is composed by vectors of DMatch as matches1to2. So in my case I fixed the code like below.
case 1: to use properly
img_match = np.empty((max(logo.shape[0], img.shape[0]), logo.shape[1] + img.shape[1], 3), dtype=np.uint8)
cv.drawMatches(logo, kp_logo, img, kp_img, good_flann,
outImg=img_match, matchColor=None, singlePointColor=(255, 255, 255), flags=2)
cv.imshow("flann matching", img_match)
cv.waitKey(0)
case 2: to use drawMatchesKnn()
img_match = np.empty((max(logo.shape[0], img.shape[0]), logo.shape[1] + img.shape[1], 3), dtype=np.uint8)
cv.drawMatchesKnn(logo, kp_logo, img, kp_img, match_flann,
outImg=img_match, matchColor=None, singlePointColor=(255, 255, 255), flags=2)
cv.imshow("flann matching", img_match)
cv.waitKey(0)
In those codes, good_flann is just vector of DMatch. And match_flann is vector of vector. So process doesn't have any problem with those parts.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With