| 1 | /********************************************************************** |
| 2 | test1.cc: test app for jiblib |
| 3 | Author(s): [email protected] |
| 4 | Copyright (C) 2016-2020 European Union (Joint Research Centre) |
| 5 | |
| 6 | This file is part of jiplib. |
| 7 | |
| 8 | jiplib is free software: you can redistribute it and/or modify |
| 9 | it under the terms of the GNU General Public License as published by |
| 10 | the Free Software Foundation, either version 3 of the License, or |
| 11 | (at your option) any later version. |
| 12 | |
| 13 | jiplib is distributed in the hope that it will be useful, |
| 14 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | GNU General Public License for more details. |
| 17 | |
| 18 | You should have received a copy of the GNU General Public License |
| 19 | along with jiplib. If not, see <https://www.gnu.org/licenses/>. |
| 20 | ***********************************************************************/ |
| 21 | #include <memory> |
| 22 | #include "base/Optionjl.h" |
| 23 | #include "algorithms/Filter2d.h" |
| 24 | #include "apps/AppFactory.h" |
| 25 | #include "imageclasses/ImgCollection.h" |
| 26 | #include "jim.h" |
| 27 | |
| 28 | using namespace std; |
| 29 | using namespace jiplib; |
| 30 | |
| 31 | int main(int argc, char *argv[]) |
| 32 | { |
| 33 | Optionjl<string> input_opt("i", "input", "Input image file(s). If input contains multiple images, a multi-band output is created"); |
| 34 | Optionjl<string> output_opt("o", "output", "Output image file"); |
| 35 | Optionjl<string> oformat_opt("of", "oformat", "Output image format (see also gdal_translate).","GTiff"); |
| 36 | Optionjl<string> option_opt("co", "co", "Creation option for output file. Multiple options can be specified."); |
| 37 | Optionjl<string> projection_opt("a_srs", "a_srs", "Override the spatial reference for the output file (leave blank to copy from input file, use epsg:3035 to use European projection and force to European grid"); |
| 38 | Optionjl<double> scale_opt("scale", "scale", "output=scale*input+offset"); |
| 39 | Optionjl<double> offset_opt("offset", "offset", "output=scale*input+offset"); |
| 40 | Optionjl<unsigned long int> memory_opt("mem", "mem", "Buffer size (in MB) to read image data blocks in memory",0,1); |
| 41 | |
| 42 | option_opt.setHide(1); |
| 43 | scale_opt.setHide(1); |
| 44 | offset_opt.setHide(1); |
| 45 | memory_opt.setHide(1); |
| 46 | |
| 47 | bool doProcess;//stop process when program was invoked with help option (-h --help) |
| 48 | try{ |
| 49 | doProcess=input_opt.retrieveOption(argc,argv); |
| 50 | output_opt.retrieveOption(argc,argv); |
| 51 | oformat_opt.retrieveOption(argc,argv); |
| 52 | option_opt.retrieveOption(argc,argv); |
| 53 | projection_opt.retrieveOption(argc,argv); |
| 54 | scale_opt.retrieveOption(argc,argv); |
| 55 | offset_opt.retrieveOption(argc,argv); |
| 56 | memory_opt.retrieveOption(argc,argv); |
| 57 | } |
| 58 | catch(string predefinedString){ |
| 59 | std::cout << predefinedString << std::endl; |
| 60 | exit(0); |
| 61 | } |
| 62 | if(doProcess){ |
| 63 | if(input_opt.empty()){ |
| 64 | std::cerr << "Error: no input file provided (use option -i). Use --help for help information" << std::endl; |
| 65 | exit(1); |
| 66 | } |
| 67 | else if(output_opt.empty()){ |
| 68 | std::cerr << "Error: no output file provided (use option -o). Use --help for help information" << std::endl; |
| 69 | exit(1); |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | app::AppFactory app(argc,argv); |
| 74 | // // app.setOption("dx","500"); |
| 75 | // // app.setOption("dy","500"); |
| 76 | |
| 77 | filter2d::Filter2d filter; |
| 78 | ImgCollection inputCollection; |
| 79 | try{ |
| 80 | shared_ptr<Jim> inputImg(new Jim); |
| 81 | inputImg->open(input_opt[0]); |
| 82 | inputCollection.pushImage(inputImg); |
| 83 | double theMin=0; |
| 84 | double theMax=0; |
| 85 | inputCollection[0]->getMinMax(theMin,theMax,0); |
| 86 | cout << "min, max: " << theMin << ", " << theMax << endl; |
| 87 | string imageType; |
| 88 | if(oformat_opt.size())//default |
| 89 | imageType=oformat_opt[0]; |
| 90 | else |
| 91 | imageType=inputCollection[0]->getImageType(); |
| 92 | |
| 93 | app.showOptions(); |
| 94 | |
| 95 | Jim outputImg; |
| 96 | shared_ptr<Jim> imgPointer=make_shared<Jim>(outputImg); |
| 97 | imgPointer=static_pointer_cast<Jim>(inputCollection.crop(app)); |
| 98 | // inputCollection.crop(imgPointer,app); |
| 99 | cout << "smoothing" << endl; |
| 100 | filter.smooth(imgPointer,imgPointer,5); |
| 101 | |
| 102 | |
| 103 | // for(int iband=0;iband<imgPointer->nrOfBand();++iband){ |
| 104 | // IMAGE mia1=imgPointer->getMIA(iband); |
| 105 | // IMAGE mia2=imgPointer->getMIA(iband); |
| 106 | // arith(&mia1, &mia2, ADD_op); |
| 107 | // } |
| 108 | |
| 109 | cout << "performing arithmetic operation" << endl; |
| 110 | imgPointer->arith(imgPointer,ADD_op); |
| 111 | imgPointer->rdil(imgPointer,1,1); |
| 112 | |
| 113 | cout << "performing erosion" << endl; |
| 114 | filter.morphology(imgPointer,imgPointer,"erode",3,3); |
| 115 | // cout << "performing dilation" << endl; |
| 116 | // filter.morphology(*imgPointer,*imgPointer,"dilate",3,3); |
| 117 | |
| 118 | inputImg->close(); |
| 119 | imgPointer->setFile(output_opt[0],imageType); |
| 120 | imgPointer->close(); |
| 121 | } |
| 122 | catch(string helpString){//help was invoked |
| 123 | std::cout << helpString << std::endl; |
| 124 | return(1); |
| 125 | } |
| 126 | return(0); |
| 127 | } |