├── .gitignore ├── CMakeLists.txt ├── daisy_descriptor ├── include │ ├── daisy │ │ └── daisy.h │ └── kutility │ │ ├── convolution.h │ │ ├── convolution_default.h │ │ ├── convolution_opencv.h │ │ ├── corecv.h │ │ ├── fileio.h │ │ ├── fileio.tcc │ │ ├── general.h │ │ ├── image.h │ │ ├── image_io.h │ │ ├── image_io_bmp.h │ │ ├── image_io_jpeg.h │ │ ├── image_io_png.h │ │ ├── image_io_pnm.h │ │ ├── image_manipulation.h │ │ ├── interaction.h │ │ ├── kutility.def │ │ ├── math.h │ │ └── progress_bar.h └── src │ ├── corecv.cpp │ ├── daisy.cpp │ ├── general.cpp │ ├── image_io_bmp.cpp │ ├── image_io_jpeg.cpp │ ├── image_io_png.cpp │ ├── image_io_pnm.cpp │ ├── image_manipulation.cpp │ ├── interaction.cpp │ └── progress_bar.cpp ├── disparity-daisy.cpp ├── disparity-elas.cpp ├── disparity-lbp.cpp ├── disparity-orb.cpp ├── disparity.cpp ├── elas ├── descriptor.cpp ├── descriptor.h ├── elas.cpp ├── elas.h ├── filter.cpp ├── filter.h ├── image.h ├── matrix.cpp ├── matrix.h ├── timer.h ├── triangle.cpp └── triangle.h ├── epipolar.cpp ├── popt_pp.h └── reconstruct.cpp /.gitignore: -------------------------------------------------------------------------------- 1 | Compiled Object files 2 | *.slo 3 | *.lo 4 | *.o 5 | *.obj 6 | 7 | # Precompiled Headers 8 | *.gch 9 | *.pch 10 | 11 | # Compiled Dynamic libraries 12 | *.so 13 | *.dylib 14 | *.dll 15 | 16 | # Fortran module files 17 | *.mod 18 | 19 | # Compiled Static libraries 20 | *.lai 21 | *.la 22 | *.a 23 | *.lib 24 | 25 | # Executables 26 | *.exe 27 | *.out 28 | *.app 29 | 30 | *.kdev4 31 | data/ 32 | build/ 33 | -------------------------------------------------------------------------------- /CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 2.8.11) 2 | project(DISPARITY_MAP) 3 | 4 | set(CMAKE_INCLUDE_CURRENT_DIR ON) 5 | 6 | find_package(OpenCV REQUIRED) 7 | include_directories($(OpenCV_INCLUDE_DIRS)) 8 | 9 | set(LIBELAS_SRC_DIR elas) 10 | set(DAISY_DESC_INC_DIR ./daisy_descriptor/include) 11 | SET(CMAKE_CXX_FLAGS "-msse3") 12 | 13 | # include directory 14 | include_directories("${LIBELAS_SRC_DIR}") 15 | include_directories("${DAISY_DESC_INC_DIR}") 16 | 17 | # sources 18 | FILE(GLOB LIBELAS_SRC_FILES "elas/*.cpp") 19 | FILE(GLOB DAISY_DESC_SRC_FILES "daisy_descriptor/src/*") 20 | 21 | add_executable(disparity-daisy ${DAISY_DESC_SRC_FILES} disparity-daisy.cpp) 22 | target_link_libraries(disparity-daisy ${OpenCV_LIBS}) 23 | 24 | add_executable(disparity-elas ${LIBELAS_SRC_FILES} disparity-elas.cpp) 25 | target_link_libraries(disparity-elas ${OpenCV_LIBS}) 26 | 27 | add_executable(epipolar epipolar.cpp popt_pp.h) 28 | target_link_libraries(epipolar ${OpenCV_LIBS} "-lpopt") 29 | 30 | add_executable(disparity disparity.cpp popt_pp.h) 31 | target_link_libraries(disparity ${OpenCV_LIBS} "-lpopt") 32 | 33 | add_executable(disparity-lbp disparity-lbp.cpp popt_pp.h) 34 | target_link_libraries(disparity-lbp ${OpenCV_LIBS} "-lpopt") 35 | 36 | add_executable(disparity-orb disparity-orb.cpp popt_pp.h) 37 | target_link_libraries(disparity-orb ${OpenCV_LIBS} "-lpopt") 38 | 39 | add_executable(reconstruct reconstruct.cpp popt_pp.h) 40 | target_link_libraries(reconstruct ${OpenCV_LIBS} "-lpopt") -------------------------------------------------------------------------------- /daisy_descriptor/include/kutility/convolution.h: -------------------------------------------------------------------------------- 1 | ////////////////////////////////////////////////////////////////////////// 2 | // Software License Agreement (BSD License) // 3 | // // 4 | // Copyright (c) 2009 // 5 | // Engin Tola // 6 | // web : http://cvlab.epfl.ch/~tola // 7 | // email : engin.tola@epfl.ch // 8 | // // 9 | // All rights reserved. // 10 | // // 11 | // Redistribution and use in source and binary forms, with or without // 12 | // modification, are permitted provided that the following conditions // 13 | // are met: // 14 | // // 15 | // * Redistributions of source code must retain the above copyright // 16 | // notice, this list of conditions and the following disclaimer. // 17 | // * Redistributions in binary form must reproduce the above // 18 | // copyright notice, this list of conditions and the following // 19 | // disclaimer in the documentation and/or other materials provided // 20 | // with the distribution. // 21 | // * Neither the name of the EPFL nor the names of its // 22 | // contributors may be used to endorse or promote products derived // 23 | // from this software without specific prior written permission. // 24 | // // 25 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS // 26 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT // 27 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS // 28 | // FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE // 29 | // COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, // 30 | // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, // 31 | // BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; // 32 | // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER // 33 | // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT // 34 | // LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN // 35 | // ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE // 36 | // POSSIBILITY OF SUCH DAMAGE. // 37 | // // 38 | // See licence.txt file for more details // 39 | ////////////////////////////////////////////////////////////////////////// 40 | 41 | #ifndef KUTILITY_CONVOLUTION_H 42 | #define KUTILITY_CONVOLUTION_H 43 | 44 | #if defined(WITH_OPENCV) && defined(WITH_OPENCV_EXTRAS) 45 | #include "kutility/convolution_opencv.h" 46 | #else 47 | #include "kutility/convolution_default.h" 48 | #endif 49 | 50 | namespace kutility 51 | { 52 | inline void convolve_sym( float* image, int h, int w, float* kernel, int ksize, float* out=NULL ) 53 | { 54 | if( out == NULL ) out = image; 55 | else memcpy( out, image, sizeof(float)*h*w ); 56 | 57 | if( h == 240 && w == 320 ) { convolve_sym_(out, 240, 320, kernel, ksize); return; } 58 | if( h == 480 && w == 640 ) { convolve_sym_(out, 480, 640, kernel, ksize); return; } 59 | if( h == 512 && w == 512 ) { convolve_sym_(out, 512, 512, kernel, ksize); return; } 60 | if( h == 512 && w == 768 ) { convolve_sym_(out, 512, 768, kernel, ksize); return; } 61 | if( h == 600 && w == 800 ) { convolve_sym_(out, 600, 800, kernel, ksize); return; } 62 | if( h == 768 && w == 1024 ) { convolve_sym_(out, 768, 1024, kernel, ksize); return; } 63 | if( h == 1024 && w == 768 ) { convolve_sym_(out, 1024, 768, kernel, ksize); return; } 64 | if( h == 256 && w == 256 ) { convolve_sym_(out, 256, 256, kernel, ksize); return; } 65 | if( h == 128 && w == 128 ) { convolve_sym_(out, 128, 128, kernel, ksize); return; } 66 | if( h == 128 && w == 192 ) { convolve_sym_(out, 128, 192, kernel, ksize); return; } 67 | cout<<"[convolve_sym] insert this h,w to unrolling list: "< inline 49 | void conv_buffer_(T1* buffer, T2* kernel, int rsize, int ksize) 50 | { 51 | for ( int i=0; i inline 63 | void conv_horizontal(T1* image, int h, int w, T2 *kernel, int ksize) 64 | { 65 | int halfsize = ksize / 2; 66 | assert(w + ksize < 4096); 67 | 68 | T1 buffer[4096]; 69 | for( int r=0; r inline 90 | void conv_vertical(T1* image, int h, int w, T2 *kernel, int ksize) 91 | { 92 | T1 buffer[4096]; 93 | 94 | int halfsize = ksize / 2; 95 | assert(h + ksize < 4096); 96 | 97 | int h_1w = (h-1)*w; 98 | 99 | for( int c=0; c inline 120 | void convolve_sym_( T* image, int h, int w, T* kernel, int ksize ) 121 | { 122 | conv_horizontal( image, h, w, kernel, ksize ); 123 | conv_vertical ( image, h, w, kernel, ksize ); 124 | } 125 | } 126 | 127 | #endif 128 | -------------------------------------------------------------------------------- /daisy_descriptor/include/kutility/convolution_opencv.h: -------------------------------------------------------------------------------- 1 | ////////////////////////////////////////////////////////////////////////// 2 | // Software License Agreement (BSD License) // 3 | // // 4 | // Copyright (c) 2009 // 5 | // Engin Tola // 6 | // web : http://cvlab.epfl.ch/~tola // 7 | // email : engin.tola@epfl.ch // 8 | // // 9 | // All rights reserved. // 10 | // // 11 | // Redistribution and use in source and binary forms, with or without // 12 | // modification, are permitted provided that the following conditions // 13 | // are met: // 14 | // // 15 | // * Redistributions of source code must retain the above copyright // 16 | // notice, this list of conditions and the following disclaimer. // 17 | // * Redistributions in binary form must reproduce the above // 18 | // copyright notice, this list of conditions and the following // 19 | // disclaimer in the documentation and/or other materials provided // 20 | // with the distribution. // 21 | // * Neither the name of the EPFL nor the names of its // 22 | // contributors may be used to endorse or promote products derived // 23 | // from this software without specific prior written permission. // 24 | // // 25 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS // 26 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT // 27 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS // 28 | // FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE // 29 | // COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, // 30 | // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, // 31 | // BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; // 32 | // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER // 33 | // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT // 34 | // LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN // 35 | // ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE // 36 | // POSSIBILITY OF SUCH DAMAGE. // 37 | // // 38 | // See licence.txt file for more details // 39 | ////////////////////////////////////////////////////////////////////////// 40 | 41 | #ifndef KUTILITY_CONVOLUTION_OPENCV_TCC 42 | #define KUTILITY_CONVOLUTION_OPENCV_TCC 43 | 44 | #if defined(WITH_OPENCV) && defined(WITH_OPENCV_EXTRAS) 45 | 46 | #include "cv.h" 47 | #include "highgui.h" 48 | 49 | namespace kutility 50 | { 51 | inline void conv_horizontal( float* image, int h, int w, float* kernel, int ksize ) 52 | { 53 | CvMat cvI; cvInitMatHeader(&cvI, h, w, CV_32FC1, (float*)image); 54 | CvMat cvK; cvInitMatHeader(&cvK, 1, ksize, CV_32FC1, (float*)kernel ); 55 | cvFilter2D( &cvI, &cvI, &cvK ); 56 | } 57 | inline void conv_horizontal( double* image, int h, int w, double* kernel, int ksize ) 58 | { 59 | CvMat cvI; cvInitMatHeader(&cvI, h, w, CV_64FC1, (double*)image); 60 | CvMat cvK; cvInitMatHeader(&cvK, 1, ksize, CV_64FC1, (double*)kernel ); 61 | cvFilter2D( &cvI, &cvI, &cvK ); 62 | } 63 | 64 | inline void conv_vertical( float* image, int h, int w, float* kernel, int ksize ) 65 | { 66 | CvMat cvI; cvInitMatHeader(&cvI, h, w, CV_32FC1, (float*)image); 67 | CvMat cvK; cvInitMatHeader(&cvK, ksize, 1, CV_32FC1, (float*)kernel ); 68 | cvFilter2D( &cvI, &cvI, &cvK ); 69 | } 70 | 71 | inline void conv_vertical( double* image, int h, int w, double* kernel, int ksize ) 72 | { 73 | CvMat cvI; cvInitMatHeader(&cvI, h, w, CV_64FC1, (double*)image); 74 | CvMat cvK; cvInitMatHeader(&cvK, ksize, 1, CV_64FC1, (double*)kernel ); 75 | cvFilter2D( &cvI, &cvI, &cvK ); 76 | } 77 | 78 | template inline 79 | void convolve_sym_( T* image, int h, int w, T* kernel, int ksize ) 80 | { 81 | conv_horizontal( image, h, w, kernel, ksize ); 82 | conv_vertical ( image, h, w, kernel, ksize ); 83 | } 84 | } 85 | 86 | #endif 87 | 88 | #endif 89 | -------------------------------------------------------------------------------- /daisy_descriptor/include/kutility/corecv.h: -------------------------------------------------------------------------------- 1 | ////////////////////////////////////////////////////////////////////////// 2 | // Software License Agreement (BSD License) // 3 | // // 4 | // Copyright (c) 2009 // 5 | // Engin Tola // 6 | // web : http://cvlab.epfl.ch/~tola // 7 | // email : engin.tola@epfl.ch // 8 | // // 9 | // All rights reserved. // 10 | // // 11 | // Redistribution and use in source and binary forms, with or without // 12 | // modification, are permitted provided that the following conditions // 13 | // are met: // 14 | // // 15 | // * Redistributions of source code must retain the above copyright // 16 | // notice, this list of conditions and the following disclaimer. // 17 | // * Redistributions in binary form must reproduce the above // 18 | // copyright notice, this list of conditions and the following // 19 | // disclaimer in the documentation and/or other materials provided // 20 | // with the distribution. // 21 | // * Neither the name of the EPFL nor the names of its // 22 | // contributors may be used to endorse or promote products derived // 23 | // from this software without specific prior written permission. // 24 | // // 25 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS // 26 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT // 27 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS // 28 | // FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE // 29 | // COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, // 30 | // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, // 31 | // BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; // 32 | // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER // 33 | // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT // 34 | // LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN // 35 | // ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE // 36 | // POSSIBILITY OF SUCH DAMAGE. // 37 | // // 38 | // See licence.txt file for more details // 39 | ////////////////////////////////////////////////////////////////////////// 40 | 41 | #ifndef KUTILITY_CORECV_H 42 | #define KUTILITY_CORECV_H 43 | 44 | #include "math.h" 45 | 46 | namespace kutility 47 | { 48 | void point_transform_via_homography( double* H, double x, double y, double &u, double &v ); 49 | 50 | double epipolar_line_slope( double y, double x, double* F ); 51 | 52 | 53 | } 54 | 55 | 56 | 57 | #endif 58 | -------------------------------------------------------------------------------- /daisy_descriptor/include/kutility/fileio.h: -------------------------------------------------------------------------------- 1 | ////////////////////////////////////////////////////////////////////////// 2 | // Software License Agreement (BSD License) // 3 | // // 4 | // Copyright (c) 2009 // 5 | // Engin Tola // 6 | // web : http://cvlab.epfl.ch/~tola // 7 | // email : engin.tola@epfl.ch // 8 | // // 9 | // All rights reserved. // 10 | // // 11 | // Redistribution and use in source and binary forms, with or without // 12 | // modification, are permitted provided that the following conditions // 13 | // are met: // 14 | // // 15 | // * Redistributions of source code must retain the above copyright // 16 | // notice, this list of conditions and the following disclaimer. // 17 | // * Redistributions in binary form must reproduce the above // 18 | // copyright notice, this list of conditions and the following // 19 | // disclaimer in the documentation and/or other materials provided // 20 | // with the distribution. // 21 | // * Neither the name of the EPFL nor the names of its // 22 | // contributors may be used to endorse or promote products derived // 23 | // from this software without specific prior written permission. // 24 | // // 25 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS // 26 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT // 27 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS // 28 | // FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE // 29 | // COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, // 30 | // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, // 31 | // BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; // 32 | // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER // 33 | // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT // 34 | // LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN // 35 | // ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE // 36 | // POSSIBILITY OF SUCH DAMAGE. // 37 | // // 38 | // See licence.txt file for more details // 39 | ////////////////////////////////////////////////////////////////////////// 40 | 41 | #ifndef KUTILITY_FILEIO_H 42 | #define KUTILITY_FILEIO_H 43 | 44 | #include "kutility/general.h" 45 | 46 | namespace kutility 47 | { 48 | enum data_types {TYPE_CHAR, TYPE_FLOAT, TYPE_DOUBLE, TYPE_INT}; 49 | 50 | // ascii 51 | template inline void save_ascii( ofstream& fout, T* data, int h, int w, int nb, int type ); 52 | template inline void save_ascii( string filename, T* data, int h, int w, int nb, int type ); 53 | template inline void load_ascii( ifstream& fin, T* &data, int &h, int &w, int &nb ); 54 | template inline void load_ascii( string filename, T* &data, int &h, int &w, int &nb ); 55 | 56 | // binary 57 | template inline void save_binary(ofstream& fout, T* data, int h, int w, int nb, int type ); 58 | template inline int save_binary(string filename, T* data, int h, int w, int nb, int type ); 59 | inline int load_binary(ifstream &fin, float* &data, int &h, int &w, int &nb ); 60 | inline int load_binary(ifstream &fin, int* &data, int &h, int &w, int &nb ); 61 | inline int load_binary(ifstream &fin, double* &data, int &h, int &w, int &nb ); 62 | inline int load_binary(ifstream &fin, char* &data, int &h, int &w, int &nb ); 63 | template inline int load_binary(string filename, T* &data, int &h, int &w, int &nb ); 64 | 65 | template inline void save_plain(ofstream& fout, T* data, int sz ); 66 | template inline void save_plain(ofstream& fout, T* data, int rs, int cs ); 67 | template inline void save(string filename, T* data, int sz ); 68 | template inline void save(string filename, T* data, int rs, int cs); 69 | 70 | template inline int load( ifstream& fin, T* &out, int size=-1 ); 71 | template inline int load( string filename, T* &out, int size=-1 ); 72 | inline void* load_array( string filename, int size, int type=1 ); 73 | 74 | #include "fileio.tcc" 75 | } 76 | 77 | #endif 78 | 79 | -------------------------------------------------------------------------------- /daisy_descriptor/include/kutility/fileio.tcc: -------------------------------------------------------------------------------- 1 | ////////////////////////////////////////////////////////////////////////// 2 | // Software License Agreement (BSD License) // 3 | // // 4 | // Copyright (c) 2009 // 5 | // Engin Tola // 6 | // web : http://cvlab.epfl.ch/~tola // 7 | // email : engin.tola@epfl.ch // 8 | // // 9 | // All rights reserved. // 10 | // // 11 | // Redistribution and use in source and binary forms, with or without // 12 | // modification, are permitted provided that the following conditions // 13 | // are met: // 14 | // // 15 | // * Redistributions of source code must retain the above copyright // 16 | // notice, this list of conditions and the following disclaimer. // 17 | // * Redistributions in binary form must reproduce the above // 18 | // copyright notice, this list of conditions and the following // 19 | // disclaimer in the documentation and/or other materials provided // 20 | // with the distribution. // 21 | // * Neither the name of the EPFL nor the names of its // 22 | // contributors may be used to endorse or promote products derived // 23 | // from this software without specific prior written permission. // 24 | // // 25 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS // 26 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT // 27 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS // 28 | // FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE // 29 | // COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, // 30 | // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, // 31 | // BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; // 32 | // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER // 33 | // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT // 34 | // LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN // 35 | // ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE // 36 | // POSSIBILITY OF SUCH DAMAGE. // 37 | // // 38 | // See licence.txt file for more details // 39 | ////////////////////////////////////////////////////////////////////////// 40 | 41 | template inline int load( ifstream& fin, T* &out, int size ) 42 | { 43 | int tsize; 44 | bool read_all = false; 45 | if( size <= 0 ) 46 | { 47 | tsize = 100; 48 | read_all = true; 49 | } 50 | else 51 | tsize = size; 52 | 53 | if( out == NULL ) out = new T[tsize]; 54 | 55 | int counter = 0; 56 | fin.peek(); 57 | while( !fin.fail() ) 58 | { 59 | float val; 60 | fin >> val; 61 | if( fin.fail() ) break; 62 | 63 | out[counter] = val; 64 | 65 | counter++; 66 | if( counter >= tsize ) 67 | { 68 | if( read_all ) 69 | { 70 | expand_array( out, tsize, 2*tsize ); 71 | tsize *= 2; 72 | } 73 | else 74 | break; 75 | } 76 | } 77 | if( read_all ) expand_array( out, counter, counter ); 78 | 79 | if( size!= -1 && size != counter ) 80 | cout<<"WARNING: I loaded only "< inline int load( string filename, T* &out, int size ) 86 | { 87 | ifstream fin; 88 | fin.open( filename.c_str() ); 89 | if( fin.fail() ) 90 | { 91 | cout<<"no such file: "< inline void save_binary(ofstream& fout, T* data, int h, int w, int nb, int type ) 100 | { 101 | fout.write((char*)&type, sizeof(int)); 102 | fout.write((char*)&h, sizeof(int)); 103 | fout.write((char*)&w, sizeof(int)); 104 | fout.write((char*)&nb, sizeof(int)); 105 | fout.write((char*)data, sizeof(T)*h*w*nb ); 106 | } 107 | template inline int save_binary(string filename, T* data, int h, int w, int nb, int type ) 108 | { 109 | ofstream fout(filename.c_str(), ofstream::binary); 110 | if( fout.fail() ) 111 | { 112 | warning("cannot open file: ", filename); 113 | return 1; 114 | } 115 | save_binary( fout, data, h, w, nb, type ); 116 | fout.close(); 117 | return 0; 118 | } 119 | 120 | template inline void save_ascii( ofstream& fout, T* data, int h, int w, int nb, int type ) 121 | { 122 | fout< inline void save_ascii( string filename, T* data, int h, int w, int nb, int type ) 129 | { 130 | std::ofstream fout; 131 | fout.open( filename.c_str(), std::ofstream::out ); 132 | 133 | save_ascii( fout, data, h, w, nb, type ); 134 | 135 | fout.close(); 136 | return; 137 | } 138 | template inline void load_ascii( ifstream& fin, T* &data, int &h, int &w, int &nb ) 139 | { 140 | int type = 0; 141 | fin >> type >> h >> w >> nb; 142 | data = new T[h*w*nb]; 143 | 144 | char tchar; 145 | int tint; 146 | float tfloat; 147 | double tdbl; 148 | 149 | int sz = h*w*nb; 150 | for( int k=0; k> tint; 155 | data[k] = (int)tint; 156 | continue; 157 | } 158 | if( type == TYPE_FLOAT ) 159 | { 160 | fin >> tfloat; 161 | data[k] = (float)tfloat; 162 | continue; 163 | } 164 | if( type == TYPE_DOUBLE ) 165 | { 166 | fin >> tdbl; 167 | data[k] = (double)tdbl; 168 | continue; 169 | } 170 | if( type == TYPE_CHAR ) 171 | { 172 | fin >> tchar; 173 | data[k] = (char)tchar; 174 | continue; 175 | } 176 | } 177 | } 178 | template inline void load_ascii( string filename, T* &data, int &h, int &w, int &nb ) 179 | { 180 | ifstream fin( filename.c_str(), ifstream::in ); 181 | if( fin.fail() ) error("cannot open file: ", filename ); 182 | load_ascii( fin, data, h, w, nb ); 183 | fin.close(); 184 | 185 | } 186 | 187 | inline int load_binary(ifstream &fin, float* &data, int &h, int &w, int &nb ) 188 | { 189 | int type = 0; 190 | fin.read((char*)&type, sizeof(int)); 191 | fin.read((char*)&h, sizeof(int)); 192 | fin.read((char*)&w, sizeof(int)); 193 | fin.read((char*)&nb, sizeof(int)); 194 | if( type != TYPE_FLOAT ) 195 | { 196 | fin.close(); 197 | return 1; 198 | } 199 | 200 | data = new float[h*w*nb]; 201 | fin.read((char*)data, sizeof(float)*h*w*nb ); 202 | return 0; 203 | } 204 | inline int load_binary(ifstream &fin, int* &data, int &h, int &w, int &nb ) 205 | { 206 | int type = 0; 207 | fin.read((char*)&type, sizeof(int)); 208 | fin.read((char*)&h, sizeof(int)); 209 | fin.read((char*)&w, sizeof(int)); 210 | fin.read((char*)&nb, sizeof(int)); 211 | if( type != TYPE_INT ) 212 | { 213 | fin.close(); 214 | return 1; 215 | } 216 | 217 | data = new int[h*w*nb]; 218 | fin.read((char*)data, sizeof(int)*h*w*nb ); 219 | return 0; 220 | } 221 | inline int load_binary(ifstream &fin, double* &data, int &h, int &w, int &nb ) 222 | { 223 | int type = 0; 224 | fin.read((char*)&type, sizeof(int)); 225 | fin.read((char*)&h, sizeof(int)); 226 | fin.read((char*)&w, sizeof(int)); 227 | fin.read((char*)&nb, sizeof(int)); 228 | if( type != TYPE_DOUBLE ) 229 | { 230 | fin.close(); 231 | return 1; 232 | } 233 | 234 | data = new double[h*w*nb]; 235 | fin.read((char*)data, sizeof(double)*h*w*nb); 236 | return 0; 237 | } 238 | inline int load_binary(ifstream &fin, char* &data, int &h, int &w, int &nb ) 239 | { 240 | int type = 0; 241 | fin.read((char*)&type, sizeof(int)); 242 | fin.read((char*)&h, sizeof(int)); 243 | fin.read((char*)&w, sizeof(int)); 244 | fin.read((char*)&nb, sizeof(int)); 245 | if( type != TYPE_CHAR ) 246 | { 247 | fin.close(); 248 | return 1; 249 | } 250 | 251 | data = new char[h*w*nb]; 252 | fin.read((char*)data, sizeof(char)*h*w*nb); 253 | return 0; 254 | } 255 | 256 | template inline int load_binary(string filename, T* &data, int &h, int &w, int &nb ) 257 | { 258 | ifstream fin( filename.c_str(), ifstream::binary ); 259 | if( fin.fail() ) error("cannot open file: ", filename ); 260 | int retval = load_binary( fin, data, h, w, nb ); 261 | fin.close(); 262 | return retval; 263 | } 264 | 265 | template inline void save_plain(ofstream& fout, T* data, int sz ) 266 | { 267 | for( int i=0; i inline void save_plain(ofstream& fout, T* data, int rs, int cs ) 273 | { 274 | for( int r=0; r inline void save(string filename, T* data, int sz) 285 | { 286 | std::ofstream fout; 287 | fout.open( filename.c_str(), std::ofstream::out ); 288 | save_plain( fout, data, sz ); 289 | fout.close(); 290 | return; 291 | } 292 | 293 | /// saves an array in matrix format with rs x cs 294 | template inline void save(string filename, T* data, int rs, int cs ) 295 | { 296 | std::ofstream fout; 297 | fout.open( filename.c_str(), std::ofstream::out ); 298 | save_plain( fout, data, rs, cs ); 299 | fout.close(); 300 | return; 301 | } 302 | 303 | /// loads an array given its size and data type. supported data 304 | /// types are int and double. "type" should be set to 0 for integer 305 | /// and to 1 for a double data. By default it is set to double. 306 | inline void* load_array( string filename, int size, int type ) 307 | { 308 | if( type > 2 ) 309 | { 310 | kutility::error("load_array: unsupported type", filename); 311 | } 312 | 313 | FILE* fp = fopen(filename.c_str(),"r"); 314 | 315 | if( fp == NULL ) 316 | { 317 | kutility::error("load_array: unable to open ", filename); 318 | } 319 | 320 | double * d_data=NULL; 321 | float * f_data=NULL; 322 | int * n_data=NULL; 323 | 324 | double d; 325 | float f; 326 | int n; 327 | 328 | int scanf_ret = 0; 329 | 330 | if( type == 0 ) n_data = new int [size]; 331 | if( type == 1 ) d_data = new double[size]; 332 | if( type == 2 ) f_data = new float [size]; 333 | 334 | for( int i=0; i inline int tload_image(string filename, T* &body, int& h, int& w, int& ch); 61 | //template inline int tload_gray_image(string filename, T* &body, int& h, int& w); 62 | //inline void save_image( string filename, uchar* body, int h, int w, int ch ); 63 | //template inline void tsave_image(string filename, T* body, int h, int w, int d); 64 | 65 | namespace kutility 66 | { 67 | inline int load_image(string filename, uchar* &body, int &h, int &w, int &ch ) 68 | { 69 | string format = get_file_format( filename ); 70 | if( !format.compare( "jpg" ) || !format.compare( "jpeg" ) ) 71 | { 72 | #ifdef WITH_JPEG 73 | return load_jpg(filename.c_str(), body, h, w, ch); 74 | #else 75 | cout<<"cannot load jpeg file: "< inline int tload_image(string filename, T* &body, int& h, int& w, int& ch) 122 | { 123 | uchar* data = NULL; 124 | if( !load_image(filename, data, h, w, ch ) ) 125 | { 126 | body = type_cast(data, h*w*ch); 127 | delete []data; 128 | return 0; 129 | } 130 | cout<<"could not load: tload_image"< inline int tload_gray_image(string filename, T* &body, int& h, int& w) 134 | { 135 | uchar* data = NULL; 136 | if( !load_gray_image( filename, data, h, w ) ) 137 | { 138 | body = type_cast(data, h*w); 139 | delete []data; 140 | return 0; 141 | } 142 | cout<<"could not load: tload_gray_image"< inline void tsave_image(string filename, T* body, int h, int w, int d) 203 | { 204 | uchar* tdata = type_cast(body, h*w*d); 205 | save_image(filename, tdata, h, w, d); 206 | delete []tdata; 207 | } 208 | 209 | } 210 | 211 | #endif 212 | -------------------------------------------------------------------------------- /daisy_descriptor/include/kutility/image_io.h: -------------------------------------------------------------------------------- 1 | ////////////////////////////////////////////////////////////////////////// 2 | // Software License Agreement (BSD License) // 3 | // // 4 | // Copyright (c) 2009 // 5 | // Engin Tola // 6 | // web : http://cvlab.epfl.ch/~tola // 7 | // email : engin.tola@epfl.ch // 8 | // // 9 | // All rights reserved. // 10 | // // 11 | // Redistribution and use in source and binary forms, with or without // 12 | // modification, are permitted provided that the following conditions // 13 | // are met: // 14 | // // 15 | // * Redistributions of source code must retain the above copyright // 16 | // notice, this list of conditions and the following disclaimer. // 17 | // * Redistributions in binary form must reproduce the above // 18 | // copyright notice, this list of conditions and the following // 19 | // disclaimer in the documentation and/or other materials provided // 20 | // with the distribution. // 21 | // * Neither the name of the EPFL nor the names of its // 22 | // contributors may be used to endorse or promote products derived // 23 | // from this software without specific prior written permission. // 24 | // // 25 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS // 26 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT // 27 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS // 28 | // FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE // 29 | // COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, // 30 | // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, // 31 | // BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; // 32 | // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER // 33 | // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT // 34 | // LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN // 35 | // ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE // 36 | // POSSIBILITY OF SUCH DAMAGE. // 37 | // // 38 | // See licence.txt file for more details // 39 | ////////////////////////////////////////////////////////////////////////// 40 | 41 | #ifndef KUTILITY_IMAGE_IO_H 42 | #define KUTILITY_IMAGE_IO_H 43 | 44 | #include "kutility/image_io_bmp.h" 45 | #include "kutility/image_io_pnm.h" 46 | 47 | #ifdef WITH_JPEG 48 | #include "kutility/image_io_jpeg.h" 49 | #endif 50 | 51 | #ifdef WITH_PNG 52 | #include "kutility/image_io_png.h" 53 | #endif 54 | 55 | #include "kutility/image_manipulation.h" 56 | 57 | 58 | //inline int load_image(string filename, uchar* &body, int &h, int &w, int &ch ); 59 | //inline int load_gray_image( string filename, uchar* &body, int &h, int &w ); 60 | //template inline int tload_image(string filename, T* &body, int& h, int& w, int& ch); 61 | //template inline int tload_gray_image(string filename, T* &body, int& h, int& w); 62 | //inline void save_image( string filename, uchar* body, int h, int w, int ch ); 63 | //template inline void tsave_image(string filename, T* body, int h, int w, int d); 64 | 65 | namespace kutility 66 | { 67 | inline int load_image(string filename, uchar* &body, int &h, int &w, int &ch ) 68 | { 69 | string format = get_file_format( filename ); 70 | if( !format.compare( "jpg" ) || !format.compare( "jpeg" ) ) 71 | { 72 | #ifdef WITH_JPEG 73 | return load_jpg(filename.c_str(), body, h, w, ch); 74 | #else 75 | cout<<"cannot load jpeg file: "< inline int tload_image(string filename, T* &body, int& h, int& w, int& ch) 122 | { 123 | uchar* data = NULL; 124 | if( !load_image(filename, data, h, w, ch ) ) 125 | { 126 | body = type_cast(data, h*w*ch); 127 | delete []data; 128 | return 0; 129 | } 130 | cout<<"could not load: tload_image"< inline int tload_gray_image(string filename, T* &body, int& h, int& w) 134 | { 135 | uchar* data = NULL; 136 | if( !load_gray_image( filename, data, h, w ) ) 137 | { 138 | body = type_cast(data, h*w); 139 | delete []data; 140 | return 0; 141 | } 142 | cout<<"could not load: tload_gray_image"< inline void tsave_image(string filename, T* body, int h, int w, int d) 203 | { 204 | uchar* tdata = type_cast(body, h*w*d); 205 | save_image(filename, tdata, h, w, d); 206 | delete []tdata; 207 | } 208 | 209 | } 210 | 211 | #endif 212 | -------------------------------------------------------------------------------- /daisy_descriptor/include/kutility/image_io_bmp.h: -------------------------------------------------------------------------------- 1 | ////////////////////////////////////////////////////////////////////////// 2 | // Software License Agreement (BSD License) // 3 | // // 4 | // Copyright (c) 2009 // 5 | // Engin Tola // 6 | // web : http://cvlab.epfl.ch/~tola // 7 | // email : engin.tola@epfl.ch // 8 | // // 9 | // All rights reserved. // 10 | // // 11 | // Redistribution and use in source and binary forms, with or without // 12 | // modification, are permitted provided that the following conditions // 13 | // are met: // 14 | // // 15 | // * Redistributions of source code must retain the above copyright // 16 | // notice, this list of conditions and the following disclaimer. // 17 | // * Redistributions in binary form must reproduce the above // 18 | // copyright notice, this list of conditions and the following // 19 | // disclaimer in the documentation and/or other materials provided // 20 | // with the distribution. // 21 | // * Neither the name of the EPFL nor the names of its // 22 | // contributors may be used to endorse or promote products derived // 23 | // from this software without specific prior written permission. // 24 | // // 25 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS // 26 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT // 27 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS // 28 | // FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE // 29 | // COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, // 30 | // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, // 31 | // BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; // 32 | // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER // 33 | // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT // 34 | // LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN // 35 | // ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE // 36 | // POSSIBILITY OF SUCH DAMAGE. // 37 | // // 38 | // See licence.txt file for more details // 39 | ////////////////////////////////////////////////////////////////////////// 40 | 41 | #ifndef KUTILITY_IMAGE_IO_BMP_H 42 | #define KUTILITY_IMAGE_IO_BMP_H 43 | 44 | #if defined(WIN32) 45 | #pragma warning( disable : 4996 ) 46 | #endif 47 | 48 | #include 49 | 50 | using std::string; 51 | 52 | #ifndef uchar 53 | typedef unsigned char uchar; 54 | #endif 55 | 56 | namespace kutility 57 | { 58 | /// converts an integer number to a hex string. 59 | inline void convert_hex(int number, int* hex_array) 60 | { 61 | for(int i=0; i<4; i++) 62 | { 63 | hex_array[i] = number%256; 64 | number = number/256; 65 | } 66 | } 67 | 68 | // void savebmp(string str, uchar* body, int h, int w, int channel); 69 | void save_bmp(const char* str, uchar* body, int h, int w, int channel); 70 | 71 | } 72 | 73 | #endif 74 | -------------------------------------------------------------------------------- /daisy_descriptor/include/kutility/image_io_jpeg.h: -------------------------------------------------------------------------------- 1 | ////////////////////////////////////////////////////////////////////////// 2 | // Software License Agreement (BSD License) // 3 | // // 4 | // Copyright (c) 2009 // 5 | // Engin Tola // 6 | // web : http://cvlab.epfl.ch/~tola // 7 | // email : engin.tola@epfl.ch // 8 | // // 9 | // All rights reserved. // 10 | // // 11 | // Redistribution and use in source and binary forms, with or without // 12 | // modification, are permitted provided that the following conditions // 13 | // are met: // 14 | // // 15 | // * Redistributions of source code must retain the above copyright // 16 | // notice, this list of conditions and the following disclaimer. // 17 | // * Redistributions in binary form must reproduce the above // 18 | // copyright notice, this list of conditions and the following // 19 | // disclaimer in the documentation and/or other materials provided // 20 | // with the distribution. // 21 | // * Neither the name of the EPFL nor the names of its // 22 | // contributors may be used to endorse or promote products derived // 23 | // from this software without specific prior written permission. // 24 | // // 25 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS // 26 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT // 27 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS // 28 | // FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE // 29 | // COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, // 30 | // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, // 31 | // BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; // 32 | // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER // 33 | // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT // 34 | // LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN // 35 | // ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE // 36 | // POSSIBILITY OF SUCH DAMAGE. // 37 | // // 38 | // See licence.txt file for more details // 39 | ////////////////////////////////////////////////////////////////////////// 40 | 41 | #ifdef WITH_JPEG 42 | 43 | #ifndef KUTILITY_IMAGE_IO_JPEG 44 | #define KUTILITY_IMAGE_IO_JPEG 45 | 46 | #include 47 | extern "C" { 48 | #include "jpeglib.h" 49 | } 50 | #include 51 | #include 52 | 53 | #ifndef uchar 54 | typedef unsigned char uchar; 55 | #endif 56 | 57 | using std::string; 58 | 59 | void save_jpg(const char* filename, uchar* body, int h, int w, int ch, int quality); 60 | int load_jpg(const char* filename, uchar* &body, int &h, int &w, int &ch); 61 | 62 | #endif 63 | 64 | #endif 65 | -------------------------------------------------------------------------------- /daisy_descriptor/include/kutility/image_io_png.h: -------------------------------------------------------------------------------- 1 | ////////////////////////////////////////////////////////////////////////// 2 | // Software License Agreement (BSD License) // 3 | // // 4 | // Copyright (c) 2009 // 5 | // Engin Tola // 6 | // web : http://cvlab.epfl.ch/~tola // 7 | // email : engin.tola@epfl.ch // 8 | // // 9 | // All rights reserved. // 10 | // // 11 | // Redistribution and use in source and binary forms, with or without // 12 | // modification, are permitted provided that the following conditions // 13 | // are met: // 14 | // // 15 | // * Redistributions of source code must retain the above copyright // 16 | // notice, this list of conditions and the following disclaimer. // 17 | // * Redistributions in binary form must reproduce the above // 18 | // copyright notice, this list of conditions and the following // 19 | // disclaimer in the documentation and/or other materials provided // 20 | // with the distribution. // 21 | // * Neither the name of the EPFL nor the names of its // 22 | // contributors may be used to endorse or promote products derived // 23 | // from this software without specific prior written permission. // 24 | // // 25 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS // 26 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT // 27 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS // 28 | // FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE // 29 | // COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, // 30 | // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, // 31 | // BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; // 32 | // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER // 33 | // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT // 34 | // LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN // 35 | // ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE // 36 | // POSSIBILITY OF SUCH DAMAGE. // 37 | // // 38 | // See licence.txt file for more details // 39 | ////////////////////////////////////////////////////////////////////////// 40 | 41 | #ifdef WITH_PNG 42 | 43 | #ifndef KUTILITY_IMAGE_IO_PNG_H 44 | #define KUTILITY_IMAGE_IO_PNG_H 45 | 46 | extern "C" { 47 | #include "png.h" 48 | } 49 | 50 | #include "kutility/kutility.def" 51 | 52 | namespace kutility 53 | { 54 | int load_png(const char* file_name, uchar* &body, int &h, int &w, int &ch); 55 | void save_png(const char* file_name, uchar* body, int height, int width, int chl); 56 | } 57 | 58 | typedef struct _write_png_info 59 | { 60 | double gamma; 61 | long width; 62 | long height; 63 | time_t modtime; 64 | FILE *infile; 65 | FILE *outfile; 66 | void *png_ptr; 67 | void *info_ptr; 68 | uchar *image_data; 69 | uchar **row_pointers; 70 | char *title; 71 | char *author; 72 | char *desc; 73 | char *copyright; 74 | char *email; 75 | char *url; 76 | int channel_no; 77 | int filter; /* command-line-filter flag, not PNG row filter! */ 78 | // int pnmtype; 79 | int sample_depth; 80 | int interlaced; 81 | int have_time; 82 | jmp_buf jmpbuf; 83 | uchar bg_red; 84 | uchar bg_green; 85 | uchar bg_blue; 86 | } write_png_info; 87 | 88 | void wpng_cleanup(write_png_info* a); 89 | 90 | void writepng_version_info (); 91 | int writepng_init (write_png_info *png_ptr); 92 | int writepng_encode_image (write_png_info *png_ptr); 93 | int writepng_encode_row (write_png_info *png_ptr); 94 | int writepng_encode_finish(write_png_info *png_ptr); 95 | void writepng_cleanup (write_png_info *png_ptr); 96 | 97 | #endif 98 | 99 | #endif 100 | -------------------------------------------------------------------------------- /daisy_descriptor/include/kutility/image_io_pnm.h: -------------------------------------------------------------------------------- 1 | ////////////////////////////////////////////////////////////////////////// 2 | // Software License Agreement (BSD License) // 3 | // // 4 | // Copyright (c) 2009 // 5 | // Engin Tola // 6 | // web : http://cvlab.epfl.ch/~tola // 7 | // email : engin.tola@epfl.ch // 8 | // // 9 | // All rights reserved. // 10 | // // 11 | // Redistribution and use in source and binary forms, with or without // 12 | // modification, are permitted provided that the following conditions // 13 | // are met: // 14 | // // 15 | // * Redistributions of source code must retain the above copyright // 16 | // notice, this list of conditions and the following disclaimer. // 17 | // * Redistributions in binary form must reproduce the above // 18 | // copyright notice, this list of conditions and the following // 19 | // disclaimer in the documentation and/or other materials provided // 20 | // with the distribution. // 21 | // * Neither the name of the EPFL nor the names of its // 22 | // contributors may be used to endorse or promote products derived // 23 | // from this software without specific prior written permission. // 24 | // // 25 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS // 26 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT // 27 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS // 28 | // FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE // 29 | // COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, // 30 | // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, // 31 | // BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; // 32 | // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER // 33 | // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT // 34 | // LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN // 35 | // ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE // 36 | // POSSIBILITY OF SUCH DAMAGE. // 37 | // // 38 | // See licence.txt file for more details // 39 | ////////////////////////////////////////////////////////////////////////// 40 | 41 | #ifndef KUTILITY_IMAGE_IO_PNM_H 42 | #define KUTILITY_IMAGE_IO_PNM_H 43 | 44 | #include 45 | #include "string.h" 46 | #include 47 | #include "limits.h" 48 | 49 | #ifndef uchar 50 | typedef unsigned char uchar; 51 | #endif 52 | 53 | namespace kutility 54 | { 55 | void load_pbm(const char* name, uchar* &data, int &height, int &width); 56 | void load_pgm(const char* name, uchar* &data, int &height, int &width); 57 | void load_ppm(const char* name, uchar* &data, int &height, int &width); 58 | 59 | void save_pbm(const char* name, uchar *im, int height, int width); 60 | 61 | template 62 | void save_pgm(const char* name, T *im, int height, int width) 63 | { 64 | std::ofstream file(name, std::ios::out | std::ios::binary); 65 | 66 | file << "P5\n" << width << " " << height << "\n" << UCHAR_MAX << "\n"; 67 | 68 | for( int k=0; k 76 | void save_ppm(const char* name, T *im, int height, int width) 77 | { 78 | std::ofstream file(name, std::ios::out | std::ios::binary); 79 | 80 | file << "P6\n" << width << " " << height << "\n" << UCHAR_MAX << "\n"; 81 | for( int k=0; k<3*width*height; k++ ) 82 | { 83 | file <<(uchar)im[k]; 84 | } 85 | file.close(); 86 | } 87 | 88 | void get_size_ppm(const char* name, int &height, int &width); 89 | } 90 | 91 | #endif 92 | -------------------------------------------------------------------------------- /daisy_descriptor/include/kutility/image_manipulation.h: -------------------------------------------------------------------------------- 1 | ////////////////////////////////////////////////////////////////////////// 2 | // Software License Agreement (BSD License) // 3 | // // 4 | // Copyright (c) 2009 // 5 | // Engin Tola // 6 | // web : http://cvlab.epfl.ch/~tola // 7 | // email : engin.tola@epfl.ch // 8 | // // 9 | // All rights reserved. // 10 | // // 11 | // Redistribution and use in source and binary forms, with or without // 12 | // modification, are permitted provided that the following conditions // 13 | // are met: // 14 | // // 15 | // * Redistributions of source code must retain the above copyright // 16 | // notice, this list of conditions and the following disclaimer. // 17 | // * Redistributions in binary form must reproduce the above // 18 | // copyright notice, this list of conditions and the following // 19 | // disclaimer in the documentation and/or other materials provided // 20 | // with the distribution. // 21 | // * Neither the name of the EPFL nor the names of its // 22 | // contributors may be used to endorse or promote products derived // 23 | // from this software without specific prior written permission. // 24 | // // 25 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS // 26 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT // 27 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS // 28 | // FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE // 29 | // COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, // 30 | // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, // 31 | // BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; // 32 | // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER // 33 | // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT // 34 | // LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN // 35 | // ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE // 36 | // POSSIBILITY OF SUCH DAMAGE. // 37 | // // 38 | // See licence.txt file for more details // 39 | ////////////////////////////////////////////////////////////////////////// 40 | 41 | #ifndef KUTILITY_IMAGE_MANIPULATION_H 42 | #define KUTILITY_IMAGE_MANIPULATION_H 43 | 44 | #include "kutility/kutility.def" 45 | #include "kutility/general.h" 46 | 47 | namespace kutility 48 | { 49 | template 50 | void scale( T1* src, int h, int w, float sc, T2* dst, int dh, int dw ) 51 | { 52 | int nh = int( h*sc ); 53 | int nw = int( w*sc ); 54 | 55 | assert( dst != NULL ); 56 | assert( nh == dh ); 57 | assert( nw == dw ); 58 | 59 | if( sc == 1 ) 60 | { 61 | for( int i=0; i= h-1 ) continue; 73 | for( int nx=0; nx= w-1 ) continue; 77 | dst[ny*nw+nx] = (T2)bilinear_interpolation(src, w, x, y); 78 | } 79 | } 80 | } 81 | 82 | template inline 83 | void rgb_to_y(T* cim, int h, int w, T* gim ) 84 | { 85 | assert( (gim!=NULL) && (cim!=NULL) ); 86 | 87 | for( int y=0; y inline 103 | void y_to_rgb(T* yim, int h, int w, T* rgbim ) 104 | { 105 | assert( rgbim != NULL ); 106 | 107 | int wh = w*h; 108 | 109 | for( int k=0; k inline 118 | void rgb_to_bgr(T* rgb, int h, int w, T* bgr ) 119 | { 120 | assert( bgr != NULL ); 121 | int wh3 = w*h*3; 122 | 123 | for( int k=0; k inline 133 | void bgr_to_rgb(T* bgr, int h, int w, T* rgb ) 134 | { 135 | rgb_to_bgr(bgr,h,w,rgb); 136 | } 137 | 138 | template inline void rgba_to_y(T* cim, int h, int w, T* gim ) 139 | { 140 | assert( (gim!=NULL) && (cim!=NULL) ); 141 | 142 | for( int y=0; y inline void rgba_to_rgb(T* rgbaim, int h, int w, T* rgbim ) 157 | { 158 | assert( (rgbim!=NULL) && (rgbaim!=NULL) ); 159 | int wh = w*h; 160 | for( int k=0; k 181 | void decompose_channels( T* image, int h, int w, T* &ch_0, T* &ch_1, T* &ch_2) 182 | { 183 | int image_size = h*w; 184 | 185 | ch_0 = kutility::allocate(image_size); 186 | ch_1 = kutility::allocate(image_size); 187 | ch_2 = kutility::allocate(image_size); 188 | 189 | #if defined(WITH_OPENMP) 190 | #pragma omp parallel for 191 | #endif 192 | for( int y=0; y inline 209 | T* gamma_correction( T* im, int h, int w, double gamma, bool in_place=false) 210 | { 211 | int sz = w*h; 212 | T* out; 213 | 214 | if( !in_place ) 215 | out = kutility::allocate(sz); 216 | else 217 | out = im; 218 | 219 | double val; 220 | 221 | for( int i=0; i 255 ) 225 | out[i] = (T)255; 226 | else 227 | out[i] = (T)val; 228 | } 229 | return out; 230 | } 231 | 232 | /// adds some noise to the pixels 233 | template inline 234 | T* add_noise( T* im, int h, int w, int noise_level, bool in_place=false) 235 | { 236 | int sz = w*h; 237 | T* out; 238 | 239 | if( !in_place ) 240 | out = kutility::allocate(sz); 241 | else 242 | out = im; 243 | 244 | 245 | for( int i=0; i 71 | void message( string str, T num ) 72 | { 73 | std::cout< inline 79 | void progress(T1 state, T2 range, int freq, time_t elapsed=-1) 80 | { 81 | if( ((int)(state)) % freq == 0 ) 82 | { 83 | std::cout.width(5); 84 | std::cout.precision(4); 85 | double percent = ((double)(state))/((double)(range)); 86 | std::cout<<"completed: "<<100*percent<<"%"; 87 | 88 | double eta; 89 | if( elapsed != -1 ) 90 | { 91 | eta = ((double)elapsed)/percent; 92 | std::cout<<"\tremaining: "<<(double)(eta-elapsed)<<"s\t total: "< inline 106 | void display( T* data, int r, int c=1, bool no_zero=false, bool legend=false, int precision=3, int width=4, string sep="\t") 107 | { 108 | cout.width(width); 109 | cout.fill(' '); 110 | cout.precision(precision); 111 | 112 | int i,j; 113 | if( legend ) 114 | { 115 | cout<<"\t"<<" "; 116 | cout.setf( ios_base::right); 117 | for(j=0; j inline 164 | void display( T** data, int r, int c=1, bool no_zero=false, bool legend=false, int precision=3, int width=4, char* sep="\t") 165 | { 166 | cout.width(width); 167 | cout.fill(' '); 168 | cout.precision(precision); 169 | 170 | int i,j; 171 | if( legend ) 172 | { 173 | cout<<"\t"<<" "; 174 | cout.setf( ios_base::right); 175 | for(j=0; j 45 | #include 46 | #include 47 | #include 48 | #include 49 | #include 50 | #include 51 | #include "assert.h" 52 | #include 53 | #include "float.h" 54 | 55 | #if !defined(PI) 56 | #define PI 3.141592653589793 57 | #endif 58 | #if !defined(RADIAN) 59 | #define RADIAN 0.017453292519943 // pi/180 60 | #endif 61 | #if !defined(DEGREE) 62 | #define DEGREE 57.29577951308232 // 180/pi 63 | #endif 64 | 65 | #ifndef UNCHAR 66 | #define UNCHAR 67 | typedef unsigned char uchar; 68 | #endif 69 | 70 | #ifndef U_INT 71 | #define U_INT 72 | typedef unsigned int uint; 73 | #endif 74 | 75 | #ifdef WIN32 76 | #include "omp.h" 77 | #define isnan(x) ((x) != (x)) 78 | #pragma warning( disable : 4996 ) 79 | #ifndef NOMINMAX 80 | #define NOMINMAX 81 | #endif 82 | #endif 83 | 84 | using std::string; 85 | using std::ostream; 86 | using std::ofstream; 87 | using std::ifstream; 88 | using std::cout; 89 | using std::cin; 90 | using std::endl; 91 | using std::ios_base; 92 | using std::flush; 93 | using std::vector; 94 | 95 | 96 | 97 | #endif 98 | -------------------------------------------------------------------------------- /daisy_descriptor/include/kutility/progress_bar.h: -------------------------------------------------------------------------------- 1 | ////////////////////////////////////////////////////////////////////////// 2 | // Software License Agreement (BSD License) // 3 | // // 4 | // Copyright (c) 2009 // 5 | // Engin Tola // 6 | // web : http://cvlab.epfl.ch/~tola // 7 | // email : engin.tola@epfl.ch // 8 | // // 9 | // All rights reserved. // 10 | // // 11 | // Redistribution and use in source and binary forms, with or without // 12 | // modification, are permitted provided that the following conditions // 13 | // are met: // 14 | // // 15 | // * Redistributions of source code must retain the above copyright // 16 | // notice, this list of conditions and the following disclaimer. // 17 | // * Redistributions in binary form must reproduce the above // 18 | // copyright notice, this list of conditions and the following // 19 | // disclaimer in the documentation and/or other materials provided // 20 | // with the distribution. // 21 | // * Neither the name of the EPFL nor the names of its // 22 | // contributors may be used to endorse or promote products derived // 23 | // from this software without specific prior written permission. // 24 | // // 25 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS // 26 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT // 27 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS // 28 | // FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE // 29 | // COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, // 30 | // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, // 31 | // BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; // 32 | // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER // 33 | // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT // 34 | // LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN // 35 | // ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE // 36 | // POSSIBILITY OF SUCH DAMAGE. // 37 | // // 38 | // See licence.txt file for more details // 39 | ////////////////////////////////////////////////////////////////////////// 40 | 41 | #ifndef KUTILITY_PROGRESS_BAR_H 42 | #define KUTILITY_PROGRESS_BAR_H 43 | 44 | #include 45 | #include 46 | 47 | class progress_bar 48 | { 49 | public: 50 | explicit inline progress_bar(int start, int end, int divisions); 51 | 52 | void reset(); 53 | 54 | void reset(int start, int end, int divisions); 55 | 56 | std::ostream& operator>>(std::ostream& os) const; 57 | 58 | const progress_bar& operator()(int current); 59 | 60 | void set_text(const std::string& text); 61 | 62 | void set_end_text( const std::string& text); 63 | 64 | void set_format(const std::string& formatString); 65 | 66 | private: 67 | int m_start; 68 | int m_current; 69 | int m_end; 70 | int m_divisions; 71 | mutable int m_progress; 72 | time_t m_starting_time; 73 | 74 | std::string m_message; 75 | std::string m_end_message; 76 | std::string m_done; 77 | std::string m_processing; 78 | std::string m_notDone; 79 | std::string m_limit; 80 | }; 81 | 82 | 83 | inline progress_bar::progress_bar(int start, int end, int divisions) 84 | : m_start(start), 85 | m_current(start), 86 | m_end(end), 87 | m_divisions(divisions), 88 | m_progress(0), 89 | m_message("Progress: "), 90 | m_end_message(" "), 91 | m_done("-"), 92 | m_processing(">"), 93 | m_notDone(" "), 94 | m_limit("|") 95 | { 96 | time(&m_starting_time); 97 | } 98 | 99 | inline std::ostream& operator<<(std::ostream& os, const progress_bar& pb) 100 | { 101 | return pb >> os; 102 | } 103 | 104 | 105 | #endif 106 | -------------------------------------------------------------------------------- /daisy_descriptor/src/corecv.cpp: -------------------------------------------------------------------------------- 1 | ////////////////////////////////////////////////////////////////////////// 2 | // Software License Agreement (BSD License) // 3 | // // 4 | // Copyright (c) 2009 // 5 | // Engin Tola // 6 | // web : http://cvlab.epfl.ch/~tola // 7 | // email : engin.tola@epfl.ch // 8 | // // 9 | // All rights reserved. // 10 | // // 11 | // Redistribution and use in source and binary forms, with or without // 12 | // modification, are permitted provided that the following conditions // 13 | // are met: // 14 | // // 15 | // * Redistributions of source code must retain the above copyright // 16 | // notice, this list of conditions and the following disclaimer. // 17 | // * Redistributions in binary form must reproduce the above // 18 | // copyright notice, this list of conditions and the following // 19 | // disclaimer in the documentation and/or other materials provided // 20 | // with the distribution. // 21 | // * Neither the name of the EPFL nor the names of its // 22 | // contributors may be used to endorse or promote products derived // 23 | // from this software without specific prior written permission. // 24 | // // 25 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS // 26 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT // 27 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS // 28 | // FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE // 29 | // COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, // 30 | // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, // 31 | // BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; // 32 | // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER // 33 | // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT // 34 | // LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN // 35 | // ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE // 36 | // POSSIBILITY OF SUCH DAMAGE. // 37 | // // 38 | // See licence.txt file for more details // 39 | ////////////////////////////////////////////////////////////////////////// 40 | 41 | #include "kutility/corecv.h" 42 | 43 | namespace kutility 44 | { 45 | /// transform a point via the homography 46 | void point_transform_via_homography( double* H, double x, double y, double &u, double &v ) 47 | { 48 | double kxp = H[0]*x + H[1]*y + H[2]; 49 | double kyp = H[3]*x + H[4]*y + H[5]; 50 | double kp = H[6]*x + H[7]*y + H[8]; 51 | u = kxp / kp; 52 | v = kyp / kp; 53 | } 54 | 55 | double epipolar_line_slope( double y, double x, double* F ) 56 | { 57 | double line[3]; 58 | line[0] = F[0]*x + F[1]*y + F[2]; 59 | line[1] = F[3]*x + F[4]*y + F[5]; 60 | line[2] = F[6]*x + F[7]*y + F[8]; 61 | 62 | double m = -line[0]/line[1]; 63 | double slope = atan( m )*DEGREE; 64 | 65 | if( slope < 0 ) slope += 360; 66 | if( slope >= 360 ) slope -= 360; 67 | 68 | return slope; 69 | } 70 | 71 | } 72 | -------------------------------------------------------------------------------- /daisy_descriptor/src/general.cpp: -------------------------------------------------------------------------------- 1 | ////////////////////////////////////////////////////////////////////////// 2 | // Software License Agreement (BSD License) // 3 | // // 4 | // Copyright (c) 2009 // 5 | // Engin Tola // 6 | // web : http://cvlab.epfl.ch/~tola // 7 | // email : engin.tola@epfl.ch // 8 | // // 9 | // All rights reserved. // 10 | // // 11 | // Redistribution and use in source and binary forms, with or without // 12 | // modification, are permitted provided that the following conditions // 13 | // are met: // 14 | // // 15 | // * Redistributions of source code must retain the above copyright // 16 | // notice, this list of conditions and the following disclaimer. // 17 | // * Redistributions in binary form must reproduce the above // 18 | // copyright notice, this list of conditions and the following // 19 | // disclaimer in the documentation and/or other materials provided // 20 | // with the distribution. // 21 | // * Neither the name of the EPFL nor the names of its // 22 | // contributors may be used to endorse or promote products derived // 23 | // from this software without specific prior written permission. // 24 | // // 25 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS // 26 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT // 27 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS // 28 | // FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE // 29 | // COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, // 30 | // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, // 31 | // BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; // 32 | // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER // 33 | // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT // 34 | // LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN // 35 | // ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE // 36 | // POSSIBILITY OF SUCH DAMAGE. // 37 | // // 38 | // See licence.txt file for more details // 39 | ////////////////////////////////////////////////////////////////////////// 40 | 41 | #include "kutility/general.h" 42 | 43 | namespace kutility 44 | { 45 | char* itoa(int value, char* str, int radix) 46 | { 47 | int rem = 0; 48 | int pos = 0; 49 | char ch = '!' ; 50 | do 51 | { 52 | rem = value % radix ; 53 | value /= radix; 54 | if ( 16 == radix ) 55 | { 56 | if( rem >= 10 && rem <= 15 ) 57 | { 58 | switch( rem ) 59 | { 60 | case 10: 61 | ch = 'a' ; 62 | break; 63 | case 11: 64 | ch ='b' ; 65 | break; 66 | case 12: 67 | ch = 'c' ; 68 | break; 69 | case 13: 70 | ch ='d' ; 71 | break; 72 | case 14: 73 | ch = 'e' ; 74 | break; 75 | case 15: 76 | ch ='f' ; 77 | break; 78 | } 79 | } 80 | } 81 | if( '!' == ch ) 82 | { 83 | str[pos++] = (char) ( rem + 0x30 ); 84 | } 85 | else 86 | { 87 | str[pos++] = ch ; 88 | } 89 | }while( value != 0 ); 90 | str[pos] = '\0' ; 91 | return strrev(str); 92 | } 93 | 94 | //strrev the standard way 95 | // the following directives to make the code portable 96 | // between windows and Linux. 97 | char* strrev(char* szT) 98 | { 99 | if ( !szT ) // handle null passed strings. 100 | return NULL; 101 | int i = strlen(szT); 102 | int t = !(i%2)? 1 : 0; // check the length of the string . 103 | for(int j = i-1 , k = 0 ; j > (i/2 -t) ; j-- ) 104 | { 105 | char ch = szT[j]; 106 | szT[j] = szT[k]; 107 | szT[k++] = ch; 108 | } 109 | return szT; 110 | } 111 | 112 | } 113 | 114 | -------------------------------------------------------------------------------- /daisy_descriptor/src/image_io_bmp.cpp: -------------------------------------------------------------------------------- 1 | ////////////////////////////////////////////////////////////////////////// 2 | // Software License Agreement (BSD License) // 3 | // // 4 | // Copyright (c) 2009 // 5 | // Engin Tola // 6 | // web : http://cvlab.epfl.ch/~tola // 7 | // email : engin.tola@epfl.ch // 8 | // // 9 | // All rights reserved. // 10 | // // 11 | // Redistribution and use in source and binary forms, with or without // 12 | // modification, are permitted provided that the following conditions // 13 | // are met: // 14 | // // 15 | // * Redistributions of source code must retain the above copyright // 16 | // notice, this list of conditions and the following disclaimer. // 17 | // * Redistributions in binary form must reproduce the above // 18 | // copyright notice, this list of conditions and the following // 19 | // disclaimer in the documentation and/or other materials provided // 20 | // with the distribution. // 21 | // * Neither the name of the EPFL nor the names of its // 22 | // contributors may be used to endorse or promote products derived // 23 | // from this software without specific prior written permission. // 24 | // // 25 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS // 26 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT // 27 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS // 28 | // FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE // 29 | // COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, // 30 | // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, // 31 | // BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; // 32 | // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER // 33 | // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT // 34 | // LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN // 35 | // ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE // 36 | // POSSIBILITY OF SUCH DAMAGE. // 37 | // // 38 | // See licence.txt file for more details // 39 | ////////////////////////////////////////////////////////////////////////// 40 | 41 | #include "kutility/image_io_bmp.h" 42 | 43 | namespace kutility 44 | { 45 | void save_bmp(const char* str, uchar* body, int h, int w, int channel) 46 | { 47 | int image_data_size = w * h; 48 | 49 | // if( channel == 3 ) 50 | image_data_size *= 4; 51 | 52 | int hexWidth[4]; 53 | int hexHeight[4]; 54 | int hexFileSize[4]; 55 | int hexIdent[4]; 56 | 57 | convert_hex(w, hexWidth); 58 | convert_hex(h, hexHeight); 59 | convert_hex(image_data_size+54,hexFileSize); 60 | convert_hex(image_data_size,hexIdent); 61 | 62 | FILE * maskFile = fopen( str , "w+b"); 63 | 64 | char headerArray[54]; 65 | headerArray[0] =(char)0x42 ; 66 | headerArray[1] =(char)0x4D ; 67 | headerArray[2] =(char)hexFileSize[0] ; 68 | headerArray[3] =(char)hexFileSize[1] ; 69 | headerArray[4] =(char)hexFileSize[2] ; 70 | headerArray[5] =(char)hexFileSize[3] ; 71 | headerArray[6] = (char)0x0; 72 | headerArray[7] = (char)0x0; 73 | headerArray[8] = (char)0x0; 74 | headerArray[9] = (char)0x0; 75 | headerArray[10] = (char)0x36; 76 | headerArray[11] = (char)0x0; 77 | headerArray[12] = (char)0x0; 78 | headerArray[13] = (char)0x0; 79 | headerArray[14] = (char)0x28; 80 | headerArray[15] = (char)0x0; 81 | headerArray[16] = (char)0x0; 82 | headerArray[17] = (char)0x0; 83 | headerArray[18] = (char)hexWidth[0]; 84 | headerArray[19] = (char)hexWidth[1]; 85 | headerArray[20] = (char)hexWidth[2]; 86 | headerArray[21] = (char)hexWidth[3]; 87 | headerArray[22] = (char)hexHeight[0]; 88 | headerArray[23] = (char)hexHeight[1]; 89 | headerArray[24] = (char)hexHeight[2]; 90 | headerArray[25] = (char)hexHeight[3]; 91 | headerArray[26] = (char)0x01; 92 | headerArray[27] = (char)0x0; 93 | headerArray[28] = (char)0x20; 94 | headerArray[29] = (char)0x0; 95 | headerArray[30] = (char)0x0; 96 | headerArray[31] = (char)0x0; 97 | headerArray[32] = (char)0x0; 98 | headerArray[33] = (char)0x0; 99 | headerArray[34] = (char)hexIdent[0]; 100 | headerArray[35] = (char)hexIdent[1]; 101 | headerArray[36] = (char)hexIdent[2]; 102 | headerArray[37] = (char)hexIdent[3]; 103 | headerArray[38] = (char)0xC4; 104 | headerArray[39] = (char)0x0E; 105 | headerArray[40] = (char)0x0; 106 | headerArray[41] = (char)0x0; 107 | headerArray[42] = (char)0xC4; 108 | headerArray[43] = (char)0x0E; 109 | headerArray[44] = (char)0x0; 110 | headerArray[45] = (char)0x0; 111 | headerArray[46] = (char)0x0; 112 | headerArray[47] = (char)0x0; 113 | headerArray[48] = (char)0x0; 114 | headerArray[49] = (char)0x0; 115 | headerArray[50] = (char)0x0; 116 | headerArray[51] = (char)0x0; 117 | headerArray[52] = (char)0x0; 118 | headerArray[53] = (char)0x0; 119 | 120 | fwrite(headerArray, sizeof(char), 54, maskFile); 121 | fclose(maskFile); 122 | maskFile = fopen( str , "a+b"); 123 | 124 | uchar* data = new uchar[image_data_size]; 125 | 126 | int index=0; 127 | //create bitmap data// 128 | for(int m=0; m 45 | 46 | #ifndef UCHAR 47 | #define UCHAR 48 | typedef unsigned char uchar; 49 | #endif 50 | 51 | void save_jpg(const char* filename, uchar* body, int h, int w, int ch, int quality) 52 | { 53 | struct jpeg_compress_struct cinfo; 54 | struct jpeg_error_mgr jerr; 55 | 56 | FILE * outfile; /* target file */ 57 | JSAMPROW row_pointer[1]; /* pointer to JSAMPLE row[s] */ 58 | int row_stride; /* physical row width in image buffer */ 59 | 60 | /* Step 1: allocate and initialize JPEG compression object */ 61 | 62 | cinfo.err = jpeg_std_error(&jerr); 63 | jpeg_create_compress(&cinfo); 64 | 65 | /* Step 2: specify data destination (eg, a file) */ 66 | /* Note: steps 2 and 3 can be done in either order. */ 67 | 68 | if ((outfile = fopen(filename, "wb")) == NULL) { 69 | fprintf(stderr, "can't open %s\n", filename); 70 | exit(1); 71 | } 72 | jpeg_stdio_dest(&cinfo, outfile); 73 | 74 | /* Step 3: set parameters for compression */ 75 | 76 | cinfo.image_width = w; 77 | cinfo.image_height = h; 78 | cinfo.input_components = ch; /* # of color components per pixel */ 79 | 80 | if( ch == 3 ) 81 | cinfo.in_color_space = JCS_RGB; /* colorspace of input image */ 82 | else 83 | cinfo.in_color_space = JCS_GRAYSCALE; /* colorspace of input image */ 84 | 85 | jpeg_set_defaults(&cinfo); 86 | 87 | /* Now you can set any non-default parameters you wish to. 88 | * Here we just illustrate the use of quality (quantization table) scaling: 89 | */ 90 | jpeg_set_quality(&cinfo, quality, TRUE /* limit to baseline-JPEG values */); 91 | 92 | /* Step 4: Start compressor */ 93 | jpeg_start_compress(&cinfo, TRUE); 94 | 95 | /* Step 5: while (scan lines remain to be written) */ 96 | /* jpeg_write_scanlines(...); */ 97 | 98 | /* Here we use the library's state variable cinfo.next_scanline as the 99 | * loop counter, so that we don't have to keep track ourselves. 100 | * To keep things simple, we pass one scanline per call; you can pass 101 | * more if you wish, though. 102 | */ 103 | row_stride = w * ch; /* JSAMPLEs per row in image_buffer */ 104 | 105 | while (cinfo.next_scanline < cinfo.image_height) { 106 | /* jpeg_write_scanlines expects an array of pointers to scanlines. 107 | * Here the array is only one element long, but you could pass 108 | * more than one scanline at a time if that's more convenient. 109 | */ 110 | row_pointer[0] = & body[cinfo.next_scanline * row_stride]; 111 | (void) jpeg_write_scanlines(&cinfo, row_pointer, 1); 112 | } 113 | 114 | /* Step 6: Finish compression */ 115 | 116 | jpeg_finish_compress(&cinfo); 117 | fclose(outfile); 118 | 119 | /* Step 7: release JPEG compression object */ 120 | 121 | jpeg_destroy_compress(&cinfo); 122 | 123 | /* And we're done! */ 124 | } 125 | 126 | struct my_error_mgr { 127 | struct jpeg_error_mgr pub; /* "public" fields */ 128 | 129 | jmp_buf setjmp_buffer; /* for return to caller */ 130 | }; 131 | 132 | typedef struct my_error_mgr * my_error_ptr; 133 | 134 | METHODDEF(void) 135 | my_error_exit (j_common_ptr cinfo) 136 | { 137 | /* cinfo->err really points to a my_error_mgr struct, so coerce pointer */ 138 | my_error_ptr myerr = (my_error_ptr) cinfo->err; 139 | 140 | /* Always display the message. */ 141 | /* We could postpone this until after returning, if we chose. */ 142 | (*cinfo->err->output_message) (cinfo); 143 | 144 | /* Return control to the setjmp point */ 145 | longjmp(myerr->setjmp_buffer, 1); 146 | } 147 | 148 | int load_jpg(const char* filename, uchar* &body, int &h, int &w, int &ch) 149 | { 150 | struct jpeg_decompress_struct cinfo; 151 | struct my_error_mgr jerr; 152 | 153 | FILE * infile; /* source file */ 154 | JSAMPARRAY buffer; /* Output row buffer */ 155 | int row_stride; /* physical row width in output buffer */ 156 | 157 | if ((infile = fopen(filename, "rb")) == NULL) { 158 | fprintf(stderr, "can't open %s\n", filename); 159 | return 0; 160 | } 161 | 162 | /* Step 1: allocate and initialize JPEG decompression object */ 163 | 164 | /* We set up the normal JPEG error routines, then override error_exit. */ 165 | cinfo.err = jpeg_std_error(&jerr.pub); 166 | jerr.pub.error_exit = my_error_exit; 167 | /* Establish the setjmp return context for my_error_exit to use. */ 168 | if (setjmp(jerr.setjmp_buffer)) { 169 | /* If we get here, the JPEG code has signaled an error. */ 170 | jpeg_destroy_decompress(&cinfo); 171 | fclose(infile); 172 | return 0; 173 | } 174 | /* Now we can initialize the JPEG decompression object. */ 175 | jpeg_create_decompress(&cinfo); 176 | 177 | /* Step 2: specify data source (eg, a file) */ 178 | jpeg_stdio_src(&cinfo, infile); 179 | 180 | /* Step 3: read file parameters with jpeg_read_header() */ 181 | (void) jpeg_read_header(&cinfo, TRUE); 182 | 183 | /* Step 4: set parameters for decompression */ 184 | 185 | /* we don't need to change any of the defaults set by jpeg_read_header(), so 186 | * we do nothing here. */ 187 | 188 | /* Step 5: Start decompressor */ 189 | 190 | (void) jpeg_start_decompress(&cinfo); 191 | 192 | /* We may need to do some setup of our own at this point before reading 193 | * the data. After jpeg_start_decompress() we have the correct scaled 194 | * output image dimensions available, as well as the output colormap 195 | * if we asked for color quantization. 196 | * In this example, we need to make an output work buffer of the right size. 197 | */ 198 | /* JSAMPLEs per row in output buffer */ 199 | row_stride = cinfo.output_width * cinfo.output_components; 200 | 201 | w = cinfo.output_width; 202 | h = cinfo.output_height; 203 | ch = cinfo.output_components; 204 | body = new uchar[ w*h*ch ]; 205 | 206 | /* Make a one-row-high sample array that will go away when done with image */ 207 | buffer = (*cinfo.mem->alloc_sarray)((j_common_ptr) &cinfo, JPOOL_IMAGE, row_stride, 1); 208 | 209 | /* Step 6: while (scan lines remain to be read) */ 210 | /* jpeg_read_scanlines(...); */ 211 | 212 | /* Here we use the library's state variable cinfo.output_scanline as the 213 | * loop counter, so that we don't have to keep track ourselves. 214 | */ 215 | while (cinfo.output_scanline < cinfo.output_height) { 216 | /* jpeg_read_scanlines expects an array of pointers to scanlines. 217 | * Here the array is only one element long, but you could ask for 218 | * more than one scanline at a time if that's more convenient. 219 | */ 220 | (void) jpeg_read_scanlines(&cinfo, buffer, 1); 221 | /* Assume put_scanline_someplace wants a pointer and sample count. */ 222 | int row_number = cinfo.output_scanline-1; 223 | // printf("scanline : %d\n",row_number); 224 | uchar* row = body+row_number*row_stride; 225 | for( int k=0; k> bitshift) & 1; 58 | bitshift--; 59 | } 60 | } 61 | void write_packed(unsigned char *data, int size, std::ofstream &f) 62 | { 63 | unsigned char c = 0; 64 | 65 | int bitshift = 7; 66 | for (int pos = 0; pos < size; pos++) { 67 | c = c | (data[pos] << bitshift); 68 | bitshift--; 69 | if ((bitshift == -1) || (pos == size-1)) { 70 | f.put(c); 71 | bitshift = 7; 72 | c = 0; 73 | } 74 | } 75 | } 76 | void pnm_read(std::ifstream &file, char *buf) 77 | { 78 | char doc[PNM_BUFFER_SIZE]; 79 | char c; 80 | 81 | file >> c; 82 | while (c == '#') { 83 | file.getline(doc, PNM_BUFFER_SIZE); 84 | file >> c; 85 | } 86 | file.putback(c); 87 | 88 | file.width(PNM_BUFFER_SIZE); 89 | file >> buf; 90 | file.ignore(); 91 | } 92 | void get_size_ppm(const char *name, int &height, int &width) 93 | { 94 | char buf[PNM_BUFFER_SIZE]; 95 | //char doc[PNM_BUFFER_SIZE] 96 | // read header 97 | std::ifstream file(name, std::ios::in | std::ios::binary); 98 | pnm_read(file, buf); 99 | if (strncmp(buf, "P6", 2)) 100 | { 101 | printf("type mismatch\n"); 102 | exit(1); 103 | } 104 | 105 | pnm_read(file, buf); 106 | width = atoi(buf); 107 | 108 | pnm_read(file, buf); 109 | height = atoi(buf); 110 | 111 | file.close(); 112 | return; 113 | } 114 | 115 | void load_pbm(const char* name, uchar* &im, int &height, int &width) 116 | { 117 | char buf[PNM_BUFFER_SIZE]; 118 | 119 | /* read header */ 120 | std::ifstream file(name, std::ios::in | std::ios::binary); 121 | pnm_read(file, buf); 122 | if (strncmp(buf, "P4", 2)) 123 | { 124 | printf("type mismatch\n"); 125 | exit(1); 126 | } 127 | 128 | pnm_read(file, buf); 129 | width = atoi(buf); 130 | 131 | pnm_read(file, buf); 132 | height = atoi(buf); 133 | 134 | /* read data */ 135 | if( im != NULL) delete[]im; 136 | im = new uchar[width*height]; 137 | for (int i = 0; i < height; i++) 138 | read_packed(im+(width*i), width, file); 139 | } 140 | void load_pgm(const char* name, uchar* &im, int &height, int& width) 141 | { 142 | char buf[PNM_BUFFER_SIZE]; 143 | 144 | /* read header */ 145 | std::ifstream file(name, std::ios::in | std::ios::binary); 146 | pnm_read(file, buf); 147 | if (strncmp(buf, "P5", 2)) 148 | { 149 | printf("type mismatch\n"); 150 | exit(1); 151 | } 152 | 153 | pnm_read(file, buf); 154 | width = atoi(buf); 155 | 156 | pnm_read(file, buf); 157 | height = atoi(buf); 158 | 159 | pnm_read(file, buf); 160 | if (atoi(buf) > UCHAR_MAX) 161 | { 162 | printf("type mismatch\n"); 163 | exit(1); 164 | } 165 | 166 | /* read data */ 167 | if( im != NULL ) delete[] im; 168 | im = new uchar[width*height]; 169 | file.read( (char *)im, width * height * sizeof(uchar)); 170 | } 171 | 172 | void load_ppm(const char* name, uchar* &im, int &height, int &width) 173 | { 174 | char buf[PNM_BUFFER_SIZE]; 175 | //char doc[PNM_BUFFER_SIZE] 176 | std::ifstream file(name, std::ios::in | std::ios::binary); 177 | pnm_read(file, buf); 178 | if (strncmp(buf, "P6", 2)) 179 | { 180 | printf("type mismatch\n");; 181 | exit(1); 182 | } 183 | pnm_read(file, buf); 184 | width = atoi(buf); 185 | 186 | pnm_read(file, buf); 187 | height = atoi(buf); 188 | 189 | pnm_read(file, buf); 190 | if (atoi(buf) > UCHAR_MAX) 191 | { 192 | printf("type mismatch\n");; 193 | exit(1); 194 | } 195 | 196 | /* read data */ 197 | if( im != NULL ) delete[] im; 198 | im = new uchar[width*height*3]; 199 | file.read((char *)im, width * height * 3 * sizeof(uchar)); 200 | } 201 | 202 | void save_pbm(const char* name, uchar* im, int height, int width ) 203 | { 204 | std::ofstream file(name, std::ios::out | std::ios::binary); 205 | 206 | file << "P4\n" << width << " " << height << "\n"; 207 | for (int i = 0; i < height; i++) 208 | write_packed(im+(width*i), width, file); 209 | } 210 | 211 | 212 | } 213 | -------------------------------------------------------------------------------- /daisy_descriptor/src/image_manipulation.cpp: -------------------------------------------------------------------------------- 1 | ////////////////////////////////////////////////////////////////////////// 2 | // Software License Agreement (BSD License) // 3 | // // 4 | // Copyright (c) 2009 // 5 | // Engin Tola // 6 | // web : http://cvlab.epfl.ch/~tola // 7 | // email : engin.tola@epfl.ch // 8 | // // 9 | // All rights reserved. // 10 | // // 11 | // Redistribution and use in source and binary forms, with or without // 12 | // modification, are permitted provided that the following conditions // 13 | // are met: // 14 | // // 15 | // * Redistributions of source code must retain the above copyright // 16 | // notice, this list of conditions and the following disclaimer. // 17 | // * Redistributions in binary form must reproduce the above // 18 | // copyright notice, this list of conditions and the following // 19 | // disclaimer in the documentation and/or other materials provided // 20 | // with the distribution. // 21 | // * Neither the name of the EPFL nor the names of its // 22 | // contributors may be used to endorse or promote products derived // 23 | // from this software without specific prior written permission. // 24 | // // 25 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS // 26 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT // 27 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS // 28 | // FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE // 29 | // COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, // 30 | // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, // 31 | // BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; // 32 | // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER // 33 | // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT // 34 | // LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN // 35 | // ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE // 36 | // POSSIBILITY OF SUCH DAMAGE. // 37 | // // 38 | // See licence.txt file for more details // 39 | ////////////////////////////////////////////////////////////////////////// 40 | 41 | #include 42 | #include 43 | 44 | namespace kutility 45 | { 46 | double* scale_intensity( uchar* image, int sz, double il, double iu) 47 | { 48 | double* out = kutility::allocate(sz); 49 | 50 | double mult = (iu-il)/255.0; 51 | 52 | for( int i=0; i( nh*nw ); 64 | 65 | double ratioy = h / (double)nh; 66 | double ratiox = w / (double)nw; 67 | 68 | int y, x, ynw; 69 | double ny, nx; 70 | 71 | #pragma omp parallel for private( y, x, ny, nx, ynw ) 72 | for( y=0; y>1; 200 | int h_s = h>>1; 201 | 202 | int tmpIndex1, tmpIndex2, tmpIndex3, tmpIndex4, tmpIndex5; 203 | 204 | uchar * out = new uchar[3*w_s*h_s]; 205 | int i; 206 | 207 | for( i=0; i> 2; 219 | out[tmpIndex1+1] = ( image[tmpIndex2+1] + image[tmpIndex3+1] + image[tmpIndex4+1] + image[tmpIndex5+1] ) >> 2; 220 | out[tmpIndex1+2] = ( image[tmpIndex2+2] + image[tmpIndex3+2] + image[tmpIndex4+2] + image[tmpIndex5+2] ) >> 2; 221 | } 222 | } 223 | return out; 224 | } 225 | 226 | int threshold_yen( double *array, int sz) 227 | { 228 | int i,c,c2; //counters 229 | double rho=0.15,threshold=0; 230 | double h=0; 231 | double hf,hb; //total objective fn, foreground and background parts 232 | double currentMaxH=0; //threshold and current max total entropy 233 | double scale=1.0/(1-rho); //used in calculation of entropic correlation 234 | 235 | double pC=0; //cumulative probabilities 236 | double pmf[256]; //probability mass function 237 | 238 | // for( i=0; i<256; i++ ) pmf[i]=0; 239 | memset(pmf, 0, sizeof(double)*256 ); 240 | 241 | //calculation of pmf 242 | for(i=0; i<256; i++) pmf[i]=array[i]/sz; 243 | 244 | for( c=0; c<256; c++ ) 245 | { 246 | 247 | if( pmf[c] != 0 ) 248 | { 249 | pC += pmf[c]; //calculate cumulative probabilities 250 | 251 | //initialization 252 | hf=0; 253 | hb=0; 254 | 255 | //foreground part 256 | for( c2=0; c2= 0 ) hf += pow( pmf[c2] , rho ); 257 | 258 | //background part 259 | for( c2=c; c2<256; c2++ ) if( pmf[c2] > 0 ) hb += pow( pmf[c2] , rho ); 260 | 261 | //total objective function 262 | if( pC < 0.99999999999 ) h = scale*(log(hf*hb)-rho*log(pC*(1-pC))); 263 | 264 | //check if max 265 | if( h>currentMaxH ) { threshold = c; currentMaxH = h; } 266 | } 267 | } 268 | 269 | return (int)threshold; 270 | } 271 | 272 | 273 | } 274 | 275 | -------------------------------------------------------------------------------- /daisy_descriptor/src/interaction.cpp: -------------------------------------------------------------------------------- 1 | ////////////////////////////////////////////////////////////////////////// 2 | // Software License Agreement (BSD License) // 3 | // // 4 | // Copyright (c) 2009 // 5 | // Engin Tola // 6 | // web : http://cvlab.epfl.ch/~tola // 7 | // email : engin.tola@epfl.ch // 8 | // // 9 | // All rights reserved. // 10 | // // 11 | // Redistribution and use in source and binary forms, with or without // 12 | // modification, are permitted provided that the following conditions // 13 | // are met: // 14 | // // 15 | // * Redistributions of source code must retain the above copyright // 16 | // notice, this list of conditions and the following disclaimer. // 17 | // * Redistributions in binary form must reproduce the above // 18 | // copyright notice, this list of conditions and the following // 19 | // disclaimer in the documentation and/or other materials provided // 20 | // with the distribution. // 21 | // * Neither the name of the EPFL nor the names of its // 22 | // contributors may be used to endorse or promote products derived // 23 | // from this software without specific prior written permission. // 24 | // // 25 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS // 26 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT // 27 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS // 28 | // FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE // 29 | // COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, // 30 | // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, // 31 | // BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; // 32 | // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER // 33 | // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT // 34 | // LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN // 35 | // ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE // 36 | // POSSIBILITY OF SUCH DAMAGE. // 37 | // // 38 | // See licence.txt file for more details // 39 | ////////////////////////////////////////////////////////////////////////// 40 | 41 | #include "kutility/interaction.h" 42 | 43 | using std::string; 44 | 45 | namespace kutility 46 | { 47 | void error( string str1, int code ) 48 | { 49 | std::cout<<"ERROR: " 50 | < 140 ) length = 140; 94 | 95 | for( int i=0; i 43 | 44 | void progress_bar::reset() 45 | { 46 | m_current = m_start; 47 | m_progress = 0; 48 | time(&m_starting_time); 49 | } 50 | 51 | void progress_bar::reset(int start, int end, int divisions) 52 | { 53 | m_start = start; 54 | m_current = start; 55 | m_end = end; 56 | m_divisions = divisions; 57 | m_progress = 0; 58 | time(&m_starting_time); 59 | } 60 | 61 | std::ostream& progress_bar::operator>>(std::ostream& os) const 62 | { 63 | if(m_current > (m_progress * (m_end - m_start) / m_divisions) || m_current == m_end) 64 | { 65 | ++m_progress; 66 | os << m_message << m_limit; 67 | for(int c = 1; c <= m_divisions; ++c) 68 | { 69 | if(c < m_progress || m_current == m_end) { 70 | os << m_done; 71 | } 72 | else if(c > m_progress) { 73 | os << m_notDone; 74 | } 75 | else { 76 | os << m_processing; 77 | } 78 | } 79 | os << m_limit; 80 | 81 | time_t now; time(&now); 82 | double percent = double(m_current-m_start)/double(m_end-m_start); 83 | double elapsed = difftime( now, m_starting_time ); 84 | double eta = elapsed / percent; 85 | 86 | os<<" "; 87 | os.width(5); 88 | os.fill(' '); 89 | os.precision(3); 90 | os.setf( std::ios_base::right ); 91 | os<= 4) 131 | { 132 | m_limit = formatString[0]; 133 | m_done = formatString[1]; 134 | m_processing = formatString[2]; 135 | m_notDone = formatString[3]; 136 | } 137 | } 138 | -------------------------------------------------------------------------------- /disparity-daisy.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "daisy/daisy.h" 5 | 6 | using namespace cv; 7 | using namespace std; 8 | using namespace kutility; 9 | 10 | int im_width, im_height; 11 | Mat leftDesc, rightDesc; 12 | int w = 2; 13 | 14 | bool inImg(int x, int y) { 15 | if (x >= 0 && x < im_width && y >= 0 && y < im_height) 16 | return true; 17 | } 18 | 19 | double descCost(Point leftpt, Point rightpt, int w) { 20 | double cost = 0; 21 | for (int j = -w; j <= w; j++) { 22 | for (int k = -w; k <= w; k++) { 23 | if (!inImg(leftpt.x+j, leftpt.y+k) || 24 | !inImg(rightpt.x+j, rightpt.y+k)) 25 | continue; 26 | int idxl = (leftpt.x+j)+(leftpt.y+k)*im_width; 27 | int idxr = (rightpt.x+j)+(rightpt.y+k)*im_width; 28 | cost += norm(leftDesc.row(idxl), rightDesc.row(idxr), CV_L1); 29 | } 30 | } 31 | return cost; 32 | } 33 | 34 | int getCorresPointRight(Point p, int ndisp) { 35 | double minCost = 1e9; 36 | int chosen_i = 0; 37 | for (int i = p.x-ndisp; i <= p.x; i++) { 38 | double cost = descCost(p, Point(i,p.y), w); 39 | if (cost < minCost) { 40 | minCost = cost; 41 | chosen_i = i; 42 | } 43 | } 44 | if (fabs(minCost) < 0.00001) 45 | return p.x; 46 | return chosen_i; 47 | } 48 | 49 | int getCorresPointLeft(Point p, int ndisp) { 50 | double minCost = 1e9; 51 | int chosen_i = 0; 52 | for (int i = p.x; i <= p.x+ndisp; i++) { 53 | double cost = descCost(Point(i,p.y), p, w); 54 | if (cost < minCost) { 55 | minCost = cost; 56 | chosen_i = i; 57 | } 58 | } 59 | if (fabs(minCost) < 0.00001) 60 | return p.x; 61 | return chosen_i; 62 | } 63 | 64 | void computeDisparityMap(int ndisp, Mat &img_disp) { 65 | img_disp = Mat(im_height, im_width, CV_8UC1, Scalar(0)); 66 | for (int i = ndisp+1; i < im_width; i++) { 67 | for (int j = 0; j < im_height; j++) { 68 | cout << i << ", " << j << endl; 69 | int right_i = getCorresPointRight(Point(i,j), ndisp); 70 | // left-right check 71 | int left_i = getCorresPointLeft(Point(right_i,j), ndisp); 72 | if (abs(left_i-i) > 4) 73 | continue; 74 | int disparity = abs(i - right_i); 75 | img_disp.at(j,i) = disparity; 76 | } 77 | } 78 | } 79 | 80 | void computeDenseDesc(const Mat &img, Mat &descrpOut) { 81 | // daisy params 82 | int verbose_level = 4; 83 | int rad = 20; 84 | int radq = 5; 85 | int thq = 8; 86 | int histq = 8; 87 | int nrm_type = NRM_FULL; 88 | bool disable_interpolation = false; 89 | 90 | // associate pointer 91 | uchar *im = img.data; 92 | int h = img.rows; 93 | int w = img.cols; 94 | 95 | daisy* desc = new daisy(); 96 | desc->set_image(im, h, w); 97 | desc->verbose(verbose_level); 98 | desc->set_parameters(rad, radq, thq, histq); 99 | desc->set_normalization(NRM_FULL); 100 | desc->initialize_single_descriptor_mode(); 101 | desc->compute_descriptors(); 102 | desc->normalize_descriptors(); 103 | 104 | int iy, ix, descSize; 105 | descSize = desc->descriptor_size(); 106 | descrpOut.create(h*w, descSize, CV_32FC1); 107 | for (iy=0; iyget_descriptor(iy, ix, thor); 113 | memcpy(descrpOut.ptr(iy*w+ix), thor, descSize*sizeof(float)); 114 | } 115 | } 116 | } 117 | 118 | void preprocess(Mat& img) { 119 | Mat dst; 120 | bilateralFilter(img, dst, 10, 15, 15); 121 | img = dst.clone(); 122 | } 123 | 124 | void normalizeDisparity(Mat &disp) { 125 | int max_disp = -1; 126 | for (int i = 0; i < disp.cols; i++) { 127 | for (int j = 0; j < disp.rows; j++) { 128 | if ((int)disp.at(j,i) > max_disp) 129 | max_disp = disp.at(j,i); 130 | } 131 | } 132 | for (int i = 0; i < disp.cols; i++) { 133 | for (int j = 0; j < disp.rows; j++) { 134 | disp.at(j,i) *= (255. / (float)max_disp); 135 | } 136 | } 137 | } 138 | 139 | int main(int argc, char** argv) { 140 | /* 141 | Mat imgL = imread(argv[1], CV_LOAD_IMAGE_GRAYSCALE); 142 | Mat imgR = imread(argv[2], CV_LOAD_IMAGE_GRAYSCALE); 143 | //preprocess(imgL); 144 | //preprocess(imgR); 145 | im_width = imgL.cols; 146 | im_height = imgL.rows; 147 | computeDenseDesc(imgL, leftDesc); 148 | computeDenseDesc(imgR, rightDesc); 149 | Mat disp; 150 | computeDisparityMap(40, disp); 151 | imshow("left", imgL); 152 | imshow("disp", disp); 153 | imwrite(argv[3], disp); 154 | waitKey(0); 155 | */ 156 | Mat disp = imread(argv[1], CV_LOAD_IMAGE_GRAYSCALE); 157 | normalizeDisparity(disp); 158 | imwrite(argv[1], disp); 159 | return 0; 160 | } -------------------------------------------------------------------------------- /disparity-elas.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include "elas.h" 7 | #include "image.h" 8 | #include "popt_pp.h" 9 | 10 | using namespace std; 11 | using namespace cv; 12 | 13 | Mat generateDisparityMap(Mat& left, Mat& right) { 14 | Mat lb, rb; 15 | if (left.empty() || right.empty()) 16 | return left; 17 | 18 | // convert images to grayscale 19 | cvtColor(left, lb, CV_BGR2GRAY); 20 | cvtColor(right, rb, CV_BGR2GRAY); 21 | 22 | const Size imsize = lb.size(); 23 | const int32_t dims[3] = {imsize.width,imsize.height,imsize.width}; 24 | 25 | Mat leftdpf = Mat::zeros(imsize, CV_32F); 26 | Mat rightdpf = Mat::zeros(imsize, CV_32F); 27 | 28 | Elas::parameters param; 29 | param.postprocess_only_left = true; 30 | Elas elas(param); 31 | 32 | elas.process(lb.data,rb.data,leftdpf.ptr(0),rightdpf.ptr< 33 | float>(0),dims); 34 | 35 | // normalize disparity values between 0 and 255 36 | int max_disp = -1; 37 | for (int i = 0; i < imsize.width; i++) { 38 | for (int j = 0; j < imsize.height; j++) { 39 | if (leftdpf.at(j,i) > max_disp) max_disp = leftdpf.at(j,i); 40 | } 41 | } 42 | for (int i = 0; i < imsize.width; i++) { 43 | for (int j = 0; j < imsize.height; j++) { 44 | leftdpf.at(j,i) = 45 | (int)max(255.0*(float)leftdpf.at(j,i)/max_disp,0.0); 46 | } 47 | } 48 | 49 | Mat show = Mat(left.rows, left.cols, CV_8UC1, Scalar(0)); 50 | leftdpf.convertTo(show, CV_8U, 5.); 51 | /* 52 | max_disp = -1; 53 | for (int i = 0; i < imsize.width; i++) { 54 | for (int j = 0; j < imsize.height; j++) { 55 | if (show.at(j,i) > max_disp) max_disp = show.at(j,i); 56 | } 57 | } 58 | for (int i = 0; i < imsize.width; i++) { 59 | for (int j = 0; j < imsize.height; j++) { 60 | show.at(j,i) = 61 | (int)max(255.0*(float)show.at(j,i)/max_disp,0.0); 62 | } 63 | } 64 | */ 65 | return show; 66 | } 67 | 68 | int main(int argc, char **argv) { 69 | Mat img_left = imread(argv[1], 1); 70 | Mat img_right = imread(argv[2], 1); 71 | Mat img_disp = generateDisparityMap(img_left, img_right); 72 | imwrite(argv[3], img_disp); 73 | return 0; 74 | while (1) { 75 | imshow("IMG-LEFT", img_left); 76 | imshow("IMG-RIGHT", img_right); 77 | if (!img_disp.empty()) 78 | imshow("IMG-DISP", img_disp); 79 | if (waitKey(30) > 0) { 80 | //imwrite(argv[3], img_disp); 81 | break; 82 | } 83 | } 84 | return 0; 85 | } -------------------------------------------------------------------------------- /disparity-lbp.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace cv; 6 | using namespace std; 7 | 8 | Mat img_left, img_right, img_disp; 9 | int niter = 30; 10 | int window_size = 5; 11 | int ndisp = 20; 12 | int smooth_lambda = 20; 13 | int smooth_trunc = 5; 14 | 15 | const int MAX_LABELS = 70; 16 | 17 | enum DIR { 18 | LEFT, 19 | RIGHT, 20 | UP, 21 | DOWN, 22 | COST 23 | }; 24 | 25 | struct Pixel { 26 | int best_label; 27 | double msg[5][MAX_LABELS]; 28 | }; 29 | 30 | struct MarkovRandomField { 31 | int width, height; 32 | vector< Pixel > img; 33 | MarkovRandomField() { 34 | width = img_left.cols; 35 | height = img_left.rows; 36 | int tsize = width * height; 37 | img.resize(tsize); 38 | for (int i = 0; i < tsize; i++) { 39 | for (int label = 0; label < ndisp; label++) { 40 | for (int k = 0; k < 5; k++) { 41 | img[i].best_label = 0; 42 | img[i].msg[k][label] = 1; 43 | } 44 | } 45 | } 46 | } 47 | }; 48 | 49 | long costFunc(Point p, int d) { 50 | long cost = 0; 51 | for (int i = -window_size; i <= window_size; i++) { 52 | for (int j = -window_size; j <= window_size; j++) { 53 | cost += abs(img_left.at(j+p.y,i+p.x) - 54 | img_right.at(j+p.y,i+p.x-d)); 55 | } 56 | } 57 | return cost/((2*window_size+1)*(2*window_size+1)); 58 | } 59 | 60 | long smoothFunc(int i, int j) { 61 | return (long)smooth_lambda * min(abs(i-j), smooth_trunc); 62 | } 63 | 64 | void initCost(MarkovRandomField& mrf) { 65 | int border = ndisp; 66 | for (int i = border; i < mrf.width-border; i++) { 67 | for (int j = border; j < mrf.height-border; j++) { 68 | for (int label = 0; label < ndisp; label++) { 69 | mrf.img[j*img_left.cols+i].msg[COST][label] = costFunc(Point(i,j), 70 | label); 71 | } 72 | } 73 | } 74 | } 75 | 76 | void passMessage(MarkovRandomField& mrf, int x, int y, DIR dir) { 77 | double updated_msg[MAX_LABELS]; 78 | double norm_const = 0; 79 | int width = mrf.width; 80 | for (int i = 0; i < ndisp; i++) { 81 | double max_val = -1; 82 | for (int j = 0; j < ndisp; j++) { 83 | double cost = exp(-smoothFunc(i,j)); 84 | cost *= exp(-mrf.img[y*width+x].msg[COST][j]); 85 | if (dir != LEFT) 86 | cost *= mrf.img[y*width+x].msg[LEFT][j]; 87 | if (dir != RIGHT) 88 | cost *= mrf.img[y*width+x].msg[RIGHT][j]; 89 | if (dir != UP) 90 | cost *= mrf.img[y*width+x].msg[UP][j]; 91 | if (dir != DOWN) 92 | cost *= mrf.img[y*width+x].msg[DOWN][j]; 93 | max_val = max(max_val, cost); 94 | } 95 | updated_msg[i] = max_val; 96 | norm_const += max_val; 97 | } 98 | for (int i = 0; i < ndisp; i++) { 99 | switch (dir) { 100 | case LEFT: 101 | mrf.img[y*width+x-1].msg[RIGHT][i] = updated_msg[i] / norm_const; 102 | break; 103 | case RIGHT: 104 | mrf.img[y*width+x+1].msg[LEFT][i] = updated_msg[i] / norm_const; 105 | break; 106 | case UP: 107 | mrf.img[(y-1)*width+x].msg[DOWN][i] = updated_msg[i] / norm_const; 108 | break; 109 | case DOWN: 110 | mrf.img[(y+1)*width+x].msg[UP][i] = updated_msg[i] / norm_const; 111 | break; 112 | default: 113 | assert(0); 114 | break; 115 | } 116 | } 117 | } 118 | 119 | void propagate(MarkovRandomField& mrf, DIR dir) { 120 | int width = mrf.width; 121 | int height = mrf.height; 122 | 123 | switch (dir) { 124 | case LEFT: 125 | for (int i = width-1; i > 0; i--) { 126 | for (int j = 0; j < height; j++) { 127 | passMessage(mrf, i, j, dir); 128 | } 129 | } 130 | break; 131 | case RIGHT: 132 | for (int i = 0; i < width-1; i++) { 133 | for (int j = 0; j < height; j++) { 134 | passMessage(mrf, i, j, dir); 135 | } 136 | } 137 | break; 138 | case DOWN: 139 | for (int i = 0; i < width; i++) { 140 | for (int j = 0; j < height-1; j++) { 141 | passMessage(mrf, i, j, dir); 142 | } 143 | } 144 | break; 145 | case UP: 146 | for (int i = 0; i < width-1; i++) { 147 | for (int j = height-1; j > 0; j--) { 148 | passMessage(mrf, i, j, dir); 149 | } 150 | } 151 | break; 152 | default: 153 | assert(0); 154 | break; 155 | } 156 | } 157 | 158 | double MAP(MarkovRandomField& mrf) { 159 | int width = mrf.width; 160 | int height = mrf.height; 161 | for (int i = 0; i < mrf.img.size(); i++) { 162 | double max_belief = -1; 163 | for (int k = 0; k < ndisp; k++) { 164 | double belief = 1; 165 | belief *= exp(-mrf.img[i].msg[COST][k]); 166 | belief *= mrf.img[i].msg[LEFT][k]; 167 | belief *= mrf.img[i].msg[RIGHT][k]; 168 | belief *= mrf.img[i].msg[UP][k]; 169 | belief *= mrf.img[i].msg[DOWN][k]; 170 | if (belief > max_belief) { 171 | max_belief = belief; 172 | mrf.img[i].best_label = k; 173 | } 174 | } 175 | } 176 | double energy = 0; 177 | 178 | for (int i = 0; i < width; i++) { 179 | for (int j = 0; j < height; j++) { 180 | int d = mrf.img[j*width+i].best_label; 181 | energy += mrf.img[j*width+i].msg[COST][d]; 182 | if (i-1 >= 0) 183 | energy += smoothFunc(d, mrf.img[j*width+i-1].best_label); 184 | if (i+1 < width) 185 | energy += smoothFunc(d, mrf.img[j*width+i+1].best_label); 186 | if (j-1 >= 0) 187 | energy += smoothFunc(d, mrf.img[(j-1)*width+i].best_label); 188 | if (j+1 < height) 189 | energy += smoothFunc(d, mrf.img[(j+1)*width+i].best_label); 190 | } 191 | } 192 | 193 | return energy; 194 | } 195 | 196 | void computeDisparityMap(MarkovRandomField& mrf) { 197 | initCost(mrf); 198 | for (int i = 0; i < niter; i++) { 199 | propagate(mrf, RIGHT); 200 | propagate(mrf, DOWN); 201 | propagate(mrf, LEFT); 202 | propagate(mrf, UP); 203 | 204 | double energy = MAP(mrf); 205 | cout << "Iter " << (i+1) << ": " << energy << endl; 206 | } 207 | int border = ndisp; 208 | for (int i = border; i < mrf.width-border; i++) { 209 | for (int j = border; j < mrf.height-border; j++) { 210 | img_disp.at(j,i) = mrf.img[j*mrf.width+i].best_label * (256 / 211 | ndisp); 212 | } 213 | } 214 | } 215 | 216 | int main(int argc, char const *argv[]) 217 | { 218 | img_left = imread(argv[1], 0); 219 | img_right = imread(argv[2], 0); 220 | img_disp = Mat(img_left.rows, img_left.cols, CV_8UC1, Scalar(0)); 221 | namedWindow("IMG-LEFT", 1); 222 | namedWindow("IMG-RIGHT", 1); 223 | 224 | MarkovRandomField mrf; 225 | computeDisparityMap(mrf); 226 | 227 | imwrite(argv[3], img_disp); 228 | return 0; 229 | 230 | while (1) { 231 | imshow("IMG-LEFT", img_left); 232 | imshow("IMG-RIGHT", img_right); 233 | imshow("IMG-DISP", img_disp); 234 | if (waitKey(30) > 0) { 235 | //imwrite("disp-lbp.png", img_disp); 236 | break; 237 | } 238 | } 239 | return 0; 240 | } -------------------------------------------------------------------------------- /disparity-orb.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace cv; 6 | using namespace std; 7 | 8 | Mat img_left, img_right, img_disp; 9 | Mat img_left_desc, img_right_desc; 10 | vector< KeyPoint > kpl, kpr; 11 | int w = 0; 12 | 13 | bool inImg(Mat& img, int x, int y) { 14 | if (x >= 0 && x < img.cols && y >= 0 && y < img.rows) 15 | return true; 16 | } 17 | 18 | bool isLeftKeyPoint(int i, int j) { 19 | int n = kpl.size(); 20 | return (i >= kpl[0].pt.x && i <= kpl[n-1].pt.x 21 | && j >= kpl[0].pt.y && j <= kpl[n-1].pt.y); 22 | } 23 | 24 | bool isRightKeyPoint(int i, int j) { 25 | int n = kpr.size(); 26 | return (i >= kpr[0].pt.x && i <= kpr[n-1].pt.x 27 | && j >= kpr[0].pt.y && j <= kpr[n-1].pt.y); 28 | } 29 | 30 | long descCost(Point leftpt, Point rightpt, int w) { 31 | int x0r = kpr[0].pt.x; 32 | int y0r = kpr[0].pt.y; 33 | int ynr = kpr[kpr.size()-1].pt.y; 34 | int x0l = kpl[0].pt.x; 35 | int y0l = kpl[0].pt.y; 36 | int ynl = kpl[kpl.size()-1].pt.y; 37 | long cost = 0; 38 | for (int j = -w; j <= w; j++) { 39 | for (int k = -w; k <= w; k++) { 40 | if (!isLeftKeyPoint(leftpt.x+j, leftpt.y+k) || 41 | !isRightKeyPoint(rightpt.x+j, rightpt.y+k)) 42 | continue; 43 | int idxl = (leftpt.x+j-x0l)*(ynl-y0l+1)+(leftpt.y+k-y0l); 44 | int idxr = (rightpt.x+j-x0r)*(ynr-y0r+1)+(rightpt.y+k-y0r); 45 | cost += norm(img_left_desc.row(idxl), img_right_desc.row(idxr), CV_L1); 46 | } 47 | } 48 | return cost / ((2*w+1)*(2*w+1)); 49 | } 50 | 51 | double descCostNCC(Point leftpt, Point rightpt, int w) { 52 | int x0r = kpr[0].pt.x; 53 | int y0r = kpr[0].pt.y; 54 | int ynr = kpr[kpr.size()-1].pt.y; 55 | int x0l = kpl[0].pt.x; 56 | int y0l = kpl[0].pt.y; 57 | int ynl = kpl[kpl.size()-1].pt.y; 58 | double costL = 0; 59 | double costR = 0; 60 | double cost = 0; 61 | int idxl0 = (leftpt.x-x0l)*(ynl-y0l+1)+(leftpt.y-y0l); 62 | int idxr0 = (rightpt.x-x0r)*(ynr-y0r+1)+(rightpt.y-y0r); 63 | for (int j = -w; j <= w; j++) { 64 | for (int k = -w; k <= w; k++) { 65 | if (!isLeftKeyPoint(leftpt.x+j, leftpt.y+k) || 66 | !isRightKeyPoint(rightpt.x+j, rightpt.y+k)) 67 | continue; 68 | int idxl = (leftpt.x+j-x0l)*(ynl-y0l+1)+(leftpt.y+k-y0l); 69 | int idxr = (rightpt.x+j-x0r)*(ynr-y0r+1)+(rightpt.y+k-y0r); 70 | double d1 = norm(img_left_desc.row(idxl), img_left_desc.row(idxl0), 71 | CV_L1); 72 | double d2 = norm(img_right_desc.row(idxr), img_right_desc.row(idxr0), 73 | CV_L1); 74 | costL += d1*d1; 75 | costR += d2*d2; 76 | cost += d1*d2; 77 | } 78 | } 79 | cost /= (sqrt(costL) * sqrt(costR)); 80 | cout << "ncc: " << cost << endl; 81 | return cost; 82 | } 83 | 84 | int getCorresPointRight(Point p, int ndisp) { 85 | long minCost = 1e9; 86 | int chosen_i = 0; 87 | for (int i = p.x-ndisp; i <= p.x; i++) { 88 | long cost = descCost(p, Point(i,p.y), w); 89 | if (cost < minCost) { 90 | minCost = cost; 91 | chosen_i = i; 92 | } 93 | } 94 | if (minCost == 0) 95 | return p.x; 96 | return chosen_i; 97 | /* 98 | double corr = -10; 99 | int chosen_i = 0; 100 | for (int i = p.x-ndisp; i <= p.x; i++) { 101 | double cost = descCostNCC(p, Point(i,p.y), w); 102 | if (cost > corr) { 103 | corr = cost; 104 | chosen_i = i; 105 | } 106 | } 107 | cout << "corr: " << corr << endl; 108 | return chosen_i; 109 | */ 110 | } 111 | 112 | int getCorresPointLeft(Point p, int ndisp) { 113 | long minCost = 1e9; 114 | int chosen_i = 0; 115 | for (int i = p.x; i <= p.x+ndisp; i++) { 116 | long cost = descCost(Point(i,p.y), p, w); 117 | if (cost < minCost) { 118 | minCost = cost; 119 | chosen_i = i; 120 | } 121 | } 122 | if (minCost == 0) 123 | return p.x; 124 | return chosen_i; 125 | } 126 | 127 | void computeDisparityMapORB(int ndisp) { 128 | img_disp = Mat(img_left.rows, img_left.cols, CV_8UC1, Scalar(0)); 129 | for (int i = ndisp+1; i < img_left.cols; i++) { 130 | for (int j = 0; j < img_left.rows; j++) { 131 | cout << i << ", " << j << endl; 132 | if (!isLeftKeyPoint(i,j)) 133 | continue; 134 | int right_i = getCorresPointRight(Point(i,j), ndisp); 135 | // left-right check 136 | /* 137 | int left_i = getCorresPointLeft(Point(right_i,j), ndisp); 138 | if (abs(left_i-i) > 4) 139 | continue; 140 | */ 141 | int disparity = abs(i - right_i); 142 | img_disp.at(j,i) = disparity; 143 | } 144 | } 145 | } 146 | 147 | void cacheDescriptorVals() { 148 | OrbDescriptorExtractor extractor; 149 | //BriefDescriptorExtractor extractor; 150 | for (int i = 0; i < img_left.cols; i++) { 151 | for (int j = 0; j < img_left.rows; j++) { 152 | kpl.push_back(KeyPoint(i,j,1)); 153 | kpr.push_back(KeyPoint(i,j,1)); 154 | } 155 | } 156 | extractor.compute(img_left, kpl, img_left_desc); 157 | extractor.compute(img_right, kpr, img_right_desc); 158 | } 159 | 160 | void preprocess(Mat& img) { 161 | Mat dst; 162 | bilateralFilter(img, dst, 10, 15, 15); 163 | img = dst.clone(); 164 | } 165 | 166 | int main(int argc, char const *argv[]) 167 | { 168 | img_left = imread(argv[1], 1); 169 | img_right = imread(argv[2], 1); 170 | //preprocess(img_left); 171 | //preprocess(img_right); 172 | cacheDescriptorVals(); 173 | computeDisparityMapORB(40); 174 | //namedWindow("IMG-LEFT", 1); 175 | //namedWindow("IMG-RIGHT", 1); 176 | while (1) { 177 | imshow("IMG-LEFT", img_left); 178 | imshow("IMG-RIGHT", img_right); 179 | imshow("IMG-DISP", img_disp); 180 | if (waitKey(30) > 0) { 181 | imwrite(argv[3], img_disp); 182 | break; 183 | } 184 | } 185 | return 0; 186 | } -------------------------------------------------------------------------------- /disparity.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace cv; 6 | using namespace std; 7 | 8 | Mat img_left, img_right, img_disp; 9 | 10 | bool inImg(Mat& img, int x, int y) { 11 | if (x >= 0 && x < img.cols && y >= 0 && y < img.rows) 12 | return true; 13 | } 14 | 15 | long costF(Mat& left, Mat& right) { 16 | long cost = 0; 17 | for (int i = 0; i < 32; i++) { 18 | cost += abs(left.at(0,i)-right.at(0,i)); 19 | } 20 | return cost; 21 | } 22 | 23 | int getCorresPoint(Point p, Mat& img1, Mat& img2, int ndisp) { 24 | int w = 7; 25 | long minCost = 1e9; 26 | int chosen_i = 0; 27 | for (int i = p.x - ndisp; i <= p.x; i++) { 28 | long error = 0; 29 | for (int k = -w; k <= w; k++) { 30 | for (int j = -w; j <= w; j++) { 31 | if (inImg(img1, p.x+k, p.y+j) && inImg(img2, i+k, p.y+j)) { 32 | error += abs(img1.at(p.y+j,p.x+k) - 33 | img2.at(p.y+j,i+k)); 34 | } 35 | } 36 | } 37 | if (error < minCost) { 38 | minCost = error; 39 | chosen_i = i; 40 | } 41 | } 42 | return chosen_i; 43 | } 44 | 45 | void computeDisparityMap(int ndisp) { 46 | img_disp = Mat(img_left.rows, img_left.cols, CV_8UC1, Scalar(0)); 47 | for (int i = ndisp; i < img_left.cols-ndisp; i++) { 48 | for (int j = ndisp; j < img_left.rows-ndisp; j++) { 49 | cout << i << ", " << j << endl; 50 | /* 51 | int valid_pixel = 0; 52 | for (int ch = 0; ch < 3; ch++) { 53 | valid_pixel += img_left.at(j,i)[ch]; 54 | } 55 | */ 56 | /* 57 | if (img_left.at(j,i) == 0) 58 | continue; 59 | */ 60 | int right_i = getCorresPoint(Point(i,j), img_left, img_right, ndisp); 61 | int disparity = abs(i - right_i); 62 | img_disp.at(j,i) = disparity * (255. / ndisp); 63 | } 64 | } 65 | } 66 | 67 | int main(int argc, char const *argv[]) 68 | { 69 | img_left = imread(argv[1], 0); 70 | img_right = imread(argv[2], 0); 71 | //computeDisparityMapORB(20); 72 | computeDisparityMap(20); 73 | namedWindow("IMG-LEFT", 1); 74 | namedWindow("IMG-RIGHT", 1); 75 | while (1) { 76 | imshow("IMG-LEFT", img_left); 77 | imshow("IMG-RIGHT", img_right); 78 | imshow("IMG-DISP", img_disp); 79 | if (waitKey(30) > 0) { 80 | //imwrite(argv[3], img_disp); 81 | break; 82 | } 83 | } 84 | return 0; 85 | } -------------------------------------------------------------------------------- /elas/descriptor.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2011. All rights reserved. 3 | Institute of Measurement and Control Systems 4 | Karlsruhe Institute of Technology, Germany 5 | 6 | This file is part of libelas. 7 | Authors: Andreas Geiger 8 | 9 | libelas is free software; you can redistribute it and/or modify it under the 10 | terms of the GNU General Public License as published by the Free Software 11 | Foundation; either version 3 of the License, or any later version. 12 | 13 | libelas is distributed in the hope that it will be useful, but WITHOUT ANY 14 | WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A 15 | PARTICULAR PURPOSE. See the GNU General Public License for more details. 16 | 17 | You should have received a copy of the GNU General Public License along with 18 | libelas; if not, write to the Free Software Foundation, Inc., 51 Franklin 19 | Street, Fifth Floor, Boston, MA 02110-1301, USA 20 | */ 21 | 22 | #include "descriptor.h" 23 | #include "filter.h" 24 | #include 25 | 26 | using namespace std; 27 | 28 | Descriptor::Descriptor(uint8_t* I,int32_t width,int32_t height,int32_t bpl,bool half_resolution) { 29 | I_desc = (uint8_t*)_mm_malloc(16*width*height*sizeof(uint8_t),16); 30 | uint8_t* I_du = (uint8_t*)_mm_malloc(bpl*height*sizeof(uint8_t),16); 31 | uint8_t* I_dv = (uint8_t*)_mm_malloc(bpl*height*sizeof(uint8_t),16); 32 | filter::sobel3x3(I,I_du,I_dv,bpl,height); 33 | createDescriptor(I_du,I_dv,width,height,bpl,half_resolution); 34 | _mm_free(I_du); 35 | _mm_free(I_dv); 36 | } 37 | 38 | Descriptor::~Descriptor() { 39 | _mm_free(I_desc); 40 | } 41 | 42 | void Descriptor::createDescriptor (uint8_t* I_du,uint8_t* I_dv,int32_t width,int32_t height,int32_t bpl,bool half_resolution) { 43 | 44 | uint8_t *I_desc_curr; 45 | uint32_t addr_v0,addr_v1,addr_v2,addr_v3,addr_v4; 46 | 47 | // do not compute every second line 48 | if (half_resolution) { 49 | 50 | // create filter strip 51 | for (int32_t v=4; v 30 | #include 31 | #include 32 | #include 33 | #include 34 | 35 | // Define fixed-width datatypes for Visual Studio projects 36 | #ifndef _MSC_VER 37 | #include 38 | #else 39 | typedef __int8 int8_t; 40 | typedef __int16 int16_t; 41 | typedef __int32 int32_t; 42 | typedef __int64 int64_t; 43 | typedef unsigned __int8 uint8_t; 44 | typedef unsigned __int16 uint16_t; 45 | typedef unsigned __int32 uint32_t; 46 | typedef unsigned __int64 uint64_t; 47 | #endif 48 | 49 | class Descriptor { 50 | 51 | public: 52 | 53 | // constructor creates filters 54 | Descriptor(uint8_t* I,int32_t width,int32_t height,int32_t bpl,bool half_resolution); 55 | 56 | // deconstructor releases memory 57 | ~Descriptor(); 58 | 59 | // descriptors accessible from outside 60 | uint8_t* I_desc; 61 | 62 | private: 63 | 64 | // build descriptor I_desc from I_du and I_dv 65 | void createDescriptor(uint8_t* I_du,uint8_t* I_dv,int32_t width,int32_t height,int32_t bpl,bool half_resolution); 66 | 67 | }; 68 | 69 | #endif 70 | -------------------------------------------------------------------------------- /elas/elas.h: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2011. All rights reserved. 3 | Institute of Measurement and Control Systems 4 | Karlsruhe Institute of Technology, Germany 5 | 6 | This file is part of libelas. 7 | Authors: Andreas Geiger 8 | 9 | libelas is free software; you can redistribute it and/or modify it under the 10 | terms of the GNU General Public License as published by the Free Software 11 | Foundation; either version 3 of the License, or any later version. 12 | 13 | libelas is distributed in the hope that it will be useful, but WITHOUT ANY 14 | WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A 15 | PARTICULAR PURPOSE. See the GNU General Public License for more details. 16 | 17 | You should have received a copy of the GNU General Public License along with 18 | libelas; if not, write to the Free Software Foundation, Inc., 51 Franklin 19 | Street, Fifth Floor, Boston, MA 02110-1301, USA 20 | */ 21 | 22 | // Main header file. Include this to use libelas in your code. 23 | 24 | #ifndef __ELAS_H__ 25 | #define __ELAS_H__ 26 | 27 | #include 28 | #include 29 | #include 30 | #include 31 | #include 32 | #include 33 | 34 | // define fixed-width datatypes for Visual Studio projects 35 | #ifndef _MSC_VER 36 | #include 37 | #else 38 | typedef __int8 int8_t; 39 | typedef __int16 int16_t; 40 | typedef __int32 int32_t; 41 | typedef __int64 int64_t; 42 | typedef unsigned __int8 uint8_t; 43 | typedef unsigned __int16 uint16_t; 44 | typedef unsigned __int32 uint32_t; 45 | typedef unsigned __int64 uint64_t; 46 | #endif 47 | 48 | #ifdef PROFILE 49 | #include "timer.h" 50 | #endif 51 | 52 | class Elas { 53 | 54 | public: 55 | 56 | enum setting {ROBOTICS,MIDDLEBURY}; 57 | 58 | // parameter settings 59 | struct parameters { 60 | int32_t disp_min; // min disparity 61 | int32_t disp_max; // max disparity 62 | float support_threshold; // max. uniqueness ratio (best vs. second best support match) 63 | int32_t support_texture; // min texture for support points 64 | int32_t candidate_stepsize; // step size of regular grid on which support points are matched 65 | int32_t incon_window_size; // window size of inconsistent support point check 66 | int32_t incon_threshold; // disparity similarity threshold for support point to be considered consistent 67 | int32_t incon_min_support; // minimum number of consistent support points 68 | bool add_corners; // add support points at image corners with nearest neighbor disparities 69 | int32_t grid_size; // size of neighborhood for additional support point extrapolation 70 | float beta; // image likelihood parameter 71 | float gamma; // prior constant 72 | float sigma; // prior sigma 73 | float sradius; // prior sigma radius 74 | int32_t match_texture; // min texture for dense matching 75 | int32_t lr_threshold; // disparity threshold for left/right consistency check 76 | float speckle_sim_threshold; // similarity threshold for speckle segmentation 77 | int32_t speckle_size; // maximal size of a speckle (small speckles get removed) 78 | int32_t ipol_gap_width; // interpolate small gaps (left<->right, top<->bottom) 79 | bool filter_median; // optional median filter (approximated) 80 | bool filter_adaptive_mean; // optional adaptive mean filter (approximated) 81 | bool postprocess_only_left; // saves time by not postprocessing the right image 82 | bool subsampling; // saves time by only computing disparities for each 2nd pixel 83 | // note: for this option D1 and D2 must be passed with size 84 | // width/2 x height/2 (rounded towards zero) 85 | 86 | // constructor 87 | parameters (setting s=ROBOTICS) { 88 | 89 | // default settings in a robotics environment 90 | // (do not produce results in half-occluded areas 91 | // and are a bit more robust towards lighting etc.) 92 | if (s==ROBOTICS) { 93 | disp_min = 0; 94 | disp_max = 40; 95 | support_threshold = 0.85; 96 | support_texture = 10; 97 | candidate_stepsize = 5; 98 | incon_window_size = 5; 99 | incon_threshold = 5; 100 | incon_min_support = 5; 101 | add_corners = 0; 102 | grid_size = 20; 103 | beta = 0.02; 104 | gamma = 3; 105 | sigma = 1; 106 | sradius = 2; 107 | match_texture = 1; 108 | lr_threshold = 2; 109 | speckle_sim_threshold = 1; 110 | speckle_size = 200; 111 | ipol_gap_width = 20; 112 | filter_median = 0; 113 | filter_adaptive_mean = 1; 114 | postprocess_only_left = 1; 115 | subsampling = 0; 116 | 117 | // default settings for middlebury benchmark 118 | // (interpolate all missing disparities) 119 | } else { 120 | disp_min = 0; 121 | disp_max = 255; 122 | support_threshold = 0.95; 123 | support_texture = 10; 124 | candidate_stepsize = 5; 125 | incon_window_size = 5; 126 | incon_threshold = 5; 127 | incon_min_support = 5; 128 | add_corners = 1; 129 | grid_size = 20; 130 | beta = 0.02; 131 | gamma = 5; 132 | sigma = 1; 133 | sradius = 3; 134 | match_texture = 0; 135 | lr_threshold = 2; 136 | speckle_sim_threshold = 1; 137 | speckle_size = 200; 138 | ipol_gap_width = 5000; 139 | filter_median = 1; 140 | filter_adaptive_mean = 0; 141 | postprocess_only_left = 0; 142 | subsampling = 0; 143 | } 144 | } 145 | }; 146 | 147 | // constructor, input: parameters 148 | Elas (parameters param) : param(param) {} 149 | 150 | // deconstructor 151 | ~Elas () {} 152 | 153 | // matching function 154 | // inputs: pointers to left (I1) and right (I2) intensity image (uint8, input) 155 | // pointers to left (D1) and right (D2) disparity image (float, output) 156 | // dims[0] = width of I1 and I2 157 | // dims[1] = height of I1 and I2 158 | // dims[2] = bytes per line (often equal to width, but allowed to differ) 159 | // note: D1 and D2 must be allocated before (bytes per line = width) 160 | // if subsampling is not active their size is width x height, 161 | // otherwise width/2 x height/2 (rounded towards zero) 162 | void process (uint8_t* I1,uint8_t* I2,float* D1,float* D2,const int32_t* dims); 163 | 164 | private: 165 | 166 | struct support_pt { 167 | int32_t u; 168 | int32_t v; 169 | int32_t d; 170 | support_pt(int32_t u,int32_t v,int32_t d):u(u),v(v),d(d){} 171 | }; 172 | 173 | struct triangle { 174 | int32_t c1,c2,c3; 175 | float t1a,t1b,t1c; 176 | float t2a,t2b,t2c; 177 | triangle(int32_t c1,int32_t c2,int32_t c3):c1(c1),c2(c2),c3(c3){} 178 | }; 179 | 180 | inline uint32_t getAddressOffsetImage (const int32_t& u,const int32_t& v,const int32_t& width) { 181 | return v*width+u; 182 | } 183 | 184 | inline uint32_t getAddressOffsetGrid (const int32_t& x,const int32_t& y,const int32_t& d,const int32_t& width,const int32_t& disp_num) { 185 | return (y*width+x)*disp_num+d; 186 | } 187 | 188 | // support point functions 189 | void removeInconsistentSupportPoints (int16_t* D_can,int32_t D_can_width,int32_t D_can_height); 190 | void removeRedundantSupportPoints (int16_t* D_can,int32_t D_can_width,int32_t D_can_height, 191 | int32_t redun_max_dist, int32_t redun_threshold, bool vertical); 192 | void addCornerSupportPoints (std::vector &p_support); 193 | inline int16_t computeMatchingDisparity (const int32_t &u,const int32_t &v,uint8_t* I1_desc,uint8_t* I2_desc,const bool &right_image); 194 | std::vector computeSupportMatches (uint8_t* I1_desc,uint8_t* I2_desc); 195 | 196 | // triangulation & grid 197 | std::vector computeDelaunayTriangulation (std::vector p_support,int32_t right_image); 198 | void computeDisparityPlanes (std::vector p_support,std::vector &tri,int32_t right_image); 199 | void createGrid (std::vector p_support,int32_t* disparity_grid,int32_t* grid_dims,bool right_image); 200 | 201 | // matching 202 | inline void updatePosteriorMinimum (__m128i* I2_block_addr,const int32_t &d,const int32_t &w, 203 | const __m128i &xmm1,__m128i &xmm2,int32_t &val,int32_t &min_val,int32_t &min_d); 204 | inline void updatePosteriorMinimum (__m128i* I2_block_addr,const int32_t &d, 205 | const __m128i &xmm1,__m128i &xmm2,int32_t &val,int32_t &min_val,int32_t &min_d); 206 | inline void findMatch (int32_t &u,int32_t &v,float &plane_a,float &plane_b,float &plane_c, 207 | int32_t* disparity_grid,int32_t *grid_dims,uint8_t* I1_desc,uint8_t* I2_desc, 208 | int32_t *P,int32_t &plane_radius,bool &valid,bool &right_image,float* D); 209 | void computeDisparity (std::vector p_support,std::vector tri,int32_t* disparity_grid,int32_t* grid_dims, 210 | uint8_t* I1_desc,uint8_t* I2_desc,bool right_image,float* D); 211 | 212 | // L/R consistency check 213 | void leftRightConsistencyCheck (float* D1,float* D2); 214 | 215 | // postprocessing 216 | void removeSmallSegments (float* D); 217 | void gapInterpolation (float* D); 218 | 219 | // optional postprocessing 220 | void adaptiveMean (float* D); 221 | void median (float* D); 222 | 223 | // parameter set 224 | parameters param; 225 | 226 | // memory aligned input images + dimensions 227 | uint8_t *I1,*I2; 228 | int32_t width,height,bpl; 229 | 230 | // profiling timer 231 | #ifdef PROFILE 232 | Timer timer; 233 | #endif 234 | }; 235 | 236 | #endif 237 | -------------------------------------------------------------------------------- /elas/filter.h: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2011. All rights reserved. 3 | Institute of Measurement and Control Systems 4 | Karlsruhe Institute of Technology, Germany 5 | 6 | This file is part of libelas. 7 | Authors: Julius Ziegler, Andreas Geiger 8 | 9 | libelas is free software; you can redistribute it and/or modify it under the 10 | terms of the GNU General Public License as published by the Free Software 11 | Foundation; either version 3 of the License, or any later version. 12 | 13 | libelas is distributed in the hope that it will be useful, but WITHOUT ANY 14 | WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A 15 | PARTICULAR PURPOSE. See the GNU General Public License for more details. 16 | 17 | You should have received a copy of the GNU General Public License along with 18 | libelas; if not, write to the Free Software Foundation, Inc., 51 Franklin 19 | Street, Fifth Floor, Boston, MA 02110-1301, USA 20 | */ 21 | 22 | #ifndef __FILTER_H__ 23 | #define __FILTER_H__ 24 | 25 | #include 26 | #include 27 | 28 | // define fixed-width datatypes for Visual Studio projects 29 | #ifndef _MSC_VER 30 | #include 31 | #else 32 | typedef __int8 int8_t; 33 | typedef __int16 int16_t; 34 | typedef __int32 int32_t; 35 | typedef __int64 int64_t; 36 | typedef unsigned __int8 uint8_t; 37 | typedef unsigned __int16 uint16_t; 38 | typedef unsigned __int32 uint32_t; 39 | typedef unsigned __int64 uint64_t; 40 | #endif 41 | 42 | // fast filters: implements 3x3 and 5x5 sobel filters and 43 | // 5x5 blob and corner filters based on SSE2/3 instructions 44 | namespace filter { 45 | 46 | // private namespace, public user functions at the bottom of this file 47 | namespace detail { 48 | void integral_image( const uint8_t* in, int32_t* out, int w, int h ); 49 | void unpack_8bit_to_16bit( const __m128i a, __m128i& b0, __m128i& b1 ); 50 | void pack_16bit_to_8bit_saturate( const __m128i a0, const __m128i a1, __m128i& b ); 51 | 52 | // convolve image with a (1,4,6,4,1) row vector. Result is accumulated into output. 53 | // output is scaled by 1/128, then clamped to [-128,128], and finally shifted to [0,255]. 54 | void convolve_14641_row_5x5_16bit( const int16_t* in, uint8_t* out, int w, int h ); 55 | 56 | // convolve image with a (1,2,0,-2,-1) row vector. Result is accumulated into output. 57 | // This one works on 16bit input and 8bit output. 58 | // output is scaled by 1/128, then clamped to [-128,128], and finally shifted to [0,255]. 59 | void convolve_12021_row_5x5_16bit( const int16_t* in, uint8_t* out, int w, int h ); 60 | 61 | // convolve image with a (1,2,1) row vector. Result is accumulated into output. 62 | // This one works on 16bit input and 8bit output. 63 | // output is scaled by 1/4, then clamped to [-128,128], and finally shifted to [0,255]. 64 | void convolve_121_row_3x3_16bit( const int16_t* in, uint8_t* out, int w, int h ); 65 | 66 | // convolve image with a (1,0,-1) row vector. Result is accumulated into output. 67 | // This one works on 16bit input and 8bit output. 68 | // output is scaled by 1/4, then clamped to [-128,128], and finally shifted to [0,255]. 69 | void convolve_101_row_3x3_16bit( const int16_t* in, uint8_t* out, int w, int h ); 70 | 71 | void convolve_cols_5x5( const unsigned char* in, int16_t* out_v, int16_t* out_h, int w, int h ); 72 | 73 | void convolve_col_p1p1p0m1m1_5x5( const unsigned char* in, int16_t* out, int w, int h ); 74 | 75 | void convolve_row_p1p1p0m1m1_5x5( const int16_t* in, int16_t* out, int w, int h ); 76 | 77 | void convolve_cols_3x3( const unsigned char* in, int16_t* out_v, int16_t* out_h, int w, int h ); 78 | } 79 | 80 | void sobel3x3( const uint8_t* in, uint8_t* out_v, uint8_t* out_h, int w, int h ); 81 | 82 | void sobel5x5( const uint8_t* in, uint8_t* out_v, uint8_t* out_h, int w, int h ); 83 | 84 | // -1 -1 0 1 1 85 | // -1 -1 0 1 1 86 | // 0 0 0 0 0 87 | // 1 1 0 -1 -1 88 | // 1 1 0 -1 -1 89 | void checkerboard5x5( const uint8_t* in, int16_t* out, int w, int h ); 90 | 91 | // -1 -1 -1 -1 -1 92 | // -1 1 1 1 -1 93 | // -1 1 8 1 -1 94 | // -1 1 1 1 -1 95 | // -1 -1 -1 -1 -1 96 | void blob5x5( const uint8_t* in, int16_t* out, int w, int h ); 97 | }; 98 | 99 | #endif 100 | -------------------------------------------------------------------------------- /elas/image.h: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2011. All rights reserved. 3 | Institute of Measurement and Control Systems 4 | Karlsruhe Institute of Technology, Germany 5 | 6 | This file is part of libelas. 7 | Authors: Andreas Geiger 8 | 9 | libelas is free software; you can redistribute it and/or modify it under the 10 | terms of the GNU General Public License as published by the Free Software 11 | Foundation; either version 3 of the License, or any later version. 12 | 13 | libelas is distributed in the hope that it will be useful, but WITHOUT ANY 14 | WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A 15 | PARTICULAR PURPOSE. See the GNU General Public License for more details. 16 | 17 | You should have received a copy of the GNU General Public License along with 18 | libelas; if not, write to the Free Software Foundation, Inc., 51 Franklin 19 | Street, Fifth Floor, Boston, MA 02110-1301, USA 20 | */ 21 | 22 | // basic image I/O, based on Pedro Felzenszwalb's code 23 | 24 | #ifndef IMAGE_H 25 | #define IMAGE_H 26 | 27 | #include 28 | #include 29 | #include 30 | #include 31 | 32 | // use imRef to access image data. 33 | #define imRef(im, x, y) (im->access[y][x]) 34 | 35 | // use imPtr to get pointer to image data. 36 | #define imPtr(im, x, y) &(im->access[y][x]) 37 | 38 | #define BUF_SIZE 256 39 | 40 | typedef unsigned char uchar; 41 | typedef struct { uchar r, g, b; } rgb; 42 | 43 | inline bool operator==(const rgb &a, const rgb &b) { 44 | return ((a.r == b.r) && (a.g == b.g) && (a.b == b.b)); 45 | } 46 | 47 | // image class 48 | template class image { 49 | public: 50 | 51 | // create image 52 | image(const int width, const int height, const bool init = false); 53 | 54 | // delete image 55 | ~image(); 56 | 57 | // init image 58 | void init(const T &val); 59 | 60 | // deep copy 61 | image *copy() const; 62 | 63 | // get image width/height 64 | int width() const { return w; } 65 | int height() const { return h; } 66 | 67 | // image data 68 | T *data; 69 | 70 | // row pointers 71 | T **access; 72 | 73 | private: 74 | int w, h; 75 | }; 76 | 77 | template image::image(const int width, const int height, const bool init) { 78 | w = width; 79 | h = height; 80 | data = new T[w * h]; // allocate space for image data 81 | access = new T*[h]; // allocate space for row pointers 82 | 83 | // initialize row pointers 84 | for (int i = 0; i < h; i++) 85 | access[i] = data + (i * w); 86 | 87 | // init to zero 88 | if (init) 89 | memset(data, 0, w * h * sizeof(T)); 90 | } 91 | 92 | template image::~image() { 93 | delete [] data; 94 | delete [] access; 95 | } 96 | 97 | template void image::init(const T &val) { 98 | T *ptr = imPtr(this, 0, 0); 99 | T *end = imPtr(this, w-1, h-1); 100 | while (ptr <= end) 101 | *ptr++ = val; 102 | } 103 | 104 | 105 | template image *image::copy() const { 106 | image *im = new image(w, h, false); 107 | memcpy(im->data, data, w * h * sizeof(T)); 108 | return im; 109 | } 110 | 111 | class pnm_error {}; 112 | 113 | void pnm_read(std::ifstream &file, char *buf) { 114 | char doc[BUF_SIZE]; 115 | char c; 116 | 117 | file >> c; 118 | while (c == '#') { 119 | file.getline(doc, BUF_SIZE); 120 | file >> c; 121 | } 122 | file.putback(c); 123 | 124 | file.width(BUF_SIZE); 125 | file >> buf; 126 | file.ignore(); 127 | } 128 | 129 | image *loadPGM(const char *name) { 130 | char buf[BUF_SIZE]; 131 | 132 | // read header 133 | std::ifstream file(name, std::ios::in | std::ios::binary); 134 | pnm_read(file, buf); 135 | if (strncmp(buf, "P5", 2)) { 136 | std::cout << "ERROR: Could not read file " << name << std::endl; 137 | throw pnm_error(); 138 | } 139 | 140 | pnm_read(file, buf); 141 | int width = atoi(buf); 142 | pnm_read(file, buf); 143 | int height = atoi(buf); 144 | 145 | pnm_read(file, buf); 146 | if (atoi(buf) > UCHAR_MAX) { 147 | std::cout << "ERROR: Could not read file " << name << std::endl; 148 | throw pnm_error(); 149 | } 150 | 151 | // read data 152 | image *im = new image(width, height); 153 | file.read((char *)imPtr(im, 0, 0), width * height * sizeof(uchar)); 154 | 155 | return im; 156 | } 157 | 158 | void savePGM(image *im, const char *name) { 159 | int width = im->width(); 160 | int height = im->height(); 161 | std::ofstream file(name, std::ios::out | std::ios::binary); 162 | 163 | file << "P5\n" << width << " " << height << "\n" << UCHAR_MAX << "\n"; 164 | file.write((char *)imPtr(im, 0, 0), width * height * sizeof(uchar)); 165 | } 166 | 167 | #endif 168 | -------------------------------------------------------------------------------- /elas/matrix.h: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2011. All rights reserved. 3 | Institute of Measurement and Control Systems 4 | Karlsruhe Institute of Technology, Germany 5 | 6 | This file is part of libviso2. 7 | Authors: Andreas Geiger 8 | 9 | libviso2 is free software; you can redistribute it and/or modify it under the 10 | terms of the GNU General Public License as published by the Free Software 11 | Foundation; either version 2 of the License, or any later version. 12 | 13 | libviso2 is distributed in the hope that it will be useful, but WITHOUT ANY 14 | WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A 15 | PARTICULAR PURPOSE. See the GNU General Public License for more details. 16 | 17 | You should have received a copy of the GNU General Public License along with 18 | libviso2; if not, write to the Free Software Foundation, Inc., 51 Franklin 19 | Street, Fifth Floor, Boston, MA 02110-1301, USA 20 | */ 21 | 22 | #ifndef MATRIX_H 23 | #define MATRIX_H 24 | 25 | #include 26 | #include 27 | #include 28 | #include 29 | #include 30 | 31 | #ifndef _MSC_VER 32 | #include 33 | #else 34 | typedef __int8 int8_t; 35 | typedef __int16 int16_t; 36 | typedef __int32 int32_t; 37 | typedef __int64 int64_t; 38 | typedef unsigned __int8 uint8_t; 39 | typedef unsigned __int16 uint16_t; 40 | typedef unsigned __int32 uint32_t; 41 | typedef unsigned __int64 uint64_t; 42 | #endif 43 | 44 | #define endll endl << endl // double end line definition 45 | 46 | typedef double FLOAT; // double precision 47 | //typedef float FLOAT; // single precision 48 | 49 | class Matrix { 50 | 51 | public: 52 | 53 | // constructor / deconstructor 54 | Matrix (); // init empty 0x0 matrix 55 | Matrix (const int32_t m,const int32_t n); // init empty mxn matrix 56 | Matrix (const int32_t m,const int32_t n,const FLOAT* val_); // init mxn matrix with values from array 'val' 57 | Matrix (const Matrix &M); // creates deepcopy of M 58 | ~Matrix (); 59 | 60 | // assignment operator, copies contents of M 61 | Matrix& operator= (const Matrix &M); 62 | 63 | // copies submatrix of M into array 'val', default values copy whole row/column/matrix 64 | void getData(FLOAT* val_,int32_t i1=0,int32_t j1=0,int32_t i2=-1,int32_t j2=-1); 65 | 66 | // set or get submatrices of current matrix 67 | Matrix getMat(int32_t i1,int32_t j1,int32_t i2=-1,int32_t j2=-1); 68 | void setMat(const Matrix &M,const int32_t i,const int32_t j); 69 | 70 | // set sub-matrix to scalar (default 0), -1 as end replaces whole row/column/matrix 71 | void setVal(FLOAT s,int32_t i1=0,int32_t j1=0,int32_t i2=-1,int32_t j2=-1); 72 | 73 | // set (part of) diagonal to scalar, -1 as end replaces whole diagonal 74 | void setDiag(FLOAT s,int32_t i1=0,int32_t i2=-1); 75 | 76 | // clear matrix 77 | void zero(); 78 | 79 | // extract columns with given index 80 | Matrix extractCols (std::vector idx); 81 | 82 | // create identity matrix 83 | static Matrix eye (const int32_t m); 84 | void eye (); 85 | 86 | // create diagonal matrix with nx1 or 1xn matrix M as elements 87 | static Matrix diag(const Matrix &M); 88 | 89 | // returns the m-by-n matrix whose elements are taken column-wise from M 90 | static Matrix reshape(const Matrix &M,int32_t m,int32_t n); 91 | 92 | // create 3x3 rotation matrices (convention: http://en.wikipedia.org/wiki/Rotation_matrix) 93 | static Matrix rotMatX(const FLOAT &angle); 94 | static Matrix rotMatY(const FLOAT &angle); 95 | static Matrix rotMatZ(const FLOAT &angle); 96 | 97 | // simple arithmetic operations 98 | Matrix operator+ (const Matrix &M); // add matrix 99 | Matrix operator- (const Matrix &M); // subtract matrix 100 | Matrix operator* (const Matrix &M); // multiply with matrix 101 | Matrix operator* (const FLOAT &s); // multiply with scalar 102 | Matrix operator/ (const Matrix &M); // divide elementwise by matrix (or vector) 103 | Matrix operator/ (const FLOAT &s); // divide by scalar 104 | Matrix operator- (); // negative matrix 105 | Matrix operator~ (); // transpose 106 | FLOAT l2norm (); // euclidean norm (vectors) / frobenius norm (matrices) 107 | FLOAT mean (); // mean of all elements in matrix 108 | 109 | // complex arithmetic operations 110 | static Matrix cross (const Matrix &a, const Matrix &b); // cross product of two vectors 111 | static Matrix inv (const Matrix &M); // invert matrix M 112 | bool inv (); // invert this matrix 113 | FLOAT det (); // returns determinant of matrix 114 | bool solve (const Matrix &M,FLOAT eps=1e-20); // solve linear system M*x=B, replaces *this and M 115 | bool lu(int32_t *idx, FLOAT &d, FLOAT eps=1e-20); // replace *this by lower upper decomposition 116 | void svd(Matrix &U,Matrix &W,Matrix &V); // singular value decomposition *this = U*diag(W)*V^T 117 | 118 | // print matrix to stream 119 | friend std::ostream& operator<< (std::ostream& out,const Matrix& M); 120 | 121 | // direct data access 122 | FLOAT **val; 123 | int32_t m,n; 124 | 125 | private: 126 | 127 | void allocateMemory (const int32_t m_,const int32_t n_); 128 | void releaseMemory (); 129 | inline FLOAT pythag(FLOAT a,FLOAT b); 130 | 131 | }; 132 | 133 | #endif // MATRIX_H 134 | -------------------------------------------------------------------------------- /elas/timer.h: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2011. All rights reserved. 3 | Institute of Measurement and Control Systems 4 | Karlsruhe Institute of Technology, Germany 5 | 6 | This file is part of libelas. 7 | Authors: Andreas Geiger 8 | 9 | libelas is free software; you can redistribute it and/or modify it under the 10 | terms of the GNU General Public License as published by the Free Software 11 | Foundation; either version 3 of the License, or any later version. 12 | 13 | libelas is distributed in the hope that it will be useful, but WITHOUT ANY 14 | WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A 15 | PARTICULAR PURPOSE. See the GNU General Public License for more details. 16 | 17 | You should have received a copy of the GNU General Public License along with 18 | libelas; if not, write to the Free Software Foundation, Inc., 51 Franklin 19 | Street, Fifth Floor, Boston, MA 02110-1301, USA 20 | */ 21 | 22 | #ifndef __TIMER_H__ 23 | #define __TIMER_H__ 24 | 25 | #include 26 | #include 27 | #include 28 | #include 29 | #include 30 | #include 31 | #include 32 | #include 33 | 34 | // Define fixed-width datatypes for Visual Studio projects 35 | #ifndef _MSC_VER 36 | #include 37 | #else 38 | typedef __int8 int8_t; 39 | typedef __int16 int16_t; 40 | typedef __int32 int32_t; 41 | typedef __int64 int64_t; 42 | typedef unsigned __int8 uint8_t; 43 | typedef unsigned __int16 uint16_t; 44 | typedef unsigned __int32 uint32_t; 45 | typedef unsigned __int64 uint64_t; 46 | #endif 47 | 48 | class Timer { 49 | 50 | public: 51 | 52 | Timer() {} 53 | 54 | ~Timer() {} 55 | 56 | void start (std::string title) { 57 | desc.push_back(title); 58 | push_back_time(); 59 | } 60 | 61 | void stop () { 62 | if (time.size()<=desc.size()) 63 | push_back_time(); 64 | } 65 | 66 | void plot () { 67 | stop(); 68 | float total_time = 0; 69 | for (int32_t i=0; i desc; 93 | std::vector time; 94 | 95 | void push_back_time () { 96 | timeval curr_time; 97 | gettimeofday(&curr_time,0); 98 | time.push_back(curr_time); 99 | } 100 | 101 | float getTimeDifferenceMilliseconds(timeval a,timeval b) { 102 | return ((float)(b.tv_sec -a.tv_sec ))*1e+3 + 103 | ((float)(b.tv_usec-a.tv_usec))*1e-3; 104 | } 105 | }; 106 | 107 | #endif 108 | -------------------------------------------------------------------------------- /epipolar.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | using namespace cv; 7 | using namespace std; 8 | 9 | Mat img_left, img_right, img_left_disp, img_right_disp; 10 | Mat img_left_desc, img_right_desc; 11 | vector< KeyPoint > kpl, kpr; 12 | 13 | bool isLeftKeyPoint(int i, int j) { 14 | int n = kpl.size(); 15 | return (i >= kpl[0].pt.x && i <= kpl[n-1].pt.x 16 | && j >= kpl[0].pt.y && j <= kpl[n-1].pt.y); 17 | } 18 | 19 | bool isRightKeyPoint(int i, int j) { 20 | int n = kpr.size(); 21 | return (i >= kpr[0].pt.x && i <= kpr[n-1].pt.x 22 | && j >= kpr[0].pt.y && j <= kpr[n-1].pt.y); 23 | } 24 | 25 | long costF(const Mat& left, const Mat& right) { 26 | long cost = 0; 27 | for (int i = 0; i < 32; i++) { 28 | cost += abs(left.at(0,i)-right.at(0,i)); 29 | } 30 | return cost; 31 | } 32 | 33 | int getCorresPoint(Point p, Mat& img, int ndisp) { 34 | ofstream mfile; 35 | mfile.open("cost.txt"); 36 | int w = 5; 37 | long minCost = 1e9; 38 | int chosen_i = 0; 39 | int x0r = kpr[0].pt.x; 40 | int y0r = kpr[0].pt.y; 41 | int ynr = kpr[kpr.size()-1].pt.y; 42 | int x0l = kpl[0].pt.x; 43 | int y0l = kpl[0].pt.y; 44 | int ynl = kpl[kpl.size()-1].pt.y; 45 | for (int i = p.x-ndisp; i <= p.x; i++) { 46 | long cost = -1; 47 | for (int j = -w; j <= w; j++) { 48 | for (int k = -w; k <= w; k++) { 49 | if (!isLeftKeyPoint(p.x+j, p.y+k) || !isRightKeyPoint(i+j, p.y+k)) 50 | continue; 51 | int idxl = (p.x+j-x0l)*(ynl-y0l+1)+(p.y+k-y0l); 52 | int idxr = (i+j-x0r)*(ynr-y0r+1)+(p.y+k-y0r); 53 | cost += costF(img_left_desc.row(idxl), img_right_desc.row(idxr)); 54 | } 55 | } 56 | cost = cost / ((2*w+1)*(2*w+1)); 57 | mfile << (p.x-i) << " " << cost << endl; 58 | if (cost < minCost) { 59 | minCost = cost; 60 | chosen_i = i; 61 | } 62 | } 63 | cout << "minCost: " << minCost << endl; 64 | return chosen_i; 65 | } 66 | 67 | void mouseClickLeft(int event, int x, int y, int flags, void* userdata) { 68 | if (event == EVENT_LBUTTONDOWN) { 69 | if (!isLeftKeyPoint(x,y)) 70 | return; 71 | int right_i = getCorresPoint(Point(x,y), img_right, 20); 72 | Scalar color = Scalar(255,255,0); 73 | circle(img_left_disp, Point(x,y), 4, color, -1, 8, 0); 74 | circle(img_right_disp, Point(right_i,y), 4, color, -1, 8, 0); 75 | line(img_right_disp, Point(0,y), Point(img_right.cols,y), 76 | color, 1, 8, 0); 77 | cout << "Left: " << x << " Right: " << right_i << endl; 78 | } 79 | } 80 | 81 | void cacheDescriptorVals() { 82 | OrbDescriptorExtractor extractor; 83 | for (int i = 0; i < img_left.cols; i++) { 84 | for (int j = 0; j < img_left.rows; j++) { 85 | kpl.push_back(KeyPoint(i,j,1)); 86 | kpr.push_back(KeyPoint(i,j,1)); 87 | } 88 | } 89 | extractor.compute(img_left, kpl, img_left_desc); 90 | extractor.compute(img_right, kpr, img_right_desc); 91 | } 92 | 93 | int main(int argc, char const *argv[]) 94 | { 95 | img_left = imread(argv[1]); 96 | img_right = imread(argv[2]); 97 | img_left_disp = imread(argv[1]); 98 | img_right_disp = imread(argv[2]); 99 | cacheDescriptorVals(); 100 | namedWindow("IMG-LEFT", 1); 101 | namedWindow("IMG-RIGHT", 1); 102 | setMouseCallback("IMG-LEFT", mouseClickLeft, NULL); 103 | while (1) { 104 | imshow("IMG-LEFT", img_left_disp); 105 | imshow("IMG-RIGHT", img_right_disp); 106 | if (waitKey(30) > 0) { 107 | break; 108 | } 109 | } 110 | return 0; 111 | } -------------------------------------------------------------------------------- /popt_pp.h: -------------------------------------------------------------------------------- 1 | #ifndef _INCLUDED_POPT_PP_H_ 2 | #define _INCLUDED_POPT_PP_H_ 3 | 4 | #include 5 | 6 | class POpt{ 7 | protected: 8 | poptContext con; 9 | public: 10 | // creation and deletion 11 | POpt(const char *name, int argc, const char **argv, 12 | const poptOption *options, int flags) 13 | {con = poptGetContext(name,argc,argv,options,flags);} 14 | POpt(const char *name, int argc, char **argv, 15 | const poptOption *options, int flags) 16 | {con = poptGetContext(name,argc,(const char **)argv,options,flags);} 17 | ~POpt() 18 | {poptFreeContext(con);} 19 | 20 | // functions for processing options 21 | int getNextOpt() 22 | {return(poptGetNextOpt(con));} 23 | void ignoreOptions() 24 | {while(getNextOpt() >= 0);} 25 | const char *getOptArg() 26 | {return(poptGetOptArg(con));} 27 | const char *strError(int error) 28 | {return(poptStrerror(error));} 29 | const char *badOption(int flags = POPT_BADOPTION_NOALIAS) 30 | {return(poptBadOption(con,flags));} 31 | 32 | // processing other arguments 33 | const char *getArg() 34 | {return(poptGetArg(con));} 35 | void ignoreArgs() 36 | {while(getArg());} 37 | }; 38 | 39 | #endif 40 | -------------------------------------------------------------------------------- /reconstruct.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | using namespace cv; 7 | using namespace std; 8 | 9 | Mat Q, XR, XT; 10 | 11 | void publishPointCloud(Mat& left, Mat& dmap, ofstream& obj_file) { 12 | Mat V = Mat(4, 1, CV_64FC1); 13 | Mat pos = Mat(4, 1, CV_64FC1); 14 | int ndisp = 40; 15 | for (int i = 0; i < dmap.cols; i++) { 16 | for (int j = 0; j < dmap.rows; j++) { 17 | int d = dmap.at(j,i) * ((float)ndisp / 255.); 18 | // if low disparity, then ignore 19 | if (d < 2) 20 | continue; 21 | // V is the vector to be multiplied to Q to get 22 | // the 3D homogenous coordinates of the image point 23 | V.at(0,0) = (double)(i); 24 | V.at(1,0) = (double)(j); 25 | V.at(2,0) = (double)d; 26 | V.at(3,0) = 1.; 27 | pos = Q * V; // 3D homogeneous coordinate 28 | double X = pos.at(0,0) / pos.at(3,0); 29 | double Y = pos.at(1,0) / pos.at(3,0); 30 | double Z = pos.at(2,0) / pos.at(3,0); 31 | Mat point3d_cam = Mat(3, 1, CV_64FC1); 32 | point3d_cam.at(0,0) = X; 33 | point3d_cam.at(1,0) = Y; 34 | point3d_cam.at(2,0) = Z; 35 | // transform 3D point from camera frame to robot frame 36 | Mat point3d_robot = XR * point3d_cam + XT; 37 | int r = (int)left.at(j,i)[2]; 38 | int g = (int)left.at(j,i)[1]; 39 | int b = (int)left.at(j,i)[0]; 40 | obj_file << "v " << point3d_robot.at(0,0) << " " << 41 | point3d_robot.at(1,0) << " " << point3d_robot.at(2,0); 42 | obj_file << " " << r << " " << g << " " << b << endl; 43 | } 44 | } 45 | } 46 | 47 | int main(int argc, char const *argv[]) 48 | { 49 | ofstream obj_file; 50 | Mat img_left = imread(argv[1], 1); 51 | Mat img_disp = imread(argv[2], 0); 52 | cv::FileStorage fs1(argv[3], cv::FileStorage::READ); 53 | fs1["Q"] >> Q; // depth to disparity mapping matrix 54 | fs1["XR"] >> XR; // rotation from camera frame to robot frame 55 | fs1["XT"] >> XT; // translation from camera frame to robot frame 56 | obj_file.open(argv[4]); 57 | publishPointCloud(img_left, img_disp, obj_file); 58 | return 0; 59 | } --------------------------------------------------------------------------------