apply_mask_image.cpp
Go to the documentation of this file.
00001 // -*- mode: c++ -*-
00002 /*********************************************************************
00003  * Software License Agreement (BSD License)
00004  *
00005  *  Copyright (c) 2015, JSK Lab
00006  *  All rights reserved.
00007  *
00008  *  Redistribution and use in source and binary forms, with or without
00009  *  modification, are permitted provided that the following conditions
00010  *  are met:
00011  *
00012  *   * Redistributions of source code must retain the above copyright
00013  *     notice, this list of conditions and the following disclaimer.
00014  *   * Redistributions in binary form must reproduce the above
00015  *     copyright notice, this list of conditions and the following
00016  *     disclaimer in the documentation and/o2r other materials provided
00017  *     with the distribution.
00018  *   * Neither the name of the JSK Lab nor the names of its
00019  *     contributors may be used to endorse or promote products derived
00020  *     from this software without specific prior written permission.
00021  *
00022  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00023  *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00024  *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00025  *  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00026  *  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00027  *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00028  *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00029  *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00030  *  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00031  *  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00032  *  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00033  *  POSSIBILITY OF SUCH DAMAGE.
00034  *********************************************************************/
00035 
00036 #include "jsk_perception/apply_mask_image.h"
00037 #include <boost/assign.hpp>
00038 #include <jsk_topic_tools/log_utils.h>
00039 #include <sensor_msgs/image_encodings.h>
00040 #include <opencv2/opencv.hpp>
00041 #include <cv_bridge/cv_bridge.h>
00042 #include <jsk_recognition_utils/cv_utils.h>
00043 
00044 namespace jsk_perception
00045 {
00046   void ApplyMaskImage::onInit()
00047   {
00048     DiagnosticNodelet::onInit();
00049     pnh_->param("approximate_sync", approximate_sync_, false);
00050     pnh_->param("clip", clip_, true);
00051     pnh_->param("negative", negative_, false);
00052     pnh_->param("negative/before_clip", negative_before_clip_, true);
00053     pnh_->param("mask_black_to_transparent", mask_black_to_transparent_, false);
00054     pnh_->param("queue_size", queue_size_, 100);
00055     pub_image_ = advertise<sensor_msgs::Image>(
00056       *pnh_, "output", 1);
00057     pub_mask_ = advertise<sensor_msgs::Image>(
00058       *pnh_, "output/mask", 1);
00059     onInitPostProcess();
00060   }
00061 
00062   void ApplyMaskImage::subscribe()
00063   {
00064     sub_image_.subscribe(*pnh_, "input", 1);
00065     sub_mask_.subscribe(*pnh_, "input/mask", 1);
00066     if (approximate_sync_) {
00067       async_ = boost::make_shared<message_filters::Synchronizer<ApproximateSyncPolicy> >(queue_size_);
00068       async_->connectInput(sub_image_, sub_mask_);
00069       async_->registerCallback(boost::bind(&ApplyMaskImage::apply, this, _1, _2));
00070     }
00071     else {
00072       sync_ = boost::make_shared<message_filters::Synchronizer<SyncPolicy> >(queue_size_);
00073       sync_->connectInput(sub_image_, sub_mask_);
00074       sync_->registerCallback(boost::bind(&ApplyMaskImage::apply, this, _1, _2));
00075     }
00076     ros::V_string names = boost::assign::list_of("~input")("~input/mask");
00077     jsk_topic_tools::warnNoRemap(names);
00078   }
00079 
00080   void ApplyMaskImage::unsubscribe()
00081   {
00082     sub_image_.unsubscribe();
00083     sub_mask_.unsubscribe();
00084   }
00085 
00086   void ApplyMaskImage::apply(
00087     const sensor_msgs::Image::ConstPtr& image_msg,
00088     const sensor_msgs::Image::ConstPtr& mask_msg)
00089   {
00090     vital_checker_->poke();
00091     cv::Mat image;
00092     if (jsk_recognition_utils::isBGRA(image_msg->encoding)) {
00093       cv::Mat tmp_image = cv_bridge::toCvShare(image_msg, image_msg->encoding)->image;
00094       cv::cvtColor(tmp_image, image, cv::COLOR_BGRA2BGR);
00095     }
00096     else if (jsk_recognition_utils::isRGBA(image_msg->encoding)) {
00097       cv::Mat tmp_image = cv_bridge::toCvShare(image_msg, image_msg->encoding)->image;
00098       cv::cvtColor(tmp_image, image, cv::COLOR_RGBA2BGR);
00099     }
00100     else {  // BGR, RGB or GRAY
00101       image = cv_bridge::toCvShare(image_msg, image_msg->encoding)->image;
00102     }
00103     cv::Mat mask = cv_bridge::toCvShare(mask_msg, "mono8")->image;
00104     if (image.cols != mask.cols || image.rows != mask.rows) {
00105       NODELET_ERROR("size of image and mask is different");
00106       NODELET_ERROR("image: %dx%dx", image.cols, image.rows);
00107       NODELET_ERROR("mask: %dx%dx", mask.cols, mask.rows);
00108       return;
00109     }
00110 
00111     if (negative_ && negative_before_clip_) {
00112       cv::bitwise_not(mask, mask);
00113     }
00114 
00115     if (clip_) {
00116       cv::Rect region = jsk_recognition_utils::boundingRectOfMaskImage(mask);
00117       mask = mask(region);
00118       image = image(region);
00119     }
00120 
00121     if (negative_ && !negative_before_clip_) {
00122       cv::bitwise_not(mask, mask);
00123     }
00124 
00125     pub_mask_.publish(cv_bridge::CvImage(
00126                         mask_msg->header,
00127                         "mono8",
00128                         mask).toImageMsg());
00129 
00130     cv::Mat masked_image;
00131     image.copyTo(masked_image, mask);
00132 
00133     cv::Mat output_image;
00134     if (mask_black_to_transparent_) {
00135       if (sensor_msgs::image_encodings::isMono(image_msg->encoding)) {
00136         cv::cvtColor(masked_image, output_image, CV_GRAY2BGRA);
00137       }
00138       else if (jsk_recognition_utils::isRGB(image_msg->encoding)) {
00139         cv::cvtColor(masked_image, output_image, CV_RGB2BGRA);
00140       }
00141       else {  // BGR, BGRA or RGBA
00142         cv::cvtColor(masked_image, output_image, CV_BGR2BGRA);
00143       }
00144       for (size_t j=0; j < mask.rows; j++) {
00145         for (int i=0; i < mask.cols; i++) {
00146           if (mask.at<uchar>(j, i) == 0) {
00147             cv::Vec4b color = output_image.at<cv::Vec4b>(j, i);
00148             color[3] = 0;  // mask black -> transparent
00149             output_image.at<cv::Vec4b>(j, i) = color;
00150           }
00151         }
00152       }
00153       // publish bgr8 image
00154       pub_image_.publish(cv_bridge::CvImage(
00155             image_msg->header,
00156             sensor_msgs::image_encodings::BGRA8,
00157             output_image).toImageMsg());
00158     }
00159     else {
00160       if (jsk_recognition_utils::isBGRA(image_msg->encoding)) {
00161         cv::cvtColor(masked_image, output_image, cv::COLOR_BGR2BGRA);
00162       }
00163       else if (jsk_recognition_utils::isRGBA(image_msg->encoding)) {
00164         cv::cvtColor(masked_image, output_image, cv::COLOR_BGR2RGBA);
00165       }
00166       else {  // BGR, RGB or GRAY
00167         masked_image.copyTo(output_image);
00168       }
00169       pub_image_.publish(cv_bridge::CvImage(
00170             image_msg->header,
00171             image_msg->encoding,
00172             output_image).toImageMsg());
00173     }
00174   }
00175 }
00176 
00177 #include <pluginlib/class_list_macros.h>
00178 PLUGINLIB_EXPORT_CLASS (jsk_perception::ApplyMaskImage, nodelet::Nodelet);


jsk_perception
Author(s): Manabu Saito, Ryohei Ueda
autogenerated on Sun Oct 8 2017 02:43:23