Я написал код, который отслеживает указатель мыши, используя фильтр Калмана. Но он дает некоторые ошибки. Пожалуйста, помогите мне.
enter code here enter code here#include "cv.h"#include "highgui.h"#include <iostream>
#include <vector>
using namespace std;
CvPoint point=cvPoint(-1,-1);
CvPoint last_point;
vector<CvPoint> mousev,kalmanv;
void my_mouse_callback(int event, int x, int y, int flags, void* param)
{
CV_EVENT_LBUTTONDBLCLK;
last_point=point;
point=cvPoint(x,y);
}
IplImage *img;
int main()
{
// Initialize Kalman filter object, window, number generator, etc
cvNamedWindow( "Kalman", 1 );
IplImage* img = cvCreateImage( cvSize(500,500), 8, 3 );
CvKalman* kalman = cvCreateKalman( 4, 2, 0 );
CvMat* state = cvCreateMat( 4, 1, CV_32FC1 );
CvMat* processnoise=cvCreateMat(4,1,CV_32FC1);
CvMat* measurement =cvCreateMat(2,1,CV_8SC1);
char code = (char)-1;
cvSetMouseCallback("kalman",my_mouse_callback,NULL);
while(1)
{
if(point.x<0 || point.y<0)
{
cvShowImage("kalman",img);
cvWaitKey(30);
continue;
}
const float F1[] = {point.x,point.y,0,0};
memcpy(kalman->state_pre->data.fl,F1,sizeof(F1));
float vals[] = {1,0,0,0,0,1,0,0,0,0,1,0,0,0,0,1};
CvMat tran;
cvInitMatHeader(&tran,4,4,CV_32FC1,vals);
memcpy(kalman->transition_matrix->data.fl,tran,sizeof(tran));
cvSetIdentity( kalman->measurement_matrix);
cvSetIdentity( kalman->process_noise_cov, cvRealScalar(1e-5) );
cvSetIdentity( kalman->measurement_noise_cov, cvRealScalar(1e-1) );
cvSetIdentity( kalman->error_cov_post, cvRealScalar(1) );
mousev.clear();
kalmanv.clear();
while(1)
{
const CvMat* prediction = cvKalmanPredict(kalman,0);
CvPoint predictionPt = cvPoint(prediction[0],prediction[0]);
measurement[0]= point.x;
measurement[1] = point.y;
CvPoint measPt = cvPoint(measurement[0],measurement[1]);
mousev.push_back(measPt);
const CvMat* estimated = cvKalmanCorrect( kalman, measurement);
CvPoint statePt=cvPoint(estimated[0],estimated[1]);
kalmanv.push_back(statePt);
cvLine( img, cvPoint(statePt.x - 5, statePt.y - 5 ),cvPoint( statePt.x + 5, statePt.y + 5 ), cvScalar(255,255,255), 2,8);
cvLine( img, cvPoint(statePt.x + 5, statePt.y - 5 ),cvPoint( statePt.x - 5, statePt.y + 5 ), cvScalar(255,255,255), 2,8);
cvLine( img, cvPoint(measPt.x - 5, measPt.y - 5 ),cvPoint( measPt.x + 5, measPt.y + 5 ), cvScalar(255,255,255), 2,8);
cvLine( img, cvPoint(measPt.x + 5, measPt.y - 5 ),cvPoint( measPt.x - 5, measPt.y + 5 ), cvScalar(255,255,255), 2,8);
for (int i = 0; i < mousev.size()-1; i++)
{
cvLine(img, mousev[i], mousev[i+1], cvScalar(255,255,0), 1);
}
for (int i = 0; i < kalmanv.size()-1; i++)
{
cvLine(img, kalmanv[i], kalmanv[i+1], cvScalar(0,255,0), 1);
}
cvShowImage( "mouse kalman", img );
code = (char)cvWaitKey(100);
if( code > 0 )
break;
}
if( code == 27 || code == 'q' || code == 'Q' )
break;
}
return 0;
}
Ошибки:
Error 3 error C2664: 'memcpy' : cannot convert parameter 2 from 'CvMat' to 'const void *' c:\users\anay\documents\visual studio 2008\projects\mission_impossible_1\mission_impossible_1\kalman_filter.cpp 56 mission_impossible_1
Error 4 error C2664: 'cvPoint' : cannot convert parameter 1 from 'const CvMat' to 'int' c:\users\anay\documents\visual studio 2008\projects\mission_impossible_1\mission_impossible_1\kalman_filter.cpp 70 mission_impossible_1
Error 5 error C2679: binary '=' : no operator found which takes a right-hand operand of type 'int' (or there is no acceptable conversion) c:\users\anay\documents\visual studio 2008\projects\mission_impossible_1\mission_impossible_1\kalman_filter.cpp 72 mission_impossible_1
Error 6 error C2679: binary '=' : no operator found which takes a right-hand operand of type 'int' (or there is no acceptable conversion) c:\users\anay\documents\visual studio 2008\projects\mission_impossible_1\mission_impossible_1\kalman_filter.cpp 73 mission_impossible_1
Error 7 error C2664: 'cvPoint' : cannot convert parameter 1 from 'CvMat' to 'int' c:\users\anay\documents\visual studio 2008\projects\mission_impossible_1\mission_impossible_1\kalman_filter.cpp 75 mission_impossible_1
Error 8 error C2664: 'cvPoint' : cannot convert parameter 1 from 'const CvMat' to 'int' c:\users\anay\documents\visual studio 2008\projects\mission_impossible_1\mission_impossible_1\kalman_filter.cpp 81 mission_impossible_1
Ошибка 3 ошибка C2664: «memcpy»: невозможно преобразовать параметр 2 из
‘CvMat’ в ‘const void *’ c: \ users \ anay \ documents \ visual studio
2008 \ Projects \ mission_impossible_1 \ mission_impossible_1 \ kalman_filter.cpp
56 mission_impossible_1
memcpy(kalman->transition_matrix->data.fl,tran,sizeof(tran));
в C ++ (в отличие от C) вы должны явно указывать указатель на void.
в настоящее время у вас есть:
CvMat tran;
memcpy(kalman->transition_matrix->data.fl,tran,sizeof(tran));
вместо этого, пожалуйста, попробуйте это:
CvMat* tran=new CvMat;
memcpy(static_cast<void*>(kalman->transition_matrix->data.fl),static_cast<void*>(tran),sizeof(tran));
следующий:
Ошибка 4 ошибка C2664: «cvPoint»: невозможно преобразовать параметр 1 из
от const CvMat до int
Я действительно хочу помочь, но, пожалуйста, приложите код вашего класса Point
Других решений пока нет …