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

Support for passing parameters to cv::imencode through "toCompressedImageMsg" function. #521

Open
wants to merge 1 commit into
base: humble
Choose a base branch
from
Open
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
34 changes: 31 additions & 3 deletions cv_bridge/include/cv_bridge/cv_bridge.h
Original file line number Diff line number Diff line change
Expand Up @@ -117,10 +117,24 @@ class CV_BRIDGE_EXPORT CvImage
* can be: jpg, jp2, bmp, png, tif at the moment
* 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)
* params are the options for cv::imencode.
* Default value is empty list.
*/
sensor_msgs::msg::CompressedImage::SharedPtr toCompressedImageMsg(
const Format dst_format =
JPG) const;
const Format dst_format = JPG,
const std::vector<int> &params=std::vector<int>()) const;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There need to be spaces around = to follow the style in the rest of the code. I also suggest using the curly braces initialiser ({}) in place of the explicit constructor for easier readability.

Adding a new parameter to a function breaks the ABI, meaning that the new library can not be replaced without recompiling all packages that link it. For backward compatibility, you have to keep a function with the old signature that calls your new function with an empty params.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the detailed feedback. I'll let a function with the original signature. I thought since the parameter is default it will not alter the other packages i did not thought of compiling.


/**
* dst_format is compress the image to desire format.
* Default value is empty string that will convert to jpg format.
* can be: jpg, jp2, bmp, png, tif at the moment
* 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)
* params are the options for cv::imencode.
* Default value is empty list.
*/
sensor_msgs::msg::CompressedImage::SharedPtr toCompressedImageMsg(
const std::vector<int> &params=std::vector<int>(), const Format dst_format = JPG) const;
Comment on lines +136 to +137

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do you need a second function of the same name with swapped parameter order?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought of this like, may be i want only provide parameters for the cv::imencode. Otherwise the dest_format need to be explicitly specified in each call we want provide params to cv::imencode.


/**
* \brief Copy the message data to a ROS sensor_msgs::msg::Image message.
Expand All @@ -136,11 +150,25 @@ class CV_BRIDGE_EXPORT CvImage
* can be: jpg, jp2, bmp, png, tif at the moment
* 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)
* params are the options for cv::imencode.
* Default value is empty list.
*/
void toCompressedImageMsg(
sensor_msgs::msg::CompressedImage & ros_image,
const Format dst_format = JPG) const;
const Format dst_format = JPG, const std::vector<int> &params=std::vector<int>()) const;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use the curly braces initialiser ({}).


/**
* dst_format is compress the image to desire format.
* Default value is empty string that will convert to jpg format.
* can be: jpg, jp2, bmp, png, tif at the moment
* 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)
* params are the options for cv::imencode.
* Default value is empty list.
*/
void toCompressedImageMsg(
sensor_msgs::msg::CompressedImage & ros_image,
const std::vector<int> &params=std::vector<int>(), const Format dst_format = JPG) const;
Comment on lines +169 to +171

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here too. Why do we need the same function with swapped parameters?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The same reason like the other function. If this is not neccessary i can remove it (swapping the parameters) and only provide cv::imencode params alongside with dst_format.


typedef std::shared_ptr<CvImage> Ptr;
typedef std::shared_ptr<CvImage const> ConstPtr;
Expand Down
21 changes: 17 additions & 4 deletions cv_bridge/src/cv_bridge.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -464,15 +464,21 @@ CvImagePtr cvtColor(

/////////////////////////////////////// CompressedImage ///////////////////////////////////////////

sensor_msgs::msg::CompressedImage::SharedPtr CvImage::toCompressedImageMsg(const Format dst_format)
sensor_msgs::msg::CompressedImage::SharedPtr CvImage::toCompressedImageMsg(const Format dst_format,const std::vector<int> &params)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add a space between the end of a parameter and the start of a new one. See the style in other parts of the code.

const
{
sensor_msgs::msg::CompressedImage::SharedPtr ptr =
std::make_shared<sensor_msgs::msg::CompressedImage>();
toCompressedImageMsg(*ptr, dst_format);
toCompressedImageMsg(*ptr, dst_format, params);
return ptr;
}

sensor_msgs::msg::CompressedImage::SharedPtr CvImage::toCompressedImageMsg(const std::vector<int> &params, const Format dst_format)
const
{
return toCompressedImageMsg(dst_format, params);
}

Comment on lines +476 to +481

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is this function actually doing? It just swaps the parameters.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's only the implementation for providing parameters to cv::imencode without explicitly providing the format.

std::string getFormat(Format format)
{
switch (format) {
Expand Down Expand Up @@ -511,7 +517,14 @@ std::string getFormat(Format format)

void CvImage::toCompressedImageMsg(
sensor_msgs::msg::CompressedImage & ros_image,
const Format dst_format) const
const std::vector<int> &params, const Format dst_format) const
{
toCompressedImageMsg(ros_image, dst_format, params);
}

void CvImage::toCompressedImageMsg(
sensor_msgs::msg::CompressedImage & ros_image,
const Format dst_format, const std::vector<int> &params) const
{
ros_image.header = header;
cv::Mat image;
Expand All @@ -531,7 +544,7 @@ void CvImage::toCompressedImageMsg(

std::string format = getFormat(dst_format);
ros_image.format = format;
cv::imencode("." + format, image, ros_image.data);
cv::imencode("." + format, image, ros_image.data, params);
}

// Deep copy data, returnee is mutable
Expand Down