MVE - Multi-View Environment mve-devel
Loading...
Searching...
No Matches
image_tools.cc
Go to the documentation of this file.
1/*
2 * Copyright (C) 2015, Simon Fuhrmann
3 * TU Darmstadt - Graphics, Capture and Massively Parallel Computing
4 * All rights reserved.
5 *
6 * This software may be modified and distributed under the terms
7 * of the BSD 3-Clause license. See the LICENSE.txt file for details.
8 */
9
10#include <algorithm>
11
12#include "mve/camera.h"
13#include "mve/image_tools.h"
14
17
18/*
19 * ----------------------- Image conversion -----------------------
20 */
21
22FloatImage::Ptr
24{
25 if (image == nullptr)
26 throw std::invalid_argument("Null image given");
27
28 FloatImage::Ptr img = FloatImage::create();
29 img->allocate(image->width(), image->height(), image->channels());
30 for (int64_t i = 0; i < image->get_value_amount(); ++i)
31 {
32 float value = (float)image->at(i) / 255.0f;
33 img->at(i) = std::min(1.0f, std::max(0.0f, value));
34 }
35 return img;
36}
37
38/* ---------------------------------------------------------------- */
39
42{
43 if (image == nullptr)
44 throw std::invalid_argument("Null image given");
45
46 DoubleImage::Ptr img = DoubleImage::create();
47 img->allocate(image->width(), image->height(), image->channels());
48 for (int64_t i = 0; i < image->get_value_amount(); ++i)
49 {
50 double value = static_cast<double>(image->at(i)) / 255.0;
51 img->at(i) = std::min(1.0, std::max(0.0, value));
52 }
53 return img;
54}
55
56/* ---------------------------------------------------------------- */
57
59float_to_byte_image (FloatImage::ConstPtr image, float vmin, float vmax)
60{
61 if (image == nullptr)
62 throw std::invalid_argument("Null image given");
63
64 ByteImage::Ptr img = ByteImage::create();
65 img->allocate(image->width(), image->height(), image->channels());
66 for (int64_t i = 0; i < image->get_value_amount(); ++i)
67 {
68 float value = std::min(vmax, std::max(vmin, image->at(i)));
69 value = 255.0f * (value - vmin) / (vmax - vmin);
70 img->at(i) = static_cast<uint8_t>(value + 0.5f);
71 }
72 return img;
73}
74
75/* ---------------------------------------------------------------- */
76
78double_to_byte_image (DoubleImage::ConstPtr image, double vmin, double vmax)
79{
80 if (image == nullptr)
81 throw std::invalid_argument("Null image given");
82
83 ByteImage::Ptr img = ByteImage::create();
84 img->allocate(image->width(), image->height(), image->channels());
85 for (int64_t i = 0; i < image->get_value_amount(); ++i)
86 {
87 double value = std::min(vmax, std::max(vmin, image->at(i)));
88 value = 255.0 * (value - vmin) / (vmax - vmin);
89 img->at(i) = static_cast<uint8_t>(value + 0.5);
90 }
91 return img;
92}
93
94/* ---------------------------------------------------------------- */
95
98{
99 if (image == nullptr)
100 throw std::invalid_argument("Null image given");
101
102 ByteImage::Ptr img = ByteImage::create();
103 img->allocate(image->width(), image->height(), image->channels());
104 for (int64_t i = 0; i < image->get_value_amount(); ++i)
105 {
106 img->at(i) = math::clamp(std::abs(image->at(i)), 0, 255);
107 }
108 return img;
109}
110
111/* ---------------------------------------------------------------- */
112
114raw_to_byte_image (RawImage::ConstPtr image, uint16_t vmin, uint16_t vmax)
115{
116 if (image == nullptr)
117 throw std::invalid_argument("Null image given");
118
119 ByteImage::Ptr img = ByteImage::create();
120 img->allocate(image->width(), image->height(), image->channels());
121 for (int64_t i = 0; i < image->get_value_amount(); ++i)
122 {
123 uint16_t value = std::min(vmax, std::max(vmin, image->at(i)));
124 value = 255.0 * static_cast<double>(value - vmin)
125 / static_cast<double>(vmax - vmin);
126 img->at(i) = static_cast<uint8_t>(value + 0.5);
127 }
128 return img;
129}
130
131/* ---------------------------------------------------------------- */
132
135{
136 if (image == nullptr)
137 throw std::invalid_argument("Null image given");
138
139 FloatImage::Ptr img = FloatImage::create();
140 img->allocate(image->width(), image->height(), image->channels());
141 for (int64_t i = 0; i < image->get_value_amount(); ++i)
142 {
143 float const value = static_cast<float>(image->at(i)) / 65535.0f;
144 img->at(i) = std::min(1.0f, std::max(0.0f, value));
145 }
146 return img;
147}
148
149/* ---------------------------------------------------------------- */
150
151void
153{
154 if (image == nullptr)
155 throw std::invalid_argument("Null image given");
156
157 float vmin, vmax;
158 find_min_max_value<float>(image, &vmin, &vmax);
159 if (vmin >= vmax)
160 {
161 image->fill(0.0f);
162 return;
163 }
164 for (float* ptr = image->begin(); ptr != image->end(); ++ptr)
165 *ptr = (*ptr - vmin) / (vmax - vmin);
166}
167
168/* ---------------------------------------------------------------- */
169
170void
171gamma_correct (ByteImage::Ptr image, float power)
172{
173 if (image == nullptr)
174 throw std::invalid_argument("Null image given");
175
176 uint8_t lookup[256];
177 for (int64_t i = 0; i < 256; ++i)
178 lookup[i] = static_cast<uint8_t>(std::pow(i / 255.0f, power)
179 * 255.0f + 0.5f);
180 for (int64_t i = 0; i < image->get_value_amount(); ++i)
181 image->at(i) = lookup[image->at(i)];
182}
183
std::shared_ptr< Image< T > > Ptr
Definition image.h:42
std::shared_ptr< Image< T > const > ConstPtr
Definition image.h:43
#define MVE_IMAGE_NAMESPACE_END
Definition defines.h:17
#define MVE_NAMESPACE_BEGIN
Definition defines.h:13
#define MVE_IMAGE_NAMESPACE_BEGIN
Definition defines.h:16
#define MVE_NAMESPACE_END
Definition defines.h:14
T const & clamp(T const &v, T const &min=T(0), T const &max=T(1))
Returns value 'v' clamped to the interval specified by 'min' and 'max'.
Definition functions.h:204
FloatImage::Ptr byte_to_float_image(ByteImage::ConstPtr image)
Converts a given byte image to a float image.
ByteImage::Ptr int_to_byte_image(IntImage::ConstPtr image)
Convertes a given int image to a byte image.
DoubleImage::Ptr byte_to_double_image(ByteImage::ConstPtr image)
Converts a given byte image to a double image.
ByteImage::Ptr float_to_byte_image(FloatImage::ConstPtr image, float vmin, float vmax)
Converts a given float image to a byte image.
void float_image_normalize(FloatImage::Ptr image)
Normalizes a float image IN-PLACE such that all values are [0, 1].
ByteImage::Ptr double_to_byte_image(DoubleImage::ConstPtr image, double vmin, double vmax)
Converts a given double image to a byte image.
void gamma_correct(ByteImage::Ptr image, float power)
Applies fast gamma correction to byte image using a lookup table.
ByteImage::Ptr raw_to_byte_image(RawImage::ConstPtr image, uint16_t vmin, uint16_t vmax)
Converts a given raw image to a byte image.
FloatImage::Ptr raw_to_float_image(RawImage::ConstPtr image)
Converts a given raw image to a float image.