Debian Codegraph @

src/imageclasses/jlfilter1d_lib.h

raw symbols SCIP
1 /**********************************************************************
2 jlfilter1d_lib.h: program to filter raster images: median, min/max, morphological, filtering
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 #ifndef _JLFILTER1D_LIB_H_
22 #define _JLFILTER1D_LIB_H_
23
24 #include "imageclasses/Jim.h"
25 #include "algorithms/Filter.h"
26 #include "apps/AppFactory.h"
27
28 /* enum FILTER_TYPE { median=100, var=101 , min=102, max=103, sum=104, mean=105, minmax=106, dilate=107, erode=108, close=109, open=110, homog=111, sobelx=112, sobely=113, sobelxy=114, sobelyx=115, smooth=116, density=117, mode=118, mixed=119, threshold=120, ismin=121, ismax=122, heterog=123, order=124, stdev=125, mrf=126, dwt=127, dwti=128, dwt_cut=129, scramble=130, shift=131, linearfeature=132, smoothnodata=133, countid=134, dwt_cut_from=135, savgolay=136, percentile=137, proportion=138, nvalid=139, sauvola=140,first=141,last=142, minindex=143, maxindex=144}; */
29
30 /* static FILTER_TYPE getFilterType(const std::string filterType){ */
31 /* std::map<std::string, filter::FILTER_TYPE> filterMap; */
32 /* filterMap["median"]=filter::median; */
33 /* filterMap["var"]=filter::var; */
34 /* filterMap["min"]=filter::min; */
35 /* filterMap["max"]=filter::max; */
36 /* filterMap["sum"]=filter::sum; */
37 /* filterMap["mean"]=filter::mean; */
38 /* filterMap["threshold"]=filter::threshold; */
39 /* filterMap["ismin"]=filter::ismin; */
40 /* filterMap["ismax"]=filter::ismax; */
41 /* filterMap["order"]=filter::order; */
42 /* filterMap["stdev"]=filter::stdev; */
43 /* filterMap["scramble"]=filter::scramble; */
44 /* filterMap["countid"]=filter::countid; */
45 /* filterMap["percentile"]=filter::percentile; */
46 /* filterMap["proportion"]=filter::proportion; */
47 /* filterMap["nvalid"]=filter::nvalid; */
48 /* filterMap["first"]=filter::first; */
49 /* filterMap["last"]=filter::last; */
50 /* return filterMap[filterType]; */
51 /* }; */
52
53 template<typename T> void Jim::firfilter1d_t(Jim& imgWriter, app::AppFactory& app){
54 Optionjl<double> taps_opt("taps", "taps", "taps used for spectral filtering");
55 Optionjl<std::string> padding_opt("pad","pad", "Padding method for filtering (how to handle edge effects). Choose between: symmetric, replicate, circular, zero (pad with 0).", "symmetric");
56 Optionjl<short> verbose_opt("v", "verbose", "verbose mode if > 0", 0,2);
57
58 bool doProcess;//stop process when program was invoked with help option (-h --help)
59 try{
60 doProcess=taps_opt.retrieveOption(app);
61 padding_opt.retrieveOption(app);
62 verbose_opt.retrieveOption(app);
63 if(!doProcess){
64 std::cout << std::endl;
65 std::ostringstream helpStream;
66 helpStream << "short option -h shows basic options only, use long option --help to show all options" << std::endl;
67 throw(helpStream.str());//help was invoked, stop processing
68 }
69
70 #if JIPLIB_PROCESS_IN_PARALLEL == 1
71 #pragma omp parallel for
72 #else
73 #endif
74 for(size_t iband=0;iband<nrOfBand();++iband){
75 T* pin=static_cast<T*>(getDataPointer(iband));
76 double* pout=static_cast<double*>(imgWriter.getDataPointer(iband));
77
78 filter::Filter filter1d;
79 if(verbose_opt[0])
80 std::cout << "set padding" << std::endl;
81 filter1d.setPadding(padding_opt[0]);
82 if(taps_opt.size()>nrOfPlane()){
83 std::ostringstream errorStream;
84 errorStream << "Error: number of taps must be < " << nrOfPlane() << std::endl;
85 throw(errorStream.str());//error was invoked, stop processing
86 }
87
88 if(verbose_opt[0]){
89 std::cout << "taps: " << std::endl;
90 std::cout << taps_opt << std::endl;
91 }
92 for(size_t index=0;index<nrOfCol()*nrOfRow();++index){
93 size_t i=0;
94 //start: extend pin by padding
95 for(i=0;i<taps_opt.size()/2;++i){
96 pout[index+i*nrOfCol()*nrOfRow()]=taps_opt[taps_opt.size()/2]*pin[index+i*nrOfCol()*nrOfRow()];
97 //todo:introduce nodata?
98 //todo: check if t<taps_opt.size() or <= ?
99 for(int t=1;t<taps_opt.size()/2;++t){
100 pout[index+i*nrOfCol()*nrOfRow()]+=taps_opt[taps_opt.size()/2+t]*pin[index+(i+t)*nrOfCol()*nrOfRow()];
101 if(i>=t)
102 pout[index+i*nrOfCol()*nrOfRow()]+=taps_opt[taps_opt.size()/2-t]*pin[index+(i-t)*nrOfCol()*nrOfRow()];
103 else{
104 switch(filter1d.getPadding(padding_opt[0])){
105 case(filter::replicate):
106 pout[index+i*nrOfCol()*nrOfRow()]+=taps_opt[taps_opt.size()/2-t]*pin[index];
107 break;
108 case(filter::circular):
109 pout[index+i*nrOfCol()*nrOfRow()]+=taps_opt[taps_opt.size()/2-t]*pin[index+(nrOfPlane()+i-t)*nrOfCol()*nrOfRow()];
110 break;
111 case(filter::zero):
112 pout[index+i*nrOfCol()*nrOfRow()]+=taps_opt[taps_opt.size()/2-t]*0;
113 break;
114 case(filter::symmetric):
115 default:
116 pout[index+i*nrOfCol()*nrOfRow()]+=taps_opt[taps_opt.size()/2-t]*pin[index+(t-i)*nrOfCol()*nrOfRow()];
117 break;
118 }
119 }
120 }
121 }
122 //main
123 for(i=taps_opt.size()/2;i<nrOfPlane()-taps_opt.size()/2;++i){
124 //todo:introduce nodata
125 pout[index+i*nrOfCol()*nrOfRow()]=0;
126 for(int t=0;t<taps_opt.size();++t)
127 pout[index+i*nrOfCol()*nrOfRow()]+=pin[index+(i-taps_opt.size()/2+t)*nrOfCol()*nrOfRow()]*taps_opt[t];
128 }
129 //end: extend pin by padding
130 for(i=nrOfPlane()-taps_opt.size()/2;i<nrOfPlane();++i){
131 //todo:introduce nodata?
132 pout[index+i*nrOfCol()*nrOfRow()]=taps_opt[taps_opt.size()/2]*pin[index+i*nrOfCol()*nrOfRow()];
133 //todo:introduce nodata?
134 //todo: check if t<taps_opt.size() or <= ?
135 for(int t=1;t<taps_opt.size()/2;++t){
136 pout[index+i*nrOfCol()*nrOfRow()]+=taps_opt[taps_opt.size()/2-t]*pin[index+(i-t)*nrOfCol()*nrOfRow()];
137 if(i+t<nrOfPlane())
138 pout[index+i*nrOfCol()*nrOfRow()]+=taps_opt[taps_opt.size()/2+t]*pin[index+(i+t)*nrOfCol()*nrOfRow()];
139 else{
140 switch(filter1d.getPadding(padding_opt[0])){
141 case(filter::replicate):
142 pout[index+i*nrOfCol()*nrOfRow()]+=taps_opt[taps_opt.size()/2+t]*pin[index+(nrOfPlane()-1)*nrOfCol()*nrOfRow()];
143 break;
144 case(filter::circular):
145 pout[index+i*nrOfCol()*nrOfRow()]+=taps_opt[taps_opt.size()/2+t]*pin[index+(t-1)*nrOfCol()*nrOfRow()];
146 break;
147 case(filter::zero):
148 pout[index+i*nrOfCol()*nrOfRow()]+=taps_opt[taps_opt.size()/2+t]*0;
149 break;
150 case(filter::symmetric):
151 default:
152 pout[index+i*nrOfCol()*nrOfRow()]+=taps_opt[taps_opt.size()/2+t]*pin[index+(i-t)*nrOfCol()*nrOfRow()];
153 break;
154 }
155 }
156 }
157 }
158 }
159 }
160 }
161 catch(std::string predefinedString){
162 std::cout << predefinedString << std::endl;
163 throw;
164 }
165 }
166
167 template<typename T> void Jim::smoothNoData1d_t(Jim& imgWriter, app::AppFactory& app){
168 Optionjl<double> nodata_opt("nodata", "nodata", "nodata to interpolate",0);
169 Optionjl<std::string> interpolationType_opt("interp", "interp", "type of interpolation for spectral filtering (see http://www.gnu.org/software/gsl/manual/html_node/Interpolation-Types.html)","akima");
170
171 bool doProcess;//stop process when program was invoked with help option (-h --help)
172 try{
173 doProcess=interpolationType_opt.retrieveOption(app);
174 nodata_opt.retrieveOption(app);
175 if(!doProcess){
176 std::cout << std::endl;
177 std::ostringstream helpStream;
178 helpStream << "short option -h shows basic options only, use long option --help to show all options" << std::endl;
179 throw(helpStream.str());//help was invoked, stop processing
180 }
181 if(nrOfPlane()<2){
182 std::ostringstream errorStream;
183 errorStream << "Error: not a 3D object, consider band2plane" << std::endl;
184 throw(errorStream.str());
185 }
186 statfactory::StatFactory stat;
187 stat.setNoDataValues(nodata_opt);
188 std::vector<double> abscis(nrOfPlane());
189 for(int i=0;i<abscis.size();++i)
190 abscis[i]=i;
191 #if JIPLIB_PROCESS_IN_PARALLEL == 1
192 #pragma omp parallel for
193 #else
194 #endif
195 for(size_t iband=0;iband<nrOfBand();++iband){
196 T* pin=static_cast<T*>(getDataPointer(iband));
197 T* pout=static_cast<T*>(imgWriter.getDataPointer(iband));
198 for(size_t index=0;index<nrOfCol()*nrOfRow();++index){
199 std::vector<double> input(nrOfPlane());
200 std::vector<double> output(imgWriter.nrOfPlane());
201 for(size_t iplane=0;iplane<nrOfPlane();++iplane)
202 input[iplane]=static_cast<double>(pin[index+iplane*nrOfCol()*nrOfRow()]);
203 stat.interpolateNoData(abscis,input,interpolationType_opt[0],output);
204 for(size_t iplane=0;iplane<nrOfPlane();++iplane)
205 pout[index+iplane*nrOfCol()*nrOfRow()]=static_cast<T>(output[iplane]);
206 }
207 }
208 }
209 catch(std::string predefinedString ){
210 std::cout << predefinedString << std::endl;
211 throw;
212 }
213 }
214
215 template<typename T> void Jim::stats1d_t(Jim& imgWriter, app::AppFactory& app){
216 Optionjl<double> nodata_opt("nodata", "nodata", "nodata to interpolate",0);
217 Optionjl<std::string> methods_opt("method", "method", "statistical method");
218 Optionjl<double> threshold_opt("t", "threshold", "threshold value(s) to use for threshold filter (one for each class), or threshold to cut for dwt_cut (use 0 to keep all) or dwt_cut_from", 0);
219
220 bool doProcess;//stop process when program was invoked with help option (-h --help)
221 try{
222 doProcess=methods_opt.retrieveOption(app);
223 nodata_opt.retrieveOption(app);
224 threshold_opt.retrieveOption(app);
225 if(!doProcess){
226 std::cout << std::endl;
227 std::ostringstream helpStream;
228 helpStream << "short option -h shows basic options only, use long option --help to show all options" << std::endl;
229 throw(helpStream.str());//help was invoked, stop processing
230 }
231 if(methods_opt.empty()){
232 std::ostringstream errorStream;
233 errorStream << "Error: no method provided" << std::endl;
234 throw(errorStream.str());
235 }
236 if(nrOfBand()>1){
237 std::ostringstream errorStream;
238 errorStream << "Error: multi-band input not supported"<< std::endl;
239 throw(errorStream.str());
240 }
241 if(nrOfPlane()<2){
242 std::ostringstream errorStream;
243 errorStream << "Error: not a 3D object, consider band2plane" << std::endl;
244 throw(errorStream.str());
245 }
246 imgWriter.open(nrOfCol(),nrOfRow(),methods_opt.size(),1,getGDALDataType());
247 imgWriter.setProjection(this->getProjection());
248 double gt[6];
249 this->getGeoTransform(gt);
250 imgWriter.setGeoTransform(gt);
251 statfactory::StatFactory stat;
252 stat.setNoDataValues(nodata_opt);
253 std::vector<double> abscis(nrOfPlane());
254 for(int i=0;i<abscis.size();++i)
255 abscis[i]=i;
256 T* pin=static_cast<T*>(getDataPointer(0));
257 std::vector<T*> pout(methods_opt.size());
258 #if JIPLIB_PROCESS_IN_PARALLEL == 1
259 #pragma omp parallel for
260 #else
261 #endif
262 for(size_t imethod=0;imethod<methods_opt.size();++imethod){
263 pout[imethod]=static_cast<T*>(imgWriter.getDataPointer(imethod));
264 for(size_t index=0;index<nrOfCol()*nrOfRow();++index){
265 std::vector<double> input(nrOfPlane());
266 for(size_t iplane=0;iplane<nrOfPlane();++iplane)
267 input[iplane]=static_cast<double>(pin[index+iplane*nrOfCol()*nrOfRow()]);
268 switch(filter::Filter::getFilterType(methods_opt[imethod])){
269 case(filter::first):
270 pout[imethod][index]=input[0];
271 break;
272 case(filter::last):
273 pout[imethod][index]=input.back();
274 break;
275 case(filter::nvalid):
276 pout[imethod][index]=stat.nvalid(input);
277 break;
278 case(filter::median):
279 pout[imethod][index]=stat.median(input);
280 break;
281 case(filter::min):
282 pout[imethod][index]=stat.mymin(input);
283 break;
284 case(filter::max):
285 pout[imethod][index]=stat.mymax(input);
286 break;
287 case(filter::sum):
288 pout[imethod][index]=stat.sum(input);
289 break;
290 case(filter::var):
291 pout[imethod][index]=stat.var(input);
292 break;
293 case(filter::stdev):
294 pout[imethod][index]=sqrt(stat.var(input));
295 break;
296 case(filter::mean):
297 pout[imethod][index]=stat.mean(input);
298 break;
299 case(filter::percentile):{
300 /* double threshold=(ithreshold<m_threshold.size())? m_threshold[ithreshold] : m_threshold[0]; */
301 pout[imethod][index]=stat.percentile(input,input.begin(),input.end(),threshold_opt[0]);
302 break;
303 }
304 default:
305 std::string errorString="method not supported";
306 throw(errorString);
307 break;
308 }
309 }
310 }
311 }
312 catch(std::string predefinedString ){
313 std::cout << predefinedString << std::endl;
314 throw;
315 }
316 }
317
318 #endif // _JLFILTER1D_LIB_H_