00001
00002 #ifndef brip_vil_float_ops_h_
00003 #define brip_vil_float_ops_h_
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
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
00039 static vil_image_view<float>
00040 convolve(vil_image_view<float> const& input,
00041 vbl_array_2d<float> const& kernel);
00042
00043
00044 static unsigned gaussian_radius(const double sigma, const double fuzz=0.02);
00045
00046
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
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
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
00065 static vil_image_view<float>
00066 half_resolution(vil_image_view<float> const& input,
00067 float filter_coef=0.359375f);
00068
00069
00070
00071 static vil_image_view<float>
00072 double_resolution(vil_image_view<float> const& input,
00073 float filter_coef=0.6f);
00074
00075
00076
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
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
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
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
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
00116 static vil_image_view<float>
00117 trace_grad_matrix_NxN(vil_image_view<float> const& input, unsigned n);
00118
00119
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
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
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
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
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
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
00169 static void fill_x_border(vil_image_view<float>& image, unsigned w, float value);
00170
00171
00172
00173 static void fill_y_border(vil_image_view<float>& image, unsigned h, float value);
00174
00175
00176 static vil_image_view<unsigned char>
00177 convert_to_byte(vil_image_view<float> const& image);
00178
00179
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
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
00190 static vil_image_view<unsigned char>
00191 convert_to_byte(vil_image_resource_sptr const& image);
00192
00193
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
00198
00199 static vil_image_view<unsigned short>
00200 convert_to_short(vil_image_view<float> const& image);
00201
00202
00203 static vil_image_view<unsigned short>
00204 convert_to_short(vil_image_resource_sptr const& image);
00205
00206
00207 static vil_image_view<float>
00208 convert_to_float(vil_image_resource const& image);
00209
00210
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
00222 static vil_image_view<bool>
00223 convert_to_bool(vil_image_view<unsigned char> const& image);
00224
00225
00226 static vil_image_view<float>
00227 convert_to_float(vil_image_view<vil_rgb<vxl_byte> > const& image);
00228
00229
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
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
00249
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
00256
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
00264 static vil_image_view<unsigned char>
00265 convert_to_grey(vil_image_resource const& img);
00266
00267
00268 static vbl_array_2d<float> load_kernel(vcl_string const& file);
00269
00270
00271 static void basis_images(vcl_vector<vil_image_view<float> > const& input_images,
00272 vcl_vector<vil_image_view<float> >& basis);
00273
00274
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
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
00285 static void resize(vil_image_view<float> const& input,
00286 unsigned width, unsigned height,
00287 vil_image_view<float>& output);
00288
00289
00290 static bool
00291 resize_to_power_of_two(vil_image_view<float> const& input,
00292 vil_image_view<float>& output);
00293
00294
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
00303 static double
00304 bilinear_interpolation(vil_image_view<float> const& input,
00305 double x, double y);
00306
00307
00308
00309
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
00317 static vil_image_view<float> rotate(vil_image_view<float> const& input,
00318 double theta_deg);
00319
00320
00321 static bool chip(vil_image_view<float> const& input,
00322 vsol_box_2d_sptr const& roi, vil_image_view<float>& chip);
00323
00324
00325 static bool chip(vil_image_resource_sptr const& image,
00326 brip_roi_sptr const& roi,
00327 vil_image_resource_sptr & chip);
00328
00329
00330 static float average_in_box(vil_image_view<float> const& v,
00331 vgl_box_2d<double> const& box);
00332
00333
00334 static vcl_vector<float> scan_region(vil_image_resource_sptr img,
00335 vgl_polygon<double> poly,
00336 float& min,
00337 float& max);
00338
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
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
00351 static void ihs_to_rgb(vil_rgb<vxl_byte>& rgb,
00352 const float i, const float h, const float s);
00353
00354
00355 static void rgb_to_ihs(vil_rgb<vxl_byte> const& rgb,
00356 float& i, float& h, float& s);
00357
00358
00359
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
00367
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
00376
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
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
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
00404
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
00416
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
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
00441
00442
00443 static vil_image_resource_sptr sum(vil_image_resource_sptr const& img0,
00444 vil_image_resource_sptr const& img1);
00445
00446
00447 static vil_image_resource_sptr difference(vil_image_resource_sptr const& img0,
00448 vil_image_resource_sptr const& img1);
00449
00450
00451 static vil_image_resource_sptr negate(vil_image_resource_sptr const& imgr);
00452 private:
00453
00454
00455 static bool local_maximum(vbl_array_2d<float> const& nighborhood,
00456 int n, float& value);
00457
00458
00459 static void interpolate_center(vbl_array_2d<float> const& neighborhood,
00460 float& dx, float& dy);
00461
00462
00463 static void half_resolution_1d(const float* input, unsigned n,
00464 float k0, float k1, float k2, float* output);
00465
00466
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
00472 static bool fft_1d(int dir, int m, double* x, double* y);
00473
00474
00475 static bool fft_2d(vnl_matrix<vcl_complex<double> >& c, int nx,int ny,int dir);
00476
00477
00478 static void ftt_fourier_2d_reorder(vnl_matrix<vcl_complex<double> > const& F1,
00479 vnl_matrix<vcl_complex<double> >& F2);
00480
00481
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
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
00496 brip_vil_float_ops() {}
00497 };
00498
00499 #endif // brip_vil_float_ops_h_