1 #ifndef MTF_HIST_UTILS_H     2 #define MTF_HIST_UTILS_H     6 #include "mtf/Macros/common.h"     7 #include "mtf/Utilities/excpUtils.h"    10 #define _1_BY_3 0.33333333333    11 #define _2_BY_3 0.66666666666    12 #define _1_BY_6 0.16666666666    13 #define _eig_set_zero(eig_mat, scalar_type) eig_mat.setZero()    22     inline void getLog(MatrixXd &eig_log,
    23         const MatrixXd &eig_mat, 
int n_rows, 
int n_cols){
    24             for(
int i = 0; i < n_rows; i++) {
    25                 for(
int j = 0; j < n_cols; j++) {
    29                         eig_log(i, j) = log(eig_mat(i, j));
    35     inline void getLog(VectorXd &eig_log,
    36         const VectorXd &eig_vec, 
int vec_size){
    37             for(
int i = 0; i < vec_size; i++) {
    41                     eig_log(i) = log(eig_vec(i));
    46     inline double cumBSpl3(
double x){
    50         } 
else if((x>-2) && (x <= -1)){
    52             double temp2 = temp*temp;
    53             return 1.0 - (temp2*temp2) / 24;
    54         } 
else if((x>-1) && (x <= 0)){
    55             return 0.5 + x*(x*x*(1.0/3.0 + x / 8) - 2.0 / 3.0);
    56         } 
else if((x>0) && (x <= 1)){
    57             return 0.5 + x*(x*x*(1.0 / 3.0 - x / 8) - 2.0 / 3.0);
    58         } 
else if((x>1) && (x<2)){
    60             double temp2 = temp*temp;
    61             return (temp2*temp2) / 24;
    67     inline void cumBSpl3WithGradFast(
double &val, 
double &diff, 
double x){
    68         printf(
"bspl_id: %d\n", bspl_id);
    69         throw utils::InvalidArgument(
"cumBSpl3WithGradFast :: Invalid bspl_id specified");
    72     inline void cumBSpl3WithGradFast<0>(
double &val, 
double &diff, 
double x){
    74         diff = -(temp*temp*temp) / 6;
    75         val = 1 + (diff * temp) / 4;
    78     inline void cumBSpl3WithGradFast<1>(
double &val, 
double &diff, 
double x){
    80         val = 0.5 + x*(x2*(_1_BY_3 + x / 8) - _2_BY_3);
    81         diff = x2*(1 + x / 2) - _2_BY_3;
    84     inline void cumBSpl3WithGradFast<2>(
double &val, 
double &diff, 
double x){
    86         val = 0.5 + x*(x2*(_1_BY_3 - x / 8) - _2_BY_3);
    87         diff = x2*(1 - x / 2) - _2_BY_3;
    90     inline void cumBSpl3WithGradFast<3>(
double &val, 
double &diff, 
double x){
    92         diff = (temp*temp*temp) / 6;
    93         val = (diff * temp) / 4;
    96     inline void cumBSpl3WithGrad(
double &val, 
double &diff, 
double x){
   100         } 
else if((x>-2) && (x <= -1)){
   102             diff = -(temp*temp*temp) / 6;
   103             val = 1 + (diff * temp) / 4;
   104         } 
else if((x>-1) && (x <= 0)){
   106             val = 0.5 + x*(x2*(_1_BY_3 + x / 8) - _2_BY_3);
   107             diff = x2*(1 + x / 2) - _2_BY_3;
   108         } 
else if((x>0) && (x <= 1)){
   110             val = 0.5 + x*(x2*(_1_BY_3 - x / 8) - _2_BY_3);
   111             diff = x2*(1 - x/2) - _2_BY_3;
   112         } 
else if((x>1) && (x<2)){
   114             diff = (temp*temp*temp) / 6;
   115             val = (diff * temp) / 4;
   121     template<
int bspl_
id>
   122     inline double cumBSpl3HessFast(
double x){
   123         printf(
"bspl_id: %d\n", bspl_id);
   124         throw utils::InvalidArgument(
"cumBSpl3HessFast :: Invalid bspl_id specified");
   127     inline double cumBSpl3HessFast<0>(
double x){
   129         return -(temp*temp) / 2;
   132     inline double cumBSpl3HessFast<1>(
double x){
   133         return x*(3 * x + 4) / 2;
   136     inline double cumBSpl3HessFast<2>(
double x){
   137         return -x*(3 * x - 4) / 2;
   140     inline double cumBSpl3HessFast<3>(
double x){
   142         return (temp * temp) / 2;
   145     inline double cumBSpl3Hess(
double x){
   147         if((x>-2) && (x <= -1)){
   149             return -(temp*temp) / 2;
   150         } 
else if((x>-1) && (x <= 0)){
   151             return x*(3 * x + 4) / 2;
   152         } 
else if((x>0) && (x <= 1)){
   153             return -x*(3 * x - 4) / 2;
   154         } 
else if((x>1) && (x<2)){
   156             return (temp * temp) / 2;
   161     inline double bSpl3(
double x){
   163         if((x>-2) && (x<=-1)){
   165             return (temp*temp*temp) / 6;
   166         } 
else if((x>-1) && (x<=0)){
   167             return (4 - 3*x*x*(2+x)) / 6;
   168         } 
else if((x>0) && (x<=1)){
   169             return (4 - 3*x*x*(2-x)) / 6;
   170         } 
else if((x>1) && (x<2)){
   172             return (temp*temp*temp) / 6;
   176     template<
int bspl_
id>
   177     void bSpl3WithGradFast(
double &val, 
double &diff, 
double x){    
   178         printf(
"bspl_id: %d\n", bspl_id);
   179         throw utils::InvalidArgument(
"bSpl3WithGradFast :: Invalid bspl_id specified");
   182     inline void bSpl3WithGradFast<0>(
double &val, 
double &diff, 
double x){
   184         diff = (temp * temp) / 2;
   185         val = (diff * temp) / 3;
   188     inline void bSpl3WithGradFast<1>(
double &val, 
double &diff, 
double x){
   190         val = _2_BY_3 - x*x*(1 + temp);
   191         diff = -x * (temp + x + 2);
   194     inline void bSpl3WithGradFast<2>(
double &val, 
double &diff, 
double x){
   196         val = _2_BY_3 - x*x*(1 - temp);
   197         diff = x * (temp + x - 2);
   200     inline void bSpl3WithGradFast<3>(
double &val, 
double &diff, 
double x){
   202         diff = -(temp * temp) / 2;
   203         val = -(diff * temp) / 3;
   206     inline void bSpl3WithGrad(
double &val, 
double &diff, 
double x){
   207         if((x>-2) && (x<=-1)){
   209             diff = (temp * temp) / 2;
   210             val = (diff * temp) / 3;
   211         } 
else if((x>-1) && (x<=0)){
   213             val =_2_BY_3 - x*x*(1 + temp);
   214             diff = -x * (temp + x + 2);
   215         } 
else if((x>0) && (x<=1)){
   217             val =_2_BY_3 - x*x*(1 - temp);
   218             diff = x * (temp + x -2);
   219         } 
else if((x>1) && (x<2)){
   221             diff = - (temp * temp) / 2;
   222             val = - (diff * temp) / 3 ;
   230         if((x>-2) && (x<=-1)){
   232             return (temp*temp) / 2;
   233         } 
else if((x>-1) && (x<=0)){
   234             return -x*(3*x + 4) / 2;
   235         } 
else if((x>0) && (x<=1)){
   236             return x*(3*x - 4) / 2;
   237         } 
else if((x>1) && (x<2)){
   239             return -(temp * temp) / 2;
   244     template<
int bspl_
id>
   245     inline double bSpl3HessFast(
double x){
   246         printf(
"bspl_id: %d\n", bspl_id);
   247         throw utils::InvalidArgument(
"bSpl3HessFast :: Invalid bspl_id specified");
   250     inline double bSpl3HessFast<0>(
double x){
   254     inline double bSpl3HessFast<1>(
double x){
   258     inline double bSpl3HessFast<2>(
double x){
   262     inline double bSpl3HessFast<3>(
double x){
   271         if((x>-2) && (x <= -1)){
   273         } 
else if((x > -1) && (x <= 0)){
   275         } 
else if((x > 0) && (x <= 1)){
   277         } 
else if((x > 1) && (x < 2)){
   284     typedef void(*BSpl3WithGradPtr)(
double &, 
double &, double);
   285     inline void bSpl3WithGrad0(
double &val, 
double &diff, 
double x){
   287         diff = (temp * temp) / 2;
   288         val = (diff * temp) / 3;
   290     inline void bSpl3WithGrad1(
double &val, 
double &diff, 
double x){
   292         diff = (temp * temp) / 2;
   293         val = (diff * temp) / 3;
   295     inline void bSpl3WithGrad2(
double &val, 
double &diff, 
double x){
   297         val = _2_BY_3 - x*x*(1 + temp);
   298         diff = -x * (temp + x + 2);
   300     inline void bSpl3WithGrad3(
double &val, 
double &diff, 
double x){
   302         diff = -(temp * temp) / 2;
   303         val = -(diff * temp) / 3;
   310     void getDiracHist(VectorXd &hist, VectorXi &pix_vals_int,
   311         const VectorXd &pix_vals, 
double hist_pre_seed, 
int n_pix);
   315     void getDiracHist(VectorXi &hist, 
const VectorXi &vals, 
int n_vals);
   321         const VectorXd &pix_vals1, 
const VectorXi &pix_vals2_int,
   322         double hist_pre_seed, 
double joint_hist_pre_seed, 
int n_pix, 
int n_bins);
   324         const VectorXd &pix_vals1, 
const VectorXd &pix_vals2,
   325         double hist_pre_seed, 
double joint_hist_pre_seed, 
int n_pix, 
int n_bins);
   328         const MatrixXdMr &patch1, 
const MatrixXdMr &patch2,
   329         int start_x, 
int end_x, 
int start_y, 
int end_y,
   330         double hist_pre_seed, 
double joint_hist_pre_seed, 
int n_pix, 
int n_bins);
   337         VectorXd &hist, VectorXi &pix_vals_int,
   339         const VectorXd &pix_vals, 
double hist_pre_seed, 
int n_pix
   347         MatrixXd &joint_hist, VectorXd &hist1, VectorXd &hist2,
   349         const VectorXd &pix_vals1, 
const VectorXd &pix_vals2,
   350         double hist_pre_seed, 
double joint_hist_pre_seed, 
int n_pix, 
int n_bins
   361     void getBSplHist(VectorXd &hist, MatrixXd &hist_mat, MatrixX2i &bspl_ids, 
   362         const VectorXd &pix_vals, 
const MatrixX2i &std_bspl_ids, 
   363         double pre_seed, 
int n_pix);
   365     void getBSplJointHist(MatrixXd &joint_hist, VectorXd &hist1, VectorXd &hist2,
   366         const VectorXd &pix_vals1, 
const VectorXd &pix_vals2,
   367         double hist_pre_seed, 
double joint_hist_pre_seed, 
int n_pix, 
int n_bins);
   368     void getBSplJointHist(MatrixXd &joint_hist, VectorXd &hist1, VectorXd &hist2,
   369         MatrixXd &hist1_mat, MatrixXd &hist2_mat,
   370         MatrixX2i &bspl_ids1, MatrixX2i &bspl_ids2,
   371         const VectorXd &pix_vals1, 
const VectorXd &pix_vals2,
   372         const MatrixX2i &std_bspl_ids, 
double hist_pre_seed,
   373         double joint_hist_pre_seed, 
int n_pix);
   380     void getBSplJointHist(MatrixXd &joint_hist, VectorXd &hist1, 
   381         MatrixXd &hist1_mat, MatrixX2i &bspl_ids1, 
   382         const VectorXd &pix_vals1, 
const MatrixX2i &bspl_ids2, 
   383         const MatrixXd &hist2_mat, 
const MatrixX2i &std_bspl_ids,
   384         double hist_pre_seed, 
double joint_hist_pre_seed, 
int n_pix);
   387     void  getBSplJointHist(MatrixXd &joint_hist, 
   388         const MatrixXd &hist_mat, 
const MatrixX2i &bspl_ids, 
   389         double pre_seed, 
int n_pix);
   411         const VectorXd &pix_vals, 
const MatrixX2i &bspl_ids, 
int n_pix
   413     void getBSplHistHess(
   416         const VectorXd &pix_vals, 
const MatrixX2i &bspline_ids,
   417         int n_pix, 
double hist_norm_mult
   444         const VectorXd &pix_vals1, 
const MatrixXd &hist2_mat, 
   445         const MatrixX2i &bspl_ids1, 
const MatrixX2i &bspl_ids2, 
   446         const MatrixXi &linear_idx, 
int n_pix);
   448         const MatrixXd &hist1_grad, 
const MatrixXd &hist2_mat, 
   449         const MatrixX2i &bspl_ids1, 
const MatrixX2i &bspl_ids2, 
   450         const MatrixXi &linear_idx, 
int n_pix);
   455         VectorXd &hist, MatrixXd &hist_mat, 
   456         MatrixXd &hist_grad, MatrixX2i &bspl_ids, 
   458         const VectorXd &pix_vals, 
const MatrixX2i &std_bspl_ids,
   459         double pre_seed, 
int n_pix, 
double hist_norm_mult
   465         MatrixXd &joint_hist, VectorXd &hist1, 
   466         MatrixXd &hist1_mat, MatrixXd &hist1_grad, 
   467         MatrixXd &joint_hist_grad, MatrixX2i &bspl_ids1, 
   469         const VectorXd &pix_vals1, 
const MatrixX2i &bspl_ids2, 
   470         const MatrixXd &hist2_mat, 
const MatrixX2i &std_bspl_ids, 
const MatrixXi &linear_idx, 
   471         double hist_pre_seed, 
double joint_hist_pre_seed, 
int n_pix, 
   472         double hist_norm_mult=1
   475     void getBSplJointHistWithGradFast(
   477         MatrixXd &joint_hist, VectorXd &hist1,
   478         MatrixXd &hist1_mat, MatrixXd &hist1_grad,
   479         MatrixXd &joint_hist_grad, MatrixX2i &bspl_ids1,
   481         const VectorXd &pix_vals1, 
const MatrixX2i &bspl_ids2,
   482         const MatrixXd &hist2_mat, 
const MatrixX2i &std_bspl_ids,
   483         const MatrixXi &linear_idx, 
double hist_pre_seed,
   484         double joint_hist_pre_seed, 
int n_pix, 
double hist_norm_mult
   491     void getEntropyVec(VectorXd &entropy_vec, 
const VectorXd &norm_hist, 
int n_bins);
   496         const VectorXd &norm_hist, 
const VectorXd &norm_hist_log, 
int n_bins
   502     void getMIVec(VectorXd &mi_vec, VectorXd &log_hist_ratio,
   503         const MatrixXd &joint_hist, 
const VectorXd &norm_hist1, 
const VectorXd &norm_hist2,
   504         const MatrixXi &linear_idx, 
int n_bins);
   508     void getMIVec(VectorXd &mi_vec, VectorXd &log_hist_ratio, 
   509         const MatrixXd &joint_hist, 
const MatrixXd &joint_hist_log, 
   510         const VectorXd &hist1_log, 
const VectorXd &hist2_log, 
const MatrixXi &linear_idx, 
int n_bins);
   534         const MatrixXd &joint_hist_grad, 
const VectorXd &log_hist_ratio, 
   535         const MatrixXd &hist1_grad_ratio, 
const MatrixXd &joint_hist, 
const MatrixX2i &bspl_ids, 
   536         const MatrixXi &linear_idx, 
int n_bins, 
int n_pix);
   546         VectorXd &cum_hist, MatrixXd &cum_hist_mat, 
   547         MatrixXd &cum_hist_grad, MatrixX2i &bspline_ids, 
   549         const VectorXd &pix_vals, 
const MatrixX2i &std_bspline_ids,
   550         double pre_seed, 
int n_bins, 
   551         int n_pix, 
double hist_norm_mult
   555     void getCumBSplJointHistWithGrad(
   557         MatrixXd &cum_joint_hist, MatrixXd &cum_joint_hist_grad,
   559         const MatrixXd &cum_hist_mat, 
const MatrixXd &hist_mat,
   560         const MatrixXd &cum_hist_grad, 
const MatrixX2i &cum_bspl_ids,
   561         const MatrixX2i &bspl_ids, 
const MatrixXi &linear_idx,
   562         double pre_seed, 
int n_bins, 
int n_pix
   565     void getCumBSplJointHistWithGrad(
   567         MatrixXd &cum_joint_hist, MatrixXd &cum_joint_hist_grad,
   568         VectorXd &hist, MatrixXd &hist_mat,
   570         const VectorXd &pix_vals, 
const MatrixXd &cum_hist_mat,
   571         const MatrixXd &cum_hist_grad, 
const MatrixX2i &cum_bspl_ids, 
   572         const MatrixX2i &bspline_ids, 
const MatrixXi &linear_idx, 
   573         double pre_seed, 
double joint_pre_seed, 
int n_bins, 
int n_pix
   575     void getCumBSplJointHistWithGrad(
   576         MatrixXd &cum_joint_hist, VectorXd &cum_hist,
   577         MatrixXd &cum_hist_mat, MatrixXd &cum_hist_grad,
   578         MatrixXd &cum_joint_hist_grad, MatrixX2i &bspline_ids1,
   580         const VectorXd &pix_vals1, 
const MatrixX2i &bspline_ids2,
   581         const MatrixXd &hist2_mat, 
const MatrixX2i &std_bspline_ids, 
const MatrixXi &linear_idx,
   582         double hist_pre_seed, 
double joint_hist_pre_seed,
   583         int n_bins, 
int n_pix, 
double hist_norm_mult
   587     void getInvCumBSplJointHistGrad(
   589         MatrixXd &cum_joint_hist_grad,
   591         const MatrixXd &hist_grad, 
const MatrixXd &cum_hist_mat,
   592         const MatrixX2i &cum_bspl_ids, 
const MatrixX2i &bspl_ids, 
   593         const MatrixXi &linear_idx, 
int n_bins, 
int n_pix
   595     void getCumBSplHistHess(
   597         MatrixXd &cum_hist_hess,
   599         const VectorXd &pix_vals,
   600         const MatrixX2i &bspline_ids,
   601         int n_pix, 
double hist_norm_mult
   606     void validateJointHist(
const MatrixXd &joint_hist, 
   607         const VectorXd &hist1, 
const VectorXd &hist2);
   609     void validateJointHistGrad(
const MatrixXd &joint_hist_grad, 
   610         const MatrixXd &hist1_grad, 
const MatrixXd &hist2_grad, 
   611         const MatrixXi &linear_idx, 
int n_bins, 
int n_pix);
 void getBSplJointHistWithGrad(MatrixXd &joint_hist, VectorXd &hist1, MatrixXd &hist1_mat, MatrixXd &hist1_grad, MatrixXd &joint_hist_grad, MatrixX2i &bspl_ids1, const VectorXd &pix_vals1, const MatrixX2i &bspl_ids2, const MatrixXd &hist2_mat, const MatrixX2i &std_bspl_ids, const MatrixXi &linear_idx, double hist_pre_seed, double joint_hist_pre_seed, int n_pix, double hist_norm_mult=1)
computes both the histogram and gradient of the first image as well as the joint histogram and its gr...
 
void getBSplJointHistGrad(MatrixXd &joint_hist_grad, MatrixXd &hist1_grad, const VectorXd &pix_vals1, const MatrixXd &hist2_mat, const MatrixX2i &bspl_ids1, const MatrixX2i &bspl_ids2, const MatrixXi &linear_idx, int n_pix)
computes a matrix with (n_bins*n_bins) rows and n_pix columns that contains the gradient of each entr...
 
void getDiracJointHist(MatrixXd &joint_hist, VectorXd &hist1, const VectorXd &pix_vals1, const VectorXi &pix_vals2_int, double hist_pre_seed, double joint_hist_pre_seed, int n_pix, int n_bins)
assumes that the histogram for the second image has already been computed along with the floors of it...
 
void getMIVec(VectorXd &mi_vec, VectorXd &log_hist_ratio, const MatrixXd &joint_hist, const VectorXd &norm_hist1, const VectorXd &norm_hist2, const MatrixXi &linear_idx, int n_bins)
computes a vector of size n_bins x n_bins whose sum gives the MI of the two images whose normalized h...
 
void getBSplHist(VectorXd &hist, MatrixXd &hist_mat, MatrixX2i &bspl_ids, const VectorXd &pix_vals, const MatrixX2i &std_bspl_ids, double pre_seed, int n_pix)
computes the histogram for the given image specified as a vector of pixel values. ...
 
void getEntropyVec(VectorXd &entropy_vec, const VectorXd &norm_hist, int n_bins)
computes a vector of size 'n_bins' whose sum is equal to the entropy of the image whose normalized hi...
 
void getMIVecGrad(MatrixXd &mi_vec_grad, const MatrixXd &joint_hist_grad, const VectorXd &log_hist_ratio, const MatrixXd &hist1_grad_ratio, const MatrixXd &joint_hist, const MatrixX2i &bspl_ids, const MatrixXi &linear_idx, int n_bins, int n_pix)
computes the gradient of each entry of the MI vector w.r.t. 
 
void getBSplHistWithGrad(VectorXd &hist, MatrixXd &hist_mat, MatrixXd &hist_grad, MatrixX2i &bspl_ids, const VectorXd &pix_vals, const MatrixX2i &std_bspl_ids, double pre_seed, int n_pix, double hist_norm_mult)
computes both the histogram and its gradient simultaneously to take advantage of the common computati...
 
double bSpl3Hess(double x)
second order derivative of the BSpline function of order 3 
Definition: histUtils.h:269
 
basic functions for preprocessing the raw input image using filtering, resizing and histogram equaliz...
Definition: histUtils.h:20
 
void getDiracHist(VectorXd &hist, VectorXi &pix_vals_int, const VectorXd &pix_vals, double hist_pre_seed, int n_pix)
computes histogram using the Dirac delta function to determine the bins to which each pixel contribut...
 
void getCumBSplHistWithGrad(VectorXd &cum_hist, MatrixXd &cum_hist_mat, MatrixXd &cum_hist_grad, MatrixX2i &bspline_ids, const VectorXd &pix_vals, const MatrixX2i &std_bspline_ids, double pre_seed, int n_bins, int n_pix, double hist_norm_mult)
computes the cumulative cubic BSpline histogram and its gradient 
 
void getBilinearHist(VectorXd &hist, VectorXi &pix_vals_int, const VectorXd &pix_vals, double hist_pre_seed, int n_pix)
computes histogram using the bilinear interpolation to determine contributions for the bins correspon...
 
void getBSplHistGrad(MatrixXd &hist_grad, const VectorXd &pix_vals, const MatrixX2i &bspl_ids, int n_pix)
computes a matrix with 'n_bins' rows and 'n_pix' columns that contains the gradient of each bin of th...
 
void getBilinearJointHist(MatrixXd &joint_hist, VectorXd &hist1, VectorXd &hist2, const VectorXd &pix_vals1, const VectorXd &pix_vals2, double hist_pre_seed, double joint_hist_pre_seed, int n_pix, int n_bins)
computes joint histogram using the bilinear interpolation to determine contributions for the bins cor...
 
double bSpl3Grad(double x)
first order derivative of the BSpline function of order 3 
Definition: histUtils.h:228