Skip to content

Commit

Permalink
Added image_transform example.
Browse files Browse the repository at this point in the history
  • Loading branch information
kmhofmann committed May 13, 2018
1 parent 4a7b238 commit e769a59
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 1 deletion.
7 changes: 7 additions & 0 deletions examples/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,13 @@ target_compile_definitions(image_rotate PRIVATE ${SELENE_COMPILER_DEFINITIONS})
target_include_directories(image_rotate PRIVATE ${SELENE_DIR}/examples ${Boost_INCLUDE_DIR})
target_link_libraries(image_rotate selene ${SELENE_BOOST_TARGET_NAME})

add_executable(image_transform
${CMAKE_CURRENT_LIST_DIR}/image_transform.cpp)
target_compile_options(image_transform PRIVATE ${SELENE_COMPILER_OPTIONS})
target_compile_definitions(image_transform PRIVATE ${SELENE_COMPILER_DEFINITIONS})
target_include_directories(image_transform PRIVATE ${SELENE_DIR}/examples ${Boost_INCLUDE_DIR})
target_link_libraries(image_transform selene ${SELENE_BOOST_TARGET_NAME})

add_executable(readme_example
${CMAKE_CURRENT_LIST_DIR}/readme_example.cpp)
target_compile_options(readme_example PRIVATE ${SELENE_COMPILER_OPTIONS})
Expand Down
2 changes: 1 addition & 1 deletion examples/image_conversion.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ int main(int argc, char** argv)

const auto img_rgb = sln_examples::read_example_image<Pixel_8u3>("bike_duck.png", data_path);

// We will now perform three conversions:
// We will now perform three color format conversions:
// - From RGB to Y (grayscale)
std::cout << "Converting the image from RGB to Y (grayscale)...\n";
const auto img_y = convert_image<PixelFormat::RGB, PixelFormat::Y>(img_rgb);
Expand Down
50 changes: 50 additions & 0 deletions examples/image_transform.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// This file is part of the `Selene` library.
// Copyright 2017-2018 Michael Hofmann (https://github.com/kmhofmann).
// Distributed under MIT license. See accompanying LICENSE file in the top-level directory.

#include <selene/img/ImageToImageData.hpp>
#include <selene/img_io/IO.hpp>
#include <selene/img_ops/Algorithms.hpp>

#include <selene/io/FileWriter.hpp>

#include <iostream>
#include <string>

#include "Utils.hpp"

constexpr auto output_filename_transformed = "bike_duck_transformed.png";

using namespace sln; // Never outside of example code

int main(int argc, char** argv)
{
// Read data path as optional command line argument
const char* data_path = (argc >= 2) ? argv[1] : nullptr;

// Read in the example image (check the implementation in Utils.hpp);
// `Pixel_8u3` designates 3 channels of unsigned 8-bit data for each pixel.

const auto img_rgb = sln_examples::read_example_image<Pixel_8u3>("bike_duck.png", data_path);

// Transform the image from 8-bit integral to 32-bit floating point type, and normalize values to be within (0...1)
std::cout << "Transforming the image from 8-bit integral to 32-bit floating point type (0...1)...\n";
auto img_f = sln::transform_pixels<sln::Pixel_32f3>(img_rgb, [](const auto& px) { return sln::Pixel_32f3(px) / 255.0; });

// Play around with the color channels
sln::for_each_pixel(img_f, [](auto& px) {
px[0] = std::pow(px[0], 0.1);
px[1] = std::pow(px[1], 0.8);
px[2] = std::pow(px[2], 0.3);
});

// Transform the image back to 8-bit integral representation (0...255)
auto img_transf = sln::transform_pixels<sln::Pixel_8u3>(img_f, [](const auto& px) { return sln::Pixel_8u3(px * 255.0); });

// Write out the transformed image to disk
std::cout << "Writing the result to disk: '" << output_filename_transformed << "'...\n";
write_image(to_image_data_view(img_transf, PixelFormat::RGB), ImageFormat::PNG,
FileWriter(output_filename_transformed));

return 0;
}

0 comments on commit e769a59

Please sign in to comment.