Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,6 @@ namespace image_compression
{
class ImageHandler
{
protected:

;

public:

ImageHandler() {}
Expand Down
34 changes: 30 additions & 4 deletions opportunistic_link/src/opportunistic_link/image_compression.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,15 @@ void ImageHandler::reset_decoder()
sensor_msgs::Image ImageHandler::decompress_image(const sensor_msgs::CompressedImage& compressed, const std::string& encoding)
{
sensor_msgs::Image decompressed;
cv::Mat decoded = cv::imdecode(compressed.data, CV_LOAD_IMAGE_ANYCOLOR);
cv::Mat decoded = cv::imdecode(compressed.data, CV_LOAD_IMAGE_UNCHANGED);
if(decoded.data == NULL)
{
throw std::runtime_error("OpenCV image decoding failed");
}
else
{
ROS_DEBUG_STREAM("Decoding successful, rows=" << decoded.rows << ", cols=" << decoded.cols << ", opencv type=" << decoded.type());
}
cv_bridge::CvImage converted;
converted = cv_bridge::CvImage(compressed.header, encoding, decoded);
converted.toImageMsg(decompressed);
Expand All @@ -37,9 +45,27 @@ sensor_msgs::CompressedImage ImageHandler::compress_image(const sensor_msgs::Ima
cv_ptr = cv_bridge::toCvCopy(image);
cv::Mat cv_image = cv_ptr->image;
std::vector<int> encoding_params;
encoding_params.push_back(CV_IMWRITE_JPEG_QUALITY);
encoding_params.push_back(quality);
bool ret = cv::imencode(".jpg", cv_image, compressed.data, encoding_params);
bool ret = false;
ROS_DEBUG_STREAM("Image encoding: " << image.encoding);
if(image.encoding == "16UC1") {
ROS_DEBUG("Encoding in png format");
ret = cv::imencode(".png", cv_image, compressed.data, encoding_params);
compressed.format = "png";
}
else if(image.encoding == "32FC1")
{
ROS_DEBUG("Encoding in tiff format");
ret = cv::imencode(".tiff", cv_image, compressed.data, encoding_params);
compressed.format = "tiff";
}
else
{
ROS_DEBUG("Encoding in jpeg format");
encoding_params.push_back(CV_IMWRITE_JPEG_QUALITY);
encoding_params.push_back(quality);
ret = cv::imencode(".jpg", cv_image, compressed.data, encoding_params);
compressed.format = "jpeg";
}
if (ret)
{
ROS_DEBUG("OpenCV encoding success");
Expand Down