问题
I am working on object tracking by camshift algorithm. For the time being I am using the inbuilt opencv code wherein I have trouble dealing with occlusion.
hsv = cv2.cvtColor(self.frame, cv2.COLOR_BGR2HSV)
mask = cv2.inRange(hsv, np.array((0., 60., 32.)), np.array((180., 255., 255.)))
prob = cv2.calcBackProject([hsv], [0], self.hist, [0, 180], 1)
cv2.imshow('prob_0',prob)
prob &= mask
cv2.imshow('prob',prob)
term_crit = ( cv2.TERM_CRITERIA_EPS | cv2.TERM_CRITERIA_COUNT, 10, 1 )
track_box, self.track_window = cv2.CamShift(prob, self.track_window, term_crit)
My problem is that in this code when my object which is a red ball goes out of the field of vision of the camera or if I cover some portion of the ball with my hand, then it crashes and give the error that:
track_box, self.track_window = cv2.CamShift(prob, self.track_window, term_crit)
error: ..\..\..\..\opencv\modules\video\src\camshift.cpp:80: error: (-5) Input
window has non-positive sizes in function cvMeanShift
This is because my parameter to cv2.Camshift -> which is "prob" is not having any values corresponding to my ball (prob is the binary image obtained which consists of the thresholded ball)
I have one idea for dealing with occlusion in this scenario. It's that I'll store the ball matrix in a global variable and if my camera's current frame cannot obtain the ball matrix then it should use the global variable instead of it till it doesn't find and track the ball. So how to apply this logic in the given code?
So can anyone help me how to deal with the occlusion in this ball situation.
回答1:
In OpenCV, I faced the same problem of the program getting stuck when there was no object to track. Later I solved it.
To SOLVE IT:
1) First calculate meanShift, which returns the number of iterations it took to converge. 2) if (iteration_meanShift != 0), then calculate the CamShift and return bounding_box+frame. Else, return only the frame.
i.e. if and only if meanshift is non-zero, calculate the camshift, else don't calculate the camshift.
回答2:
Just use a try catch block or a simple if statement. Execute the CamShift statement only if the variable prob
has a valid value
if not prob:
//do nothing or print an error statement since ball is occluded
else:
track_box, self.track_window = cv2.CamShift(prob, self.track_window, term_crit)
回答3:
Be sure self.track_window is not None. thanks
来源:https://stackoverflow.com/questions/25043761/occlusion-with-camshift