35 #ifndef SHARK_DATA_IMPORT_PGM_H 36 #define SHARK_DATA_IMPORT_PGM_H 40 #include <boost/format.hpp> 41 #include <boost/filesystem.hpp> 42 #include <boost/archive/text_oarchive.hpp> 51 void importPGM( std::string
const& fileName, std::vector<unsigned char>& ppData, std::size_t& sx, std::size_t& sy )
53 std::ifstream file(fileName.c_str(), std::ios::binary);
57 std::size_t nGrayValues = 0;
62 while(file.peek() ==
'#'){
63 file.ignore(std::numeric_limits<std::streamsize>::max(),
'\n');
65 file >> sx >> sy >> nGrayValues;
70 file.read((
char*)ppData.data(),sx*sy);
86 void writePGM( std::string
const& fileName, std::vector<unsigned char>
const& data, std::size_t sx, std::size_t sy )
88 std::ofstream file(fileName.c_str(), std::ios::binary);
91 file<<
"P5\n"<<sx<<
" "<<sy<<
"\n"<<255<<
"\n";
92 file.write((
char*)data.data(),sx*sy);
103 void importPGM( std::string
const& fileName, T& data, std::size_t& sx, std::size_t& sy ) {
104 std::vector<unsigned char> rawData;
107 std::copy(rawData.begin(), rawData.end(), data.begin());
118 void exportPGM(std::string
const& fileName, T
const& data, std::size_t sx, std::size_t sy,
bool normalize =
false) {
120 std::vector<unsigned char> rawData(data.size());
121 typename T::const_iterator it = data.begin();
124 double lb = *std::min_element(data.begin(),data.end());
125 double ub = *std::max_element(data.begin(), data.end());
126 for( it = data.begin() ; it != data.end(); ++it, ++i )
127 rawData[i] = (
unsigned char)( (*it - lb) / (ub - lb) * 255 );
129 for( it = data.begin() ; it != data.end(); ++it, ++i )
130 rawData[i] = (
unsigned char)( *it );
132 detail::writePGM(fileName, rawData, sx, sy);
146 inline void exportFiltersToPGMGrid(std::string
const& basename, RealMatrix
const& filters,std::size_t width, std::size_t height) {
149 std::size_t gridX = std::size_t(std::sqrt(
double(filters.size1())));
150 std::size_t gridY = gridX;
151 while(gridX*gridY < filters.size1()) ++gridX;
153 RealMatrix image((height+1)*gridY,(width+1)*gridX,min(filters));
155 for(std::size_t filter = 0; filter != filters.size1(); ++filter){
157 std::size_t i = filter/gridX;
158 std::size_t j = filter%gridX;
159 std::size_t startY = (height+1)*i;
160 std::size_t startX = (width+1)*j;
162 noalias(subrange(image,startY,startY+height,startX,startX+width)) = to_matrix(row(filters,filter),height,width);
165 (basename+
".pgm").c_str(),
166 blas::adapt_vector((height+1)*gridY*(width+1)*gridX,&image(0,0)),
167 (width+1)*gridX, (height+1)*gridY,
187 std::size_t gridX = std::size_t(std::sqrt(
double(numFilters)));
188 std::size_t gridY = gridX;
189 while(gridX*gridY < numFilters) ++gridX;
191 double minimum = std::numeric_limits<double>::max();
193 minimum =std::min(minimum,min(filters.
batch(i)));
196 RealMatrix image((height+1)*gridY,(width+1)*gridX,minimum);
198 for(std::size_t filter = 0; filter != numFilters; ++filter){
200 std::size_t i = filter/gridX;
201 std::size_t j = filter%gridX;
202 std::size_t startY = (height+1)*i;
203 std::size_t startX = (width+1)*j;
204 RealVector filterImage =filters.
element(filter);
206 noalias(subrange(image,startY,startY+height,startX,startX+width)) = to_matrix(filterImage,height,width);
209 (basename+
".pgm").c_str(),
210 blas::adapt_vector((height+1)*gridY*(width+1)*gridX,&image(0,0)),
211 (width+1)*gridX, (height+1)*gridY,
223 std::vector<T> container;
224 std::vector<std::pair<std::size_t,std::size_t> > info;
225 if (boost::filesystem::is_directory(p)) {
226 for (boost::filesystem::recursive_directory_iterator itr(p); itr!=boost::filesystem::recursive_directory_iterator(); ++itr) {
227 if (boost::filesystem::is_regular(itr->status())) {
228 if ((boost::filesystem::extension(itr->path()) ==
".PGM") ||
229 (boost::filesystem::extension(itr->path()) ==
".pgm")) {
231 std::pair<std::size_t,std::size_t> imgInfo;
232 importPGM(itr->path().string().c_str(), img, imgInfo.first, imgInfo.second);
233 container.push_back(img);
234 info.push_back(imgInfo);
239 throw( std::invalid_argument(
"[importPGMDir] cannot open file" ) );
243 for(
auto const& i: info){
244 if(i.first != info.front().first || i.second != info.front().second){
245 throw SHARKEXCEPTION(
"[importPGMSet] all images are required to have the same size");
249 set.shape() = {info.front().second,info.front().first};