INFO702 - TPs
Loading...
Searching...
No Matches
testImage2D.cpp
1#include <iostream>
2#include <fstream>
3#include <sstream>
4#include "Image2D.hpp"
5#include "Image2DReader.hpp"
6#include "Image2DWriter.hpp"
7
8int main( int argc, char** argv )
9{
10 typedef unsigned char GrayLevel;
12 typedef Image2DReader<GrayLevel> GrayLevelImage2DReader;
13 typedef Image2DWriter<GrayLevel> GrayLevelImage2DWriter;
14
15 if ( argc < 3 )
16 {
17 std::cerr << "Usage: testImage2D <input.pgm> <output.pgm> <kernel_size>" << std::endl;
18 return 0;
19 }
20 int kernel = (argc > 3) ? atoi( argv[ 3 ] ) : 1;
22 std::ifstream input( argv[1] ); // récupère le 1er argument.
23 bool ok = GrayLevelImage2DReader::read( img, input );
24 if ( !ok )
25 {
26 std::cerr << "Error reading input file." << std::endl;
27 return 1;
28 }
29 input.close();
30
31 GrayLevelImage2D img2( img.w(), img.h() );
32 std::vector<GrayLevel> values;
33 for ( GrayLevelImage2D::Iterator outIt = img2.begin(), outItE = img2.end();
34 outIt != outItE; ++outIt )
35 {
36 std::pair<int,int> p = img2.position( outIt );
37 int xmin = std::max( p.first-kernel, 0 );
38 int ymin = std::max( p.second-kernel, 0 );
39 int xmax = std::min( p.first+kernel, img.w()-1 );
40 int ymax = std::min( p.second+kernel, img.h()-1 );
41 GrayLevelImage2D::SimpleImageConstIterator it2d = img.start2d( xmin, ymin );
42 GrayLevelImage2D::SimpleImageConstIterator it2dE = img.start2d( xmax, ymax );
43 for ( GrayLevelImage2D::SimpleImageConstIterator it2dRow = it2d;
44 it2dRow.y <= it2dE.y;
45 ++it2dRow.y )
46 for ( GrayLevelImage2D::SimpleImageConstIterator it2dCol = it2dRow;
47 it2dCol.x <= it2dE.x; ++it2dCol.x )
48 values.push_back( *it2dCol );
49 int mid = values.size() / 2;
50 std::nth_element( values.begin(), values.begin() + mid, values.end() );
51 *outIt = values[ mid ];
52 values.clear();
53 }
54 std::ofstream output( argv[2] ); // récupère le 2ème argument.
55 bool ok2 = GrayLevelImage2DWriter::write( img2, output, false );
56 if ( !ok2 )
57 {
58 std::cerr << "Error writing output file." << std::endl;
59 return 1;
60 }
61 output.close();
62
63
64 return 0;
65}
int w() const
int h() const
Classe générique pour représenter des images 2D.
Definition Image2D.hpp:8