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

incorrect 16bit PNG compression #383

Open
christian-rauch opened this issue Apr 19, 2021 · 0 comments
Open

incorrect 16bit PNG compression #383

christian-rauch opened this issue Apr 19, 2021 · 0 comments

Comments

@christian-rauch
Copy link
Contributor

christian-rauch commented Apr 19, 2021

When a 16bit PNG image is compressed via toCompressedImageMsg the resulting data is not in 16bit format anymore. This can be easily verified by decoding the raw data back to a cv Image:

// original image
cv::Mat depth_mm = cv::Mat(..., CV_16UC1);
{
std::cout << "orig type: " << depth_mm.type() << std::endl;
double min, max;
cv::minMaxLoc(depth_mm, &min, &max);
std::cout << "min/max: " << min << "/" << max << std::endl;
}

// encode / decode
sensor_msgs::CompressedImage::Ptr msg_depth = cv_bridge::CvImage(hdr, "mono16", depth_mm).toCompressedImageMsg(cv_bridge::PNG);
cv::Mat img = cv::imdecode(msg_depth->data, cv::IMREAD_UNCHANGED);
{
std::cout << "DEC type: " << img.type() << std::endl;
double min, max;
cv::minMaxLoc(img, &min, &max);
std::cout << "DEC min/max: " << min << "/" << max << std::endl;
}

This will get me something like:

orig type: 2
min/max: 0/2308
DEC type: 16
DEC min/max: 0/9

The datatype do not match anymore and the original 16 bit value cannot be represented anymore.

A workaround is to manually compress the image:

sensor_msgs::CompressedImage msg_depth;
cv::imencode(".png", depth_mm, msg_depth.data);
msg_depth.format = "16UC1; png compressed ";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant