ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question

Kalman filter of opencv doesnt work in ROS

asked 2015-07-07 16:56:35 -0500

erivera1802 gravatar image

updated 2015-07-12 05:37:03 -0500

gvdhoorn gravatar image

Hi, im trying to adapt a Kalman filter cpp code to a node cpp in ROS, but in the very beginning, the initialization goes wrong, more precisly, when i try to set the matrices, this is the relevant part of the code.

int stateSize = 4;
int measSize = 2;
int contrSize = 0;
unsigned int type = CV_32F;
cv::KalmanFilter kf(stateSize, measSize, contrSize, type);
cv::Mat state(stateSize, 1, type);  // [x,y,v_x,v_y,w,h]
cv::Mat meas(measSize, 1, type);

The compiler shows me the next error:

error: expected constructor, destructor, or type conversion before ‘(’ token

I really have no idea what is happening, because those lines show no problem in the "pure" cpp code. Hope someone can help me, thanks.


#include <ros/ros.h>
#include <image_transport/image_transport.h>
#include <sensor_msgs/image_encodings.h>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <cv_bridge/cv_bridge.h>
#include <geometry_msgs/Twist.h>
#include <std_msgs/Empty.h>
#include <std_msgs/String.h>
#include <sstream>
#include <image_transport/image_transport.h>
#include <cv_bridge/cv_bridge.h>
#include <stdio.h>
#include <iostream>
#include <cmath>
#include "opencv2/core/core.hpp"
#include "opencv2/features2d/features2d.hpp"
#include "opencv2/nonfree/features2d.hpp"
#include "opencv2/nonfree/nonfree.hpp"
#include <ros/package.h>
#include "opencv2/calib3d/calib3d.hpp"
#include <geometry_msgs/Point.h>
#include <fstream>
#include <opencv2/video/video.hpp>
#include "opencv2/video/tracking.hpp"
edit retag flag offensive close merge delete

1 Answer

Sort by » oldest newest most voted

answered 2015-07-07 19:25:12 -0500

Tom Moore gravatar image

Where is setIdentity defined? It looks like the compiler can't find it. Can you do something like this instead?

kf.measurementMatrix = Mat::eye(kf.measurementMatrix.rows, kf.measurementMatrix.cols, CV_64F);
edit flag offensive delete link more


I tried your solution, but it shows me the next error:

error: ‘kf’ does not name a type

kf.measurementMatrix = Mat::eye(kf.measurementMatrix.rows, kf.measurementMatrix.cols, CV_64F); ^ setIdentity is defined in opencv operations.

erivera1802 gravatar image erivera1802  ( 2015-07-08 16:16:58 -0500 )edit

Sorry, I didn't mean that you should only use that line of code. Just replace the line where you were calling setIdentity with the line I gave you. You declared kf in the line just before it.

Tom Moore gravatar image Tom Moore  ( 2015-07-08 16:40:15 -0500 )edit

Yeah thats what I have done. I didnt delete the KalmanFilter kf linea, but it showed me that

erivera1802 gravatar image erivera1802  ( 2015-07-08 18:19:03 -0500 )edit

What do your includes look like?

Tom Moore gravatar image Tom Moore  ( 2015-07-08 18:49:56 -0500 )edit

The include is in the question, dont know why the format crushed

erivera1802 gravatar image erivera1802  ( 2015-07-12 00:05:18 -0500 )edit

dont know why the format crushed

Because you didn't use the Preformatted text toolbar button to format it properly (it's the ony with 101010 on it).

gvdhoorn gravatar image gvdhoorn  ( 2015-07-12 05:38:50 -0500 )edit

Question Tools

1 follower


Asked: 2015-07-07 16:56:35 -0500

Seen: 713 times

Last updated: Jul 12 '15