[ros-users] problems with image_proc using a modified camera

Top Page
Attachments:
Message as email
+ (text/plain)
+ image_letterboxer.cc (text/x-c++src)
Delete this message
Reply to this message
Author: David Feil-Seifer
Date:  
To: ros-users
Subject: [ros-users] problems with image_proc using a modified camera
I am trying to use image_proc to rectify an image which is just a
camera image that has a border added around. The purpose is to make a
rectified image that includes all of the image (including what does
not strictly fit into a square), not just the centermost part. To do
this I made a new node image_letterboxer (included source) to add the
border and to adjust the camera calibration cx and cy params to
reflect the new center of the image. This has worked when not using
image_proc to do the undistort.

However, when I try to use this node with image_proc, it does not work
at all. I dug a little into the source and found that the image
transport is subscribing to the new image topics correctly, and that
image transport is registering that subscription, but no callbacks are
being called. My image_letterboxer node works well enough so that
image_view can display the new image_raw topic with the letterboxed
image, but image_proc does not work, though it does work on the camera
itself.

I assume that the problem is in the image_letterboxer node, but I
cannot figure out what I did wrong. Is there some flaw in the way that
I'm using image_transport to publish the images? Or is there something
wrong with image_proc?

Thanks in advance,
-Dave
/*
 * Software License Agreement (BSD License)
 *
 *  Copyright (c) 2010. David Feil-Seifer
 *  All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions
 *  are met:
 *
 *   * Redistributions of source code must retain the above copyright
 *     notice, this list of conditions and the following disclaimer.
 *   * Redistributions in binary form must reproduce the above
 *     copyright notice, this list of conditions and the following
 *     disclaimer in the documentation and/or other materials provided
 *     with the distribution.
 *   * Neither the name of Willow Garage, Inc. nor the names of its
 *     contributors may be used to endorse or promote products derived
 *     from this software without specific prior written permission.
 *
 *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
 *  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
 *  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
 *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
 *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
 *  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 *  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
 *  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 *  POSSIBILITY OF SUCH DAMAGE.
 */


#include <ros/ros.h>
#include <sensor_msgs/Image.h>
#include <sensor_msgs/CameraInfo.h>
#include <cv_bridge/CvBridge.h>
#include <opencv/cv.h>
#include <opencv/highgui.h>
#include <opencv/cxtypes.h>
#include <image_transport/image_transport.h>


image_transport::Subscriber sub_;
image_transport::Publisher pub_;
sensor_msgs::ImageConstPtr last_msg_;
sensor_msgs::CvBridge img_bridge_;

ros::Publisher info_pub_;
ros::Subscriber info_sub_;

int border;

void info_cb(const sensor_msgs::CameraInfoConstPtr& msg)
{
    sensor_msgs::CameraInfo newmsg = *msg; 
  newmsg.height += 2*border;
    newmsg.width += 2*border;


double fx = newmsg.P[0] ;
double fy = newmsg.P[5] ;

double cx = newmsg.P[2] + border;
double cy = newmsg.P[6] + border;

    newmsg.P[2] = cx;
    newmsg.P[6] = cy;
  newmsg.K[2] = cx;
  newmsg.K[5] = cy;


    newmsg.P[0] = fx;
    newmsg.P[5] = fy;


    info_pub_.publish(newmsg);
}


void image_cb(const sensor_msgs::ImageConstPtr& msg)
{
  if (msg->encoding.find("bayer") != std::string::npos)
    boost::const_pointer_cast<sensor_msgs::Image>(msg)->encoding = "mono8";


  if (img_bridge_.fromImage(*msg, "bgr8"))
  {
    IplImage *orig, *dest;
    orig = img_bridge_.toIpl();
        CvPoint offset; offset.x = border; offset.y = border;
        dest = cvCreateImage(cvSize(orig->width+border*2,orig->height+border*2), 8, 3 );
        cvZero(dest);
    cvCopyMakeBorder(orig,dest,offset,IPL_BORDER_CONSTANT,cvScalar(0,0,0));
        sensor_msgs::ImagePtr newmsg = sensor_msgs::CvBridge::cvToImgMsg(dest, "bgr8");
        pub_.publish( newmsg );
    cvReleaseImage(&dest);
  }
  else
    ROS_ERROR("Unable to convert %s image to bgr8", msg->encoding.c_str());
}


int main( int argc, char* argv[] )
{
  ros::init(argc, argv, "image_letterboxer" );
  ros::NodeHandle nh;
  nh.param("border", border, 100 );
  image_transport::ImageTransport it(nh);
  sub_ = it.subscribe("image_raw", 1, &image_cb);
  pub_ = it.advertise("letterbox/image_raw", 1);
    info_sub_ = nh.subscribe( "camera_info", 1, &info_cb);
    info_pub_ = nh.advertise<sensor_msgs::CameraInfo>( "letterbox/camera_info", 1 );
  ros::spin();


return 0;
}