20 #include "ImgRegression.h" 25 ImgRegression::ImgRegression(
void)
26 : m_threshold(0), m_down(1)
29 ImgRegression::~ImgRegression(
void)
32 double ImgRegression::getRMSE(
ImgReaderGdal& imgReader1,
ImgReaderGdal& imgReader2,
double& c0,
double& c1,
unsigned short band1,
unsigned short band2,
short verbose)
const{
36 std::vector<double> rowBuffer1(imgReader1.
nrOfCol());
37 std::vector<double> rowBuffer2(imgReader2.
nrOfCol());
38 std::vector<double> buffer1;
39 std::vector<double> buffer2;
42 for(irow1=0;irow1<imgReader1.
nrOfRow();++irow1){
46 double icol2=0,irow2=0;
48 imgReader1.
readData(rowBuffer1,GDT_Float64,irow1,band1);
49 imgReader1.
image2geo(icol1,irow1,geox,geoy);
50 imgReader2.
geo2image(geox,geoy,icol2,irow2);
51 icol2=
static_cast<int>(icol2);
52 irow2=
static_cast<int>(irow2);
53 if(irow2<0||irow2>=imgReader2.
nrOfRow())
55 imgReader2.
readData(rowBuffer2,GDT_Float64,irow2,band2);
56 for(icol1=0;icol1<imgReader1.
nrOfCol();++icol1){
60 double p=
static_cast<double>(rand())/(RAND_MAX);
65 imgReader1.
image2geo(icol1,irow1,geox,geoy);
66 imgReader2.
geo2image(geox,geoy,icol2,irow2);
67 if(icol2<0||icol2>=imgReader2.
nrOfCol())
69 icol2=
static_cast<int>(icol2);
70 irow2=
static_cast<int>(irow2);
72 double value1=rowBuffer1[icol1];
73 double value2=rowBuffer2[icol2];
77 buffer1.push_back(value1);
78 buffer2.push_back(value2);
80 std::cout << geox <<
" " << geoy <<
" " << icol1 <<
" " << irow1 <<
" " << icol2 <<
" " << irow2 <<
" " << buffer1.back() <<
" " << buffer2.back() << std::endl;
84 if(buffer1.size()&&buffer2.size()){
86 err=stat.linear_regression_err(buffer1,buffer2,c0,c1);
89 std::cout <<
"linear regression based on " << buffer1.size() <<
" points: " << c0 <<
"+" << c1 <<
" * x " <<
" with rmse: " << err << std::endl;
93 double ImgRegression::getR2(
ImgReaderGdal& imgReader1,
ImgReaderGdal& imgReader2,
double& c0,
double& c1,
unsigned short band1,
unsigned short band2,
short verbose)
const{
97 std::vector<double> rowBuffer1(imgReader1.
nrOfCol());
98 std::vector<double> rowBuffer2(imgReader2.
nrOfCol());
99 std::vector<double> buffer1;
100 std::vector<double> buffer2;
103 for(irow1=0;irow1<imgReader1.
nrOfRow();++irow1){
107 double icol2=0,irow2=0;
108 double geox=0,geoy=0;
109 imgReader1.
readData(rowBuffer1,GDT_Float64,irow1,band1);
110 imgReader1.
image2geo(icol1,irow1,geox,geoy);
111 imgReader2.
geo2image(geox,geoy,icol2,irow2);
112 icol2=
static_cast<int>(icol2);
113 irow2=
static_cast<int>(irow2);
114 if(irow2<0||irow2>=imgReader2.
nrOfRow())
116 imgReader2.
readData(rowBuffer2,GDT_Float64,irow2,band2);
117 for(icol1=0;icol1<imgReader1.
nrOfCol();++icol1){
121 double p=
static_cast<double>(rand())/(RAND_MAX);
126 imgReader1.
image2geo(icol1,irow1,geox,geoy);
127 imgReader2.
geo2image(geox,geoy,icol2,irow2);
128 if(icol2<0||icol2>=imgReader2.
nrOfCol())
130 icol2=
static_cast<int>(icol2);
131 irow2=
static_cast<int>(irow2);
133 double value1=rowBuffer1[icol1];
134 double value2=rowBuffer2[icol2];
138 buffer1.push_back(value1);
139 buffer2.push_back(value2);
141 std::cout << geox <<
" " << geoy <<
" " << icol1 <<
" " << irow1 <<
" " << icol2 <<
" " << irow2 <<
" " << buffer1.back() <<
" " << buffer2.back() << std::endl;
145 if(buffer1.size()&&buffer2.size()){
147 r2=stat.linear_regression(buffer1,buffer2,c0,c1);
150 std::cout <<
"linear regression based on " << buffer1.size() <<
" points: " << c0 <<
"+" << c1 <<
" * x " <<
" with r^2: " << r2 << std::endl;
154 double ImgRegression::pgetR2(
ImgReaderGdal& imgReader1,
ImgReaderGdal& imgReader2,
double& c0,
double& c1,
unsigned short band1,
unsigned short band2,
short verbose)
const{
158 std::vector<double> rowBuffer1(imgReader1.
nrOfCol());
159 std::vector<double> rowBuffer2(imgReader2.
nrOfCol());
160 std::vector<double> buffer1;
161 std::vector<double> buffer2;
164 for(irow1=0;irow1<imgReader1.
nrOfRow();++irow1){
168 double icol2=0,irow2=0;
169 double geox=0,geoy=0;
170 imgReader1.
readData(rowBuffer1,GDT_Float64,irow1,band1);
171 imgReader1.
image2geo(icol1,irow1,geox,geoy);
172 imgReader2.
geo2image(geox,geoy,icol2,irow2);
173 icol2=
static_cast<int>(icol2);
174 irow2=
static_cast<int>(irow2);
175 if(irow2<0||irow2>=imgReader2.
nrOfRow())
177 imgReader2.
readData(rowBuffer2,GDT_Float64,irow2,band2);
178 for(icol1=0;icol1<imgReader1.
nrOfCol();++icol1){
182 double p=
static_cast<double>(rand())/(RAND_MAX);
187 imgReader1.
image2geo(icol1,irow1,geox,geoy);
188 imgReader2.
geo2image(geox,geoy,icol2,irow2);
189 if(icol2<0||icol2>=imgReader2.
nrOfCol())
191 icol2=
static_cast<int>(icol2);
192 irow2=
static_cast<int>(irow2);
194 double value1=rowBuffer1[icol1];
195 double value2=rowBuffer2[icol2];
199 buffer1.push_back(value1);
200 buffer2.push_back(value2);
202 std::cout << geox <<
" " << geoy <<
" " << icol1 <<
" " << irow1 <<
" " << icol2 <<
" " << irow2 <<
" " << buffer1.back() <<
" " << buffer2.back() << std::endl;
206 if(buffer1.size()&&buffer2.size()){
208 r=stat.correlation(buffer1,buffer2);
214 stat.meanVar(buffer1,m1,v1);
215 stat.meanVar(buffer2,m2,v2);
225 std::cout <<
"orthogonal regression based on " << buffer1.size() <<
" points: " << c0 <<
"+" << c1 <<
" * x " <<
" with r^2: " << r*r << std::endl;
229 double ImgRegression::getRMSE(
ImgReaderGdal& imgReader,
unsigned short band1,
unsigned short band2,
double& c0,
double& c1,
short verbose)
const{
233 std::vector<double> rowBuffer1(imgReader.
nrOfCol());
234 std::vector<double> rowBuffer2(imgReader.
nrOfCol());
235 std::vector<double> buffer1;
236 std::vector<double> buffer2;
243 for(irow=0;irow<imgReader.
nrOfRow();++irow){
247 imgReader.
readData(rowBuffer1,GDT_Float64,irow,band1);
248 imgReader.
readData(rowBuffer2,GDT_Float64,irow,band2);
249 for(icol=0;icol<imgReader.
nrOfCol();++icol){
253 double p=
static_cast<double>(rand())/(RAND_MAX);
259 double value1=rowBuffer1[icol];
260 double value2=rowBuffer2[icol];
264 buffer1.push_back(value1);
265 buffer2.push_back(value2);
267 std::cout << icol <<
" " << irow <<
" " << buffer1.back() <<
" " << buffer2.back() << std::endl;
271 if(buffer1.size()&&buffer2.size()){
273 err=stat.linear_regression_err(buffer1,buffer2,c0,c1);
276 std::cout <<
"linear regression based on " << buffer1.size() <<
" points: " << c0 <<
"+" << c1 <<
" * x " <<
" with rmse: " << err << std::endl;
280 double ImgRegression::getR2(
ImgReaderGdal& imgReader,
unsigned short band1,
unsigned short band2,
double& c0,
double& c1,
short verbose)
const{
284 std::vector<double> rowBuffer1(imgReader.
nrOfCol());
285 std::vector<double> rowBuffer2(imgReader.
nrOfCol());
286 std::vector<double> buffer1;
287 std::vector<double> buffer2;
294 for(irow=0;irow<imgReader.
nrOfRow();++irow){
298 imgReader.
readData(rowBuffer1,GDT_Float64,irow,band1);
299 imgReader.
readData(rowBuffer2,GDT_Float64,irow,band2);
300 for(icol=0;icol<imgReader.
nrOfCol();++icol){
304 double p=
static_cast<double>(rand())/(RAND_MAX);
310 double value1=rowBuffer1[icol];
311 double value2=rowBuffer2[icol];
315 buffer1.push_back(value1);
316 buffer2.push_back(value2);
318 std::cout << icol <<
" " << irow <<
" " << buffer1.back() <<
" " << buffer2.back() << std::endl;
322 if(buffer1.size()&&buffer2.size()){
324 r2=stat.linear_regression(buffer1,buffer2,c0,c1);
327 std::cout <<
"linear regression based on " << buffer1.size() <<
" points: " << c0 <<
"+" << c1 <<
" * x " <<
" with r^2: " << r2 << std::endl;
331 double ImgRegression::pgetR2(
ImgReaderGdal& imgReader,
unsigned short band1,
unsigned short band2,
double& c0,
double& c1,
short verbose)
const{
335 std::vector<double> rowBuffer1(imgReader.
nrOfCol());
336 std::vector<double> rowBuffer2(imgReader.
nrOfCol());
337 std::vector<double> buffer1;
338 std::vector<double> buffer2;
345 for(irow=0;irow<imgReader.
nrOfRow();++irow){
349 imgReader.
readData(rowBuffer1,GDT_Float64,irow,band1);
350 imgReader.
readData(rowBuffer2,GDT_Float64,irow,band2);
351 for(icol=0;icol<imgReader.
nrOfCol();++icol){
355 double p=
static_cast<double>(rand())/(RAND_MAX);
361 double value1=rowBuffer1[icol];
362 double value2=rowBuffer2[icol];
366 buffer1.push_back(value1);
367 buffer2.push_back(value2);
369 std::cout << icol <<
" " << irow <<
" " << buffer1.back() <<
" " << buffer2.back() << std::endl;
373 if(buffer1.size()&&buffer2.size()){
375 r=stat.correlation(buffer1,buffer2);
381 stat.meanVar(buffer1,m1,v1);
382 stat.meanVar(buffer2,m2,v2);
392 std::cout <<
"orthogonal regression based on " << buffer1.size() <<
" points: " << c0 <<
"+" << c1 <<
" * x " <<
" with r^2: " << r*r << std::endl;
bool geo2image(double x, double y, double &i, double &j) const
Convert georeferenced coordinates (x and y) to image coordinates (column and row) ...
bool isNoData(double value) const
Check if value is nodata in this dataset.
int nrOfCol(void) const
Get the number of columns of this dataset.
void readData(T &value, int col, int row, int band=0)
Read a single pixel cell value at a specific column and row for a specific band (all indices start co...
int nrOfRow(void) const
Get the number of rows of this dataset.
bool image2geo(double i, double j, double &x, double &y) const
Convert image coordinates (column and row) to georeferenced coordinates (x and y) ...
int nrOfBand(void) const
Get the number of bands of this dataset.