Ask Your Question
0

A bug causes crash when class inherit from cv2.KeyPoint

asked 2019-07-15 02:39:12 -0600

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!

edit retag flag offensive close merge delete

Comments

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 gravatar imageberak ( 2019-07-15 03:48:36 -0600 )edit

@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)) with cv2.KeyPoint(x=coor[0], y=coor[1], _size=0). At first, I just want to use the drawKeypoints 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.

0811张庆昊 gravatar image0811张庆昊 ( 2019-07-15 21:26:15 -0600 )edit

I replaced T(tuple(coor)) with cv2.KeyPoint(x=coor[0],...

yea, you found the solution already, good ! ;)

so, you only need keypoints, to use the drawKeyPoints() function ?

berak gravatar imageberak ( 2019-07-16 00:23:30 -0600 )edit

@berak Yes! Do you have any better or more elegant solution?

0811张庆昊 gravatar image0811张庆昊 ( 2019-07-16 02:10:35 -0600 )edit

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..

berak gravatar imageberak ( 2019-07-16 02:17:39 -0600 )edit

1 answer

Sort by » oldest newest most voted
0

answered 2019-07-16 05:50:28 -0600

@berak Yes, of course I can implement it in this way. In fact, I have implemented it in other project. But I think that use it in different projects is a little cumbersome. So I want use the API in cv2 straightly. Exactly, I use gftt for LK-tracking to implement a video stablization algorithm, so gftt is fit for me. Anyway, thanks for your reply! ^_^

edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2019-07-15 02:39:12 -0600

Seen: 441 times

Last updated: Jul 15 '19