Classes | Functions
core/vil/vil_math.h File Reference

Various mathematical manipulations of 2D images. More...

#include <vcl_cassert.h>
#include <vcl_vector.h>
#include <vcl_cmath.h>
#include <vcl_algorithm.h>
#include <vil/vil_image_view.h>
#include <vil/vil_view_as.h>
#include <vil/vil_plane.h>
#include <vil/vil_transform.h>

Go to the source code of this file.

Classes

class  vil_math_sqrt_functor
 Functor class to compute square roots (returns zero if x<0). More...
class  vil_math_scale_functor
 Functor class to scale by s. More...
class  vil_math_scale_and_translate_functor
 Functor class to scale by s and translate (offset) by t. More...
class  vil_math_log_functor
 Functor class to compute logarithms (returns zero if x<=0). More...

Functions

template<class T >
void vil_math_value_range (const vil_image_view< T > &view, T &min_value, T &max_value)
 Compute minimum and maximum values over view.
VCL_DEFINE_SPECIALIZATION void vil_math_value_range (const vil_image_view< vil_rgb< vxl_byte > > &rgb_view, vil_rgb< vxl_byte > &min_value, vil_rgb< vxl_byte > &max_value)
 Compute minimum and maximum values over view.
VCL_DEFINE_SPECIALIZATION void vil_math_value_range (const vil_image_view< vil_rgb< float > > &rgb_view, vil_rgb< float > &min_value, vil_rgb< float > &max_value)
 Compute minimum and maximum values over view.
template<class T >
void vil_math_value_range_percentiles (const vil_image_view< T > &im, const vcl_vector< double > &fraction, vcl_vector< T > &value)
 Compute the values corresponding to several percentiles of the range of im.
template<class T >
void vil_math_value_range_percentile (const vil_image_view< T > &im, const double fraction, T &value)
 Compute the value corresponding to a percentile of the range of im.
template<class imT , class sumT >
sumT vil_math_ssd (const vil_image_view< imT > &imA, const vil_image_view< imT > &imB, sumT)
 Sum of squared differences between two images.
template<class imT , class sumT >
sumT vil_math_ssd_complex (const vil_image_view< vcl_complex< imT > > &imA, const vil_image_view< vcl_complex< imT > > &imB, sumT)
 Sum squared magnitude differences between two complex images.
template<class aT , class sumT >
void vil_math_mean_over_planes (const vil_image_view< aT > &src, vil_image_view< sumT > &dest)
 Calc the mean of each pixel over all the planes.
template<class inT , class outT , class sumT >
void vil_math_mean_over_planes (const vil_image_view< inT > &src, vil_image_view< outT > &dest, sumT)
 Calc the mean of each pixel over all the planes.
template<class imT , class sumT >
void vil_math_sum (sumT &sum, const vil_image_view< imT > &im, unsigned p)
 Sum of elements in plane p of image.
template<class imT , class sumT >
void vil_math_mean (sumT &mean, const vil_image_view< imT > &im, unsigned p)
 Mean of elements in plane p of image.
void vil_math_median_unimplemented ()
template<class imT >
void vil_math_median (imT &median, const vil_image_view< imT > &im, unsigned p)
 Median of elements in plane p of an image.
VCL_DEFINE_SPECIALIZATION void vil_math_median (vxl_byte &median, const vil_image_view< vxl_byte > &im, unsigned p)
template<class imT , class sumT >
void vil_math_sum_squares (sumT &sum, sumT &sum_sq, const vil_image_view< imT > &im, unsigned p)
 Sum of squares of elements in plane p of image.
template<class imT , class sumT >
void vil_math_mean_and_variance (sumT &mean, sumT &var, const vil_image_view< imT > &im, unsigned p)
 Mean and variance of elements in plane p of image.
template<class T >
void vil_math_sqrt (vil_image_view< T > &image)
 Compute square-root of each pixel element (or zero if negative).
template<class T >
void vil_math_truncate_range (vil_image_view< T > &image, T min_v, T max_v)
 Truncate each pixel value so it fits into range [min_v,max_v].
template<class T >
void vil_math_scale_values (vil_image_view< T > &image, double scale)
 Multiply values in-place in image view by scale.
template<class imT , class offsetT >
void vil_math_scale_and_offset_values (vil_image_view< imT > &image, double scale, offsetT offset)
 Multiply values in-place in image view by scale and add offset.
template<class imT >
void vil_math_normalise (vil_image_view< imT > &image)
 Scale and offset values so their mean is zero and their variance is one.
template<class srcT , class destT >
void vil_math_rms (const vil_image_view< srcT > &src, vil_image_view< destT > &dest)
 Computes RMS of each pixel over the planes of src image.
template<class srcT , class destT >
void vil_math_rss (const vil_image_view< srcT > &src, vil_image_view< destT > &dest)
 Computes Root Sum of Squares of each pixel over the planes of src image.
template<class srcT , class destT >
void vil_math_sum_sqr (const vil_image_view< srcT > &src, vil_image_view< destT > &dest)
 Computes sum of squares of each pixel over the planes of src image.
template<class aT , class bT , class sumT >
void vil_math_image_sum (const vil_image_view< aT > &imA, const vil_image_view< bT > &imB, vil_image_view< sumT > &im_sum)
 Compute sum of two images (im_sum = imA+imB).
template<class aT , class bT , class sumT >
void vil_math_image_product (const vil_image_view< aT > &imA, const vil_image_view< bT > &imB, vil_image_view< sumT > &im_product)
 Compute pixel-wise product of two images (im_prod(i,j) = imA(i,j)*imB(i,j).
template<class aT , class bT , class maxT >
void vil_math_image_max (const vil_image_view< aT > &imA, const vil_image_view< bT > &imB, vil_image_view< maxT > &im_max)
 Compute the max of two images (im_max = max(imA, imB)).
template<class aT , class bT , class minT >
void vil_math_image_min (const vil_image_view< aT > &imA, const vil_image_view< bT > &imB, vil_image_view< minT > &im_min)
 Compute the min of two images (im_min = min(imA, imB)).
template<class aT , class bT , class sumT >
void vil_math_image_ratio (const vil_image_view< aT > &imA, const vil_image_view< bT > &imB, vil_image_view< sumT > &im_ratio)
 Compute pixel-wise ratio of two images : im_ratio(i,j) = imA(i,j)/imB(i,j).
template<class aT , class bT , class sumT >
void vil_math_image_difference (const vil_image_view< aT > &imA, const vil_image_view< bT > &imB, vil_image_view< sumT > &im_sum)
 Compute difference of two images (im_sum = imA-imB).
template<class aT , class bT , class sumT >
void vil_math_image_abs_difference (const vil_image_view< aT > &imA, const vil_image_view< bT > &imB, vil_image_view< sumT > &im_sum)
 Compute absolute difference of two images (im_sum = |imA-imB|).
template<class aT , class bT >
double vil_math_image_abs_difference (const vil_image_view< aT > &imA, const vil_image_view< bT > &imB)
 Compute sum of absolute difference between two images (|imA-imB|).
template<class aT , class bT , class magT >
void vil_math_image_vector_mag (const vil_image_view< aT > &imA, const vil_image_view< bT > &imB, vil_image_view< magT > &im_mag)
 Compute magnitude of two images taken as vector components, sqrt(A^2 + B^2).
template<class aT , class bT , class scaleT >
void vil_math_add_image_fraction (vil_image_view< aT > &imA, scaleT fa, const vil_image_view< bT > &imB, scaleT fb)
 imA = fa*imA + fb*imB (Useful for moving averages!).
template<class aT , class sumT >
void vil_math_integral_image (const vil_image_view< aT > &imA, vil_image_view< sumT > &im_sum)
 Compute integral image im_sum(i+1,j+1) = sum (x<=i,y<=j) imA(x,y).
template<class aT , class sumT >
void vil_math_integral_sqr_image (const vil_image_view< aT > &imA, vil_image_view< sumT > &im_sum, vil_image_view< sumT > &im_sum_sq)
 Compute integral image im_sum_sq(i+1,j+1) = sum (x<=i,y<=j) imA(x,y)^2.

Detailed Description

Various mathematical manipulations of 2D images.

Author:
Tim Cootes

Definition in file vil_math.h.


Function Documentation

template<class aT , class bT , class scaleT >
void vil_math_add_image_fraction ( vil_image_view< aT > &  imA,
scaleT  fa,
const vil_image_view< bT > &  imB,
scaleT  fb 
) [inline]

imA = fa*imA + fb*imB (Useful for moving averages!).

Can do running sum using vil_add_image_fraction(running_mean,1-f,new_im,f) to update current mean by a fraction f of new_im

Definition at line 933 of file vil_math.h.

template<class aT , class bT , class sumT >
void vil_math_image_abs_difference ( const vil_image_view< aT > &  imA,
const vil_image_view< bT > &  imB,
vil_image_view< sumT > &  im_sum 
) [inline]

Compute absolute difference of two images (im_sum = |imA-imB|).

Definition at line 825 of file vil_math.h.

template<class aT , class bT >
double vil_math_image_abs_difference ( const vil_image_view< aT > &  imA,
const vil_image_view< bT > &  imB 
) [inline]

Compute sum of absolute difference between two images (|imA-imB|).

Definition at line 861 of file vil_math.h.

template<class aT , class bT , class sumT >
void vil_math_image_difference ( const vil_image_view< aT > &  imA,
const vil_image_view< bT > &  imB,
vil_image_view< sumT > &  im_sum 
) [inline]

Compute difference of two images (im_sum = imA-imB).

Definition at line 792 of file vil_math.h.

template<class aT , class bT , class maxT >
void vil_math_image_max ( const vil_image_view< aT > &  imA,
const vil_image_view< bT > &  imB,
vil_image_view< maxT > &  im_max 
) [inline]

Compute the max of two images (im_max = max(imA, imB)).

Definition at line 679 of file vil_math.h.

template<class aT , class bT , class minT >
void vil_math_image_min ( const vil_image_view< aT > &  imA,
const vil_image_view< bT > &  imB,
vil_image_view< minT > &  im_min 
) [inline]

Compute the min of two images (im_min = min(imA, imB)).

Definition at line 712 of file vil_math.h.

template<class aT , class bT , class sumT >
void vil_math_image_product ( const vil_image_view< aT > &  imA,
const vil_image_view< bT > &  imB,
vil_image_view< sumT > &  im_product 
) [inline]

Compute pixel-wise product of two images (im_prod(i,j) = imA(i,j)*imB(i,j).

If images have the same number of planes, then im_prod(i,j,p) = imA(i,j,p)*imB(i,j,p). If imB only has one plane, then im_prod(i,j,p) = imA(i,j,p)*imB(i,j,0).

Definition at line 640 of file vil_math.h.

template<class aT , class bT , class sumT >
void vil_math_image_ratio ( const vil_image_view< aT > &  imA,
const vil_image_view< bT > &  imB,
vil_image_view< sumT > &  im_ratio 
) [inline]

Compute pixel-wise ratio of two images : im_ratio(i,j) = imA(i,j)/imB(i,j).

Pixels cast to type sumT before calculation. If imB(i,j,p)==0, im_ration(i,j,p)=0

If images have the same number of planes, then im_ratio(i,j,p) = imA(i,j,p)/imB(i,j,p). If imB only has one plane, then im_ratio(i,j,p) = imA(i,j,p)/imB(i,j,0).

Definition at line 751 of file vil_math.h.

template<class aT , class bT , class sumT >
void vil_math_image_sum ( const vil_image_view< aT > &  imA,
const vil_image_view< bT > &  imB,
vil_image_view< sumT > &  im_sum 
) [inline]

Compute sum of two images (im_sum = imA+imB).

Definition at line 604 of file vil_math.h.

template<class aT , class bT , class magT >
void vil_math_image_vector_mag ( const vil_image_view< aT > &  imA,
const vil_image_view< bT > &  imB,
vil_image_view< magT > &  im_mag 
) [inline]

Compute magnitude of two images taken as vector components, sqrt(A^2 + B^2).

Definition at line 893 of file vil_math.h.

template<class aT , class sumT >
void vil_math_integral_image ( const vil_image_view< aT > &  imA,
vil_image_view< sumT > &  im_sum 
) [inline]

Compute integral image im_sum(i+1,j+1) = sum (x<=i,y<=j) imA(x,y).

Useful thing for quickly computing mean over large regions, as demonstrated in Viola and Jones (CVPR01). The sum of elements in the ni x nj square with corner (i,j) is given by im_sum(i,j)+im_sum(i+ni,j+nj)-im_sum(i+ni,j)-im_sum(i,j+nj)

Definition at line 964 of file vil_math.h.

template<class aT , class sumT >
void vil_math_integral_sqr_image ( const vil_image_view< aT > &  imA,
vil_image_view< sumT > &  im_sum,
vil_image_view< sumT > &  im_sum_sq 
) [inline]

Compute integral image im_sum_sq(i+1,j+1) = sum (x<=i,y<=j) imA(x,y)^2.

Also computes sum im_sum(i+1,j+1) = sum (x<=i,y<=j) imA(x,y)

Useful thing for quickly computing mean and variance over large regions, as demonstrated in Viola and Jones (CVPR01).

The sum of elements in the ni x nj square with corner (i,j) is given by im_sum(i,j)+im_sum(i+ni,j+nj)-im_sum(i+ni,j)-im_sum(i,j+nj)

Similar result holds for sum of squares, allowing rapid calculation of variance etc.

Definition at line 1013 of file vil_math.h.

template<class imT , class sumT >
void vil_math_mean ( sumT &  mean,
const vil_image_view< imT > &  im,
unsigned  p 
) [inline]

Mean of elements in plane p of image.

Definition at line 257 of file vil_math.h.

template<class imT , class sumT >
void vil_math_mean_and_variance ( sumT &  mean,
sumT &  var,
const vil_image_view< imT > &  im,
unsigned  p 
) [inline]

Mean and variance of elements in plane p of image.

Definition at line 307 of file vil_math.h.

template<class aT , class sumT >
void vil_math_mean_over_planes ( const vil_image_view< aT > &  src,
vil_image_view< sumT > &  dest 
) [inline]

Calc the mean of each pixel over all the planes.

Definition at line 200 of file vil_math.h.

template<class inT , class outT , class sumT >
void vil_math_mean_over_planes ( const vil_image_view< inT > &  src,
vil_image_view< outT > &  dest,
sumT   
) [inline]

Calc the mean of each pixel over all the planes.

Definition at line 222 of file vil_math.h.

template<class imT >
void vil_math_median ( imT &  median,
const vil_image_view< imT > &  im,
unsigned  p 
) [inline]

Median of elements in plane p of an image.

For integral types, if the median is half way between two values, the result will be the floor of the average.

Definition at line 275 of file vil_math.h.

VCL_DEFINE_SPECIALIZATION void vil_math_median ( vxl_byte &  median,
const vil_image_view< vxl_byte > &  im,
unsigned  p 
)

Definition at line 21 of file vil_math.cxx.

void vil_math_median_unimplemented ( )

Definition at line 14 of file vil_math.cxx.

template<class imT >
void vil_math_normalise ( vil_image_view< imT > &  image) [inline]

Scale and offset values so their mean is zero and their variance is one.

Only works on signed types!

Definition at line 447 of file vil_math.h.

template<class srcT , class destT >
void vil_math_rms ( const vil_image_view< srcT > &  src,
vil_image_view< destT > &  dest 
) [inline]

Computes RMS of each pixel over the planes of src image.

Dest is a single plane image, $dest(i,j)^2 = 1/np sum_p src(i,j,p)^2$ Summation is performed using type destT

Definition at line 462 of file vil_math.h.

template<class srcT , class destT >
void vil_math_rss ( const vil_image_view< srcT > &  src,
vil_image_view< destT > &  dest 
) [inline]

Computes Root Sum of Squares of each pixel over the planes of src image.

Dest is a single plane image, $dest(i,j) = sqrt(sum_p src(i,j,p)^2)$ Differs from RMS by the scaling factor sqrt(nplanes) Summation is performed using type destT

Definition at line 512 of file vil_math.h.

template<class imT , class offsetT >
void vil_math_scale_and_offset_values ( vil_image_view< imT > &  image,
double  scale,
offsetT  offset 
) [inline]

Multiply values in-place in image view by scale and add offset.

Definition at line 428 of file vil_math.h.

template<class T >
void vil_math_scale_values ( vil_image_view< T > &  image,
double  scale 
) [inline]

Multiply values in-place in image view by scale.

Definition at line 420 of file vil_math.h.

template<class T >
void vil_math_sqrt ( vil_image_view< T > &  image) [inline]

Compute square-root of each pixel element (or zero if negative).

Definition at line 331 of file vil_math.h.

template<class imT , class sumT >
sumT vil_math_ssd ( const vil_image_view< imT > &  imA,
const vil_image_view< imT > &  imB,
sumT   
) [inline]

Sum of squared differences between two images.

Definition at line 163 of file vil_math.h.

template<class imT , class sumT >
sumT vil_math_ssd_complex ( const vil_image_view< vcl_complex< imT > > &  imA,
const vil_image_view< vcl_complex< imT > > &  imB,
sumT   
) [inline]

Sum squared magnitude differences between two complex images.

Definition at line 181 of file vil_math.h.

template<class imT , class sumT >
void vil_math_sum ( sumT &  sum,
const vil_image_view< imT > &  im,
unsigned  p 
) [inline]

Sum of elements in plane p of image.

Definition at line 240 of file vil_math.h.

template<class srcT , class destT >
void vil_math_sum_sqr ( const vil_image_view< srcT > &  src,
vil_image_view< destT > &  dest 
) [inline]

Computes sum of squares of each pixel over the planes of src image.

Dest is a single plane image, $dest(i,j) = sum_p src(i,j,p)^2$ Summation is performed using type destT

Definition at line 562 of file vil_math.h.

template<class imT , class sumT >
void vil_math_sum_squares ( sumT &  sum,
sumT &  sum_sq,
const vil_image_view< imT > &  im,
unsigned  p 
) [inline]

Sum of squares of elements in plane p of image.

Definition at line 290 of file vil_math.h.

template<class T >
void vil_math_truncate_range ( vil_image_view< T > &  image,
min_v,
max_v 
) [inline]

Truncate each pixel value so it fits into range [min_v,max_v].

If value < min_v value=min_v If value > max_v value=max_v

Definition at line 342 of file vil_math.h.

template<class T >
void vil_math_value_range ( const vil_image_view< T > &  view,
T &  min_value,
T &  max_value 
) [inline]

Compute minimum and maximum values over view.

Definition at line 21 of file vil_math.h.

VCL_DEFINE_SPECIALIZATION void vil_math_value_range ( const vil_image_view< vil_rgb< vxl_byte > > &  rgb_view,
vil_rgb< vxl_byte > &  min_value,
vil_rgb< vxl_byte > &  max_value 
) [inline]

Compute minimum and maximum values over view.

Definition at line 51 of file vil_math.h.

VCL_DEFINE_SPECIALIZATION void vil_math_value_range ( const vil_image_view< vil_rgb< float > > &  rgb_view,
vil_rgb< float > &  min_value,
vil_rgb< float > &  max_value 
) [inline]

Compute minimum and maximum values over view.

Definition at line 63 of file vil_math.h.

template<class T >
void vil_math_value_range_percentile ( const vil_image_view< T > &  im,
const double  fraction,
T &  value 
) [inline]

Compute the value corresponding to a percentile of the range of im.

Percentile is expressed as fraction, e.g. 0.05, or 0.95.

Parameters:
imThe image to examine.
fractionThe fraction of the data range (from the lower end).
Return values:
valueThe image data value corresponding to the specified percentile.
Note:
This function requires the sorting of large parts of the image data and can be very expensive in terms of both processing and memory.

Definition at line 148 of file vil_math.h.

template<class T >
void vil_math_value_range_percentiles ( const vil_image_view< T > &  im,
const vcl_vector< double > &  fraction,
vcl_vector< T > &  value 
) [inline]

Compute the values corresponding to several percentiles of the range of im.

Percentiles are expressed as fraction, e.g. 0.05, or 0.95.

Parameters:
imThe image to examine.
fractionThe fractions of the data range (from the lower end).
Return values:
valueThe image data values corresponding to the specified percentiles.
Note:
This function requires the sorting of large parts of the image data and can be very expensive in terms of both processing and memory.

Definition at line 83 of file vil_math.h.