2 #include <dynamic_reconfigure/server.h> 10 #if ( CV_MAJOR_VERSION >= 4) 12 #include <opencv/cv.hpp> 13 #include <opencv/highgui.h> 18 #include <boost/assign.hpp> 19 #include <boost/thread.hpp> 21 #include <geometry_msgs/Polygon.h> 22 #include <geometry_msgs/PolygonStamped.h> 24 #include <jsk_perception/VirtualCameraMonoConfig.h> 41 geometry_msgs::PolygonStamped
poly_;
45 VirtualCameraMono() : private_nh_(
"~"), it_(nh_), it_priv_(private_nh_), subscriber_count_(0)
51 dynamic_reconfigure::Server<jsk_perception::VirtualCameraMonoConfig>::CallbackType
f =
53 srv_ = boost::make_shared<dynamic_reconfigure::Server<jsk_perception::VirtualCameraMonoConfig> >(
private_nh_);
56 private_nh_.
param(
"frame_id", trans_.
frame_id_, std::string(
"/elevator_inside_panel"));
57 private_nh_.
param(
"child_frame_id", trans_.
child_frame_id_, std::string(
"/virtual_camera_frame"));
60 std::vector<double> initial_pos, initial_rot;
77 geometry_msgs::Point32 pt;
78 pt.x=0, pt.y=1, pt.z=0; poly_.polygon.points.push_back(pt);
79 pt.x=0, pt.y=-1, pt.z=0; poly_.polygon.points.push_back(pt);
80 pt.x=0, pt.y=-1, pt.z=-1; poly_.polygon.points.push_back(pt);
81 pt.x=0, pt.y=1, pt.z=-1; poly_.polygon.points.push_back(pt);
89 void configCb(
const jsk_perception::VirtualCameraMonoConfig &config, uint32_t level)
91 boost::mutex::scoped_lock(mutex_);
92 switch (config.interpolation_method) {
94 interpolation_method_ = cv::INTER_NEAREST;
97 interpolation_method_ = cv::INTER_LINEAR;
100 interpolation_method_ = cv::INTER_AREA;
103 interpolation_method_ = cv::INTER_CUBIC;
106 interpolation_method_ = cv::INTER_LANCZOS4;
109 ROS_ERROR(
"Invalid interpolation method: %d", config.interpolation_method);
116 if (subscriber_count_ == 0){
120 ROS_DEBUG(
"connected new node. current subscriber: %d", subscriber_count_);
126 if (subscriber_count_ == 0) {
129 ROS_DEBUG(
"disconnected node. current subscriber: %d", subscriber_count_);
134 ROS_INFO(
"Subscribing to image topic");
142 ROS_INFO(
"Unsubscibing from image topic");
146 void imageCb(
const sensor_msgs::ImageConstPtr& image_msg,
147 const sensor_msgs::CameraInfoConstPtr& info_msg)
153 image = cv_ptr->image;
156 ROS_ERROR(
"[virtual_camera_mono] Failed to convert image");
167 cv::Mat outimage = image.clone();
169 ROS_DEBUG(
"publish image and transform.");
170 sensor_msgs::CameraInfo virtual_info = *info_msg;
171 cv_ptr->image = outimage;
172 sensor_msgs::Image::Ptr
img_msg = cv_ptr->toImageMsg();
173 img_msg->header.stamp = trans_.
stamp_;
174 virtual_info.header.stamp = trans_.
stamp_;
177 pub_.
publish(*img_msg, virtual_info);
182 void polyCb(
const geometry_msgs::PolygonStampedConstPtr& poly) { poly_ = *poly; }
185 void transCb(
const geometry_msgs::TransformStampedConstPtr&
tf)
190 tf->transform.translation.y,
191 tf->transform.translation.z));
193 tf->transform.rotation.z, tf->transform.rotation.w));
203 std::vector<tf::Point> target_poly, target_poly_translated;
204 for(std::vector<geometry_msgs::Point32>::iterator pit=poly.polygon.points.begin(); pit != poly.polygon.points.end(); pit++) {
205 geometry_msgs::PointStamped
point, cpoint, vpoint;
206 point.point.x = pit->x, point.point.y = pit->y, point.point.z = pit->z;
207 point.header = poly.header;
214 target_poly.push_back(
tf::Point(cpoint.point.x, cpoint.point.y, cpoint.point.z));
215 target_poly_translated.push_back(
tf::Point(vpt_vec.
x(),vpt_vec.
y(),vpt_vec.
z()));
219 cv::Point2f src_pnt[4], dst_pnt[4];
220 for(
int i = 0; i < 4; i++) {
221 cv::Point3d xyz(target_poly[i].
x(),target_poly[i].
y(),target_poly[i].
z());
222 cv::Point3d xyz_trans(target_poly_translated[i].
x(),
223 target_poly_translated[i].
y(),
224 target_poly_translated[i].
z());
225 cv::Point2d uv,uv_trans;
227 src_pnt[i] = cv::Point (uv.x, uv.y);
229 dst_pnt[i] = cv::Point (uv_trans.x, uv_trans.y);
232 cv::Mat map_matrix = cv::getPerspectiveTransform (src_pnt, dst_pnt);
236 ROS_WARN_THROTTLE(10,
"[virtual_camera_mono] failed to transform image: %s", e.what());
243 int main(
int argc,
char **argv)
245 ros::init(argc, argv,
"virtual_camera_mono");
tf::StampedTransform trans_
image_transport::ImageTransport it_priv_
void publish(const sensor_msgs::Image &image, const sensor_msgs::CameraInfo &info) const
#define ROS_WARN_THROTTLE(rate,...)
Subscriber subscribe(const std::string &topic, uint32_t queue_size, void(T::*fp)(M), T *obj, const TransportHints &transport_hints=TransportHints())
image_transport::CameraSubscriber sub_
CameraSubscriber subscribeCamera(const std::string &base_topic, uint32_t queue_size, const CameraSubscriber::Callback &callback, const ros::VoidPtr &tracked_object=ros::VoidPtr(), const TransportHints &transport_hints=TransportHints())
int main(int argc, char **argv)
void disconnectCb(const image_transport::SingleSubscriberPublisher &)
ROSCPP_DECL void init(int &argc, char **argv, const std::string &name, uint32_t options=0)
boost::function< void(const SingleSubscriberPublisher &)> SubscriberStatusCallback
ROSCPP_DECL const std::string & getName()
TFSIMD_FORCE_INLINE const tfScalar & getY() const
TFSIMD_FORCE_INLINE const tfScalar & getW() const
void transCb(const geometry_msgs::TransformStampedConstPtr &tf)
TFSIMD_FORCE_INLINE const tfScalar & getZ() const
ROSCPP_DECL void spin(Spinner &spinner)
geometry_msgs::PolygonStamped poly_
void connectCb(const image_transport::SingleSubscriberPublisher &)
std::string tfFrame() const
TFSIMD_FORCE_INLINE const tfScalar & x() const
void imageCb(const sensor_msgs::ImageConstPtr &image_msg, const sensor_msgs::CameraInfoConstPtr &info_msg)
bool TransformImage(cv::Mat src, cv::Mat dest, tf::StampedTransform &trans, geometry_msgs::PolygonStamped &poly, image_geometry::PinholeCameraModel &cam_model_)
image_geometry::PinholeCameraModel cam_model_
CameraPublisher advertiseCamera(const std::string &base_topic, uint32_t queue_size, bool latch=false)
void configCb(const jsk_perception::VirtualCameraMonoConfig &config, uint32_t level)
std::vector< std::string > V_string
TFSIMD_FORCE_INLINE const tfScalar & z() const
bool fromCameraInfo(const sensor_msgs::CameraInfo &msg)
bool param(const std::string ¶m_name, T ¶m_val, const T &default_val) const
CvImagePtr toCvCopy(const sensor_msgs::ImageConstPtr &source, const std::string &encoding=std::string())
TFSIMD_FORCE_INLINE const tfScalar & y() const
ros::Subscriber sub_trans_
tf::TransformListener tf_listener_
image_transport::CameraPublisher pub_
tf::TransformBroadcaster tf_broadcaster_
int interpolation_method_
void polyCb(const geometry_msgs::PolygonStampedConstPtr &poly)
TFSIMD_FORCE_INLINE const tfScalar & getX() const
boost::shared_ptr< dynamic_reconfigure::Server< jsk_perception::VirtualCameraMonoConfig > > srv_
cv::Point2d project3dToPixel(const cv::Point3d &xyz) const
ros::NodeHandle private_nh_
image_transport::ImageTransport it_
ros::Subscriber sub_poly_