Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add initial porting of ros2 cv_bridge #212

Merged
merged 1 commit into from
Apr 16, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 36 additions & 18 deletions cv_bridge/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,16 @@
cmake_minimum_required(VERSION 2.8)
cmake_minimum_required(VERSION 3.5)
project(cv_bridge)

find_package(catkin REQUIRED COMPONENTS rosconsole sensor_msgs)
find_package(ament_cmake_ros REQUIRED)

# Default to C++14
if(NOT CMAKE_CXX_STANDARD)
set(CMAKE_CXX_STANDARD 14)
endif()

if(CMAKE_COMPILER_IS_GNUCXX OR CMAKE_CXX_COMPILER_ID MATCHES "Clang")
add_compile_options(-Wall -Wextra)
endif()

if(NOT ANDROID)
find_package(PythonLibs)
Expand All @@ -11,38 +20,47 @@ if(NOT ANDROID)
find_package(Boost REQUIRED python3)
endif()
else()
find_package(Boost REQUIRED)
find_package(Boost REQUIRED )
endif()
find_package(OpenCV 3 REQUIRED

find_package(sensor_msgs REQUIRED)
find_package(OpenCV REQUIRED
COMPONENTS
opencv_core
opencv_imgproc
opencv_imgcodecs
CONFIG
)

catkin_package(
INCLUDE_DIRS include
LIBRARIES ${PROJECT_NAME}
CATKIN_DEPENDS rosconsole sensor_msgs
DEPENDS OpenCV
CFG_EXTRAS cv_bridge-extras.cmake
)

catkin_python_setup()

include_directories(include ${Boost_INCLUDE_DIRS} ${OpenCV_INCLUDE_DIRS} ${catkin_INCLUDE_DIRS})
include_directories(include)

if(NOT ANDROID)
add_subdirectory(python)
add_subdirectory(python)
endif()

add_subdirectory(src)
if(CATKIN_ENABLE_TESTING)

# cv_bridge_lib_dir is passed as APPEND_LIBRARY_DIRS for each ament_add_gtest call so
# the project library that they link against is on the library path.
# This is especially important on Windows.
# This is overwritten each loop, but which one it points to doesn't really matter.
set(cv_bridge_lib_dir "$<TARGET_FILE_DIR:${PROJECT_NAME}>")

if(BUILD_TESTING)
add_subdirectory(test)
endif()

ament_export_dependencies(OpenCV)

ament_export_include_directories(include)
ament_export_libraries(${PROJECT_NAME})

# install the include folder
install(
DIRECTORY include/${PROJECT_NAME}/
DESTINATION ${CATKIN_PACKAGE_INCLUDE_DESTINATION}
DESTINATION include/${PROJECT_NAME}
)

ament_package(
CONFIG_EXTRAS "cmake/cv_bridge-extras.cmake.in"
)
105 changes: 53 additions & 52 deletions cv_bridge/include/cv_bridge/cv_bridge.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,15 @@
#ifndef CV_BRIDGE_CV_BRIDGE_H
#define CV_BRIDGE_CV_BRIDGE_H

#include <sensor_msgs/Image.h>
#include <sensor_msgs/CompressedImage.h>
#include <sensor_msgs/image_encodings.h>
#include <ros/static_assert.h>
#include <sensor_msgs/msg/image.hpp>
#include <sensor_msgs/msg/compressed_image.hpp>
#include <sensor_msgs/image_encodings.hpp>
#include <opencv2/core/core.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/imgproc/types_c.h>
#include <stdexcept>


namespace cv_bridge {

class Exception : public std::runtime_error
Expand All @@ -55,8 +55,8 @@ class Exception : public std::runtime_error

class CvImage;

typedef boost::shared_ptr<CvImage> CvImagePtr;
typedef boost::shared_ptr<CvImage const> CvImageConstPtr;
typedef std::shared_ptr<CvImage> CvImagePtr;
typedef std::shared_ptr<CvImage const> CvImageConstPtr;

//from: http://docs.opencv.org/modules/highgui/doc/reading_and_writing_images_and_video.html#Mat imread(const string& filename, int flags)
typedef enum {
Expand All @@ -76,7 +76,7 @@ typedef enum {
class CvImage
{
public:
std_msgs::Header header; //!< ROS header
std_msgs::msg::Header header; //!< ROS header
std::string encoding; //!< Image encoding ("mono8", "bgr8", etc.)
cv::Mat image; //!< Image data for use with OpenCV

Expand All @@ -88,18 +88,18 @@ class CvImage
/**
* \brief Constructor.
*/
CvImage(const std_msgs::Header& header, const std::string& encoding,
CvImage(const std_msgs::msg::Header& header, const std::string& encoding,
const cv::Mat& image = cv::Mat())
: header(header), encoding(encoding), image(image)
{
}

/**
* \brief Convert this message to a ROS sensor_msgs::Image message.
* \brief Convert this message to a ROS sensor_msgs::msg::Image message.
*
* The returned sensor_msgs::Image message contains a copy of the image data.
* The returned sensor_msgs::msg::Image message contains a copy of the image data.
*/
sensor_msgs::ImagePtr toImageMsg() const;
sensor_msgs::msg::Image::SharedPtr toImageMsg() const;

/**
* dst_format is compress the image to desire format.
Expand All @@ -108,15 +108,15 @@ class CvImage
* support this format from opencv:
* http://docs.opencv.org/modules/highgui/doc/reading_and_writing_images_and_video.html#Mat imread(const string& filename, int flags)
*/
sensor_msgs::CompressedImagePtr toCompressedImageMsg(const Format dst_format = JPG) const;
sensor_msgs::msg::CompressedImage::SharedPtr toCompressedImageMsg(const Format dst_format = JPG) const;

/**
* \brief Copy the message data to a ROS sensor_msgs::Image message.
* \brief Copy the message data to a ROS sensor_msgs::msg::Image message.
*
* This overload is intended mainly for aggregate messages such as stereo_msgs::DisparityImage,
* which contains a sensor_msgs::Image as a data member.
* which contains a sensor_msgs::msg::Image as a data member.
*/
void toImageMsg(sensor_msgs::Image& ros_image) const;
void toImageMsg(sensor_msgs::msg::Image& ros_image) const;

/**
* dst_format is compress the image to desire format.
Expand All @@ -125,29 +125,29 @@ class CvImage
* support this format from opencv:
* http://docs.opencv.org/modules/highgui/doc/reading_and_writing_images_and_video.html#Mat imread(const string& filename, int flags)
*/
void toCompressedImageMsg(sensor_msgs::CompressedImage& ros_image, const Format dst_format = JPG) const;
void toCompressedImageMsg(sensor_msgs::msg::CompressedImage& ros_image, const Format dst_format = JPG) const;


typedef boost::shared_ptr<CvImage> Ptr;
typedef boost::shared_ptr<CvImage const> ConstPtr;
typedef std::shared_ptr<CvImage> Ptr;
typedef std::shared_ptr<CvImage const> ConstPtr;

protected:
boost::shared_ptr<void const> tracked_object_; // for sharing ownership
std::shared_ptr<void const> tracked_object_; // for sharing ownership

/// @cond DOXYGEN_IGNORE
friend
CvImageConstPtr toCvShare(const sensor_msgs::Image& source,
const boost::shared_ptr<void const>& tracked_object,
CvImageConstPtr toCvShare(const sensor_msgs::msg::Image& source,
const std::shared_ptr<void const>& tracked_object,
const std::string& encoding);
/// @endcond
};


/**
* \brief Convert a sensor_msgs::Image message to an OpenCV-compatible CvImage, copying the
* \brief Convert a sensor_msgs::msg::Image message to an OpenCV-compatible CvImage, copying the
* image data.
*
* \param source A shared_ptr to a sensor_msgs::Image message
* \param source A shared_ptr to a sensor_msgs::msg::Image message
* \param encoding The desired encoding of the image data, one of the following strings:
* - \c "mono8"
* - \c "bgr8"
Expand All @@ -159,17 +159,17 @@ class CvImage
* If \a encoding is the empty string (the default), the returned CvImage has the same encoding
* as \a source.
*/
CvImagePtr toCvCopy(const sensor_msgs::ImageConstPtr& source,
CvImagePtr toCvCopy(const sensor_msgs::msg::Image::ConstSharedPtr& source,
const std::string& encoding = std::string());

CvImagePtr toCvCopy(const sensor_msgs::CompressedImageConstPtr& source,
CvImagePtr toCvCopy(const sensor_msgs::msg::CompressedImage::ConstSharedPtr& source,
const std::string& encoding = std::string());

/**
* \brief Convert a sensor_msgs::Image message to an OpenCV-compatible CvImage, copying the
* \brief Convert a sensor_msgs::msg::Image message to an OpenCV-compatible CvImage, copying the
* image data.
*
* \param source A sensor_msgs::Image message
* \param source A sensor_msgs::msg::Image message
* \param encoding The desired encoding of the image data, one of the following strings:
* - \c "mono8"
* - \c "bgr8"
Expand All @@ -184,21 +184,21 @@ CvImagePtr toCvCopy(const sensor_msgs::CompressedImageConstPtr& source,
* 255/65535 respectively). Otherwise, no scaling is applied and the rules from the convertTo OpenCV
* function are applied (capping): http://docs.opencv.org/modules/core/doc/basic_structures.html#mat-convertto
*/
CvImagePtr toCvCopy(const sensor_msgs::Image& source,
CvImagePtr toCvCopy(const sensor_msgs::msg::Image& source,
const std::string& encoding = std::string());

CvImagePtr toCvCopy(const sensor_msgs::CompressedImage& source,
CvImagePtr toCvCopy(const sensor_msgs::msg::CompressedImage& source,
const std::string& encoding = std::string());

/**
* \brief Convert an immutable sensor_msgs::Image message to an OpenCV-compatible CvImage, sharing
* \brief Convert an immutable sensor_msgs::msg::Image message to an OpenCV-compatible CvImage, sharing
* the image data if possible.
*
* If the source encoding and desired encoding are the same, the returned CvImage will share
* the image data with \a source without copying it. The returned CvImage cannot be modified, as that
* could modify the \a source data.
*
* \param source A shared_ptr to a sensor_msgs::Image message
* \param source A shared_ptr to a sensor_msgs::msg::Image message
* \param encoding The desired encoding of the image data, one of the following strings:
* - \c "mono8"
* - \c "bgr8"
Expand All @@ -210,22 +210,22 @@ CvImagePtr toCvCopy(const sensor_msgs::CompressedImage& source,
* If \a encoding is the empty string (the default), the returned CvImage has the same encoding
* as \a source.
*/
CvImageConstPtr toCvShare(const sensor_msgs::ImageConstPtr& source,
CvImageConstPtr toCvShare(const sensor_msgs::msg::Image::ConstSharedPtr& source,
const std::string& encoding = std::string());

/**
* \brief Convert an immutable sensor_msgs::Image message to an OpenCV-compatible CvImage, sharing
* \brief Convert an immutable sensor_msgs::msg::Image message to an OpenCV-compatible CvImage, sharing
* the image data if possible.
*
* If the source encoding and desired encoding are the same, the returned CvImage will share
* the image data with \a source without copying it. The returned CvImage cannot be modified, as that
* could modify the \a source data.
*
* This overload is useful when you have a shared_ptr to a message that contains a
* sensor_msgs::Image, and wish to share ownership with the containing message.
* sensor_msgs::msg::Image, and wish to share ownership with the containing message.
*
* \param source The sensor_msgs::Image message
* \param tracked_object A shared_ptr to an object owning the sensor_msgs::Image
* \param source The sensor_msgs::msg::Image message
* \param tracked_object A shared_ptr to an object owning the sensor_msgs::msg::Image
* \param encoding The desired encoding of the image data, one of the following strings:
* - \c "mono8"
* - \c "bgr8"
Expand All @@ -237,8 +237,8 @@ CvImageConstPtr toCvShare(const sensor_msgs::ImageConstPtr& source,
* If \a encoding is the empty string (the default), the returned CvImage has the same encoding
* as \a source.
*/
CvImageConstPtr toCvShare(const sensor_msgs::Image& source,
const boost::shared_ptr<void const>& tracked_object,
CvImageConstPtr toCvShare(const sensor_msgs::msg::Image& source,
const std::shared_ptr<void const>& tracked_object,
const std::string& encoding = std::string());

/**
Expand All @@ -263,27 +263,27 @@ struct CvtColorForDisplayOptions {


/**
* \brief Converts an immutable sensor_msgs::Image message to another CvImage for display purposes,
* \brief Converts an immutable sensor_msgs::msg::Image message to another CvImage for display purposes,
* using practical conversion rules if needed.
*
* Data will be shared between input and output if possible.
*
* Recall: sensor_msgs::image_encodings::isColor and isMono tell whether an image contains R,G,B,A, mono
* Recall: sensor_msgs::msg::image_encodings::isColor and isMono tell whether an image contains R,G,B,A, mono
* (or any combination/subset) with 8 or 16 bit depth.
*
* The following rules apply:
* - if the output encoding is empty, the fact that the input image is mono or multiple-channel is
* preserved in the ouput image. The bit depth will be 8. it tries to convert to BGR no matter what
* encoding image is passed.
* - if the output encoding is not empty, it must have sensor_msgs::image_encodings::isColor and
* - if the output encoding is not empty, it must have sensor_msgs::msg::image_encodings::isColor and
* isMono return true. It must also be 8 bit in depth
* - if the input encoding is an OpenCV format (e.g. 8UC1), and if we have 1,3 or 4 channels, it is
* respectively converted to mono, BGR or BGRA.
* - if the input encoding is 32SC1, this estimate that image as label image and will convert it as
* bgr image with different colors for each label.
*
* \param source A shared_ptr to a sensor_msgs::Image message
* \param encoding Either an encoding string that returns true in sensor_msgs::image_encodings::isColor
* \param source A shared_ptr to a sensor_msgs::msg::Image message
* \param encoding Either an encoding string that returns true in sensor_msgs::msg::image_encodings::isColor
* isMono or the empty string as explained above.
* \param options (cv_bridge::CvtColorForDisplayOptions) Options to convert the source image with.
* - do_dynamic_scaling If true, the image is dynamically scaled between its minimum and maximum value
Expand All @@ -306,7 +306,7 @@ int getCvType(const std::string& encoding);

} // namespace cv_bridge


#if 0
// CvImage as a first class message type

// The rest of this file hooks into the roscpp serialization API to make CvImage
Expand All @@ -322,26 +322,26 @@ namespace message_traits {

template<> struct MD5Sum<cv_bridge::CvImage>
{
static const char* value() { return MD5Sum<sensor_msgs::Image>::value(); }
static const char* value() { return MD5Sum<sensor_msgs::msg::Image>::value(); }
static const char* value(const cv_bridge::CvImage&) { return value(); }

static const uint64_t static_value1 = MD5Sum<sensor_msgs::Image>::static_value1;
static const uint64_t static_value2 = MD5Sum<sensor_msgs::Image>::static_value2;
static const uint64_t static_value1 = MD5Sum<sensor_msgs::msg::Image>::static_value1;
static const uint64_t static_value2 = MD5Sum<sensor_msgs::msg::Image>::static_value2;

// If the definition of sensor_msgs/Image changes, we'll get a compile error here.
ROS_STATIC_ASSERT(MD5Sum<sensor_msgs::Image>::static_value1 == 0x060021388200f6f0ULL);
ROS_STATIC_ASSERT(MD5Sum<sensor_msgs::Image>::static_value2 == 0xf447d0fcd9c64743ULL);
ROS_STATIC_ASSERT(MD5Sum<sensor_msgs::msg::Image>::static_value1 == 0x060021388200f6f0ULL);
ROS_STATIC_ASSERT(MD5Sum<sensor_msgs::msg::Image>::static_value2 == 0xf447d0fcd9c64743ULL);
};

template<> struct DataType<cv_bridge::CvImage>
{
static const char* value() { return DataType<sensor_msgs::Image>::value(); }
static const char* value() { return DataType<sensor_msgs::msg::Image>::value(); }
static const char* value(const cv_bridge::CvImage&) { return value(); }
};

template<> struct Definition<cv_bridge::CvImage>
{
static const char* value() { return Definition<sensor_msgs::Image>::value(); }
static const char* value() { return Definition<sensor_msgs::msg::Image>::value(); }
static const char* value(const cv_bridge::CvImage&) { return value(); }
};

Expand Down Expand Up @@ -406,7 +406,7 @@ template<> struct Printer<cv_bridge::CvImage>
template<typename Stream>
static void stream(Stream&, const std::string&, const cv_bridge::CvImage&)
{
/// @todo Replicate printing for sensor_msgs::Image
/// @todo Replicate printing for sensor_msgs::msg::Image
}
};

Expand All @@ -423,6 +423,7 @@ inline std::ostream& operator<<(std::ostream& s, const CvImage& m)
}

} // namespace cv_bridge
#endif

/// @endcond

Expand Down
Loading