56void importPGM( std::string
const& fileName, std::vector<unsigned char>& ppData, std::size_t& sx, std::size_t& sy )
85void writePGM( std::string
const& fileName, std::vector<unsigned char>
const& data, std::size_t sx, std::size_t sy )
102void importPGM( std::string
const& fileName, T& data, std::size_t& sx, std::size_t& sy ) {
103 std::vector<unsigned char> rawData;
104 detail::importPGM(fileName, rawData, sx, sy);
106 std::copy(rawData.begin(), rawData.end(), data.begin());
117void exportPGM(std::string
const& fileName, T
const& data, std::size_t sx, std::size_t sy,
bool normalize =
false) {
119 std::vector<unsigned char> rawData(data.size());
120 typename T::const_iterator it = data.begin();
123 double lb = *std::min_element(data.begin(),data.end());
124 double ub = *std::max_element(data.begin(), data.end());
125 for( it = data.begin() ; it != data.end(); ++it, ++i )
126 rawData[i] = (
unsigned char)( (*it - lb) / (ub - lb) * 255 );
128 for( it = data.begin() ; it != data.end(); ++it, ++i )
129 rawData[i] = (
unsigned char)( *it );
131 detail::writePGM(fileName, rawData, sx, sy);
145inline void exportFiltersToPGMGrid(std::string
const& basename, RealMatrix
const& filters,std::size_t width, std::size_t height) {
148 std::size_t gridX = std::size_t(std::sqrt(
double(filters.size1())));
149 std::size_t gridY = gridX;
150 while(gridX*gridY < filters.size1()) ++gridX;
152 RealMatrix image((height+1)*gridY,(width+1)*gridX,min(filters));
154 for(std::size_t filter = 0; filter != filters.size1(); ++filter){
156 std::size_t i = filter/gridX;
157 std::size_t j = filter%gridX;
158 std::size_t startY = (height+1)*i;
159 std::size_t startX = (width+1)*j;
161 noalias(subrange(image,startY,startY+height,startX,startX+width)) = to_matrix(row(filters,filter),height,width);
164 (basename+
".pgm").c_str(),
165 blas::adapt_vector((height+1)*gridY*(width+1)*gridX,&image(0,0)),
166 (width+1)*gridX, (height+1)*gridY,
186 std::size_t gridX = std::size_t(std::sqrt(
double(numFilters)));
187 std::size_t gridY = gridX;
188 while(gridX*gridY < numFilters) ++gridX;
190 double minimum = std::numeric_limits<double>::max();
192 minimum =std::min(minimum,min(filters.
batch(i)));
195 RealMatrix image((height+1)*gridY,(width+1)*gridX,minimum);
197 for(std::size_t filter = 0; filter != numFilters; ++filter){
199 std::size_t i = filter/gridX;
200 std::size_t j = filter%gridX;
201 std::size_t startY = (height+1)*i;
202 std::size_t startX = (width+1)*j;
203 RealVector filterImage =filters.
element(filter);
205 noalias(subrange(image,startY,startY+height,startX,startX+width)) = to_matrix(filterImage,height,width);
208 (basename+
".pgm").c_str(),
209 blas::adapt_vector((height+1)*gridY*(width+1)*gridX,&image(0,0)),
210 (width+1)*gridX, (height+1)*gridY,
222 std::vector<T> container;
223 std::vector<std::pair<std::size_t,std::size_t> > info;
224 if (boost::filesystem::is_directory(p)) {
225 for (boost::filesystem::recursive_directory_iterator itr(p); itr!=boost::filesystem::recursive_directory_iterator(); ++itr) {
226 if (boost::filesystem::is_regular(itr->status())) {
227 if ((boost::filesystem::extension(itr->path()) ==
".PGM") ||
228 (boost::filesystem::extension(itr->path()) ==
".pgm")) {
230 std::pair<std::size_t,std::size_t> imgInfo;
231 importPGM(itr->path().string().c_str(), img, imgInfo.first, imgInfo.second);
232 container.push_back(img);
233 info.push_back(imgInfo);
238 throw( std::invalid_argument(
"[importPGMDir] cannot open file" ) );
242 for(
auto const& i: info){
243 if(i.first != info.front().first || i.second != info.front().second){
244 throw SHARKEXCEPTION(
"[importPGMSet] all images are required to have the same size");
248 set.
shape() = {info.front().second,info.front().first};