36 #include <jsk_topic_tools/log_utils.h>
48 std::string _pose_str;
49 double template_width;
50 double template_height;
51 std::string template_filename;
52 std::string window_name;
54 pnh_->param(
"child_frame_id",
_child_frame_id, std::string(
"matching"));
55 pnh_->param(
"object_width", template_width, 0.06);
56 pnh_->param(
"object_height", template_height, 0.0739);
57 pnh_->param(
"relative_pose", _pose_str, std::string(
"0 0 0 0 0 0 1"));
58 std::string default_template_file_name;
62 rospack::Package *
p =
rp.get_pkg(
"jsk_perception");
63 if (
p!=NULL) default_template_file_name =
p->path + std::string(
"/sample/opencv-logo2.png");
66 std::vector<std::string> search_path;
70 if (
rp.
find(
"jsk_perception",path)==
true) default_template_file_name = path + std::string(
"/sample/opencv-logo2.png");
72 }
catch (std::runtime_error &e) {
74 pnh_->param(
"template_filename", template_filename, default_template_file_name);
77 pnh_->param(
"error_threshold",
_err_thr, 50.0);
78 pnh_->param(
"theta_step",
_th_step, 5.0);
80 pnh_->param(
"viewer_window",
_viewer,
true);
81 pnh_->param(
"window_name", window_name, std::string(
"sample1"));
82 pnh_->param(
"autosize",
_autosize,
false);
83 pnh_->param(
"publish_null_object_detection",
pnod,
false);
88 template_img = cv::imread (template_filename, 1);
89 if ( template_img.empty()) {
90 ROS_ERROR (
"template picture <%s> cannot read. template picture is not found or uses unsuported format.", template_filename.c_str());
97 std::vector<double> rv(7);
98 std::istringstream iss(_pose_str);
100 for(
int i=0;
i<6;
i++)
105 tf::Vector3(rv[0], rv[1], rv[2]));
109 tf::Vector3(rv[0], rv[1], rv[2]));
120 double template_width,
double template_height, cv::Size &size){
122 cv::Point3f coner[4] = {cv::Point3f(-(template_width/2.0), -(template_height/2.0),0),
123 cv::Point3f(template_width/2.0,-(template_height/2.0),0),
124 cv::Point3f(template_width/2.0,template_height/2.0,0),
125 cv::Point3f(-(template_width/2.0),template_height/2.0,0)};
126 cv::Mat coner_mat (cv::Size(4, 1), CV_32FC3, coner);
127 std::vector<cv::Point2f> coner_img_points;
129 cv::Mat zero_distortion_mat = cv::Mat::zeros(4, 1, CV_64FC1);
130 cv::projectPoints(coner_mat, rvec, tvec,
134 float x_min = 10000, x_max = 0;
135 float y_min = 10000, y_max = 0;
136 for (
int i = 0;
i < (
int)coner_img_points.size();
i++){
137 x_min = std::min(x_min, coner_img_points.at(i).x);
138 x_max = std::max(x_max, coner_img_points.at(i).x);
139 y_min = std::min(y_min, coner_img_points.at(i).y);
140 y_max = std::max(y_max, coner_img_points.at(i).y);
143 std::vector<cv::Point2f> coner_img_points_trans;
144 for (
int i = 0;
i < (
int)coner_img_points.size();
i++){
145 cv::Point2f pt_tmp(coner_img_points.at(i).x - x_min,
146 coner_img_points.at(i).y - y_min);
147 coner_img_points_trans.push_back(pt_tmp);
150 cv::Point2f template_points[4] = {cv::Point2f(0,0),
151 cv::Point2f(src.size().width,0),
152 cv::Point2f(src.size().width,src.size().height),
153 cv::Point2f(0,src.size().height)};
154 cv::Mat template_points_mat (cv::Size(4, 1), CV_32FC2, template_points);
156 size = cv::Size(x_max - x_min, y_max - y_min);
157 return cv::findHomography(template_points_mat, cv::Mat(coner_img_points_trans), 0, 3);
162 std::vector<cv:: Mat> &Mvec,
163 double template_width,
double template_height,
164 double th_step,
double phi_step){
166 std::vector<cv::Size> sizevec;
168 for (
int i = (
int)((-3.14/4.0)/th_step);
i*th_step < 3.14/4.0;
i++){
169 for (
int j = (
int)((-3.14/4.0)/phi_step); j*phi_step < 3.14/4.0; j++){
170 double fR3[3], fT3[3];
171 cv::Mat rvec(3, 1, CV_64FC1, fR3);
172 cv::Mat tvec(3, 1, CV_64FC1, fT3);
175 quat.
setEuler(0, th_step*i, phi_step*j);
185 M =
make_homography(src, rvec, tvec, template_width, template_height, size);
189 sizevec.push_back(size);
193 for (
int i = 0;
i < (
int)Mvec.size();
i++){
195 cv::warpPerspective(src, dst, Mvec.at(
i), sizevec.at(
i),
196 CV_INTER_LINEAR, IPL_BORDER_CONSTANT, 0);
203 double template_width,
double template_height,
204 double theta_step=5.0,
double phi_step=5.0)
206 std::vector<cv::Mat> imgs;
207 std::vector<cv::Mat> Mvec;
209 template_width, template_height, theta_step, phi_step);
211 for (
int i = 0;
i < (
int)imgs.size();
i++){
212 std::string
type = typestr;
213 if(imgs.size() > 1) {
215 sprintf(chr,
"%d", i);
216 type +=
"_" + std::string(chr);
221 img.size().width,
img.size().height,
222 template_width, template_height,
223 relative_pose, Mvec.at(i),
230 cv::namedWindow(type,
_autosize ? CV_WINDOW_AUTOSIZE : 0);
238 jsk_perception::SetTemplate::Response &res){
240 ROS_WARN(
"SetTemplate service is available only after receiving input ImageFeature0D");
245 cv::Mat
img(cv_ptr->image);
252 req.dimx,
req.dimy, 1.0, 1.0);
258 std::vector<cv::KeyPoint> sourceimg_keypoints;
259 cv::Mat sourceimg_descriptors;
260 std::vector<posedetection_msgs::Object6DPose> vo6p;
261 posedetection_msgs::ObjectDetection od;
265 cv::Mat src_img(cv_ptr->image);
271 ROS_FATAL(
"intrinsic matrix is zero, your camera info looks invalid");
276 std::cerr <<
"initialize templates done" << std::endl;
280 if ( sourceimg_keypoints.size () < 2 ) {
281 ROS_INFO (
"The number of keypoints in source image is less than 2");
285 cv::flann::Index *ft =
new cv::flann::Index(sourceimg_descriptors, cv::flann::KDTreeIndexParams(1));
290 posedetection_msgs::Object6DPose o6p;
300 out_msg.
image = debug_img;
304 if (((
int)vo6p.size() != 0) ||
pnod) {
305 od.header.stamp =
msg->image.header.stamp;
306 od.header.frame_id =
msg->image.header.frame_id;
311 geometry_msgs::PoseStamped pose_msg;
312 pose_msg.header = od.header;
313 if ((
int)vo6p.size() != 0) {
314 pose_msg.pose = od.objects[0].pose;
324 msg->image.header.stamp,
325 msg->image.header.frame_id,
340 DiagnosticNodelet::onInit();
341 dynamic_reconfigure::Server<Config>
server;
342 dynamic_reconfigure::Server<Config>::CallbackType
f;
350 _client = nh_->serviceClient<posedetection_msgs::Feature0DDetect>(
"Feature0DDetect");
351 _pub = advertise<posedetection_msgs::ObjectDetection>(*nh_,
"ObjectDetection", 10);
352 _pub_agg = advertise<posedetection_msgs::ObjectDetection>(*nh_,
"ObjectDetection_agg", 10);
353 _pub_pose = advertise<geometry_msgs::PoseStamped>(*nh_,
"object_pose", 10);
363 _sub = nh_->subscribe(
"ImageFeature0D", 1,
375 std::cout <<
"id = " <<
config.template_id << std::endl;
376 std::cout <<
"lvl = " << level << std::endl;