44 #include <ar_track_alvar_msgs/AlvarMarker.h> 45 #include <ar_track_alvar_msgs/AlvarMarkers.h> 49 #include <Eigen/StdVector> 51 using namespace alvar;
55 #define VISIBLE_MARKER 2 56 #define GHOST_MARKER 3 86 void getCapCallback (
const sensor_msgs::ImageConstPtr & image_msg);
88 void makeMarkerMsgs(
int type,
int id,
Pose &p, sensor_msgs::ImageConstPtr image_msg,
tf::StampedTransform &CamToOutput, visualization_msgs::Marker *rvizMarker, ar_track_alvar_msgs::AlvarMarker *ar_pose_marker);
92 static bool init=
true;
96 vector<int> id_vector;
98 id_vector.push_back(i);
111 error = multi_marker_init->
Update(marker_detector.
markers, cam, pose);
116 error = multi_marker_bundle->
Update(marker_detector.
markers, cam, pose);
119 error = multi_marker_bundle->
Update(marker_detector.
markers, cam, pose);
125 cout <<
"Markers seen: " << marker_detector.
markers->size() <<
"\n";
126 if (marker_detector.
markers->size() >= 2) {
127 cout<<
"Adding measurement..."<<endl;
131 cout <<
"Not enough markers to capture measurement\n";
137 cout<<
"Initializing..."<<endl;
139 cout<<
"Initialization failed, add some more measurements."<<endl;
143 multi_marker_bundle->
Reset();
149 const std::vector<MultiMarkerInitializer::MarkerMeasurement, Eigen::aligned_allocator<MultiMarkerInitializer::MarkerMeasurement> > markers = multi_marker_init->
getMeasurementMarkers(i);
154 cout<<
"Optimizing..."<<endl;
155 if (multi_marker_bundle->
Optimize(cam, 0.01, 20)) {
156 cout<<
"Optimizing done"<<endl;
160 cout<<
"Optimizing FAILED!"<<endl;
168 void makeMarkerMsgs(
int type,
int id,
Pose &p, sensor_msgs::ImageConstPtr image_msg,
tf::StampedTransform &CamToOutput, visualization_msgs::Marker *rvizMarker, ar_track_alvar_msgs::AlvarMarker *ar_pose_marker){
169 double px,py,pz,qx,qy,qz,qw;
190 std::string markerFrame =
"ar_marker_";
191 std::stringstream out;
193 std::string id_string = out.str();
194 markerFrame += id_string;
195 tf::StampedTransform camToMarker (t, image_msg->header.stamp, image_msg->header.frame_id, markerFrame.c_str());
201 rvizMarker->header.frame_id = image_msg->header.frame_id;
202 rvizMarker->header.stamp = image_msg->header.stamp;
208 rvizMarker->ns =
"basic_shapes";
209 rvizMarker->type = visualization_msgs::Marker::CUBE;
210 rvizMarker->action = visualization_msgs::Marker::ADD;
214 rvizMarker->color.r = 1.0f;
215 rvizMarker->color.g = 0.0f;
216 rvizMarker->color.b = 0.0f;
217 rvizMarker->color.a = 1.0;
220 rvizMarker->color.r = 0.0f;
221 rvizMarker->color.g = 1.0f;
222 rvizMarker->color.b = 0.0f;
223 rvizMarker->color.a = 1.0;
226 rvizMarker->color.r = 0.0f;
227 rvizMarker->color.g = 0.0f;
228 rvizMarker->color.b = 1.0f;
229 rvizMarker->color.a = 0.5;
240 ar_pose_marker->header.stamp = image_msg->header.stamp;
241 ar_pose_marker->id = id;
268 visualization_msgs::Marker rvizMarker;
269 ar_track_alvar_msgs::AlvarMarker ar_pose_marker;
280 IplImage ipl_image = cv_ptr_->image;
283 static Pose bundlePose;
289 rvizMarkerPub_.
publish (rvizMarker);
293 for (
size_t i=0; i<marker_detector.
markers->size(); i++)
295 int id = (*(marker_detector.
markers))[i].GetId();
301 rvizMarkerPub_.
publish (rvizMarker);
308 ROS_ERROR (
"Could not convert from '%s' to 'rgb8'.", image_msg->encoding.c_str ());
323 cout<<
"Reseting multi marker"<<endl;
324 multi_marker_init->
Reset();
326 multi_marker_bundle->
Reset();
336 cout<<
"Multi marker loaded"<<endl;
341 cout<<
"Cannot load multi marker"<<endl;
346 cout<<
"Multi marker saved"<<endl;
348 cout<<
"Cannot save multi marker"<<endl;
373 int main(
int argc,
char *argv[])
379 std::cout << std::endl;
380 cout <<
"Not enough arguments provided." << endl;
381 cout <<
"Usage: ./trainMarkerBundle <num of markers> <marker size in cm> <max new marker error> <max track error> <cam image topic> <cam info topic> <output frame>" << endl;
382 std::cout << std::endl;
399 arMarkerPub_ = n.
advertise < ar_track_alvar_msgs::AlvarMarkers > (
"ar_pose_marker", 0);
400 rvizMarkerPub_ = n.
advertise < visualization_msgs::Marker > (
"visualization_marker", 0);
407 ROS_INFO (
"Subscribing to image topic");
412 std::cout << std::endl;
413 std::cout <<
"Keyboard Shortcuts:" << std::endl;
414 std::cout <<
" l: load marker configuration from mmarker.txt" << std::endl;
415 std::cout <<
" s: save marker configuration to mmarker.txt" << std::endl;
416 std::cout <<
" r: reset marker configuration" << std::endl;
417 std::cout <<
" p: add measurement" << std::endl;
418 std::cout <<
" a: auto add measurements (captures once a second for 5 seconds)" << std::endl;
419 std::cout <<
" o: optimize bundle" << std::endl;
420 std::cout <<
" q: quit" << std::endl;
421 std::cout << std::endl;
422 std::cout <<
"Please type commands with the openCV window selected" << std::endl;
423 std::cout << std::endl;
425 cvNamedWindow(
"Command input window", CV_WINDOW_AUTOSIZE);
428 int key = cvWaitKey(20);
double getMeasurementPose(int measurement, Camera *cam, Pose &pose)
Subscriber subscribe(const std::string &base_topic, uint32_t queue_size, const boost::function< void(const sensor_msgs::ImageConstPtr &)> &callback, const ros::VoidPtr &tracked_object=ros::VoidPtr(), const TransportHints &transport_hints=TransportHints())
image_transport::Subscriber cam_sub_
std::string cam_image_topic
virtual void Reset()
Resets the multi marker.
void MeasurementsAdd(const std::vector< M, Eigen::aligned_allocator< M > > *markers, const Pose &camera_pose)
Adds new measurements that are used in bundle adjustment.
void publish(const boost::shared_ptr< M > &message) const
void SetMarkerSize(double _edge_length=1, int _res=5, double _margin=2)
tf::TransformListener * tf_listener
This file implements a generic marker detector.
ROSCPP_DECL void init(int &argc, char **argv, const std::string &name, uint32_t options=0)
void getCapCallback(const sensor_msgs::ImageConstPtr &image_msg)
bool Optimize(Camera *_cam, double stop, int max_iter, Optimization::OptimizeMethod method=Optimization::TUKEY_LM)
Runs the bundle adjustment optimization.
Initializes multi marker by estimating their relative positions from one or more images.
std::string cam_info_topic
int DetectAdditional(IplImage *image, Camera *cam, bool visualize=false, double max_track_error=0.2)
Simple Camera class for calculating distortions, orientation or projections with pre-calibrated camer...
double GetMultiMarkerPose(IplImage *image, Pose &pose)
std::vector< M, Eigen::aligned_allocator< M > > * markers
MultiMarkerBundle * multi_marker_bundle
MarkerDetector for detecting markers of type M
int main(int argc, char *argv[])
static const Quaternion & getIdentity()
ar_track_alvar_msgs::AlvarMarkers arPoseMarkers_
MultiMarkerInitializer * multi_marker_init
Multi marker that uses bundle adjustment to refine the 3D positions of the markers (point cloud)...
CvImagePtr toCvCopy(const sensor_msgs::ImageConstPtr &source, const std::string &encoding=std::string())
void PointCloudCopy(const MultiMarker *m)
Copies the 3D point cloud from other multi marker object.
Pose representation derived from the Rotation class
Publisher advertise(const std::string &topic, uint32_t queue_size, bool latch=false)
void PointCloudAdd(int marker_id, double edge_length, Pose &pose)
Adds marker corners to 3D point cloud of multi marker.
cv_bridge::CvImagePtr cv_ptr_
int getMeasurementCount()
void makeMarkerMsgs(int type, int id, Pose &p, sensor_msgs::ImageConstPtr image_msg, tf::StampedTransform &CamToOutput, visualization_msgs::Marker *rvizMarker, ar_track_alvar_msgs::AlvarMarker *ar_pose_marker)
void MeasurementsAdd(MarkerIterator &begin, MarkerIterator &end)
static void poseTFToMsg(const Pose &bt, geometry_msgs::Pose &msg)
This file implements a initialization algorithm to create a multi-marker field configuration.
MarkerDetector< MarkerData > marker_detector
bool Load(const char *fname, FILE_FORMAT format=FILE_FORMAT_DEFAULT)
Loads multi marker configuration from a text file.
int Detect(IplImage *image, Camera *cam, bool track=false, bool visualize=false, double max_new_marker_error=0.08, double max_track_error=0.2, LabelingMethod labeling_method=CVSEQ, bool update_pose=true)
Detect Marker 's from image
double max_new_marker_error
ros::Publisher arMarkerPub_
This file implements an algorithm to create a multi-marker field configuration.
int Initialize(Camera *cam)
void MeasurementsReset()
Resets the measurements and camera poses that are stored for bundle adjustment. If something goes fro...
ROSCPP_DECL void spinOnce()
int SetTrackMarkers(MarkerDetector< M > &marker_detector, Camera *cam, Pose &pose, IplImage *image=0)
Set new markers to be tracked for MarkerDetector Sometimes the MultiMarker implementation knows more ...
const std::vector< MarkerMeasurement, Eigen::aligned_allocator< MarkerMeasurement > > & getMeasurementMarkers(int measurement)
double Update(const std::vector< M, Eigen::aligned_allocator< M > > *markers, Camera *cam, Pose &pose, IplImage *image=0)
Calls GetPose to obtain camera pose.
bool Save(const char *fname, FILE_FORMAT format=FILE_FORMAT_DEFAULT)
Saves multi marker configuration to a text file.
tf::TransformBroadcaster * tf_broadcaster
ros::Publisher rvizMarkerPub_