crop_non_zero.cpp
Go to the documentation of this file.
1 /*********************************************************************
2 * Software License Agreement (BSD License)
3 *
4 * Copyright (c) 2008, Willow Garage, Inc.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 *
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
17 * * Neither the name of the Willow Garage nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 * POSSIBILITY OF SUCH DAMAGE.
33 *********************************************************************/
34 #include <nodelet/nodelet.h>
36 #include <boost/thread.hpp>
37 #include <cv_bridge/cv_bridge.h>
38 //#include <algorithm> // for std::max_element
39 
40 namespace image_proc {
41 
43 
44 class CropNonZeroNodelet : public nodelet::Nodelet
45 {
46  // Subscriptions
49 
50  // Publications
51  boost::mutex connect_mutex_;
53 
54  virtual void onInit();
55 
56  void connectCb();
57 
58  void imageCb(const sensor_msgs::ImageConstPtr& raw_msg);
59 };
60 
62 {
64  it_.reset(new image_transport::ImageTransport(nh));
65 
66  // Monitor whether anyone is subscribed to the output
68  // Make sure we don't enter connectCb() between advertising and assigning to pub_depth_
69  boost::lock_guard<boost::mutex> lock(connect_mutex_);
70  pub_ = it_->advertise("image", 1, connect_cb, connect_cb);
71 }
72 
73 // Handles (un)subscribing when clients (un)subscribe
75 {
76  boost::lock_guard<boost::mutex> lock(connect_mutex_);
77  if (pub_.getNumSubscribers() == 0)
78  {
80  }
81  else if (!sub_raw_)
82  {
84  sub_raw_ = it_->subscribe("image_raw", 1, &CropNonZeroNodelet::imageCb, this, hints);
85  }
86 }
87 
88 void CropNonZeroNodelet::imageCb(const sensor_msgs::ImageConstPtr& raw_msg)
89 {
90  cv_bridge::CvImagePtr cv_ptr;
91  try
92  {
93  cv_ptr = cv_bridge::toCvCopy(raw_msg);
94  }
95  catch (cv_bridge::Exception& e)
96  {
97  ROS_ERROR("cv_bridge exception: %s", e.what());
98  return;
99  }
100 
101  // Check the number of channels
102  if(sensor_msgs::image_encodings::numChannels(raw_msg->encoding) != 1){
103  NODELET_ERROR_THROTTLE(2, "Only grayscale image is acceptable, got [%s]", raw_msg->encoding.c_str());
104  return;
105  }
106 
107  std::vector<std::vector<cv::Point> >cnt;
108  cv::Mat1b m(raw_msg->width, raw_msg->height);
109 
110  if (raw_msg->encoding == enc::TYPE_8UC1){
111  m = cv_ptr->image;
112  }else{
113  double minVal, maxVal;
114  cv::minMaxIdx(cv_ptr->image, &minVal, &maxVal, 0, 0, cv_ptr->image != 0.);
115  double ra = maxVal - minVal;
116 
117  cv_ptr->image.convertTo(m, CV_8U, 255./ra, -minVal*255./ra);
118  }
119 
120  cv::findContours(m, cnt, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_NONE);
121 
122  // search the largest area
123  /* // -std=c++11
124  std::vector<std::vector<cv::Point> >::iterator it = std::max_element(cnt.begin(), cnt.end(), [](std::vector<cv::Point> a, std::vector<cv::Point> b) {
125  return a.size() < b.size();
126  });
127  */
128  std::vector<std::vector<cv::Point> >::iterator it = cnt.begin();
129  for(std::vector<std::vector<cv::Point> >::iterator i=cnt.begin();i!=cnt.end();++i){
130  it = (*it).size() < (*i).size() ? i : it;
131  }
132  cv::Rect r = cv::boundingRect(cnt[std::distance(cnt.begin(), it)]);
133 
134  cv_bridge::CvImage out_msg;
135  out_msg.header = raw_msg->header;
136  out_msg.encoding = raw_msg->encoding;
137  out_msg.image = cv_ptr->image(r);
138 
139  pub_.publish(out_msg.toImageMsg());
140 }
141 
142 } // namespace image_proc
143 
144 // Register as nodelet
image_proc::CropNonZeroNodelet::connectCb
void connectCb()
Definition: crop_non_zero.cpp:106
sensor_msgs::image_encodings
image_proc::CropNonZeroNodelet::it_
boost::shared_ptr< image_transport::ImageTransport > it_
Definition: crop_non_zero.cpp:111
nodelet::Nodelet::getNodeHandle
ros::NodeHandle & getNodeHandle() const
image_transport::ImageTransport
boost::shared_ptr< image_transport::ImageTransport >
cv_bridge::CvImage::toImageMsg
sensor_msgs::ImagePtr toImageMsg() const
image_proc::CropNonZeroNodelet::connect_mutex_
boost::mutex connect_mutex_
Definition: crop_non_zero.cpp:115
NODELET_ERROR_THROTTLE
#define NODELET_ERROR_THROTTLE(rate,...)
image_transport::Publisher::getNumSubscribers
uint32_t getNumSubscribers() const
image_proc::CropNonZeroNodelet::pub_
image_transport::Publisher pub_
Definition: crop_non_zero.cpp:116
ros::TransportHints
cv_bridge::Exception
nodelet::Nodelet::getPrivateNodeHandle
ros::NodeHandle & getPrivateNodeHandle() const
image_transport::Subscriber
image_proc::CropNonZeroNodelet
Definition: crop_non_zero.cpp:76
cv_bridge::toCvCopy
CvImagePtr toCvCopy(const sensor_msgs::CompressedImage &source, const std::string &encoding=std::string())
cv_bridge::CvImage::header
std_msgs::Header header
sensor_msgs::image_encodings::numChannels
static int numChannels(const std::string &encoding)
image_transport::Publisher::publish
void publish(const sensor_msgs::Image &message) const
image_transport.h
image_proc::CropNonZeroNodelet::imageCb
void imageCb(const sensor_msgs::ImageConstPtr &raw_msg)
Definition: crop_non_zero.cpp:120
nodelet::Nodelet
cv_bridge::CvImage::encoding
std::string encoding
image_proc
Definition: advertisement_checker.h:39
PLUGINLIB_EXPORT_CLASS
PLUGINLIB_EXPORT_CLASS(image_proc::CropNonZeroNodelet, nodelet::Nodelet)
nodelet.h
image_transport::Publisher
cv_bridge.h
ROS_ERROR
#define ROS_ERROR(...)
cv_bridge::CvImage
class_list_macros.hpp
image_transport::SubscriberStatusCallback
boost::function< void(const SingleSubscriberPublisher &)> SubscriberStatusCallback
image_proc::CropNonZeroNodelet::onInit
virtual void onInit()
Definition: crop_non_zero.cpp:93
image_transport::TransportHints
cv_bridge::CvImage::image
cv::Mat image
ros::NodeHandle
image_proc::CropNonZeroNodelet::sub_raw_
image_transport::Subscriber sub_raw_
Definition: crop_non_zero.cpp:112
image_transport::Subscriber::shutdown
void shutdown()


image_proc
Author(s): Patrick Mihelich, Kurt Konolige, Jeremy Leibs
autogenerated on Wed Jan 24 2024 03:57:17