CVB++ 15.0
Cvb/CppPixelFormatConversion
1// ----------------------------------------------------------------------------
4// ----------------------------------------------------------------------------
5
6#include <cvb/global.hpp>
7#include <cvb/image.hpp>
8#include <cvb/pixel_format_converter.hpp>
9
10#include <iostream>
11#include <utility>
12
14{
15 // Create a pair of an image and its pixel format.
16 const auto pixelFormat = Cvb::Pfnc::PfncFormat::BayerGB8;
17 return std::make_pair(Cvb::Image::Load(fileName), pixelFormat);
18}
19
20Cvb::ImagePtr GetAnotherRepresentation(const Cvb::ImagePtr &srcImage, const Cvb::PixelFormatConverterPtr &converter)
21{
22 // Create a PFNC-compliant image that will hold the converted image data.
23 auto dstImage = Cvb::Image::FromPixelFormat(srcImage->Size(), converter->DestinationFormat());
24
25 // Convert the source data by following the configuration; it will overwrite
26 // the destination image object with the converted image data.
27 converter->Execute(*srcImage, *dstImage);
28
29 return dstImage;
30}
31
32Cvb::String ToString(Cvb::PfncFormat pixelFormat)
33{
34 return Cvb::PfncFormatValue::ToString(pixelFormat);
35}
36
37void Save(const Cvb::Image &image, const Cvb::String path)
38{
39 image.Save(path);
40 std::cout << " Saved as: " << path << std::endl;
41}
42
43int main()
44{
45 // Objective: Getting another pixel format representation of an image.
46 try
47 {
48 std::cout << "As a source, a Bayer pattern image is given." << std::endl;
49 const auto baseName = Cvb::String("FruitBowl");
50 const auto extension = Cvb::String(".bmp");
51 auto srcImageFormatPair =
52 GetImage(Cvb::InstallPath() + Cvb::String("tutorial/") + baseName + Cvb::String("Bayer") + extension);
53 Save(*srcImageFormatPair.first, baseName + ToString(srcImageFormatPair.second) + extension);
54
55 std::cout << "As the destination, an RGB format is given." << std::endl;
56 const Cvb::PfncFormat dstFormat = Cvb::PfncFormat::RGB8;
57
58 std::cout << "Create a pixel format converter." << std::endl;
59 auto converter = std::make_shared<Cvb::PixelFormatConverter>(srcImageFormatPair.second, dstFormat);
60
61 std::cout << "Convert the source to another." << std::endl;
62 auto dstImage = GetAnotherRepresentation(srcImageFormatPair.first, converter);
63
64 std::cout << "Saving the converted image as a file." << std::endl;
65 Save(*dstImage, baseName + ToString(dstFormat) + extension);
66 }
67 catch (const std::exception &error)
68 {
69 std::cout << error.what() << std::endl;
70 }
71 return 0;
72}
The Common Vision Blox image.
Definition: decl_image.hpp:45
static std::unique_ptr< Image > FromPixelFormat(const Cvb::Size2D< int > &size, Cvb::PfncFormat format)
Creates an image from a specified pixel format.
Definition: decl_image.hpp:537
static std::unique_ptr< Image > Load(const String &fileName)
Loads an image with the given file name.
Definition: detail_image.hpp:32
static String ToString(const PfncFormat &pixelFormat)
Gets the official name for the given pixelFormat .
Definition: pfnc_format.hpp:1099
PfncFormat
GenICam Pixel Format Naming Convention (PFNC) format values.
Definition: pfnc_format.hpp:21
@ BayerGB8
Bayer Green-Blue 8-bit.
std::string String
String for wide characters or unicode characters.
Definition: string.hpp:56