Skip to content

Commit

Permalink
Add support for alpha pngs by adding per stream type decode functions (
Browse files Browse the repository at this point in the history
…#106)

Co-authored-by: Matthew Bries <[email protected]>
  • Loading branch information
matt-attack and Matthew Bries authored Oct 6, 2024
1 parent d6eaa8f commit 70aa532
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 19 deletions.
1 change: 1 addition & 0 deletions include/web_video_server/image_streamer.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ class ImageTransportImageStreamer : public ImageStreamer
virtual void start();

protected:
virtual cv::Mat decodeImage(const sensor_msgs::ImageConstPtr& msg);
virtual void sendImage(const cv::Mat &, const ros::Time &time) = 0;
virtual void restreamFrame(double max_age);
virtual void initialize(const cv::Mat &);
Expand Down
2 changes: 2 additions & 0 deletions include/web_video_server/png_streamers.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class PngStreamer : public ImageTransportImageStreamer
~PngStreamer();
protected:
virtual void sendImage(const cv::Mat &, const ros::Time &time);
virtual cv::Mat decodeImage(const sensor_msgs::ImageConstPtr& msg);

private:
MultipartStream stream_;
Expand All @@ -42,6 +43,7 @@ class PngSnapshotStreamer : public ImageTransportImageStreamer
~PngSnapshotStreamer();
protected:
virtual void sendImage(const cv::Mat &, const ros::Time &time);
virtual cv::Mat decodeImage(const sensor_msgs::ImageConstPtr& msg);

private:
int quality_;
Expand Down
43 changes: 24 additions & 19 deletions src/image_streamer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,29 @@ void ImageTransportImageStreamer::restreamFrame(double max_age)
}
}

cv::Mat ImageTransportImageStreamer::decodeImage(const sensor_msgs::ImageConstPtr& msg)
{
if (msg->encoding.find("F") != std::string::npos)
{
// scale floating point images
cv::Mat float_image_bridge = cv_bridge::toCvCopy(msg, msg->encoding)->image;
cv::Mat_<float> float_image = float_image_bridge;
double max_val;
cv::minMaxIdx(float_image, 0, &max_val);

if (max_val > 0)
{
float_image *= (255 / max_val);
}
return float_image;
}
else
{
// Convert to OpenCV native BGR color
return cv_bridge::toCvCopy(msg, "bgr8")->image;
}
}

void ImageTransportImageStreamer::imageCallback(const sensor_msgs::ImageConstPtr &msg)
{
if (inactive_)
Expand All @@ -89,25 +112,7 @@ void ImageTransportImageStreamer::imageCallback(const sensor_msgs::ImageConstPtr
cv::Mat img;
try
{
if (msg->encoding.find("F") != std::string::npos)
{
// scale floating point images
cv::Mat float_image_bridge = cv_bridge::toCvCopy(msg, msg->encoding)->image;
cv::Mat_<float> float_image = float_image_bridge;
double max_val;
cv::minMaxIdx(float_image, 0, &max_val);

if (max_val > 0)
{
float_image *= (255 / max_val);
}
img = float_image;
}
else
{
// Convert to OpenCV native BGR color
img = cv_bridge::toCvCopy(msg, "bgr8")->image;
}
img = decodeImage(msg);

int input_width = img.cols;
int input_height = img.rows;
Expand Down
31 changes: 31 additions & 0 deletions src/png_streamers.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#include "web_video_server/png_streamers.h"
#include "async_web_server_cpp/http_reply.hpp"

#include <cv_bridge/cv_bridge.h>

namespace web_video_server
{

Expand All @@ -18,6 +20,21 @@ PngStreamer::~PngStreamer()
boost::mutex::scoped_lock lock(send_mutex_); // protects sendImage.
}

cv::Mat PngStreamer::decodeImage(const sensor_msgs::ImageConstPtr& msg)
{
// Handle alpha values since PNG supports it
if (sensor_msgs::image_encodings::hasAlpha(msg->encoding))
{
return cv_bridge::toCvCopy(msg, "bgra8")->image;
}
else
{
// Use the normal decode otherwise
return ImageTransportImageStreamer::decodeImage(msg);
}
}


void PngStreamer::sendImage(const cv::Mat &img, const ros::Time &time)
{
std::vector<int> encode_params;
Expand Down Expand Up @@ -64,6 +81,20 @@ PngSnapshotStreamer::~PngSnapshotStreamer()
boost::mutex::scoped_lock lock(send_mutex_); // protects sendImage.
}

cv::Mat PngSnapshotStreamer::decodeImage(const sensor_msgs::ImageConstPtr& msg)
{
// Handle alpha values since PNG supports it
if (sensor_msgs::image_encodings::hasAlpha(msg->encoding))
{
return cv_bridge::toCvCopy(msg, "bgra8")->image;
}
else
{
// Use the normal decode otherwise
return ImageTransportImageStreamer::decodeImage(msg);
}
}

void PngSnapshotStreamer::sendImage(const cv::Mat &img, const ros::Time &time)
{
std::vector<int> encode_params;
Expand Down

0 comments on commit 70aa532

Please sign in to comment.