|
| 1 | +#include "precomp.hpp" |
| 2 | +#include "opencv2/imgproc.hpp" |
| 3 | +#include "opencv2/highgui.hpp" |
| 4 | +#include "opencv2/core.hpp" |
| 5 | + |
| 6 | + |
| 7 | + |
| 8 | +#include <iostream> |
| 9 | +#include <fstream> |
| 10 | +#include <sstream> |
| 11 | +#include <queue> |
| 12 | +#include <algorithm> |
| 13 | +#include <iosfwd> |
| 14 | +#include <memory> |
| 15 | +#include <string> |
| 16 | +#include <utility> |
| 17 | +#include <vector> |
| 18 | + |
| 19 | +namespace cv { namespace text { |
| 20 | +//************************************************************************************ |
| 21 | +//****************** ImagePreprocessor ******************************************* |
| 22 | +//************************************************************************************ |
| 23 | + |
| 24 | +void ImagePreprocessor::preprocess(InputArray input,OutputArray output,Size sz,int outputChannels){ |
| 25 | + Mat inpImg=input.getMat(); |
| 26 | + Mat outImg; |
| 27 | + this->preprocess_(inpImg,outImg,sz,outputChannels); |
| 28 | + outImg.copyTo(output); |
| 29 | +} |
| 30 | +void ImagePreprocessor::set_mean(Mat mean){ |
| 31 | + |
| 32 | + |
| 33 | + this->set_mean_(mean); |
| 34 | + |
| 35 | +} |
| 36 | + |
| 37 | + |
| 38 | + |
| 39 | +class ResizerPreprocessor: public ImagePreprocessor{ |
| 40 | +protected: |
| 41 | + void preprocess_(const Mat& input,Mat& output,Size outputSize,int outputChannels){ |
| 42 | + //TODO put all the logic of channel and depth conversions in ImageProcessor class |
| 43 | + CV_Assert(outputChannels==1 || outputChannels==3); |
| 44 | + CV_Assert(input.channels()==1 || input.channels()==3); |
| 45 | + if(input.channels()!=outputChannels) |
| 46 | + { |
| 47 | + Mat tmpInput; |
| 48 | + if(outputChannels==1){ |
| 49 | + cvtColor(input,tmpInput,COLOR_BGR2GRAY); |
| 50 | + if(input.depth()==CV_8U) |
| 51 | + { |
| 52 | + tmpInput.convertTo(output,CV_32FC1,1/255.0); |
| 53 | + }else |
| 54 | + {//Assuming values are at the desired [0,1] range |
| 55 | + tmpInput.convertTo(output, CV_32FC1); |
| 56 | + } |
| 57 | + }else |
| 58 | + { |
| 59 | + cvtColor(input,tmpInput,COLOR_GRAY2BGR); |
| 60 | + if(input.depth()==CV_8U) |
| 61 | + { |
| 62 | + tmpInput.convertTo(output,CV_32FC3,1/255.0); |
| 63 | + }else |
| 64 | + {//Assuming values are at the desired [0,1] range |
| 65 | + tmpInput.convertTo(output, CV_32FC3); |
| 66 | + } |
| 67 | + } |
| 68 | + }else |
| 69 | + { |
| 70 | + if(input.channels()==1) |
| 71 | + { |
| 72 | + if(input.depth()==CV_8U) |
| 73 | + { |
| 74 | + input.convertTo(output, CV_32FC1,1/255.0); |
| 75 | + }else |
| 76 | + {//Assuming values are at the desired [0,1] range |
| 77 | + input.convertTo(output, CV_32FC1); |
| 78 | + } |
| 79 | + }else |
| 80 | + { |
| 81 | + if(input.depth()==CV_8U){ |
| 82 | + input.convertTo(output, CV_32FC3,1/255.0); |
| 83 | + }else |
| 84 | + {//Assuming values are at the desired [0,1] range |
| 85 | + input.convertTo(output, CV_32FC3); |
| 86 | + } |
| 87 | + } |
| 88 | + } |
| 89 | + if(outputSize.width!=0 && outputSize.height!=0) |
| 90 | + { |
| 91 | + resize(output,output,outputSize); |
| 92 | + } |
| 93 | + } |
| 94 | + //void set_mean_(Mat m){} |
| 95 | +public: |
| 96 | + ResizerPreprocessor(){} |
| 97 | + ~ResizerPreprocessor(){} |
| 98 | +}; |
| 99 | + |
| 100 | +class StandarizerPreprocessor: public ImagePreprocessor{ |
| 101 | +protected: |
| 102 | + double sigma_; |
| 103 | + //void set_mean_(Mat M){} |
| 104 | + |
| 105 | + void preprocess_(const Mat& input,Mat& output,Size outputSize,int outputChannels){ |
| 106 | + |
| 107 | + //TODO put all the logic of channel and depth conversions in ImageProcessor class |
| 108 | + CV_Assert(outputChannels==1 || outputChannels==3); |
| 109 | + CV_Assert(input.channels()==1 || input.channels()==3); |
| 110 | + if(input.channels()!=outputChannels) |
| 111 | + { |
| 112 | + Mat tmpInput; |
| 113 | + if(outputChannels==1) |
| 114 | + { |
| 115 | + cvtColor(input,tmpInput,COLOR_BGR2GRAY); |
| 116 | + if(input.depth()==CV_8U) |
| 117 | + { |
| 118 | + tmpInput.convertTo(output,CV_32FC1,1/255.0); |
| 119 | + }else |
| 120 | + {//Assuming values are at the desired [0,1] range |
| 121 | + tmpInput.convertTo(output, CV_32FC1); |
| 122 | + } |
| 123 | + }else |
| 124 | + { |
| 125 | + cvtColor(input,tmpInput,COLOR_GRAY2BGR); |
| 126 | + if(input.depth()==CV_8U) |
| 127 | + { |
| 128 | + tmpInput.convertTo(output,CV_32FC3,1/255.0); |
| 129 | + }else |
| 130 | + {//Assuming values are at the desired [0,1] range |
| 131 | + tmpInput.convertTo(output, CV_32FC3); |
| 132 | + } |
| 133 | + } |
| 134 | + }else |
| 135 | + { |
| 136 | + if(input.channels()==1) |
| 137 | + { |
| 138 | + if(input.depth()==CV_8U) |
| 139 | + { |
| 140 | + input.convertTo(output, CV_32FC1,1/255.0); |
| 141 | + }else |
| 142 | + {//Assuming values are at the desired [0,1] range |
| 143 | + input.convertTo(output, CV_32FC1); |
| 144 | + } |
| 145 | + }else |
| 146 | + { |
| 147 | + if(input.depth()==CV_8U) |
| 148 | + { |
| 149 | + input.convertTo(output, CV_32FC3,1/255.0); |
| 150 | + }else |
| 151 | + {//Assuming values are at the desired [0,1] range |
| 152 | + input.convertTo(output, CV_32FC3); |
| 153 | + } |
| 154 | + } |
| 155 | + } |
| 156 | + if(outputSize.width!=0 && outputSize.height!=0) |
| 157 | + { |
| 158 | + resize(output,output,outputSize); |
| 159 | + } |
| 160 | + |
| 161 | + Scalar mean,dev; |
| 162 | + meanStdDev(output,mean,dev); |
| 163 | + subtract(output,mean[0],output); |
| 164 | + divide(output,(dev[0]/sigma_),output); |
| 165 | + } |
| 166 | +public: |
| 167 | + StandarizerPreprocessor(double sigma):sigma_(sigma){} |
| 168 | + ~StandarizerPreprocessor(){} |
| 169 | + |
| 170 | +}; |
| 171 | + |
| 172 | +class customPreprocessor:public ImagePreprocessor{ |
| 173 | +protected: |
| 174 | + |
| 175 | + double rawval_; |
| 176 | + Mat mean_; |
| 177 | + String channel_order_; |
| 178 | + |
| 179 | + void set_mean_(Mat imMean_){ |
| 180 | + |
| 181 | + imMean_.copyTo(this->mean_); |
| 182 | + |
| 183 | + |
| 184 | + } |
| 185 | + |
| 186 | + void set_raw_scale(int rawval){ |
| 187 | + rawval_ = rawval; |
| 188 | + |
| 189 | + } |
| 190 | + void set_channels(String channel_order){ |
| 191 | + channel_order_=channel_order; |
| 192 | + } |
| 193 | + |
| 194 | + |
| 195 | + void preprocess_(const Mat& input,Mat& output,Size outputSize,int outputChannels){ |
| 196 | + //TODO put all the logic of channel and depth conversions in ImageProcessor class |
| 197 | + |
| 198 | + CV_Assert(outputChannels==1 || outputChannels==3); |
| 199 | + CV_Assert(input.channels()==1 || input.channels()==3); |
| 200 | + if(input.channels()!=outputChannels) |
| 201 | + { |
| 202 | + Mat tmpInput; |
| 203 | + if(outputChannels==1) |
| 204 | + { |
| 205 | + cvtColor(input,tmpInput,COLOR_BGR2GRAY); |
| 206 | + if(input.depth()==CV_8U) |
| 207 | + { |
| 208 | + if (rawval_ == 1) |
| 209 | + tmpInput.convertTo(output,CV_32FC3,1/255.0); |
| 210 | + else |
| 211 | + tmpInput.convertTo(output,CV_32FC1); |
| 212 | + }else |
| 213 | + {//Assuming values are at the desired [0,1] range |
| 214 | + if (rawval_ ==1) |
| 215 | + tmpInput.convertTo(output, CV_32FC1); |
| 216 | + else |
| 217 | + tmpInput.convertTo(output, CV_32FC1,rawval_); |
| 218 | + } |
| 219 | + }else |
| 220 | + { |
| 221 | + cvtColor(input,tmpInput,COLOR_GRAY2BGR); |
| 222 | + if(input.depth()==CV_8U) |
| 223 | + { |
| 224 | + if (rawval_ == 1) |
| 225 | + tmpInput.convertTo(output,CV_32FC3,1/255.0); |
| 226 | + else |
| 227 | + tmpInput.convertTo(output,CV_32FC1); |
| 228 | + }else |
| 229 | + {//Assuming values are at the desired [0,1] range |
| 230 | + if (rawval_ ==1) |
| 231 | + tmpInput.convertTo(output, CV_32FC1); |
| 232 | + else |
| 233 | + tmpInput.convertTo(output, CV_32FC1,rawval_); |
| 234 | + } |
| 235 | + } |
| 236 | + }else |
| 237 | + { |
| 238 | + if(input.channels()==1) |
| 239 | + { |
| 240 | + if(input.depth()==CV_8U) |
| 241 | + { |
| 242 | + if (rawval_ == 1) |
| 243 | + input.convertTo(output,CV_32FC1,1/255.0); |
| 244 | + else |
| 245 | + input.convertTo(output,CV_32FC1); |
| 246 | + }else |
| 247 | + {//Assuming values are at the desired [0,1] range |
| 248 | + if (rawval_ ==1) |
| 249 | + input.convertTo(output, CV_32FC1); |
| 250 | + else |
| 251 | + input.convertTo(output, CV_32FC1,rawval_); |
| 252 | + } |
| 253 | + }else |
| 254 | + { |
| 255 | + if(input.depth()==CV_8U) |
| 256 | + { |
| 257 | + if (rawval_ == 1) |
| 258 | + input.convertTo(output,CV_32FC3,1/255.0); |
| 259 | + else |
| 260 | + input.convertTo(output,CV_32FC3); |
| 261 | + }else |
| 262 | + {//Assuming values are at the desired [0,1] range |
| 263 | + if (rawval_ ==1) |
| 264 | + input.convertTo(output, CV_32FC3); |
| 265 | + else |
| 266 | + input.convertTo(output, CV_32FC3,rawval_); |
| 267 | + } |
| 268 | + } |
| 269 | + } |
| 270 | + if(outputSize.width!=0 && outputSize.height!=0) |
| 271 | + { |
| 272 | + resize(output,output,outputSize); |
| 273 | + } |
| 274 | + |
| 275 | + if (!this->mean_.empty()){ |
| 276 | + |
| 277 | + Scalar mean_s(this->mean_.at<uchar>(0,0),this->mean_.at<uchar>(0,1),this->mean_.at<uchar>(0,2)); |
| 278 | + subtract(output,mean_s,output); |
| 279 | + } |
| 280 | + else{ |
| 281 | + Scalar mean_s; |
| 282 | + mean_s = mean(output); |
| 283 | + subtract(output,mean_s,output); |
| 284 | + } |
| 285 | + |
| 286 | + } |
| 287 | + |
| 288 | +public: |
| 289 | + customPreprocessor( double rawval,String channel_order):rawval_(rawval),channel_order_(channel_order){} |
| 290 | + ~customPreprocessor(){} |
| 291 | + |
| 292 | +}; |
| 293 | + |
| 294 | +class MeanSubtractorPreprocessor: public ImagePreprocessor{ |
| 295 | +protected: |
| 296 | + Mat mean_; |
| 297 | + //void set_mean_(Mat m){} |
| 298 | + void preprocess_(const Mat& input,Mat& output,Size outputSize,int outputChannels){ |
| 299 | + //TODO put all the logic of channel and depth conversions in ImageProcessor class |
| 300 | + CV_Assert(this->mean_.cols==outputSize.width && this->mean_.rows ==outputSize.height); |
| 301 | + CV_Assert(outputChannels==1 || outputChannels==3); |
| 302 | + CV_Assert(input.channels()==1 || input.channels()==3); |
| 303 | + if(input.channels()!=outputChannels) |
| 304 | + { |
| 305 | + Mat tmpInput; |
| 306 | + if(outputChannels==1) |
| 307 | + { |
| 308 | + cvtColor(input,tmpInput,COLOR_BGR2GRAY); |
| 309 | + if(input.depth()==CV_8U) |
| 310 | + { |
| 311 | + tmpInput.convertTo(output,CV_32FC1,1/255.0); |
| 312 | + }else |
| 313 | + {//Assuming values are at the desired [0,1] range |
| 314 | + tmpInput.convertTo(output, CV_32FC1); |
| 315 | + } |
| 316 | + }else |
| 317 | + { |
| 318 | + cvtColor(input,tmpInput,COLOR_GRAY2BGR); |
| 319 | + if(input.depth()==CV_8U) |
| 320 | + { |
| 321 | + tmpInput.convertTo(output,CV_32FC3,1/255.0); |
| 322 | + }else |
| 323 | + {//Assuming values are at the desired [0,1] range |
| 324 | + tmpInput.convertTo(output, CV_32FC3); |
| 325 | + } |
| 326 | + } |
| 327 | + }else |
| 328 | + { |
| 329 | + if(input.channels()==1) |
| 330 | + { |
| 331 | + if(input.depth()==CV_8U) |
| 332 | + { |
| 333 | + input.convertTo(output, CV_32FC1,1/255.0); |
| 334 | + }else |
| 335 | + {//Assuming values are at the desired [0,1] range |
| 336 | + input.convertTo(output, CV_32FC1); |
| 337 | + } |
| 338 | + }else |
| 339 | + { |
| 340 | + if(input.depth()==CV_8U) |
| 341 | + { |
| 342 | + input.convertTo(output, CV_32FC3,1/255.0); |
| 343 | + }else |
| 344 | + {//Assuming values are at the desired [0,1] range |
| 345 | + input.convertTo(output, CV_32FC3); |
| 346 | + } |
| 347 | + } |
| 348 | + } |
| 349 | + if(outputSize.width!=0 && outputSize.height!=0) |
| 350 | + { |
| 351 | + resize(output,output,outputSize); |
| 352 | + } |
| 353 | + subtract(output,this->mean_,output); |
| 354 | + } |
| 355 | +public: |
| 356 | + MeanSubtractorPreprocessor(Mat mean) |
| 357 | + { |
| 358 | + mean.copyTo(this->mean_); |
| 359 | + } |
| 360 | + |
| 361 | + ~MeanSubtractorPreprocessor(){} |
| 362 | +}; |
| 363 | + |
| 364 | + |
| 365 | + |
| 366 | +Ptr<ImagePreprocessor> ImagePreprocessor::createResizer() |
| 367 | +{ |
| 368 | + return Ptr<ImagePreprocessor>(new ResizerPreprocessor); |
| 369 | +} |
| 370 | + |
| 371 | +Ptr<ImagePreprocessor> ImagePreprocessor::createImageStandarizer(double sigma) |
| 372 | +{ |
| 373 | + return Ptr<ImagePreprocessor>(new StandarizerPreprocessor(sigma)); |
| 374 | +} |
| 375 | +Ptr<ImagePreprocessor> ImagePreprocessor::createImageCustomPreprocessor(double rawval,String channel_order) |
| 376 | +{ |
| 377 | + |
| 378 | + return Ptr<ImagePreprocessor>(new customPreprocessor(rawval,channel_order)); |
| 379 | +} |
| 380 | + |
| 381 | +Ptr<ImagePreprocessor> ImagePreprocessor::createImageMeanSubtractor(InputArray meanImg) |
| 382 | +{ |
| 383 | + Mat tmp=meanImg.getMat(); |
| 384 | + return Ptr<ImagePreprocessor>(new MeanSubtractorPreprocessor(tmp)); |
| 385 | +} |
| 386 | +} |
| 387 | +} |
0 commit comments