A bug causes crash when class inherit from cv2.KeyPoint
Look this code:
import numpy as np
import cv2
class T(cv2.KeyPoint):
def __init__(self, pt):
super().__init__()
self.pt = pt
def calculate_corners(A):
A_gray = cv2.cvtColor(A, cv2.COLOR_BGR2GRAY)
pa = cv2.goodFeaturesToTrack(A_gray, maxCorners=100, qualityLevel=0.01, minDistance=15)
pa = np.squeeze(pa)
kpa = []
for coor in pa:
kpa.append(T(tuple(coor)))
return kpa
cap = cv2.VideoCapture("E:\\video\\test.mp4")
while True:
frame = cap.read()[1]
if frame is None:
break
kpa = calculate_corners(frame)
frame_corner = cv2.drawKeypoints(frame, kpa, outImage=None, color=(255, 0, 125))
cv2.imshow('frame_corner', frame_corner)
cv2.waitKey(1)
cv2.destroyAllWindows()
cap.release()
This code will crash in my system(Windows10, python3.7.3, opencv4.1.0)
After test, I'm sure this is caused by class T. I guess that class T dose not inherit release moudle of cv2.KeyPoint, so it cause memory leak. It's just my conjecture. And I didn't know how to fix it. Could anyone give me some advice? Thanks a lot!
error msg is missing, please add that.
please also try to explain, WHY you wanted to do it like this, and why you think you need OOP / inheritance here (which is mostly a terrible design idea)
please also remember, that opencv is mainly a c++ library, not a pure python one.
@berak Yes, I recognize it's a terrible design idea. I would like to post the error message if I got it. But the program crash without any error message. Finally I replaced
T(tuple(coor))
withcv2.KeyPoint(x=coor[0], y=coor[1], _size=0)
. At first, I just want to use thedrawKeypoints
function, but I don't how to transform points from matrix into KeyPoints, so I thought out this terrible idea. I'm sorry about that.yea, you found the solution already, good ! ;)
so, you only need keypoints, to use the drawKeyPoints() function ?
@berak Yes! Do you have any better or more elegant solution?
gftt returns a list of points, you could just iterate it and draw a circle() per point on your own (homework, hehe ;)
then, again donn't worry too much about it now. gfft points are usually more used for (LK-) tracking, not so much for keypoint/decscriptor/matching, you'll probably use ORB, AKAZE, SURF or similar for this purpose instead later..