Skip to content

Commit 5c04c02

Browse files
committed
Camera response function(Gamma function) remover.
1 parent b1ee858 commit 5c04c02

File tree

2 files changed

+119
-0
lines changed

2 files changed

+119
-0
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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+
#ifndef _OPENCV_GAMMAREMOVER_HPP
6+
#define _OPENCV_GAMMAREMOVER_HPP
7+
8+
#include "opencv2/photometric_calib.hpp"
9+
10+
namespace cv { namespace photometric_calib {
11+
12+
class CV_EXPORTS GammaRemover
13+
{
14+
public:
15+
GammaRemover(const std::string &gammaPath, int w_, int h_);
16+
17+
Mat getUnGammaImageMat(Mat inputIm);
18+
void getUnGammaImageArr(Mat inputIm, std::vector<float> &outImVec);
19+
20+
inline float* getG()
21+
{
22+
if(!validGamma) return nullptr;
23+
else return G;
24+
};
25+
inline float* getGInv()
26+
{
27+
if(!validGamma) return nullptr;
28+
else return GInv;
29+
};
30+
31+
private:
32+
float G[256];
33+
float GInv[256];
34+
int w,h;
35+
bool validGamma;
36+
};
37+
38+
39+
}}
40+
41+
#endif //_OPENCV__GAMMAREMOVER_HPP
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
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

Comments
 (0)