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

Visualize single channel 8 bit image data #176

Merged
merged 2 commits into from
Feb 9, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
57 changes: 57 additions & 0 deletions src/plugins/image_display/ImageDisplay.cc
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,9 @@ void ImageDisplay::ProcessImage()
case msgs::PixelFormatType::L_INT16:
this->UpdateFromLInt16();
break;
case msgs::PixelFormatType::L_INT8:
this->UpdateFromLInt8();
break;
default:
{
ignwarn << "Unsupported image type: "
Expand Down Expand Up @@ -339,6 +342,60 @@ void ImageDisplay::UpdateFromLInt16()
delete[] buffer;
}

/////////////////////////////////////////////////
void ImageDisplay::UpdateFromLInt8()
adlarkin marked this conversation as resolved.
Show resolved Hide resolved
{
unsigned int height = this->dataPtr->imageMsg.height();
unsigned int width = this->dataPtr->imageMsg.width();
QImage::Format qFormat = QImage::Format_RGB888;

QImage image = QImage(width, height, qFormat);

unsigned int samples = width * height;
unsigned char type;
// cppchecker recommends using sizeof(varname)
unsigned int bufferSize = samples * sizeof(type);

unsigned char *buffer = new unsigned char[samples];
memcpy(buffer, this->dataPtr->imageMsg.data().c_str(),
bufferSize);

// get min and max of temperature values
unsigned int min = 255;
unsigned int max = 0;
for (unsigned int i = 0; i < samples; ++i)
{
unsigned int temp = static_cast<unsigned int>(buffer[i]);
if (temp > max)
max = temp;
if (temp < min)
min = temp;
}

// convert temperature to grayscale image
double range = static_cast<double>(max - min);
if (ignition::math::equal(range, 0.0))
range = 1.0;
unsigned int idx = 0;
for (unsigned int j = 0; j < height; ++j)
{
for (unsigned int i = 0; i < width; ++i)
{
unsigned int temp = static_cast<unsigned int>(buffer[idx++]);
double t = static_cast<double>(temp-min) / range;
int r = 255*t;
int g = r;
int b = r;
QRgb value = qRgb(r, g, b);
image.setPixel(i, j, value);
}
}
this->dataPtr->provider->SetImage(image);
this->newImage();

delete[] buffer;
}

/////////////////////////////////////////////////
QStringList ImageDisplay::TopicList() const
{
Expand Down
3 changes: 3 additions & 0 deletions src/plugins/image_display/ImageDisplay.hh
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,9 @@ namespace plugins
/// \brief Update from L_INT16
private: void UpdateFromLInt16();

/// \brief Update from L_IN8T
private: void UpdateFromLInt8();
adlarkin marked this conversation as resolved.
Show resolved Hide resolved

/// \brief Subscriber callback when new image is received
/// \param[in] _msg New image
private: void OnImageMsg(const ignition::msgs::Image &_msg);
Expand Down