Main Page | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Class Members | File Members

brip_vil_float_ops.h

Go to the documentation of this file.
00001 // This is brl/bseg/brip/brip_vil_float_ops.h
00002 #ifndef brip_vil_float_ops_h_
00003 #define brip_vil_float_ops_h_
00004 //-----------------------------------------------------------------------------
00005 //:
00006 // \file
00007 // \author J.L. Mundy
00008 // \brief operations on image_view<float> operands
00009 //
00010 // These methods are similar to the VanDuc gevd_float_ops methods. However,
00011 // they use vil_image_view<float> buffers rather than the old bufferxy
00012 // structure. The purpose is to provide efficient foundational
00013 // segmentation routines. They are not meant to be generic.
00014 //
00015 // \verbatim
00016 //  Modifications
00017 //   Feb 15 2003 Initial version
00018 //   Jan 24 2004 Renamed to brip_vil_float_ops, to support moving from vil1 to vil
00019 // \endverbatim
00020 //
00021 //-----------------------------------------------------------------------------
00022 #include <vcl_vector.h>
00023 #include <vcl_complex.h>
00024 #include <vnl/vnl_matrix.h>
00025 #include <vbl/vbl_array_2d.h>
00026 #include <vgl/algo/vgl_h_matrix_2d.h>
00027 #include <vil/vil_image_resource.h>
00028 #include <vil/vil_image_view.h>
00029 #include <vil/vil_rgb.h>
00030 #include <vsol/vsol_box_2d_sptr.h>
00031 #include <brip/brip_roi_sptr.h>
00032 
00033 class brip_vil_float_ops
00034 {
00035  public:
00036   ~brip_vil_float_ops() {}
00037 
00038   //: convolves with the specified kernel
00039   static vil_image_view<float>
00040     convolve(vil_image_view<float> const& input,
00041              vbl_array_2d<float> const& kernel);
00042 
00043   //: helper to determine processing border required by Gaussian smoothing
00044   static unsigned gaussian_radius(const double sigma, const double fuzz=0.02);
00045 
00046   //: convolves with a Gaussian kernel
00047   static vil_image_view<float> gaussian(vil_image_view<float> const& input,
00048                                         float sigma);
00049 
00050 #if 1 // #ifdef VIL_CONVOLVE_WITH_MASK_EXISTS // TODO
00051   //: convolves with a Gaussian kernel and for a given mask
00052   static vil_image_view<float> gaussian(vil_image_view<float> const& input,
00053                                         float sigma,
00054                                         vil_image_view<float> mask);
00055 #endif
00056 
00057   //: non-maximum suppression on a NxN neighborhood, with sub-pixel location
00058   static void non_maximum_suppression(vil_image_view<float> const& input,
00059                                       int n, float thresh,
00060                                       vcl_vector<float>& x_pos,
00061                                       vcl_vector<float>& y_pos,
00062                                       vcl_vector<float>& value);
00063 
00064   //: downsamples the input using the Bert-Adelson algorithm
00065   static vil_image_view<float>
00066     half_resolution(vil_image_view<float> const& input,
00067                     float filter_coef=0.359375f);
00068 
00069 
00070   //: interpolates the input using the Bert-Adelson algorithm
00071   static vil_image_view<float>
00072     double_resolution(vil_image_view<float> const& input,
00073                       float filter_coef=0.6f);
00074 
00075 
00076   //: subtracts image_1 from image_2
00077   static vil_image_view<float>
00078     difference(vil_image_view<float> const& image_1,
00079                vil_image_view<float> const& image_2);
00080 
00081   //: sets values greater than thresh to specified level and the rest to zero
00082   static vil_image_view<float>
00083     threshold(vil_image_view<float> const & image,
00084               const float thresh, const float level = 255.0);
00085 
00086   //: sets absolute values greater than thresh to specified level
00087   static vil_image_view<float>
00088     abs_clip_to_level(vil_image_view<float> const & image,
00089                       const float thresh, const float level = 0.0);
00090 
00091   static vil_image_view<float>
00092     average_NxN(vil_image_view<float> const & img, int N);
00093 
00094   //: The gradient using a 3x3 kernel
00095   static void gradient_3x3(vil_image_view<float> const& input,
00096                            vil_image_view<float>& grad_x,
00097                            vil_image_view<float>& grad_y);
00098 
00099   static void hessian_3x3(vil_image_view<float> const& input,
00100                           vil_image_view<float>& Ixx,
00101                           vil_image_view<float>& Ixy,
00102                           vil_image_view<float>& Iyy);
00103 
00104   static vil_image_view<float>
00105     beaudet(vil_image_view<float> const& Ixx,
00106             vil_image_view<float> const& Ixy,
00107             vil_image_view<float> const& Iyy);
00108 
00109   //: IxIx.transpose gradient matrix elements for a NxN region(N = 2n+1)
00110   static void grad_matrix_NxN(vil_image_view<float> const& input, unsigned n,
00111                               vil_image_view<float>& IxIx,
00112                               vil_image_view<float>& IxIy,
00113                               vil_image_view<float>& IyIy);
00114 
00115   //: Tr(IxIx.transpose) for a NxN region, N = 2n+1)
00116   static  vil_image_view<float>
00117     trace_grad_matrix_NxN(vil_image_view<float> const& input, unsigned n);
00118 
00119   //: Computes the Harris corner measure
00120   static vil_image_view<float> harris(vil_image_view<float> const& IxIx,
00121                                       vil_image_view<float> const& IxIy,
00122                                       vil_image_view<float> const& IyIy,
00123                                       double scale=0.04);
00124 
00125   //: computes the conditioning of the 2n+1 x 2n+1 gradient neigborhood
00126   static vil_image_view<float>
00127     sqrt_grad_singular_values(vil_image_view<float>& input, int n);
00128 
00129   static vil_image_view<float> max_scale_trace(vil_image_view<float> input,
00130                                                float min_scale,
00131                                                float max_scale,
00132                                                float scale_inc);
00133 
00134   //: computes Lucas-Kanade optical flow on a 2n+1 neighborhood
00135   static void Lucas_KanadeMotion(vil_image_view<float>& current_frame,
00136                                  vil_image_view<float>& previous_frame,
00137                                  int n, double thresh,
00138                                  vil_image_view<float>& vx,
00139                                  vil_image_view<float>& vy);
00140 
00141   //: computes velocity of a region(view) using Lucas Kanade
00142   static 
00143     void lucas_kanade_motion_on_view(vil_image_view<float> const& curr_frame,
00144                                      vil_image_view<float> const& prev_frame,
00145                                      const double thresh,
00146                                      float& vx,
00147                                      float& vy);
00148 
00149   //: computes velocity of a region(view) using correlation
00150   static void
00151     velocity_by_correlation(vil_image_view<float> const& curr_image,
00152                             vil_image_view<float> const& prev_region,
00153                             const unsigned start_i, const unsigned end_i,
00154                             const unsigned start_j, const unsigned end_j,
00155                             const unsigned zero_i, const unsigned zero_j,
00156                             float& vx,
00157                             float& vy);
00158 
00159 
00160   //: computes optical flow using Horn & Schunck's method
00161   static int Horn_SchunckMotion(vil_image_view<float> const & current_frame,
00162                                 vil_image_view<float> const& previous_frame,
00163                                 vil_image_view<float>& vx,
00164                                 vil_image_view<float>& vy,
00165                                 const float alpha_coef=10000.0f,
00166                                 const int no_of_iterations=5);
00167 
00168   //: fills a border of width w on left and right of image with value
00169   static void fill_x_border(vil_image_view<float>& image, unsigned w, float value);
00170 
00171 
00172   //: fills a border of width h on top and bottom of image with value
00173   static void fill_y_border(vil_image_view<float>& image, unsigned h, float value);
00174 
00175   //: converts a float image to a byte value range
00176   static vil_image_view<unsigned char>
00177     convert_to_byte(vil_image_view<float> const& image);
00178 
00179   //: converts a float image to a byte value range within a specified range
00180   static vil_image_view<unsigned char>
00181     convert_to_byte(vil_image_view<float> const& image,
00182                     float min_val, float max_val);
00183 
00184   //: converts an unsigned short image to a byte value range within a specified range
00185   static vil_image_view<unsigned char>
00186     convert_to_byte(vil_image_view<unsigned short> const& image,
00187                     unsigned short min_val, unsigned short max_val);
00188 
00189   //: converts a generic image to a byte image. Use this instead of convert_to_grey
00190   static vil_image_view<unsigned char>
00191     convert_to_byte(vil_image_resource_sptr const& image);
00192 
00193   //: converts a float image to an unsigned short image within a range
00194   static vil_image_view<unsigned short>
00195     convert_to_short(vil_image_view<float> const& image,
00196                      float min_val, float max_val);
00197   //: converts a float image to an unsigned short image.
00198   // range determined automatically
00199   static vil_image_view<unsigned short>
00200     convert_to_short(vil_image_view<float> const& image);
00201 
00202   //: converts a generic image to an unsigned short image
00203   static vil_image_view<unsigned short>
00204     convert_to_short(vil_image_resource_sptr const& image);
00205 
00206   //: converts a vil_image_resource to a float image
00207   static vil_image_view<float>
00208     convert_to_float(vil_image_resource const& image);
00209 
00210   //: converts a vil_image_resource to a float image (preferred interface)
00211   static vil_image_view<float>
00212     convert_to_float(vil_image_resource_sptr const& image)
00213     {return brip_vil_float_ops::convert_to_float(*image);}
00214 
00215   static vil_image_view<float>
00216     convert_to_float(vil_image_view<unsigned char> const& image);
00217 
00218   static vil_image_view<float>
00219     convert_to_float(vil_image_view<unsigned short> const& image);
00220 
00221   //: converts a byte image to a bool image
00222   static vil_image_view<bool>
00223     convert_to_bool(vil_image_view<unsigned char> const& image);
00224 
00225   //: converts an RGB image to a float image
00226   static vil_image_view<float>
00227     convert_to_float(vil_image_view<vil_rgb<vxl_byte> > const& image);
00228 
00229   //: convert a color image to float IHS images
00230   static void
00231     convert_to_IHS(vil_image_view<vil_rgb<vxl_byte> > const& image,
00232                    vil_image_view<float>& I,
00233                    vil_image_view<float>& H,
00234                    vil_image_view<float>& S);
00235   static void
00236     convert_to_IHS(vil_image_view<unsigned char> const& image,
00237                    vil_image_view<float>& I,
00238                    vil_image_view<float>& H,
00239                    vil_image_view<float>& S);
00240 
00241   //: display IHS images as RGB (not conversion from IHS to RGB)
00242   static void
00243     display_IHS_as_RGB(vil_image_view<float> const& I,
00244                        vil_image_view<float> const& H,
00245                        vil_image_view<float> const& S,
00246                        vil_image_view<vil_rgb<vxl_byte> >& image);
00247 
00248   //: Create a color image from multiple view channels
00249   // all views have to have the same array dimensions
00250   static vil_image_view<vil_rgb<vxl_byte> >
00251     combine_color_planes(vil_image_view<unsigned char> const& R,
00252                          vil_image_view<unsigned char> const& G,
00253                          vil_image_view<unsigned char> const& B);
00254 
00255   //: Create a unsigned char color image from multiple resource channels
00256   // images do not have to be the same size arraysp
00257   static vil_image_view<vil_rgb<vxl_byte> >
00258    combine_color_planes(vil_image_resource_sptr const& R,
00259                         vil_image_resource_sptr const& G,
00260                         vil_image_resource_sptr const& B);
00261 
00262 
00263   //: converts a generic image to greyscale (RGB<unsigned char>)
00264   static vil_image_view<unsigned char>
00265     convert_to_grey(vil_image_resource const& img);
00266 
00267   //: loads a 2n+1 x 2n+1 convolution kernel (see .cxx for file format)
00268   static vbl_array_2d<float> load_kernel(vcl_string const& file);
00269 
00270   //: compute basis images for a set of input images
00271   static void basis_images(vcl_vector<vil_image_view<float> > const& input_images,
00272                            vcl_vector<vil_image_view<float> >& basis);
00273 
00274   //: compute the Fourier transform using the vnl FFT algorithm
00275   static bool fourier_transform(vil_image_view<float> const& input,
00276                                 vil_image_view<float>& mag,
00277                                 vil_image_view<float>& phase);
00278 
00279   //: compute the inverse Fourier transform using the vnl FFT algorithm
00280   static bool inverse_fourier_transform(vil_image_view<float> const& mag,
00281                                         vil_image_view<float> const& phase,
00282                                         vil_image_view<float>& output);
00283 
00284   //: resize to specified dimensions, fill with zeros if output is larger
00285   static void resize(vil_image_view<float> const& input,
00286                      unsigned width, unsigned height,
00287                      vil_image_view<float>& output);
00288 
00289   //: resize to closest power of two larger dimensions than the input
00290   static bool
00291     resize_to_power_of_two(vil_image_view<float> const& input,
00292                            vil_image_view<float>& output);
00293 
00294   //: filter the input image with a Gaussian blocking filter
00295   static bool
00296     spatial_frequency_filter(vil_image_view<float> const& input,
00297                              float dir_fx, float dir_fy,
00298                              float f0, float radius,
00299                              bool output_fourier_mag,
00300                              vil_image_view<float>& output);
00301 
00302   //: 2x2 bilinear interpolation of image at specified location
00303   static double
00304     bilinear_interpolation(vil_image_view<float> const& input,
00305                            double x, double y);
00306 
00307   //: map the input to the output by a homography.
00308   // \note if the output size is fixed then only the corresponding
00309   // input image space is transformed.
00310   static bool homography(vil_image_view<float> const& input,
00311                          vgl_h_matrix_2d<double>const& H,
00312                          vil_image_view<float>& output,
00313                          bool output_size_fixed = false,
00314                          float output_fill_value = 0.0f);
00315 
00316   //: rotate the input image counter-clockwise about the image origin
00317   static vil_image_view<float> rotate(vil_image_view<float> const& input,
00318                                       double theta_deg);
00319 
00320   //: extract a region of interest. If roi does not overlap input, return false
00321   static bool chip(vil_image_view<float> const& input,
00322                    vsol_box_2d_sptr const& roi, vil_image_view<float>& chip);
00323 
00324   //: convert image resource to a chip of equivalent pixel type
00325   static bool chip(vil_image_resource_sptr const& image,
00326                    brip_roi_sptr const& roi,
00327                    vil_image_resource_sptr & chip);
00328 
00329   //: compute the average of the image intensity within the specified region
00330   static float average_in_box(vil_image_view<float> const& v,
00331                               vgl_box_2d<double> const& box);
00332 
00333   //: scan a polygon and return the pixel values as well as max min
00334   static vcl_vector<float> scan_region(vil_image_resource_sptr img,
00335                                        vgl_polygon<double> poly,
00336                                        float& min,
00337                                        float& max);
00338   //: cross-correlate two images at a given sub-pixel location
00339   static float cross_correlate(vil_image_view<float> const& image1,
00340                                vil_image_view<float> const& image2,
00341                                float x, float y, int radius = 5,
00342                                float intensity_thresh=25.0f);
00343 
00344   //: cross_correlate two images using running sums
00345   static bool cross_correlate(vil_image_view<float> const& image1,
00346                               vil_image_view<float> const& image2,
00347                               vil_image_view<float>& out,
00348                               int radius = 5, float intensity_thresh=25.0f);
00349 
00350   //: convert a single i,h,s  pixel to rgb
00351   static void ihs_to_rgb(vil_rgb<vxl_byte>& rgb,
00352                          const float i, const float h, const float s);
00353 
00354   //: convert a single rgb pixel to ihs
00355   static void rgb_to_ihs(vil_rgb<vxl_byte> const& rgb,
00356                          float& i, float& h, float& s);
00357 
00358   //: Compute the intensity entropy of a region about the specified pixel
00359   //  No bounds check
00360   static float entropy_i(const unsigned i, const unsigned j,
00361                          const unsigned i_radius,
00362                          const unsigned j_radius,
00363                          vil_image_view<float> const& intensity,
00364                          const float range = 255.0, const unsigned bins = 16);
00365 
00366   //: Compute the gradient entropy of a region about the specified pixel
00367   //  No bounds check
00368   static float entropy_g(const unsigned i, const unsigned j,
00369                          const unsigned i_radius,
00370                          const unsigned j_radius,
00371                          vil_image_view<float> const& gradx,
00372                          vil_image_view<float> const& grady,
00373                          const float range = 360.0, const unsigned bins = 8);
00374 
00375   //: Compute the hue and saturation entropy of a region about the specified pixel
00376   //  No bounds check
00377   static float entropy_hs(const unsigned i, const unsigned j,
00378                           const unsigned i_radius,
00379                           const unsigned j_radius,
00380                           vil_image_view<float> const& hue,
00381                           vil_image_view<float> const& sat,
00382                           const float range = 360.0, const unsigned bins = 8);
00383 
00384   //: Compute the entropy of the specified region about each pixel
00385   static vil_image_view<float> entropy(const unsigned i_radius,
00386                                        const unsigned j_radius,
00387                                        const unsigned step,
00388                                        vil_image_resource_sptr const& img,
00389                                        const float sigma = 1.0f,
00390                                        const bool intensity = true,
00391                                        const bool gradient = true,
00392                                        const bool ihs = false);
00393 
00394   //no bounds check
00395   static float minfo_i(const unsigned i0, const unsigned j0,
00396                        const unsigned i1, const unsigned j1,
00397                        const unsigned i_radius,
00398                        const unsigned j_radius,
00399                        vil_image_view<float> const& intensity0,
00400                        vil_image_view<float> const& intensity1,
00401                        const float range = 255.0, const unsigned bins = 16);
00402 
00403   //:Compute the gradient minfo of a region about the specified pixel
00404   // No bounds check
00405   static float minfo_g(const unsigned i0, const unsigned j0,
00406                        const unsigned i1, const unsigned j1,
00407                        const unsigned i_radius,
00408                        const unsigned j_radius,
00409                        vil_image_view<float> const& gradx0,
00410                        vil_image_view<float> const& grady0,
00411                        vil_image_view<float> const& gradx1,
00412                        vil_image_view<float> const& grady1,
00413                        const float range = 360.0, const unsigned bins = 8);
00414 
00415   //:Compute the hue and saturation minfo of a region about the specified pixel
00416   // No bounds check
00417   static float minfo_hs(const unsigned i0, const unsigned j0,
00418                         const unsigned i1, const unsigned j1,
00419                         const unsigned i_radius,
00420                         const unsigned j_radius,
00421                         vil_image_view<float> const& hue0,
00422                         vil_image_view<float> const& sat0,
00423                         vil_image_view<float> const& hue1,
00424                         vil_image_view<float> const& sat1,
00425                         const float range = 360.0, const unsigned bins = 8);
00426 
00427   //:compute the minfo of the specified region about each pixel
00428   static bool minfo(const unsigned i_radius,
00429                     const unsigned j_radius,
00430                     const unsigned step,
00431                     vil_image_resource_sptr const& img0,
00432                     vil_image_resource_sptr const& img1,
00433                     vil_image_view<float>& MI0,
00434                     vil_image_view<float>& MI1,
00435                     const float sigma = 1.0f,
00436                     const bool intensity = true,
00437                     const bool gradient = true,
00438                     const bool ihs = false);
00439 
00440   // Arithmetic operations
00441 
00442   //: Add two images from a general resource (forces types to be the same)
00443   static vil_image_resource_sptr sum(vil_image_resource_sptr const& img0,
00444                                      vil_image_resource_sptr const& img1);
00445 
00446   //: subtract two generic images, return img0-img1 (forces types to the same)
00447   static vil_image_resource_sptr difference(vil_image_resource_sptr const& img0,
00448                                             vil_image_resource_sptr const& img1);
00449 
00450   //: negate an image returning the same pixel type (only greyscale)
00451   static vil_image_resource_sptr negate(vil_image_resource_sptr const& imgr);
00452  private:
00453 
00454   //: find if the center pixel of a neighborhood is the maximum value
00455   static bool local_maximum(vbl_array_2d<float> const& nighborhood,
00456                             int n, float& value);
00457 
00458   //: find the sub-pixel offset to the maximum using a 3x3 quad interpolation
00459   static void interpolate_center(vbl_array_2d<float> const& neighborhood,
00460                                  float& dx, float& dy);
00461 
00462   //: sub-sample a 1-d array using the Bert-Adelson algorithm
00463   static void half_resolution_1d(const float* input, unsigned n,
00464                                  float k0, float k1, float k2, float* output);
00465 
00466   //: interpolate a 1-d array using the Bert-Adelson algorithm
00467 static void double_resolution_1d(const float* input, const unsigned n_input,
00468                                  const float k0, const float k1, const float k2,
00469                                  float* output);
00470 
00471   //: One dimensional fft
00472   static bool fft_1d(int dir, int m, double* x, double* y);
00473 
00474   //: Two dimensonal fft
00475   static bool fft_2d(vnl_matrix<vcl_complex<double> >& c, int nx,int ny,int dir);
00476 
00477   //: Transform the fft coefficients from/to fft/frequency order(self inverse).
00478   static void ftt_fourier_2d_reorder(vnl_matrix<vcl_complex<double> > const& F1,
00479                                      vnl_matrix<vcl_complex<double> >& F2);
00480 
00481   //: Blocking filter function
00482   static float gaussian_blocking_filter(float dir_fx, float dir_fy,
00483                                         float f0, float radius,
00484                                         float fx, float fy);
00485 #if 0 // TODO ?
00486   //: converting IHS to RGB
00487   static void
00488     convert_IHS_as_RGB(vil_image_view<float> const& I,
00489                        vil_image_view<float> const& H,
00490                        vil_image_view<float> const& S,
00491                        vil_image_view<vil_rgb<vxl_byte> >& image);
00492 #endif // 0
00493 
00494 
00495   //: Default constructor is private
00496   brip_vil_float_ops() {}
00497 };
00498 
00499 #endif // brip_vil_float_ops_h_

Generated on Thu Jan 10 14:52:31 2008 for contrib/brl/bseg/brip by  doxygen 1.4.4