IDS Peak comfortSDK, genericSDK, IPL, and AFL developer manuals are external documents. Please contact us if you need them.
Saving an image with a type specification in file extension
To save an image, you must pass the image itself as a parameter in the path to the output file. The function recognizes which image type it is based on the specified file extension. The following image types are possible:
File extension
|
Standard quality
|
JPG
|
75 %
|
PNG
|
100 %
|
BMP
|
always 100 %, no compression
|
The following call uses the default quality parameters. The higher the compression for PNG and JPG, the lower the quality. A BMP (bitmap) file is not compressed.
genericC++
|
std::string file = "c:/entwicklung/test.jpg";
peak::ipl::ImageWriter::Write(file, image);
|
Saving a special image type with quality specification
There are additional functions for all image types. You can also specify the quality for PNG and JPG. The file extension must match the image type, otherwise the function returns an error. Additionally, you can save an image as a RAW image.
genericC++
|
std::string file = "c:/entwicklung/test.bmp";
peak::ipl::ImageWriter::WriteAsBMP(file, image);
|
genericC++
|
std::string file = "c:/entwicklung/test.jpg";
// Default Quality
peak::ipl::ImageWriter::WriteAsJPG(file, image);
// Quality set to 50%
peak::ipl::ImageWriter::JPEGParameter jpgParams;
jpgParams.Quality = 50;
peak::ipl::ImageWriter::WriteAsJPG(file, image, jpgParams);
|
genericC++
|
std::string file = "c:/entwicklung/test.png";
// Default Quality
peak::ipl::ImageWriter::WriteAsPNG(file, image);
// Quality set to 50%
peak::ipl::ImageWriter::PNGParameter pngParams;
pngParams.Quality = 50;
peak::ipl::ImageWriter::WriteAsPNG(file, image, pngParams);
|
genericC++
|
std::string file = "c:/entwicklung/test";
peak::ipl::ImageWriter::WriteAsRAW(file, image);
|
Complete example: Image acquisition loop with image saving
int index = 0;
while (m_running)
{
try
{
// Get buffer from device's DataStream. Wait 5000 ms. The buffer is automatically locked until it is queued again.
const auto buffer = m_dataStream->WaitForFinishedBuffer(5000);
// Create IDS peak IPL image from buffer
const auto image = peak::BufferTo<peak::ipl::Image>(buffer);
// Create IDS peak IPL image for debayering and convert it to RGBa8 format
auto imageProcessed = image.ConvertTo(peak::ipl::PixelFormatName::BGRa8, peak::ipl::ConversionMode::Fast);
// Queue buffer again
m_dataStream->QueueBuffer(buffer);
std::string file = "c:/entwicklung/test_" + index + ".jpg";
peak::ipl::ImageWriter::Write(file, imageProcessed);
++index;
}
catch (const std::exception& e)
{
// ...
}
}
|
int index = 0;
while (m_running)
{
try
{
// Get buffer from device's DataStream. Wait 5000 ms. The buffer is automatically locked until it is queued again.
var buffer = m_dataStream.WaitForFinishedBuffer(5000);
// Create IDS peak IPL image from buffer
var image = new peak.ipl.Image((peak.ipl.PixelFormatName)buffer.PixelFormat(), buffer.BasePtr(),
buffer.Size(), buffer.Width(), buffer.Height(), buffer.Timestamp_ns());
// Create IDS peak IPL image for debayering and convert it to RGBa8 format
var imageProcessed = image.ConvertTo(peak.ipl.PixelFormatName.BGRa8, peak.ipl.ConversionMode.Fast);
// Queue buffer again
m_dataStream.QueueBuffer(buffer);
var file = "c:/entwicklung/test_" + index + ".jpg";
peak.ipl.ImageWriter.Write(file, imageProcessed);
++index;
}
catch (Exception e)
{
// ...
}
}
|
index = 0
while m_running:
try:
# Get buffer from device's DataStream. Wait 5000 ms. The buffer is automatically locked until it is queued again.
buffer = m_data_stream.WaitForFinishedBuffer(5000)
# Create IDS peak IPL image from buffer
image = ids_peak_ipl.Image.CreateFromSizeAndBuffer(
buffer.PixelFormat(),
buffer.BasePtr(),
buffer.Size(),
buffer.Width(),
buffer.Height()
)
# Create IDS peak IPL image for debayering and convert it to RGBa8 format
image_processed = image.ConvertTo(ids_peak_ipl.PixelFormatName_BGRa8, ids_peak_ipl.ConversionMode_Fast)
# Queue buffer again
m_data_stream.QueueBuffer(buffer)
file = "c:/entwicklung/test_" + index + ".jpg"
ids_peak_ipl.ImageWriter.Write(file, image_processed)
index = index + 1
except Exception as e:
# ...
str_error = str(e)
|
Example: Saving an image and loading it afterwards
An image file can be loaded into a peak::ipl::Image. When specifying the pixel format, the peak::ipl::Image must match the image file exactly, otherwise the call will fail.
genericC++
|
try
{
QImage qImage(m_imageWidth, m_imageHeight, QImage::Format_RGB32);
// Get buffer from device's DataStream
const auto buffer = m_dataStream->WaitForFinishedBuffer(5000);
// Create IDS peak IPL image for debayering and convert it to BGRa8 format
auto image = peak::BufferTo<peak::ipl::Image>(buffer).ConvertTo(
peak::ipl::PixelFormatName::BGRa8, qImage.bits(), static_cast<size_t>(qImage.byteCount()));
// Queue buffer so that it can be used again
m_dataStream->QueueBuffer(buffer);
// Save as BMP
std::string file = "c:/entwicklung/test.bmp";
peak::ipl::ImageWriter::Write(file, image);
// Load from BMP file and create new image
auto image2 = peak::ipl::ImageReader::Read(file, peak::ipl::PixelFormatName::BGRa8);
// Copy into QImage
memcpy(qImage.bits(), image2.Data(), image2.ByteCount());
}
catch (const std::exception& e)
{
// ...
}
|