Reputation: 111
I want to implement a Kalman filter to track a point , here is my code
public Mat onCameraFrame(CvCameraViewFrame inputFrame) {
mRgba = inputFrame.rgba();
mGray = inputFrame.gray();
switch (viewMode) {
case VIEW_MODE_ColorDetect:
/**
*
* some code for color detect
*
*/
int ObjectCenterX = (int)((mboundingRect.tl().x + mboundingRect.br().x) / 2);
int ObjectCenterY = (int)((mboundingRect.tl().y + mboundingRect.br().y) / 2);
//get the object center point , and I whant to track it by kalman filter.
Core.circle(mRgba, new Point(ObjectCenterX, ObjectCenterY), 5, ColorGreen, 2);
/***********************************************Kalman************************************************************/
try {
Mat ObjectPoint = new Mat(1, 2, CvType.CV_32F);
int[] PreObjectCenter = {ObjectCenterX, ObjectCenterY};
ObjectPoint.put(0, 0, PreObjectCenter[0]);
ObjectPoint.put(0, 1, PreObjectCenter[1]);
ObjectPoint = myKalmanFilter(ObjectPoint);
/**Question 2: How to get corrected point from Mat?**/
} catch (Exception e) {
Log.e("In camera fram: ", e.toString());
}
/*****************************************************************************************************************/
}
return mRgba;
return mRgba;
}
private Mat myKalmanFilter(Mat objectPoint){
KalmanFilter KF = new KalmanFilter();
try {
KF.predict(objectPoint);
KF.correct(objectPoint);
} catch (Exception e) {
Log.e("myKalmanFilter: ", e.toString());
}
return objectPoint;
}
I have 2 question , Q1. I got the following error for this code :
17:44:22.727: E/cv::error()(24902): OpenCV Error: Assertion failed (type == B.type() && (type == CV_32FC1 || type == CV_64FC1 || type == CV_32FC2 || type == CV_64FC2)) in void cv::gemm(cv::InputArray, cv::InputArray, double, cv::InputArray, double, cv::OutputArray, int), file /home/reports/ci/slave_desktop/50-SDK/opencv/modules/core/src/matmul.cpp, line 711 17:44:22.727: E/org.opencv.video(24902): video::predict_10() caught cv::Exception: /home/reports/ci/slave_desktop/50-SDK/opencv/modules/core/src/matmul.cpp:711: error: (-215) type == B.type() && (type == CV_32FC1 || type == CV_64FC1 || type == CV_32FC2 || type == CV_64FC2) in function void cv::gemm(cv::InputArray, cv::InputArray, double, cv::InputArray, double, cv::OutputArray, int) 17:44:22.727: E/myKalmanFilter:(24902): CvException [org.opencv.core.CvException: cv::Exception: /home/reports/ci/slave_desktop/50-SDK/opencv/modules/core/src/matmul.cpp:711: error: (-215) type == B.type() && (type == CV_32FC1 || type == CV_64FC1 || type == CV_32FC2 || type == CV_64FC2) in function void cv::gemm(cv::InputArray, cv::InputArray, double, cv::InputArray, double, cv::OutputArray, int) 17:44:22.727: E/myKalmanFilter:(24902): ]
Q2. After revise the code that it can work , how to get the x and y point from Mat?
EDIT:
I still don't know how to set KalmanFilter dimensions , I set like this private KalmanFilter KF = new KalmanFilter(1, 2, 16, CvType.CV_32F);
and eclipse show
Thread [<1> main] (Suspended (exception UnsatisfiedLinkError))
KalmanFilter.(int, int, int, int) line: 72
Tutorial1Activity.() line: 65 Class.newInstanceImpl() line: not available [native method] Class.newInstance() line: 1319
Instrumentation.newActivity(ClassLoader, String, Intent) line: 1069 ActivityThread.performLaunchActivity(ActivityThread$ActivityClientRecord, Intent) line: 2258
ActivityThread.handleLaunchActivity(ActivityThread$ActivityClientRecord, Intent) line: 2387 ActivityThread.access$600(ActivityThread, ActivityThread$ActivityClientRecord, Intent) line: 151
ActivityThread$H.handleMessage(Message) line: 1331
ActivityThread$H(Handler).dispatchMessage(Message) line: 99 Looper.loop() line: 155 ActivityThread.main(String[]) line: 5485
Method.invokeNative(Object, Object[], Class, Class[], Class, int, boolean) line: not available [native method]
Method.invoke(Object, Object...) line: 511
ZygoteInit$MethodAndArgsCaller.run() line: 1028 ZygoteInit.main(String[]) line: 795 NativeStart.main(String[]) line: not available [native method]
Upvotes: 1
Views: 1857
Reputation: 3047
good example
Kalman Filter Implementation (Tracking mouse position)
http://opencvexamples.blogspot.com/2014/01/kalman-filter-implementation-tracking.html
Upvotes: -1
Reputation: 39796
please don't create a new KalmanFilter for each prediction/correction , it has to keep state for the whole duration of your program to work.
you need to create the KalmanFilter with correct dimensions for measurement size an dynparamsize, calling the empty constructor will give the above error.
KF.predict(objectPoint) will overwrite the contents in objectPoint, so you're correcting with the predicted value, not with your measurement. either use 2 different Mats there, or reverse the predict/correct order
unfortunately, the opencv java wrapper don't allow you to set statePre or transition Mats for the Kalman filter. it's current interface is a bit unusable. you'll either need jni to set them, or use the javacv bindings ( which are shabby outdated, but at least handle this part better )
Upvotes: 2