diff --git a/CMakeLists.txt b/CMakeLists.txt old mode 100755 new mode 100644 index 2ad0904..f1e2b4c --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -5,6 +5,8 @@ CMAKE_MINIMUM_REQUIRED(VERSION 2.6) set(BUILD_TYPE Release) +set(CMAKE_GENERATOR "Ninja") + set(CMAKE_MODULE_PATH "${CMAKE_MODULE_PATH}" "${CMAKE_CURRENT_SOURCE_DIR}/cmake") set(EXECUTABLE_OUTPUT_PATH bin) @@ -25,10 +27,10 @@ include_directories( ) -add_executable(responseCalib src/main_responseCalib.cpp src/FOVUndistorter.cpp src/PhotometricUndistorter.cpp) +add_executable(responseCalib src/main_responseCalib.cpp src/FOVUndistorter.cpp src/Undistorter.cpp src/PhotometricUndistorter.cpp) target_link_libraries(responseCalib ${OpenCV_LIBS} ${LIBZIP_LIBRARY}) -add_executable(playDataset src/main_playbackDataset.cpp src/FOVUndistorter.cpp src/PhotometricUndistorter.cpp) +add_executable(playDataset src/main_playbackDataset.cpp src/FOVUndistorter.cpp src/Undistorter.cpp src/PhotometricUndistorter.cpp) target_link_libraries(playDataset ${OpenCV_LIBS} ${LIBZIP_LIBRARY}) @@ -40,7 +42,7 @@ SET(CMAKE_MODULE_PATH ${CMAKE_INSTALL_PREFIX}/lib/cmake/ ) find_package(aruco) IF(aruco_FOUND) - add_executable(vignetteCalib src/main_vignetteCalib.cpp src/FOVUndistorter.cpp src/PhotometricUndistorter.cpp) + add_executable(vignetteCalib src/main_vignetteCalib.cpp src/FOVUndistorter.cpp src/Undistorter.cpp src/PhotometricUndistorter.cpp) target_link_libraries(vignetteCalib ${OpenCV_LIBS} ${aruco_LIBS} ${LIBZIP_LIBRARY}) ELSE() message("================ aruco not found. not compiling vignetteCalib. ========================") diff --git a/src/BenchmarkDatasetReader.h b/src/BenchmarkDatasetReader.h old mode 100755 new mode 100644 index 6dafdc1..143a17e --- a/src/BenchmarkDatasetReader.h +++ b/src/BenchmarkDatasetReader.h @@ -35,7 +35,8 @@ #include #include "opencv2/opencv.hpp" -#include "FOVUndistorter.h" +//#include "FOVUndistorter.h" +#include "Undistorter.h" #include "PhotometricUndistorter.h" #include "zip.h" @@ -132,7 +133,8 @@ class DatasetReader // create undistorter. - undistorter = new UndistorterFOV((path+"camera.txt").c_str()); + //undistorter = new UndistorterFOV((path+"camera.txt").c_str()); + undistorter = Undistort::getUndistorterForFile((path+"camera.txt").c_str()); photoUndistorter = new PhotometricUndistorter(path+"pcalib.txt", path+"vignette.png",undistorter->getInputDims()[0],undistorter->getInputDims()[1]); @@ -156,7 +158,12 @@ class DatasetReader } - UndistorterFOV* getUndistorter() + /*UndistorterFOV* getUndistorter() + { + return undistorter; + }*/ + + Undistort* getUndistorter() { return undistorter; } @@ -249,7 +256,7 @@ class DatasetReader if(!isZipped) { // CHANGE FOR ZIP FILE - return cv::imread(files[id],CV_LOAD_IMAGE_GRAYSCALE); + return cv::imread(files[id],cv::IMREAD_GRAYSCALE); } else { @@ -271,7 +278,7 @@ class DatasetReader exit(1); } } - return cv::imdecode(cv::Mat(readbytes,1,CV_8U, databuffer), CV_LOAD_IMAGE_GRAYSCALE); + return cv::imdecode(cv::Mat(readbytes,1,CV_8U, databuffer), cv::IMREAD_GRAYSCALE); } } @@ -336,7 +343,8 @@ class DatasetReader // internal structures. - UndistorterFOV* undistorter; + //UndistorterFOV* undistorter; + Undistort* undistorter; PhotometricUndistorter* photoUndistorter; zip_t* ziparchive; char* databuffer; diff --git a/src/FOVUndistorter.cpp b/src/FOVUndistorter.cpp index 8c2b455..0653470 100755 --- a/src/FOVUndistorter.cpp +++ b/src/FOVUndistorter.cpp @@ -55,7 +55,7 @@ UndistorterFOV::UndistorterFOV(const char* configFileName) std::ifstream infile(configFileName); if(!infile.good()) { - printf("Failed to read camera calibration (invalid format?)\nCalibration file: %s\n", configFileName); + printf("Failed to read camera calibration [1] (invalid format?)\nCalibration file: %s\n", configFileName); return; } @@ -77,7 +77,7 @@ UndistorterFOV::UndistorterFOV(const char* configFileName) } else { - printf("Failed to read camera calibration (invalid format?)\nCalibration file: %s\n", configFileName); + printf("Failed to read camera calibration [2] (invalid format?)\nCalibration file: %s\n", configFileName); return; } diff --git a/src/PhotometricUndistorter.cpp b/src/PhotometricUndistorter.cpp index 969b9da..f9abdf8 100755 --- a/src/PhotometricUndistorter.cpp +++ b/src/PhotometricUndistorter.cpp @@ -40,175 +40,149 @@ PhotometricUndistorter::PhotometricUndistorter( - std::string file, - std::string vignetteImage, - int w_, int h_) -{ - - validVignette=false; - validGamma=false; - - vignetteMap=0; - vignetteMapInv=0; - w = w_; - h = h_; - - if(file=="" || vignetteImage=="") return; - - - // read G. - std::ifstream f(file.c_str()); - printf("Reading Photometric Calibration from file %s\n",file.c_str()); - if (!f.good()) - { - printf("PhotometricUndistorter: Could not open file!\n"); - return; - } - - - - std::string line; - std::getline( f, line ); - std::istringstream l1i( line ); - std::vector GInvvec = std::vector( std::istream_iterator(l1i), std::istream_iterator() ); - if(GInvvec.size() != 256) - { - printf("PhotometricUndistorter: invalid format! got %d entries in first line, expected 256!\n",(int)GInvvec.size()); - return; - } - for(int i=0;i<256;i++) GInv[i] = GInvvec[i]; - - for(int i=0;i<255;i++) - { - if(GInv[i+1] <= GInv[i]) - { - printf("PhotometricUndistorter: G invalid! it has to be strictly increasing, but it isnt!\n"); - return; - } - } - float min=GInv[0]; - float max=GInv[255]; - for(int i=0;i<256;i++) GInv[i] = 255.0 * (GInv[i] - min) / (max-min); // make it to 0..255 => 0..255. - - // invert Gamma (maybe someone needs this). - for(int i=1;i<255;i++) - { - // find val, such that Binv[val] = i. - // I dont care about speed for this, so do it the stupid way. - for(int s=1;s<255;s++) - { - if(GInv[s] <= i && GInv[s+1] >= i) - { - G[i] = s+(i - GInv[s]) / (GInv[s+1]-GInv[s]); - break; - } - } - } - G[0] = 0; - G[255] = 255; - f.close(); - validGamma=true; - - - - - - - - - printf("Reading Vignette Image from %s\n",vignetteImage.c_str()); - cv::Mat vignetteMat = cv::imread(vignetteImage.c_str(), CV_LOAD_IMAGE_UNCHANGED); - vignetteMap = new float[w*h]; - vignetteMapInv = new float[w*h]; - if(vignetteMat.rows != h || vignetteMat.cols != w) - { - printf("PhotometricUndistorter: Invalid vignette image size! got %d x %d, expected %d x %d. Set vignette to 1.\n", - vignetteMat.cols, vignetteMat.rows, w, h); - return; - } - - if(vignetteMat.type() == CV_8U) - { - float maxV=0; - for(int i=0;i(i) > maxV) maxV = vignetteMat.at(i); - - for(int i=0;i(i) / maxV; - } - else if(vignetteMat.type() == CV_16U) - { - float maxV=0; - for(int i=0;i(i) > maxV) maxV = vignetteMat.at(i); - - for(int i=0;i(i) / maxV; - } - else - assert(false); - - for(int i=0;i GInvvec = std::vector(std::istream_iterator(l1i), std::istream_iterator()); + if (GInvvec.size() != 256) { + printf("PhotometricUndistorter: invalid format! got %d entries in first line, expected 256!\n", + (int) GInvvec.size()); + return; + } + for (int i = 0; i < 256; i++) GInv[i] = GInvvec[i]; + + for (int i = 0; i < 255; i++) { + if (GInv[i + 1] <= GInv[i]) { + printf("PhotometricUndistorter: G invalid! it has to be strictly increasing, but it isnt!\n"); + return; + } + } + float min = GInv[0]; + float max = GInv[255]; + for (int i = 0; i < 256; i++) GInv[i] = 255.0 * (GInv[i] - min) / + (max - min); // make it to 0..255 => 0..255. + + // invert Gamma (maybe someone needs this). + for (int i = 1; i < 255; i++) { + // find val, such that Binv[val] = i. + // I dont care about speed for this, so do it the stupid way. + for (int s = 1; s < 255; s++) { + if (GInv[s] <= i && GInv[s + 1] >= i) { + G[i] = s + (i - GInv[s]) / (GInv[s + 1] - GInv[s]); + break; + } + } + } + G[0] = 0; + G[255] = 255; + f.close(); + validGamma = true; + + + printf("Reading Vignette Image from %s\n", vignetteImage.c_str()); + cv::Mat vignetteMat = cv::imread(vignetteImage.c_str(), cv::IMREAD_UNCHANGED); + vignetteMap = new float[w * h]; + vignetteMapInv = new float[w * h]; + if (vignetteMat.rows != h || vignetteMat.cols != w) { + printf("PhotometricUndistorter: Invalid vignette image size! got %d x %d, expected %d x %d. Set vignette to 1.\n", + vignetteMat.cols, vignetteMat.rows, w, h); + return; + } + + if (vignetteMat.type() == CV_8U) { + float maxV = 0; + for (int i = 0; i < w * h; i++) + if (vignetteMat.at(i) > maxV) maxV = vignetteMat.at(i); + + for (int i = 0; i < w * h; i++) + vignetteMap[i] = vignetteMat.at(i) / maxV; + } else if (vignetteMat.type() == CV_16U) { + float maxV = 0; + for (int i = 0; i < w * h; i++) + if (vignetteMat.at(i) > maxV) maxV = vignetteMat.at(i); + + for (int i = 0; i < w * h; i++) + vignetteMap[i] = vignetteMat.at(i) / maxV; + } else + assert(false); + + for (int i = 0; i < w * h; i++) + vignetteMapInv[i] = 1.0f / vignetteMap[i]; + + + printf("Successfully read photometric calibration!\n"); + validVignette = true; } -PhotometricUndistorter::~PhotometricUndistorter() -{ - if(vignetteMap != 0) delete[] vignetteMap; - if(vignetteMapInv != 0) delete[] vignetteMapInv; + +PhotometricUndistorter::~PhotometricUndistorter() { + if (vignetteMap != 0) delete[] vignetteMap; + if (vignetteMapInv != 0) delete[] vignetteMapInv; } void PhotometricUndistorter::unMapImage( - unsigned char* image_in, - float* image_out, - int n, - bool undoGamma, - bool undoVignette, - bool killOverexposed) -{ - if(!validGamma && undoGamma) - { - printf("Photometric Undistorter did not load Gamma correctly. correctly. Not undoing gamma!\n"); - undoGamma = false; - } - if(!validVignette && undoVignette) - { - printf("Photometric Undistorter did not load Vignette correctly. correctly. Not undoing Vignette!\n"); - undoVignette = false; - } - - if(!undoGamma && undoVignette) - { - printf("it doesn't make sense to undo vignette without undoing gamma! not doing neither.\n"); - undoVignette=false; - undoGamma=false; - } - - assert(n == w*h); - - if(!undoGamma && !undoVignette) - { - for(int i=0;igetUndistorter()->getK_rect(); @@ -218,7 +221,6 @@ int main( int argc, char** argv ) int wI = reader->getUndistorter()->getInputDims()[0]; int hI = reader->getUndistorter()->getInputDims()[1]; - float meanExposure = 0; for(int i=0;igetNumImages();i+=imageSkip) meanExposure+=reader->getExposure(i); @@ -231,6 +233,7 @@ int main( int argc, char** argv ) { std::vector Markers; ExposureImage* img = reader->getImage(i,true, false, false, false); + std::cout << i << std::endl; cv::Mat InImage; cv::Mat(h_out, w_out, CV_32F, img->image).convertTo(InImage, CV_8U, 1, 0); @@ -264,7 +267,6 @@ int main( int argc, char** argv ) ExposureImage* imgRaw = reader->getImage(i,false, true, false, false); - float* plane2imgX = new float[gw*gh]; float* plane2imgY = new float[gw*gh]; @@ -276,12 +278,18 @@ int main( int argc, char** argv ) for(int x=0;x 0){ + plane2imgX[idx] = pp[0] / pp[2]; + plane2imgY[idx] = pp[1] / pp[2]; + }else{ + plane2imgX[idx] = NAN; + plane2imgY[idx] = NAN; + } idx++; } - reader->getUndistorter()->distortCoordinates(plane2imgX, plane2imgY, gw*gh); + reader->getUndistorter()->distortCoordinates(plane2imgX, plane2imgY, plane2imgX, plane2imgY, gw*gh); if(imgRaw->exposure_time == 0) imgRaw->exposure_time = 1; @@ -321,7 +329,7 @@ int main( int argc, char** argv ) int v_dT = plane2imgY[idxT]+0.5; if(u_dS>=0 && v_dS >=0 && u_dS=0 && v_dT >=0 && u_dT=0 && v_dS >=0 && u_dS=0 && v_dT >=0 && u_dT %f\n", R, sqrtf(E/R)); @@ -570,7 +578,7 @@ int main( int argc, char** argv ) cv::Mat wrap = cv::Mat(hI, wI, CV_32F, vignetteFactorTT)*254.9*254.9; cv::Mat wrap16; wrap.convertTo(wrap16, CV_16U,1,0); - cv::imwrite("vignetteCalibResult/vignetteSmoothed.png", wrap16); + cv::imwrite(path + "/" + "vignetteCalibResult/vignetteSmoothed.png", wrap16); cv::waitKey(50); } { @@ -578,7 +586,7 @@ int main( int argc, char** argv ) cv::Mat wrap = cv::Mat(hI, wI, CV_32F, vignetteFactor)*254.9*254.9; cv::Mat wrap16; wrap.convertTo(wrap16, CV_16U,1,0); - cv::imwrite("vignetteCalibResult/vignette.png", wrap16); + cv::imwrite(path + "/" + "vignetteCalibResult/vignette.png", wrap16); cv::waitKey(50); } }