00001 #define _CRT_SECURE_NO_DEPRECATE
00002 #ifndef PANOUTILS_H
00003 #define PANOUTILS_H
00004
00005 #include <iostream>
00006 #include <fstream>
00007
00008 #include <vector>
00009 #include <string>
00010
00011 #include <exception>
00012 #include <map>
00013 #include <string>
00014 #include <exception>
00015 #include <typeinfo>
00016 #include <iomanip>
00017
00018 #include <opencv2/core/core.hpp>
00019 #include <opencv2/calib3d/calib3d.hpp>
00020
00021
00022 namespace pano {
00023
00024
00025
00026
00029 inline cv::Mat getRv( const cv::Mat& R ) {
00030 cv::Mat v = cv::Mat::zeros(3, 1, cv::DataType<float>::type);
00031 v.at<float> (2, 0) = 1;
00032 cv::Mat Rv = R * v;
00033 return Rv;
00034 }
00035
00038 inline bool compareRealToIndexPair(const std::pair<double,int>& lhs, const std::pair<double,int>& rhs){
00039 return lhs.first < rhs.first;
00040 }
00041
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00072 template <typename CSVSavable>
00073 void saveMatCsv( const CSVSavable& mat, const std::string& name, bool append) {
00074 std::ofstream fs(name.c_str(), append ? std::fstream::app
00075 : std::fstream::out);
00076 writeCSV(fs, mat);
00077 fs.close();
00078 }
00079
00080 typedef void (*MDoperator)(cv::InputArray, cv::InputArray, cv::OutputArray, double, int);
00081
00082 void mdImageByDoubleWeights(const cv::Mat& img, const cv::Mat& weights,
00083 cv::Mat& output, MDoperator md,std::vector<cv::Mat>* cache_channels);
00084
00085 void mdImageByFloatWeights(const cv::Mat& img, const cv::Mat& weights,
00086 cv::Mat& output, MDoperator md,std::vector<cv::Mat>* cache_channels);
00087
00088 inline void multiplyImageByFloatWeights(const cv::Mat& img, const cv::Mat& weights, cv::Mat& output,std::vector<cv::Mat>* cache_channels=0){
00089 mdImageByFloatWeights(img,weights,output,cv::multiply,cache_channels);
00090 }
00091
00092 inline void divideImageByFloatWeights(const cv::Mat& img, const cv::Mat& weights,cv::Mat& output,std::vector<cv::Mat>* cache_channels = 0){
00093 mdImageByFloatWeights(img,weights,output,cv::divide,cache_channels);
00094 }
00095
00096 inline void multiplyImageByDoubleWeights(const cv::Mat& img, const cv::Mat& weights, cv::Mat& output,std::vector<cv::Mat>* cache_channels=0){
00097 mdImageByDoubleWeights(img,weights,output,cv::multiply,cache_channels);
00098 }
00099
00100 inline void divideImageByDoubleWeights(const cv::Mat& img, const cv::Mat& weights,cv::Mat& output,std::vector<cv::Mat>* cache_channels = 0){
00101 mdImageByDoubleWeights(img,weights,output,cv::divide,cache_channels);
00102 }
00103
00104
00105 inline void rescaleFloatImage256( cv::Mat& img ) {
00106
00107 cv::Mat flimage;
00108 img.convertTo(flimage, CV_32FC3);
00109 std::vector<cv::Mat> channels;
00110 split(flimage, channels);
00111 for (size_t k = 0; k < channels.size(); k++) {
00112 cv::Mat cc = channels[k];
00113 double fmin,fmax;
00114 cv::minMaxLoc( cc, &fmin, &fmax );
00115 if( fmax > 1.0 )
00116 fmax = 255.0 ;
00117 else
00118 fmax = 1.0;
00119 channels[k] = 255.0 * ( channels[k] / (fmax + 1e-9) );
00120 }
00121
00122 merge(channels, flimage);
00123 flimage.convertTo( img, img.type() );
00124 }
00125
00126 inline void rescaleFloatImage1_shiftMin_divideMax( cv::Mat& img ) {
00127
00128 cv::Mat flimage;
00129 img.convertTo(flimage, CV_32FC3);
00130 std::vector<cv::Mat> channels;
00131 split(flimage, channels);
00132 for (size_t k = 0; k < channels.size(); k++) {
00133 cv::Mat cc = channels[k];
00134 double fmin,fmax;
00135 cv::minMaxLoc( cc, &fmin, &fmax );
00136 channels[k] = channels[k] - fmin;
00137 channels[k] = ( channels[k] / (fmax + 1e-9) );
00138 }
00139 merge(channels, flimage);
00140 flimage.convertTo( img, img.type() );
00141 }
00142
00143 inline void rescaleFloatImage1( cv::Mat& img ) {
00144
00145 cv::Mat flimage;
00146 img.convertTo(flimage, CV_32FC3);
00147 std::vector<cv::Mat> channels;
00148 split(flimage, channels);
00149 for (size_t k = 0; k < channels.size(); k++) {
00150 cv::Mat cc = channels[k];
00151 double fmin,fmax;
00152 cv::minMaxLoc( cc, &fmin, &fmax );
00153 if( fmax > 1.0 )
00154 fmax = 255.0 ;
00155 else
00156 fmax = 1.0;
00157 channels[k] = ( channels[k] / (fmax + 1e-9) );
00158 }
00159 merge(channels, flimage);
00160 flimage.convertTo( img, img.type() );
00161 }
00162
00163
00164 inline void rescaleFloatImage1_clip( cv::Mat& img ) {
00165
00166 cv::Mat flimage;
00167 img.convertTo(flimage, CV_32FC3);
00168 std::vector<cv::Mat> channels;
00169 split(flimage, channels);
00170 for (size_t k = 0; k < channels.size(); k++) {
00171 cv::Mat cc = channels[k];
00172 double fmin,fmax;
00173 cv::minMaxLoc( cc, &fmin, &fmax );
00174 if( fmax > 1.0 )
00175 fmax = 255.0 ;
00176 else
00177 fmax = 1.0;
00178 cc = ( cc / (fmax + 1e-9) );
00179 for( int i = 0; i < cc.rows; i++ ) {
00180 for( int j = 0; j < cc.cols; j++ ) {
00181 if( cc.at<float>(i,j) > 1.0 )
00182 cc.at<float>(i,j) = 1.0;
00183 else if( cc.at<float>(i,j) < 0.0 )
00184 cc.at<float>(i,j) = 0.0;
00185 else
00186 cc.at<float>(i,j) = 1.0 * cc.at<float>(i,j);
00187 }
00188 }
00189
00190 }
00191 merge(channels, flimage);
00192 flimage.convertTo( img, img.type() );
00193 }
00194
00195 inline void rescaleFloatImage256_clip( cv::Mat& img ) {
00196
00197 cv::Mat flimage;
00198 img.convertTo(flimage, CV_32FC3);
00199 std::vector<cv::Mat> channels;
00200 split(flimage, channels);
00201 for (size_t k = 0; k < channels.size(); k++) {
00202 cv::Mat cc = channels[k];
00203 double fmin,fmax;
00204 cv::minMaxLoc( cc, &fmin, &fmax );
00205 if( fmax > 1.0 )
00206 fmax = 255.0 ;
00207 else
00208 fmax = 1.0;
00209 cc = ( cc / (fmax + 1e-9) );
00210 for( int i = 0; i < cc.rows; i++ ) {
00211 for( int j = 0; j < cc.cols; j++ ) {
00212 if( cc.at<float>(i,j) > 1.0 )
00213 cc.at<float>(i,j) = 255.0;
00214 else if( cc.at<float>(i,j) < 0.0 )
00215 cc.at<float>(i,j) = 0.0;
00216 else
00217 cc.at<float>(i,j) = 255.0 * cc.at<float>(i,j);
00218 }
00219 }
00220
00221 }
00222 merge(channels, flimage);
00223 flimage.convertTo( img, img.type() );
00224 }
00225
00226 }
00227 #endif // PANOUTILS_H