Common Vision Blox 15.0
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Friends Modules Pages
Image Manager/Cvb++/CppPixelFormatConversion

This example program is located in your CVB installation under %CVB%Tutorial/Image Manager/Cvb++/CppPixelFormatConversion.

main.cpp:

// Demonstrates how an arbitrary PFNC-compliant image can be converted to another pixel format.
// ----------------------------------------------------------------------------
// ----------------------------------------------------------------------------
#include <cvb/global.hpp>
#include <cvb/image.hpp>
#include <cvb/pixel_format_converter.hpp>
#include <iostream>
#include <utility>
std::pair<Cvb::ImagePtr, Cvb::PfncFormat> GetImage(const Cvb::String &fileName)
{
// Create a pair of an image and its pixel format.
const auto pixelFormat = Cvb::Pfnc::PfncFormat::BayerGB8;
return std::make_pair(Cvb::Image::Load(fileName), pixelFormat);
}
Cvb::ImagePtr GetAnotherRepresentation(const Cvb::ImagePtr &srcImage, const Cvb::PixelFormatConverterPtr &converter)
{
// Create a PFNC-compliant image that will hold the converted image data.
auto dstImage = Cvb::Image::FromPixelFormat(srcImage->Size(), converter->DestinationFormat());
// Convert the source data by following the configuration; it will overwrite
// the destination image object with the converted image data.
converter->Execute(*srcImage, *dstImage);
return dstImage;
}
Cvb::String ToString(Cvb::PfncFormat pixelFormat)
{
return Cvb::PfncFormatValue::ToString(pixelFormat);
}
void Save(const Cvb::Image &image, const Cvb::String path)
{
image.Save(path);
std::cout << " Saved as: " << path << std::endl;
}
int main()
{
// Objective: Getting another pixel format representation of an image.
try
{
std::cout << "As a source, a Bayer pattern image is given." << std::endl;
const auto baseName = Cvb::String("FruitBowl");
const auto extension = Cvb::String(".bmp");
auto srcImageFormatPair =
GetImage(Cvb::InstallPath() + Cvb::String("tutorial/") + baseName + Cvb::String("Bayer") + extension);
Save(*srcImageFormatPair.first, baseName + ToString(srcImageFormatPair.second) + extension);
std::cout << "As the destination, an RGB format is given." << std::endl;
const Cvb::PfncFormat dstFormat = Cvb::PfncFormat::RGB8;
std::cout << "Create a pixel format converter." << std::endl;
auto converter = std::make_shared<Cvb::PixelFormatConverter>(srcImageFormatPair.second, dstFormat);
std::cout << "Convert the source to another." << std::endl;
auto dstImage = GetAnotherRepresentation(srcImageFormatPair.first, converter);
std::cout << "Saving the converted image as a file." << std::endl;
Save(*dstImage, baseName + ToString(dstFormat) + extension);
}
catch (const std::exception &error)
{
std::cout << error.what() << std::endl;
}
return 0;
}
static std::unique_ptr< Image > FromPixelFormat(const Cvb::Size2D< int > &size, Cvb::PfncFormat format)
static std::unique_ptr< Image > Load(const String &fileName)
std::string String
std::shared_ptr< Image > ImagePtr
std::shared_ptr< PixelFormatConverter > PixelFormatConverterPtr
String InstallPath()