Made hog_image clearable and serializable.

This commit is contained in:
Davis King 2011-08-11 22:26:51 -04:00
parent 259c32ec4d
commit 38b78800f7
3 changed files with 162 additions and 1 deletions

View File

@ -63,6 +63,14 @@ namespace dlib
num_block_cols(0)
{}
void clear (
)
{
num_block_rows = 0;
num_block_cols = 0;
hist_cells.clear();
}
template <
typename image_type
>
@ -201,6 +209,32 @@ namespace dlib
return rectangle(feat_to_image_space(rect.tl_corner()), feat_to_image_space(rect.br_corner()));
}
template <
unsigned long T1,
unsigned long T2,
unsigned long T3,
unsigned long T4,
int T5,
int T6
>
friend void serialize (
const hog_image<T1,T2,T3,T4,T5,T6>& item,
std::ostream& out
);
template <
unsigned long T1,
unsigned long T2,
unsigned long T3,
unsigned long T4,
int T5,
int T6
>
friend void deserialize (
hog_image<T1,T2,T3,T4,T5,T6>& item,
std::istream& in
);
private:
template <
@ -430,6 +464,63 @@ namespace dlib
};
// ----------------------------------------------------------------------------------------
template <
unsigned long T1,
unsigned long T2,
unsigned long T3,
unsigned long T4,
int T5,
int T6
>
void serialize (
const hog_image<T1,T2,T3,T4,T5,T6>& item,
std::ostream& out
)
{
// serialize item.hist_cells
serialize(item.hist_cells.nc(),out);
serialize(item.hist_cells.nr(),out);
item.hist_cells.reset();
while (item.hist_cells.move_next())
serialize(item.hist_cells.element().values,out);
item.hist_cells.reset();
serialize(item.num_block_rows, out);
serialize(item.num_block_cols, out);
}
template <
unsigned long T1,
unsigned long T2,
unsigned long T3,
unsigned long T4,
int T5,
int T6
>
void deserialize (
hog_image<T1,T2,T3,T4,T5,T6>& item,
std::istream& in
)
{
// deserialize item.hist_cells
long nc, nr;
deserialize(nc,in);
deserialize(nr,in);
item.hist_cells.set_size(nr,nc);
while (item.hist_cells.move_next())
deserialize(item.hist_cells.element().values,in);
item.hist_cells.reset();
deserialize(item.num_block_rows, in);
deserialize(item.num_block_cols, in);
}
// ----------------------------------------------------------------------------------------
}

View File

@ -120,6 +120,13 @@ namespace dlib
- this object is properly initialized
!*/
void clear (
);
/*!
ensures
- this object will have its initial value
!*/
template <
typename image_type
>
@ -237,6 +244,40 @@ namespace dlib
};
// ----------------------------------------------------------------------------------------
template <
unsigned long T1,
unsigned long T2,
unsigned long T3,
unsigned long T4,
int T5,
int T6
>
void serialize (
const hog_image<T1,T2,T3,T4,T5,T6>& item,
std::ostream& out
);
/*!
provides serialization support
!*/
template <
unsigned long T1,
unsigned long T2,
unsigned long T3,
unsigned long T4,
int T5,
int T6
>
void deserialize (
hog_image<T1,T2,T3,T4,T5,T6>& item,
std::istream& in
);
/*!
provides deserialization support
!*/
// ----------------------------------------------------------------------------------------
}

View File

@ -40,7 +40,7 @@ namespace
assign_all_pixels(img, 0);
hog_image<3,3,1,4,hog_signed_gradient,hog_full_interpolation> hog1;
hog_image<3,3,1,4,hog_signed_gradient,hog_full_interpolation> hog1, hog1_deserialized;
hog_image<4,4,2,4,hog_signed_gradient,hog_full_interpolation> hog2;
hog1.load(img);
@ -87,6 +87,35 @@ namespace
DLIB_TEST(hog2.image_to_feat_space(hog2.feat_to_image_space(point(1,1))) == point(1,1));
DLIB_TEST(hog1.image_to_feat_space(hog1.feat_to_image_space(point(1,2))) == point(1,2));
DLIB_TEST(hog2.image_to_feat_space(hog2.feat_to_image_space(point(1,2))) == point(1,2));
DLIB_TEST(hog1_deserialized.size() != hog1.size());
DLIB_TEST(hog1_deserialized.nr() != hog1.nr());
DLIB_TEST(hog1_deserialized.nc() != hog1.nc());
ostringstream sout;
serialize(hog1, sout);
istringstream sin(sout.str());
deserialize(hog1_deserialized, sin);
DLIB_TEST(hog1_deserialized.size() == hog1.size());
DLIB_TEST(hog1_deserialized.nr() == hog1.nr());
DLIB_TEST(hog1_deserialized.nc() == hog1.nc());
DLIB_TEST(hog1_deserialized(0,2) == hog1(0,2));
DLIB_TEST(hog1_deserialized.get_block_rect(1,2) == hog1.get_block_rect(1,2));
DLIB_TEST(hog1_deserialized.image_to_feat_space(hog1_deserialized.feat_to_image_space(point(0,0))) == point(0,0));
DLIB_TEST(hog1_deserialized.image_to_feat_space(hog1_deserialized.feat_to_image_space(point(1,1))) == point(1,1));
DLIB_TEST(hog1_deserialized.image_to_feat_space(hog1_deserialized.feat_to_image_space(point(1,2))) == point(1,2));
DLIB_TEST(hog1.size() > 1);
DLIB_TEST(hog1.nr() > 1);
DLIB_TEST(hog1.nc() > 1);
hog1.clear();
DLIB_TEST(hog1.size() == 0);
DLIB_TEST(hog1.nr() == 0);
DLIB_TEST(hog1.nc() == 0);
}
} a;