31 static const int prime = 307189;
36 processed_original_image = 0;
38 smoothed_generated_object_view = 0;
39 orientation_corrector = 0;
41 white_noise =
new char[
prime];
42 limited_white_noise =
new char[
prime];
46 local_keypoint_array =
new object_keypoint[affine_image_generator::max_point_number];
51 if (original_image != 0)
52 cvReleaseImage(&original_image);
53 if (processed_original_image != 0)
54 cvReleaseImage(&processed_original_image);
55 if (affine_image != 0)
56 cvReleaseImage(&affine_image);
57 if (smoothed_generated_object_view != 0)
58 delete smoothed_generated_object_view;
59 if (orientation_corrector != 0)
60 delete orientation_corrector;
61 delete[] local_keypoint_array;
63 if (white_noise)
delete[] white_noise;
64 if (limited_white_noise)
delete[] limited_white_noise;
69 localization_noise = 0;
73 set_range_variation_for_theta(0,
float(2.*3.1416));
74 set_range_variation_for_phi(0, 3.1516f);
75 independent_scaling(0.5, 1.5, 0.5, 1.5);
78 set_use_orientation_correction(
true);
80 set_use_random_background(
false);
84 set_gaussian_smoothing_kernel_size(3);
86 set_add_gaussian_smoothing(
false);
87 set_change_intensities(
false);
92 this->noise_level = noise_level;
94 index_white_noise = 0;
95 for(
int i = 0; i <
prime; i++)
97 limited_white_noise[i] = char(
rand() % (2 * noise_level) - noise_level);
98 white_noise[i] = char(
rand() % 256);
103 int _u_corner1,
int _v_corner1,
int _u_corner2,
int _v_corner2,
104 int _u_corner3,
int _v_corner3,
int _u_corner4,
int _v_corner4,
105 int affine_image_width,
int affine_image_height)
107 if (original_image != 0)
108 cvReleaseImage(&original_image);
109 original_image = cvCloneImage(p_original_image);
111 if (processed_original_image != 0)
112 cvReleaseImage(&processed_original_image);
113 processed_original_image = cvCloneImage(p_original_image);
115 if (affine_image != 0)
116 cvReleaseImage(&affine_image);
117 if (affine_image_width < 0)
118 affine_image = cvCloneImage(p_original_image);
120 affine_image = cvCreateImage(cvSize(affine_image_width, affine_image_height), IPL_DEPTH_8U, 1);
122 if (smoothed_generated_object_view != 0)
123 delete smoothed_generated_object_view;
124 smoothed_generated_object_view =
new object_view(original_image->width, original_image->height, level_number);
126 if (orientation_corrector != 0)
127 delete orientation_corrector;
129 patch_size, level_number);
131 set_roi(_u_corner1, _v_corner1,
132 _u_corner2, _v_corner2,
133 _u_corner3, _v_corner3,
134 _u_corner4, _v_corner4);
140 if (original_image != 0)
141 cvReleaseImage(&original_image);
142 original_image = cvCloneImage(preprocessed);
144 if (processed_original_image != 0)
145 cvReleaseImage(&processed_original_image);
146 processed_original_image = cvCloneImage(preprocessed);
148 if (affine_image != 0)
149 cvReleaseImage(&affine_image);
150 affine_image = cvCloneImage(preprocessed);
152 if (smoothed_generated_object_view != 0)
153 delete smoothed_generated_object_view;
154 smoothed_generated_object_view =
new object_view(preprocessed->width, preprocessed->height, level_number);
156 if (orientation_corrector != 0)
157 delete orientation_corrector;
159 patch_size, level_number);
161 u_corner1 = 0; v_corner1 = 0;
162 u_corner2 = preprocessed->width - 1; v_corner2 = 0;
163 u_corner3 = preprocessed->width - 1; v_corner3 = preprocessed->height - 1;
164 u_corner4 = 0; v_corner4 = preprocessed->height - 1;
169 used_keypoint_array = keypoint_array;
170 point_number = p_point_number;
175 used_keypoint_array = local_keypoint_array;
181 assert((
unsigned)point_number < 1000);
183 local_keypoint_array[point_number].M[0] = u;
184 local_keypoint_array[point_number].M[1] = v;
185 local_keypoint_array[point_number].scale = float(scale);
186 local_keypoint_array[point_number].class_index = class_index;
192 level_number = p_level_number;
194 if (affine_image != 0)
196 if (orientation_corrector != 0)
197 delete orientation_corrector;
199 patch_size, level_number);
201 if (smoothed_generated_object_view != 0)
202 delete smoothed_generated_object_view;
203 smoothed_generated_object_view =
new object_view(affine_image->width, affine_image->height, level_number);
209 patch_size = p_patch_size;
211 if (original_image != 0)
213 if (orientation_corrector != 0)
214 delete orientation_corrector;
217 patch_size, level_number);
222 int _u_corner2,
int _v_corner2,
223 int _u_corner3,
int _v_corner3,
224 int _u_corner4,
int _v_corner4)
228 u_corner1 = _u_corner1; v_corner1 = _v_corner1;
229 u_corner2 = _u_corner2; v_corner2 = _v_corner2;
230 u_corner3 = _u_corner3; v_corner3 = _v_corner3;
231 u_corner4 = _u_corner4; v_corner4 = _v_corner4;
235 u_corner1 = 0; v_corner1 = 0;
236 u_corner2 = original_image->width - 1; v_corner2 = 0;
237 u_corner3 = original_image->width - 1; v_corner3 = original_image->height - 1;
238 u_corner4 = 0; v_corner4 = original_image->height - 1;
241 cvSmooth(original_image, processed_original_image, CV_MEDIAN, 3, 3);
242 for(
int j = 0; j < processed_original_image->height; j++)
244 unsigned char * row =
mcvRow(processed_original_image, j,
unsigned char);
245 for(
int i = 0; i < processed_original_image->width; i++)
246 if (i <= u_corner1 || i <= u_corner4 || i >= u_corner2 || i >= u_corner3 ||
247 j <= v_corner1 || j <= v_corner2 || j >= v_corner3 || j >= v_corner4)
250 if (row[i] == 128) row[i] = 127;
254 static int min4(
int a,
int b,
int c,
int d)
256 return MIN(a, MIN(b, MIN(c,d)));
259 static int max4(
int a,
int b,
int c,
int d)
261 return MAX(a, MAX(b, MAX(c,d)));
264 static float min4(
float a,
float b,
float c,
float d)
266 return MIN(a, MIN(b, MIN(c,d)));
269 static float max4(
float a,
float b,
float c,
float d)
271 return MAX(a, MAX(b, MAX(c,d)));
278 if (u - border_size <
min4(u_corner1, u_corner2, u_corner3, u_corner4))
return false;
279 if (v - border_size <
min4(v_corner1, v_corner2, v_corner3, v_corner4))
return false;
280 if (u + border_size >
max4(u_corner1, u_corner2, u_corner3, u_corner4))
return false;
281 if (v + border_size >
max4(v_corner1, v_corner2, v_corner3, v_corner4))
return false;
290 min_theta = p_min_theta;
291 max_theta = p_max_theta;
303 float p_min_lambda2,
float p_max_lambda2)
307 min_lambda1 = p_min_lambda1;
308 max_lambda1 = p_max_lambda1;
310 min_lambda2 = p_min_lambda2;
311 max_lambda2 = p_max_lambda2;
316 float p_min_lambda2,
float p_max_lambda2,
317 float p_min_l1_l2,
float p_max_l1_l2)
321 min_lambda1 = p_min_lambda1;
322 max_lambda1 = p_max_lambda1;
324 min_lambda2 = p_min_lambda2;
325 max_lambda2 = p_max_lambda2;
327 min_l1_l2 = p_min_l1_l2;
328 max_l1_l2 = p_max_l1_l2;
334 float theta = min_theta +
rand_01() * (max_theta - min_theta);
335 float phi = min_phi +
rand_01() * (max_phi - min_phi);
336 float lambda1, lambda2;
337 int image_width = affine_image->width;
338 int image_height = affine_image->height;
340 if (scaling_method == 0)
342 lambda1 = min_lambda1 +
rand_01() * (max_lambda1 - min_lambda1);
343 lambda2 = min_lambda2 +
rand_01() * (max_lambda2 - min_lambda2);
349 lambda1 = min_lambda1 +
rand_01() * (max_lambda1 - min_lambda1);
350 lambda2 = min_lambda2 +
rand_01() * (max_lambda2 - min_lambda2);
351 }
while (lambda1 * lambda2 < min_l1_l2 || lambda1 * lambda2 > max_l1_l2);
355 image_width / 2, image_height / 2,
360 float nu0, nv0, nu1, nv1, nu2, nv2, nu3, nv3;
362 affine_transformation(a, image_width, image_height,
float(u_corner1),
float(v_corner1), nu0, nv0);
363 affine_transformation(a, image_width, image_height,
float(u_corner2),
float(v_corner2), nu1, nv1);
364 affine_transformation(a, image_width, image_height,
float(u_corner3),
float(v_corner3), nu2, nv2);
365 affine_transformation(a, image_width, image_height,
float(u_corner4),
float(v_corner4), nu3, nv3);
369 Tx = - int(
min4(nu0, nu1, nu2, nu3)) + patch_size;
371 Tx = image_width - int(
max4(nu0, nu1, nu2, nu3)) - patch_size;
374 Ty = -int(
min4(nv0, nv1, nv2, nv3)) + patch_size;
376 Ty = image_height - int(
max4(nv0, nv1, nv2, nv3)) - patch_size;
379 image_width / 2, image_height / 2,
382 float(Tx),
float(Ty));
387 int image_width = affine_image->width;
388 int image_height = affine_image->height;
391 image_width / 2, image_height / 2,
399 float & nu,
float & nv)
401 float det = p_a[0] * p_a[4] - p_a[3] * p_a[1];
403 nu = width / 2 + 1.f / det * ( p_a[4] * (u - p_a[2]) - p_a[1] * (v - p_a[5]));
404 nv = height / 2 + 1.f / det * (-p_a[3] * (u - p_a[2]) + p_a[0] * (v - p_a[5]));
409 float & nu,
float & nv)
411 nu = p_a[0] * (u - width / 2) + p_a[1] * (v - height / 2) + p_a[2];
412 nv = p_a[3] * (u - width / 2) + p_a[4] * (v - height / 2) + p_a[5];
417 affine_transformation(a, affine_image->width, affine_image->height, u, v, nu, nv);
422 inverse_affine_transformation(a, affine_image->width, affine_image->height, u, v, nu, nv);
427 if (use_orientation_correction)
433 orientation_corrector->estimate_orientation_in_radians(ov->
image[s],
439 orientation_corrector->correct_orientationf(ov->
image[s],
454 for(
int y = 0; y < image->height; y++)
456 unsigned char * line = (
unsigned char *)(image->imageData + y * image->widthStep);
458 for(
int x = 0; x < image->width; x++)
462 p += limited_white_noise[index_white_noise];
469 line[x] = (
unsigned char)p;
472 if (index_white_noise >=
prime) index_white_noise = 1 +
rand() % 6;
479 for(
int y = 0; y < image->height; y++)
481 unsigned char * row =
mcvRow(image, y,
unsigned char);
483 for(
int x = 0; x < image->width; x++)
484 if (
int(row[x]) == value)
486 row[x] = white_noise[index_white_noise];
488 if (index_white_noise >=
prime) index_white_noise = 1 +
rand() % 6;
495 CvMat A = cvMat(2, 3, CV_32F, a);
497 cvSet(affine_image, cvScalar(128));
500 cvGetQuadrangleSubPix(processed_original_image, affine_image, &A);
502 float w = -(affine_image->width-1)/2;
503 float h = -(affine_image->height-1)/2;
505 memcpy(sa,a,9*
sizeof(
float));
506 a[2] += w*a[0] +h*a[1];
507 a[5] += w*a[3] +h*a[4];
508 cvWarpAffine(processed_original_image, affine_image, &A,CV_INTER_LINEAR+CV_WARP_INVERSE_MAP);
509 memcpy(a,sa,9*
sizeof(
float));
512 if (use_random_background)
513 replace_by_noise(affine_image, 128);
517 if (add_gaussian_smoothing)
521 int aperture = 3 + 2 * (
rand() % 3);
522 cvSmooth(affine_image, affine_image, CV_GAUSSIAN, aperture, aperture);
525 if (change_intensities)
526 cvCvtScale(affine_image, affine_image,
rand(0.8f, 1.2f),
rand(-10.f, 10.f));
529 add_white_noise(affine_image);
542 generate_affine_image();
544 smoothed_generated_object_view->build(affine_image, gaussian_smoothing_kernel_size);
549 generate_random_affine_transformation();
550 generate_object_view();
552 vector<image_class_example *> * result =
new vector<image_class_example *>;
554 for(
int i = 0; i < point_number; i++)
557 int scale = int(kp->
scale);
558 IplImage * im = smoothed_generated_object_view->image[scale];
568 if ((
int)nu > patch_size && (
int)nu < im->width - patch_size &&
569 (
int)nv > patch_size && (
int)nv < im->height - patch_size)
572 nu, nv, (
float)scale,
573 smoothed_generated_object_view,
578 preprocess_point_view(pv, smoothed_generated_object_view);
580 result->push_back(pv);
581 point_views.push_back(pv);
590 for(vector<object_view *>::iterator ov_it = generated_object_views_for_learning.begin();
591 ov_it < generated_object_views_for_learning.end();
597 generated_object_views_for_learning.clear();
599 for(vector<image_class_example *>::iterator pv_it = point_views.begin();
600 pv_it < point_views.end();