|
| 1 | +// This file is part of OpenCV project. |
| 2 | +// It is subject to the license terms in the LICENSE file found in the top-level directory |
| 3 | +// of this distribution and at http://opencv.org/license.html. |
| 4 | + |
| 5 | +#include "precomp.hpp" |
| 6 | +#include "opencv2/photometric_calib/GammaRemover.hpp" |
| 7 | + |
| 8 | +namespace cv { namespace photometric_calib { |
| 9 | + |
| 10 | +GammaRemover::GammaRemover(const std::string &gammaPath, int w_, int h_) |
| 11 | +{ |
| 12 | + validGamma = false; |
| 13 | + w = w_; |
| 14 | + h = h_; |
| 15 | + |
| 16 | + // check the extension of the time file. |
| 17 | + CV_Assert(gammaPath.substr(gammaPath.find_last_of(".") + 1) == "yaml" || gammaPath.substr(gammaPath.find_last_of(".") + 1) == "yml"); |
| 18 | + |
| 19 | + FileStorage gammaFile; |
| 20 | + gammaFile.open(gammaPath, FileStorage::READ); |
| 21 | + CV_Assert(gammaFile.isOpened()); |
| 22 | + |
| 23 | + FileNode gammaNode = gammaFile["gamma"]; |
| 24 | + CV_Assert(gammaNode.type() == FileNode::SEQ); |
| 25 | + FileNodeIterator itS = gammaNode.begin(), itE = gammaNode.end(); |
| 26 | + std::vector<float> GInvVec; |
| 27 | + for (; itS != itE; ++itS) |
| 28 | + GInvVec.push_back((float)*itS); |
| 29 | + CV_Assert(GInvVec.size() == 256); |
| 30 | + |
| 31 | + for(int i=0;i<256;i++) GInv[i] = GInvVec[i]; |
| 32 | + for(int i=0;i<255;i++) |
| 33 | + { |
| 34 | + CV_Assert(GInv[i+1] > GInv[i]); |
| 35 | + } |
| 36 | + float min = GInv[0]; |
| 37 | + float max = GInv[255]; |
| 38 | + for(int i=0;i<256;i++) GInv[i] = 255.0 * (GInv[i] - min) / (max-min); |
| 39 | + for(int i=1;i<255;i++) |
| 40 | + { |
| 41 | + for(int s=1;s<255;s++) |
| 42 | + { |
| 43 | + if(GInv[s] <= i && GInv[s+1] >= i) |
| 44 | + { |
| 45 | + G[i] = s+(i - GInv[s]) / (GInv[s+1]-GInv[s]); |
| 46 | + break; |
| 47 | + } |
| 48 | + } |
| 49 | + } |
| 50 | + G[0] = 0; |
| 51 | + G[255] = 255; |
| 52 | + gammaFile.release(); |
| 53 | + validGamma=true; |
| 54 | +} |
| 55 | + |
| 56 | +Mat GammaRemover::getUnGammaImageMat(Mat inputIm) |
| 57 | +{ |
| 58 | + CV_Assert(validGamma); |
| 59 | + uchar *inputImArr = inputIm.data; |
| 60 | + float outImArr[w*h]; |
| 61 | + for (int i = 0; i < w * h; ++i) |
| 62 | + { |
| 63 | + outImArr[i] = GInv[inputImArr[i]]; |
| 64 | + } |
| 65 | + Mat _outIm(h, w, CV_32F, outImArr); |
| 66 | + Mat outIm = _outIm * (1/255.0f); |
| 67 | + return outIm; |
| 68 | +} |
| 69 | + |
| 70 | +void GammaRemover::getUnGammaImageArr(Mat inputIm, std::vector<float> &outImVec) |
| 71 | +{ |
| 72 | + CV_Assert(validGamma); |
| 73 | + uchar *inputImArr = inputIm.data; |
| 74 | + CV_Assert(outImVec.size() == (unsigned long)w * h); |
| 75 | + for(int i = 0; i < w * h; i++) outImVec[i] = GInv[inputImArr[i]]; |
| 76 | +} |
| 77 | + |
| 78 | +}} |
0 commit comments