19 #include <sensor_msgs/Image.h>
20 #include <sensor_msgs/CameraInfo.h>
21 #include <posedetection_msgs/ImageFeature0D.h>
22 #include <posedetection_msgs/Feature0DDetect.h>
25 #if ( CV_MAJOR_VERSION >= 4)
26 #include <opencv2/highgui.hpp>
28 #include <opencv/highgui.h>
29 #include <opencv/cv.h>
32 #include <boost/shared_ptr.hpp>
33 #include <boost/thread/mutex.hpp>
41 #include <siftfast/siftfast.h>
53 void SiftNode::onInit()
55 DiagnosticNodelet::onInit();
57 std::string transport;
58 pnh_->param(
"image_transport", transport, std::string(
"raw"));
59 ROS_INFO_STREAM(
"Using transport \"" << transport <<
"\" for " << pnh_->getNamespace());
63 pnh_->param(
"use_mask", _useMask,
false);
65 _pubFeatures = advertise<posedetection_msgs::Feature0D>(*nh_,
"Feature0D", 1);
66 _pubSift = advertise<posedetection_msgs::ImageFeature0D>(*nh_,
"ImageFeature0D", 1);
67 _srvDetect = nh_->advertiseService(
"Feature0DDetect", &SiftNode::detectCb,
this);
69 _bInfoInitialized =
false;
74 SiftNode::~SiftNode() {
76 DestroyAllResources();
87 void SiftNode::subscribe()
90 _subImage = _it->subscribe(nh_->resolveName(
"image"), 1, &SiftNode::imageCb,
this, _hints);
93 _subImageWithMask.subscribe(*nh_,
"image", 1);
94 _subMask.subscribe(*nh_,
"mask", 1);
95 _sync = boost::make_shared<message_filters::Synchronizer<SyncPolicy> >(100);
96 _sync->connectInput(_subImageWithMask, _subMask);
97 _sync->registerCallback(boost::bind(&SiftNode::imageCb,
this,
_1,
_2));
99 _subInfo = nh_->subscribe(
"camera_info", 1, &SiftNode::infoCb,
this);
102 void SiftNode::unsubscribe()
105 _subImage.shutdown();
108 _subImageWithMask.unsubscribe();
109 _subMask.unsubscribe();
114 void SiftNode::infoCb(
const sensor_msgs::CameraInfoConstPtr& msg_ptr)
116 boost::mutex::scoped_lock lock(_mutex);
117 _sift_msg.info = *msg_ptr;
118 _bInfoInitialized =
true;
121 bool SiftNode::detectCb(posedetection_msgs::Feature0DDetect::Request& req,
122 posedetection_msgs::Feature0DDetect::Response& res)
124 return detect(res.features,req.image, sensor_msgs::Image::ConstPtr());
127 bool SiftNode::detect(posedetection_msgs::Feature0D& features,
const sensor_msgs::Image& imagemsg,
128 const sensor_msgs::Image::ConstPtr& mask_ptr)
131 boost::mutex::scoped_lock lock(_mutex);
147 image = framefloat->image;
152 ROS_DEBUG (
"region x:%d y:%d width:%d height:%d", region.x, region.y, region.width, region.height);
153 if (region.width == 0 || region.height ==0) {
154 region = cv::Rect(0, 0, imagemsg.width, imagemsg.height);
156 image = image(region);
159 region = cv::Rect(0, 0, imagemsg.width, imagemsg.height);
163 imagesift = CreateImage(imagemsg.height,imagemsg.width);
165 for(
int i = 0;
i < imagemsg.height; ++
i) {
166 uint8_t* psrc = (uint8_t*)image.data+image.step*
i;
168 for(
int j = 0; j < imagemsg.width; ++j)
169 pdst[j] = (float)psrc[j]*(1.0
f/255.0
f);
180 Keypoint keypts = GetKeypoints(
imagesift);
183 Keypoint key = keypts;
190 features.header = imagemsg.header;
191 features.positions.resize(numkeys*2);
192 features.scales.resize(numkeys);
193 features.orientations.resize(numkeys);
194 features.confidences.resize(numkeys);
195 features.descriptors.resize(numkeys*128);
196 features.descriptor_dim = 128;
197 features.type =
"libsiftfast";
203 for(
int j = 0; j < 128; ++j)
204 features.descriptors[128*index+j] = key->descrip[j];
206 features.positions[2*index+0] = key->col + region.x;
207 features.positions[2*index+1] = key->row + region.y;
208 features.scales[index] = key->scale;
209 features.orientations[index] = key->ori;
210 features.confidences[index] = 1.0;
216 FreeKeypoints(keypts);
219 ROS_DEBUG(
"imagesift: image: %d(size=%lu), num: %d, sift time: %.3fs, total: %.3fs", imagemsg.header.seq,
220 imagemsg.data.size(), numkeys,
226 void SiftNode::imageCb(
const sensor_msgs::ImageConstPtr& msg_ptr,
227 const sensor_msgs::ImageConstPtr& mask_ptr)
229 vital_checker_->poke();
230 if(_pubFeatures.getNumSubscribers()==0 && _pubSift.getNumSubscribers()==0) {
231 ROS_DEBUG(
"number of subscribers is 0, ignoring image");
234 detect(_sift_msg.features,*msg_ptr, mask_ptr);
235 _pubFeatures.publish(_sift_msg.features);
237 if(!_bInfoInitialized) {
238 ROS_DEBUG(
"camera info not initialized, ignoring image");
241 _sift_msg.image = *msg_ptr;
243 boost::mutex::scoped_lock lock(_mutex);
244 _pubSift.publish(_sift_msg);
248 void SiftNode::imageCb(
const sensor_msgs::ImageConstPtr& msg_ptr)
250 imageCb(msg_ptr, sensor_msgs::ImageConstPtr());