1

我写了一个使用卡尔曼滤波器跟踪鼠标指针的代码。但它给出了一些错误。请帮帮我。

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
4

1 回答 1

0

错误 3 错误 C2664: 'memcpy' : 无法将参数 2 从 'CvMat' 转换为 'const void *' c:\users\anay\documents\visual studio 2008\projects\mission_impossible_1\mission_impossible_1\kalman_filter.cpp 56mission_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”

我真的很想帮忙,但请附上你的班级代码

于 2013-03-20T22:56:23.487 回答