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/vgl_box_2d.h>
00027 #include <vgl/vgl_polygon.h>
00028 #include <vgl/algo/vgl_h_matrix_2d.h>
00029 #include <vil/vil_image_resource.h>
00030 #include <vil/vil_image_view.h>
00031 #include <vil/vil_rgb.h>
00032 #include <vsol/vsol_box_2d_sptr.h>
00033 #include <brip/brip_roi_sptr.h>
00034
00035 class brip_vil_float_ops
00036 {
00037 public:
00038 ~brip_vil_float_ops() {}
00039
00040
00041 static vil_image_view<float>
00042 convolve(vil_image_view<float> const& input,
00043 vbl_array_2d<float> const& kernel);
00044
00045
00046 static unsigned gaussian_radius(const double sigma, const double fuzz=0.02);
00047
00048
00049 static vil_image_view<float> gaussian(vil_image_view<float> const& input,
00050 float sigma,
00051 float fill = 0.0f);
00052
00053 #ifdef VIL_CONVOLVE_WITH_MASK_EXISTS // TODO
00054
00055 static vil_image_view<float> gaussian(vil_image_view<float> const& input,
00056 float sigma,
00057 vil_image_view<float> mask);
00058 #endif
00059
00060
00061 static void non_maximum_suppression(vil_image_view<float> const& input,
00062 int n, float thresh,
00063 vcl_vector<float>& x_pos,
00064 vcl_vector<float>& y_pos,
00065 vcl_vector<float>& value);
00066
00067
00068 static vil_image_view<float>
00069 half_resolution(vil_image_view<float> const& input,
00070 float filter_coef=0.359375f);
00071
00072
00073 static vil_image_view<float>
00074 double_resolution(vil_image_view<float> const& input,
00075 float filter_coef=0.6f);
00076
00077
00078 static vil_image_view<float>
00079 difference(vil_image_view<float> const& image_1,
00080 vil_image_view<float> const& image_2);
00081
00082
00083 static vil_image_view<float>
00084 threshold(vil_image_view<float> const & image,
00085 const float thresh, const float level = 255.0);
00086
00087
00088 static vil_image_view<float>
00089 abs_clip_to_level(vil_image_view<float> const & image,
00090 const float thresh, const float level = 0.0);
00091
00092 static vil_image_view<float>
00093 average_NxN(vil_image_view<float> const & img, int N);
00094
00095
00096 static void gradient_3x3(vil_image_view<float> const& input,
00097 vil_image_view<float>& grad_x,
00098 vil_image_view<float>& grad_y);
00099
00100 static void gradient_mag_3x3(vil_image_view<float> const& input,
00101 vil_image_view<float>& mag);
00102
00103 static void hessian_3x3(vil_image_view<float> const& input,
00104 vil_image_view<float>& Ixx,
00105 vil_image_view<float>& Ixy,
00106 vil_image_view<float>& Iyy);
00107
00108 static vil_image_view<float>
00109 beaudet(vil_image_view<float> const& Ixx,
00110 vil_image_view<float> const& Ixy,
00111 vil_image_view<float> const& Iyy,
00112 bool determinant = true);
00113
00114 static void extrema_kernel_mask(float lambda0, float lambda1, float theta,
00115 vbl_array_2d<float>& kernel,
00116 vbl_array_2d<bool>& mask,
00117 float cutoff_percentage = 0.01f);
00118
00119
00120
00121 static vil_image_view<float>
00122 std_dev_operator(vil_image_view<float> const& sd_image,
00123 vbl_array_2d<float> const& kernel);
00124
00125
00126
00127 static vil_image_view<float>
00128 std_dev_operator_method2(vil_image_view<float> const& sd_image,
00129 vbl_array_2d<float> const& kernel);
00130
00131
00132 static float extrema_revert_angle(float angle);
00133
00134
00135 static vil_image_view<float> extrema(vil_image_view<float> const& input,
00136 float lambda0, float lambda1,
00137 float theta, bool bright = true,
00138 bool output_response_mask = true,
00139 bool unclipped_response = false);
00140
00141
00142
00143
00144 static vil_image_view<float> extrema_rotational(vil_image_view<float> const& input,
00145 float lambda0, float lambda1,
00146 float theta_interval, bool bright = true);
00147
00148
00149 static void max_inscribed_rect(float lambda0, float lambda1, float theta,
00150 float& u_rect, float& v_rect);
00151
00152
00153
00154 static vil_image_view<float> fast_extrema(vil_image_view<float> const& input,
00155 float lambda0, float lambda1,
00156 float theta, bool bright = true,
00157 bool output_response_mask = true,
00158 bool unclipped_response = false,
00159 float cutoff_percentage = 0.01f);
00160
00161
00162 static void grad_matrix_NxN(vil_image_view<float> const& input, unsigned n,
00163 vil_image_view<float>& IxIx,
00164 vil_image_view<float>& IxIy,
00165 vil_image_view<float>& IyIy);
00166
00167
00168 static vil_image_view<float>
00169 trace_grad_matrix_NxN(vil_image_view<float> const& input, unsigned n);
00170
00171
00172 static vil_image_view<float> harris(vil_image_view<float> const& IxIx,
00173 vil_image_view<float> const& IxIy,
00174 vil_image_view<float> const& IyIy,
00175 double scale=0.04);
00176
00177
00178 static vil_image_view<float>
00179 sqrt_grad_singular_values(vil_image_view<float>& input, int n);
00180
00181 static vil_image_view<float> max_scale_trace(vil_image_view<float> input,
00182 float min_scale,
00183 float max_scale,
00184 float scale_inc);
00185
00186 static vil_image_view<float> max_scale_trace_value(vil_image_view<float> input,
00187 float min_scale,
00188 float max_scale,
00189 float scale_inc);
00190
00191
00192 static void Lucas_KanadeMotion(vil_image_view<float>& current_frame,
00193 vil_image_view<float>& previous_frame,
00194 int n, double thresh,
00195 vil_image_view<float>& vx,
00196 vil_image_view<float>& vy);
00197
00198
00199 static void
00200 lucas_kanade_motion_on_view(vil_image_view<float> const& curr_frame,
00201 vil_image_view<float> const& prev_frame,
00202 const double thresh,
00203 float& vx,
00204 float& vy);
00205
00206
00207 static void
00208 velocity_by_correlation(vil_image_view<float> const& curr_image,
00209 vil_image_view<float> const& prev_region,
00210 const unsigned start_i, const unsigned end_i,
00211 const unsigned start_j, const unsigned end_j,
00212 const unsigned zero_i, const unsigned zero_j,
00213 float& vx,
00214 float& vy);
00215
00216
00217 static int Horn_SchunckMotion(vil_image_view<float> const & current_frame,
00218 vil_image_view<float> const& previous_frame,
00219 vil_image_view<float>& vx,
00220 vil_image_view<float>& vy,
00221 const float alpha_coef=10000.0f,
00222 const int no_of_iterations=5);
00223
00224
00225 static void fill_x_border(vil_image_view<float>& image, unsigned w, float value);
00226
00227
00228 static void fill_y_border(vil_image_view<float>& image, unsigned h, float value);
00229
00230
00231 static vil_image_view<unsigned char>
00232 convert_to_byte(vil_image_view<float> const& image);
00233
00234
00235 static vil_image_view<unsigned char>
00236 convert_to_byte(vil_image_view<float> const& image,
00237 float min_val, float max_val);
00238
00239
00240 static vil_image_view<unsigned char>
00241 convert_to_byte(vil_image_view<unsigned short> const& image,
00242 unsigned short min_val, unsigned short max_val);
00243
00244
00245 static vil_image_view<unsigned char>
00246 convert_to_byte(vil_image_resource_sptr const& image);
00247
00248
00249 static vil_image_view<unsigned short>
00250 convert_to_short(vil_image_view<float> const& image,
00251 float min_val, float max_val);
00252
00253
00254 static vil_image_view<unsigned short>
00255 convert_to_short(vil_image_view<float> const& image);
00256
00257
00258 static vil_image_view<unsigned short>
00259 convert_to_short(vil_image_resource_sptr const& image);
00260
00261
00262 static vil_image_view<float>
00263 convert_to_float(vil_image_resource const& image);
00264
00265
00266 static vil_image_view<float>
00267 convert_to_float(vil_image_resource_sptr const& image)
00268 { return brip_vil_float_ops::convert_to_float(*image); }
00269
00270 static vil_image_view<float>
00271 convert_to_float(vil_image_view<unsigned char> const& image);
00272
00273 static vil_image_view<float>
00274 convert_to_float(vil_image_view<unsigned short> const& image);
00275
00276
00277 static vil_image_view<bool>
00278 convert_to_bool(vil_image_view<unsigned char> const& image);
00279
00280
00281 static vil_image_view<float>
00282 convert_to_float(vil_image_view<vil_rgb<vxl_byte> > const& image);
00283
00284
00285 static void
00286 convert_to_IHS(vil_image_view<vil_rgb<vxl_byte> > const& image,
00287 vil_image_view<float>& I,
00288 vil_image_view<float>& H,
00289 vil_image_view<float>& S);
00290 static void
00291 convert_to_IHS(vil_image_view<unsigned char> const& image,
00292 vil_image_view<float>& I,
00293 vil_image_view<float>& H,
00294 vil_image_view<float>& S);
00295
00296
00297 static void
00298 display_IHS_as_RGB(vil_image_view<float> const& I,
00299 vil_image_view<float> const& H,
00300 vil_image_view<float> const& S,
00301 vil_image_view<vil_rgb<vxl_byte> >& image);
00302
00303
00304
00305 static vil_image_view<vil_rgb<vxl_byte> >
00306 combine_color_planes(vil_image_view<unsigned char> const& R,
00307 vil_image_view<unsigned char> const& G,
00308 vil_image_view<unsigned char> const& B);
00309
00310
00311
00312 static vil_image_view<vil_rgb<vxl_byte> >
00313 combine_color_planes(vil_image_resource_sptr const& R,
00314 vil_image_resource_sptr const& G,
00315 vil_image_resource_sptr const& B);
00316
00317
00318 static vil_image_view<unsigned char>
00319 convert_to_grey(vil_image_resource const& img);
00320
00321
00322 static vbl_array_2d<float> load_kernel(vcl_string const& file);
00323
00324
00325 static void basis_images(vcl_vector<vil_image_view<float> > const& input_images,
00326 vcl_vector<vil_image_view<float> >& basis);
00327
00328
00329 static bool fourier_transform(vil_image_view<float> const& input,
00330 vil_image_view<float>& mag,
00331 vil_image_view<float>& phase);
00332
00333
00334 static bool inverse_fourier_transform(vil_image_view<float> const& mag,
00335 vil_image_view<float> const& phase,
00336 vil_image_view<float>& output);
00337
00338
00339 static void resize(vil_image_view<float> const& input,
00340 unsigned width, unsigned height,
00341 vil_image_view<float>& output);
00342
00343
00344 static bool
00345 resize_to_power_of_two(vil_image_view<float> const& input,
00346 vil_image_view<float>& output);
00347
00348
00349 static bool
00350 spatial_frequency_filter(vil_image_view<float> const& input,
00351 float dir_fx, float dir_fy,
00352 float f0, float radius,
00353 bool output_fourier_mag,
00354 vil_image_view<float>& output);
00355
00356
00357 static double
00358 bilinear_interpolation(vil_image_view<float> const& input,
00359 double x, double y);
00360
00361
00362
00363
00364 static bool homography(vil_image_view<float> const& input,
00365 vgl_h_matrix_2d<double>const& H,
00366 vil_image_view<float>& output,
00367 bool output_size_fixed = false,
00368 float output_fill_value = 0.0f);
00369
00370
00371 static vil_image_view<float> rotate(vil_image_view<float> const& input,
00372 double theta_deg);
00373
00374
00375 static bool chip(vil_image_view<float> const& input,
00376 vsol_box_2d_sptr const& roi, vil_image_view<float>& chip);
00377
00378
00379 static bool chip(vil_image_resource_sptr const& image,
00380 brip_roi_sptr const& roi,
00381 vil_image_resource_sptr & chip);
00382
00383
00384 static float average_in_box(vil_image_view<float> const& v,
00385 vgl_box_2d<double> const& box);
00386
00387
00388 static vcl_vector<float> scan_region(vil_image_resource_sptr img,
00389 vgl_polygon<double> poly,
00390 float& min,
00391 float& max);
00392
00393 static float cross_correlate(vil_image_view<float> const& image1,
00394 vil_image_view<float> const& image2,
00395 float x, float y, int radius = 5,
00396 float intensity_thresh=25.0f);
00397
00398
00399 static bool cross_correlate(vil_image_view<float> const& image1,
00400 vil_image_view<float> const& image2,
00401 vil_image_view<float>& out,
00402 int radius = 5, float intensity_thresh=25.0f);
00403
00404
00405 static void ihs_to_rgb(vil_rgb<vxl_byte>& rgb,
00406 const float i, const float h, const float s);
00407
00408
00409 static void rgb_to_ihs(vil_rgb<vxl_byte> const& rgb,
00410 float& i, float& h, float& s);
00411
00412
00413
00414 static float entropy_i(const unsigned i, const unsigned j,
00415 const unsigned i_radius,
00416 const unsigned j_radius,
00417 vil_image_view<float> const& intensity,
00418 const float range = 255.0, const unsigned bins = 16);
00419
00420
00421
00422 static float entropy_g(const unsigned i, const unsigned j,
00423 const unsigned i_radius,
00424 const unsigned j_radius,
00425 vil_image_view<float> const& gradx,
00426 vil_image_view<float> const& grady,
00427 const float range = 360.0, const unsigned bins = 8);
00428
00429
00430
00431 static float entropy_hs(const unsigned i, const unsigned j,
00432 const unsigned i_radius,
00433 const unsigned j_radius,
00434 vil_image_view<float> const& hue,
00435 vil_image_view<float> const& sat,
00436 const float range = 360.0, const unsigned bins = 8);
00437
00438
00439 static vil_image_view<float> entropy(const unsigned i_radius,
00440 const unsigned j_radius,
00441 const unsigned step,
00442 vil_image_resource_sptr const& img,
00443 const float sigma = 1.0f,
00444 const bool intensity = true,
00445 const bool gradient = true,
00446 const bool ihs = false);
00447
00448
00449 static float minfo_i(const unsigned i0, const unsigned j0,
00450 const unsigned i1, const unsigned j1,
00451 const unsigned i_radius,
00452 const unsigned j_radius,
00453 vil_image_view<float> const& intensity0,
00454 vil_image_view<float> const& intensity1,
00455 const float range = 255.0, const unsigned bins = 16);
00456
00457
00458
00459 static float minfo_g(const unsigned i0, const unsigned j0,
00460 const unsigned i1, const unsigned j1,
00461 const unsigned i_radius,
00462 const unsigned j_radius,
00463 vil_image_view<float> const& gradx0,
00464 vil_image_view<float> const& grady0,
00465 vil_image_view<float> const& gradx1,
00466 vil_image_view<float> const& grady1,
00467 const float range = 360.0, const unsigned bins = 8);
00468
00469
00470
00471 static float minfo_hs(const unsigned i0, const unsigned j0,
00472 const unsigned i1, const unsigned j1,
00473 const unsigned i_radius,
00474 const unsigned j_radius,
00475 vil_image_view<float> const& hue0,
00476 vil_image_view<float> const& sat0,
00477 vil_image_view<float> const& hue1,
00478 vil_image_view<float> const& sat1,
00479 const float range = 360.0, const unsigned bins = 8);
00480
00481
00482 static bool minfo(const unsigned i_radius,
00483 const unsigned j_radius,
00484 const unsigned step,
00485 vil_image_resource_sptr const& img0,
00486 vil_image_resource_sptr const& img1,
00487 vil_image_view<float>& MI0,
00488 vil_image_view<float>& MI1,
00489 const float sigma = 1.0f,
00490 const bool intensity = true,
00491 const bool gradient = true,
00492 const bool ihs = false);
00493
00494
00495
00496
00497 static vil_image_resource_sptr sum(vil_image_resource_sptr const& img0,
00498 vil_image_resource_sptr const& img1);
00499
00500
00501 static vil_image_resource_sptr difference(vil_image_resource_sptr const& img0,
00502 vil_image_resource_sptr const& img1);
00503
00504
00505 static vil_image_resource_sptr negate(vil_image_resource_sptr const& imgr);
00506
00507
00508 static vil_image_view<unsigned char>
00509 color_order(vil_image_view<float> const& color_image, float eq_tol);
00510
00511 private:
00512
00513
00514 static bool local_maximum(vbl_array_2d<float> const& nighborhood,
00515 int n, float& value);
00516
00517
00518 static void interpolate_center(vbl_array_2d<float> const& neighborhood,
00519 float& dx, float& dy);
00520
00521
00522 static void half_resolution_1d(const float* input, unsigned n,
00523 float k0, float k1, float k2, float* output);
00524
00525
00526 static void double_resolution_1d(const float* input, const unsigned n_input,
00527 const float k0, const float k1, const float k2,
00528 float* output);
00529
00530
00531 static bool fft_1d(int dir, int m, double* x, double* y);
00532
00533
00534 static bool fft_2d(vnl_matrix<vcl_complex<double> >& c, int nx,int ny,int dir);
00535
00536
00537 static void ftt_fourier_2d_reorder(vnl_matrix<vcl_complex<double> > const& F1,
00538 vnl_matrix<vcl_complex<double> >& F2);
00539
00540
00541 static float gaussian_blocking_filter(float dir_fx, float dir_fy,
00542 float f0, float radius,
00543 float fx, float fy);
00544
00545 #if 0 // TODO ?
00546
00547 static void
00548 convert_IHS_as_RGB(vil_image_view<float> const& I,
00549 vil_image_view<float> const& H,
00550 vil_image_view<float> const& S,
00551 vil_image_view<vil_rgb<vxl_byte> >& image);
00552 #endif // 0
00553
00554
00555 static float elu(float phi, float lamda0, float lambda1, float theta);
00556
00557 static float elv(float phi, float lamda0, float lambda1, float theta);
00558
00559
00560 brip_vil_float_ops() {}
00561 };
00562
00563 #endif // brip_vil_float_ops_h_