Commit d8fe4355 authored by Davis King's avatar Davis King

Added input_rgb_image_sized.

parent dc778081
......@@ -7,6 +7,7 @@
#include "../matrix.h"
#include "../array2d.h"
#include "../pixel.h"
#include <sstream>
namespace dlib
......@@ -24,6 +25,9 @@ namespace dlib
// ----------------------------------------------------------------------------------------
template <size_t NR, size_t NC=NR>
class input_rgb_image_sized;
class input_rgb_image
{
public:
......@@ -45,6 +49,11 @@ namespace dlib
) : avg_red(avg_red_), avg_green(avg_green_), avg_blue(avg_blue_)
{}
template <size_t NR, size_t NC>
inline input_rgb_image (
const input_rgb_image_sized<NR,NC>& item
);
float get_avg_red() const { return avg_red; }
float get_avg_green() const { return avg_green; }
float get_avg_blue() const { return avg_blue; }
......@@ -136,6 +145,147 @@ namespace dlib
float avg_blue;
};
// ----------------------------------------------------------------------------------------
template <size_t NR, size_t NC>
class input_rgb_image_sized
{
public:
static_assert(NR != 0 && NC != 0, "The input image can't be empty.");
typedef matrix<rgb_pixel> input_type;
const static unsigned int sample_expansion_factor = 1;
input_rgb_image_sized (
) :
avg_red(122.782),
avg_green(117.001),
avg_blue(104.298)
{
}
input_rgb_image_sized (
const input_rgb_image& item
) : avg_red(item.get_avg_red()),
avg_green(item.get_avg_green()),
avg_blue(item.get_avg_blue())
{}
input_rgb_image_sized (
float avg_red_,
float avg_green_,
float avg_blue_
) : avg_red(avg_red_), avg_green(avg_green_), avg_blue(avg_blue_)
{}
float get_avg_red() const { return avg_red; }
float get_avg_green() const { return avg_green; }
float get_avg_blue() const { return avg_blue; }
template <typename input_iterator>
void to_tensor (
input_iterator ibegin,
input_iterator iend,
resizable_tensor& data
) const
{
DLIB_CASSERT(std::distance(ibegin,iend) > 0,"");
// make sure all input images have the correct size
for (auto i = ibegin; i != iend; ++i)
{
DLIB_CASSERT(i->nr()==NR && i->nc()==NC,
"\t input_rgb_image_sized::to_tensor()"
<< "\n\t All input images must have "<<NR<<" rows and "<<NC<< " columns, but we got one with "<<i->nr()<<" rows and "<<i->nc()<<" columns."
);
}
// initialize data to the right size to contain the stuff in the iterator range.
data.set_size(std::distance(ibegin,iend), 3, NR, NC);
const size_t offset = NR*NC;
auto ptr = data.host();
for (auto i = ibegin; i != iend; ++i)
{
for (long r = 0; r < NR; ++r)
{
for (long c = 0; c < NC; ++c)
{
rgb_pixel temp = (*i)(r,c);
auto p = ptr++;
*p = (temp.red-avg_red)/256.0;
p += offset;
*p = (temp.green-avg_green)/256.0;
p += offset;
*p = (temp.blue-avg_blue)/256.0;
p += offset;
}
}
ptr += offset*(data.k()-1);
}
}
friend void serialize(const input_rgb_image_sized& item, std::ostream& out)
{
serialize("input_rgb_image_sized", out);
serialize(item.avg_red, out);
serialize(item.avg_green, out);
serialize(item.avg_blue, out);
serialize(NR, out);
serialize(NC, out);
}
friend void deserialize(input_rgb_image_sized& item, std::istream& in)
{
std::string version;
deserialize(version, in);
if (version != "input_rgb_image_sized")
throw serialization_error("Unexpected version found while deserializing dlib::input_rgb_image_sized.");
deserialize(item.avg_red, in);
deserialize(item.avg_green, in);
deserialize(item.avg_blue, in);
size_t nr, nc;
deserialize(nr, in);
deserialize(nc, in);
if (nr != NR || nc != NC)
{
std::ostringstream sout;
sout << "Wrong image dimensions found while deserializing dlib::input_rgb_image_sized.\n";
sout << "Expected "<<NR<<" rows and "<<NC<< " columns, but found "<<nr<<" rows and "<<nc<<" columns.";
throw serialization_error(sout.str());
}
}
friend std::ostream& operator<<(std::ostream& out, const input_rgb_image_sized& item)
{
out << "input_rgb_image_sized("<<item.avg_red<<","<<item.avg_green<<","<<item.avg_blue<<") nr="<<NR<<" nc="<<NC;
return out;
}
friend void to_xml(const input_rgb_image_sized& item, std::ostream& out)
{
out << "<input_rgb_image_sized r='"<<item.avg_red<<"' g='"<<item.avg_green<<"' b='"<<item.avg_blue<<"' nr='"<<NR<<"' nc='"<<NC<<"'/>";
}
private:
float avg_red;
float avg_green;
float avg_blue;
};
// ----------------------------------------------------------------------------------------
template <size_t NR, size_t NC>
input_rgb_image::
input_rgb_image (
const input_rgb_image_sized<NR,NC>& item
) : avg_red(item.get_avg_red()),
avg_green(item.get_avg_green()),
avg_blue(item.get_avg_blue())
{}
// ----------------------------------------------------------------------------------------
template <typename T, long NR, long NC, typename MM, typename L>
......
......@@ -235,6 +235,25 @@ namespace dlib
!*/
};
// ----------------------------------------------------------------------------------------
template <size_t NR, size_t NC=NR>
class input_rgb_image_sized
{
/*!
WHAT THIS OBJECT REPRESENTS
This layer has an interface and behavior identical to input_rgb_image
except that it requires input images to have NR rows and NC columns. This
is checked by a DLIB_CASSERT inside to_tensor().
You can also convert between input_rgb_image and input_rgb_image_sized by
copy construction or assignment.
!*/
};
// ----------------------------------------------------------------------------------------
}
#endif // DLIB_DNn_INPUT_ABSTRACT_H_
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment