00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #include <fvutils/base/roi.h>
00025
00026 #include <cstdlib>
00027
00028 using namespace fawkes;
00029
00030 namespace firevision {
00031 #if 0
00032 }
00033 #endif
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047 ROI* ROI::roi_full_image = NULL;
00048
00049
00050
00051 ROI::ROI()
00052 {
00053 num_hint_points = 1;
00054 start.x = start.y = width = height = image_width = image_height = line_step = pixel_step = 0;
00055 }
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066 ROI::ROI(unsigned int start_x, unsigned int start_y,
00067 unsigned int width, unsigned int height,
00068 unsigned int image_width, unsigned int image_height)
00069 {
00070 num_hint_points = 1;
00071 start.x = start_x;
00072 start.y = start_y;
00073 this->width = width;
00074 this->height = height;
00075 this->image_width = image_width;
00076 this->image_height = image_height;
00077 line_step = image_width;
00078 pixel_step = 1;
00079 }
00080
00081
00082
00083
00084
00085 ROI::ROI(const ROI &roi)
00086 {
00087 start = roi.start;
00088 width = roi.width;
00089 height = roi.height;
00090 image_width = roi.image_width;
00091 image_height = roi.image_height;
00092 line_step = roi.line_step;
00093 pixel_step = roi.pixel_step;
00094 hint = roi.hint;
00095 num_hint_points = roi.num_hint_points;
00096 }
00097
00098
00099
00100
00101
00102 ROI::ROI(const ROI *roi)
00103 {
00104 start = roi->start;
00105 width = roi->width;
00106 height = roi->height;
00107 image_width = roi->image_width;
00108 image_height = roi->image_height;
00109 line_step = roi->line_step;
00110 pixel_step = roi->pixel_step;
00111 hint = roi->hint;
00112 num_hint_points = roi->num_hint_points;
00113 }
00114
00115
00116
00117
00118
00119 void
00120 ROI::set_start(point_t p)
00121 {
00122 start.x = p.x;
00123 start.y = p.y;
00124 }
00125
00126
00127
00128
00129
00130
00131 void
00132 ROI::set_start(unsigned int x, unsigned int y)
00133 {
00134 start.x = x;
00135 start.y = y;
00136 }
00137
00138
00139
00140
00141
00142 void
00143 ROI::set_width(unsigned int width)
00144 {
00145 this->width = width;
00146 }
00147
00148
00149
00150
00151
00152 unsigned int
00153 ROI::get_width() const
00154 {
00155 return width;
00156 }
00157
00158
00159
00160
00161
00162 void
00163 ROI::set_height(unsigned int height)
00164 {
00165 this->height = height;
00166 }
00167
00168
00169
00170
00171
00172 unsigned int
00173 ROI::get_height() const
00174 {
00175 return height;
00176 }
00177
00178
00179
00180
00181
00182
00183 void
00184 ROI::set_image_width(unsigned int image_width)
00185 {
00186 this->image_width = image_width;
00187 }
00188
00189
00190
00191
00192
00193
00194 unsigned int
00195 ROI::get_image_width() const
00196 {
00197 return image_width;
00198 }
00199
00200
00201
00202
00203
00204
00205 void
00206 ROI::set_image_height(unsigned int image_height)
00207 {
00208 this->image_height = image_height;
00209 }
00210
00211
00212
00213
00214
00215
00216 unsigned int
00217 ROI::get_image_height() const
00218 {
00219 return image_height;
00220 }
00221
00222
00223
00224
00225
00226
00227
00228 void
00229 ROI::set_line_step(unsigned int step)
00230 {
00231 line_step = step;
00232 }
00233
00234
00235
00236
00237
00238
00239 unsigned int
00240 ROI::get_line_step() const
00241 {
00242 return line_step;
00243 }
00244
00245
00246
00247
00248
00249
00250
00251 void
00252 ROI::set_pixel_step(unsigned int step)
00253 {
00254 pixel_step = step;
00255 }
00256
00257
00258
00259
00260
00261
00262 unsigned int
00263 ROI::get_pixel_step() const
00264 {
00265 return pixel_step;
00266 }
00267
00268
00269
00270
00271
00272
00273
00274 hint_t
00275 ROI::get_hint() const
00276 {
00277 return hint;
00278 }
00279
00280
00281
00282
00283
00284
00285 void
00286 ROI::set_hint(hint_t hint)
00287 {
00288 this->hint = hint;
00289 }
00290
00291
00292
00293
00294
00295
00296
00297 bool
00298 ROI::contains(unsigned int x, unsigned int y)
00299 {
00300 if ( (x >= start.x) &&
00301 (x <= start.x+width) &&
00302 (y >= start.y) &&
00303 (y <= start.y+height) ) {
00304
00305 num_hint_points += 1;
00306 return true;
00307 } else {
00308 return false;
00309 }
00310 }
00311
00312
00313
00314
00315
00316
00317
00318
00319
00320
00321 bool
00322 ROI::neighbours(unsigned int x, unsigned int y, unsigned int margin) const
00323 {
00324 return ( (static_cast<int>(x) >= static_cast<int>(start.x) - static_cast<int>(margin)) &&
00325 (x <= start.x + width + margin) &&
00326 (static_cast<int>(y) >= static_cast<int>(start.y) - static_cast<int>(margin)) &&
00327 (y <= start.y + height + margin) );
00328 }
00329
00330
00331
00332
00333
00334
00335
00336
00337
00338 bool
00339 ROI::neighbours(ROI *roi, unsigned int margin) const
00340 {
00341
00342 bool overlapping_x = neighbours(roi->start.x, start.y, margin)
00343 || neighbours(roi->start.x + roi->width, start.y, margin)
00344 || roi->neighbours(start.x, roi->start.y, margin)
00345 || roi->neighbours(start.x + width, roi->start.y, margin);
00346
00347
00348 bool overlapping_y = roi->neighbours(roi->start.x, start.y, margin)
00349 || roi->neighbours(roi->start.x, start.y + height, margin)
00350 || neighbours(start.x, roi->start.y, margin)
00351 || neighbours(start.x, roi->start.y + roi->height, margin);
00352
00353 return overlapping_x && overlapping_y;
00354 }
00355
00356
00357
00358
00359
00360
00361 void
00362 ROI::extend(unsigned int x, unsigned int y)
00363 {
00364
00365 if (x < start.x) { width += start.x - x; start.x = x; }
00366 if (y < start.y) { height += start.y - y; start.y = y; }
00367 if (x > start.x + width) { width += (x - (start.x + width)); }
00368 if (y > start.y + height) { height += (y - (start.y + height)); }
00369
00370 num_hint_points += 1;
00371 }
00372
00373
00374
00375
00376
00377 void
00378 ROI::grow(unsigned int margin)
00379 {
00380 if (start.x < margin) {
00381 start.x = 0;
00382 } else {
00383 start.x -= margin;
00384 }
00385
00386 if (start.y < margin) {
00387 start.y = 0;
00388 } else {
00389 start.y -= margin;
00390 }
00391
00392 if ((start.x + width + margin) > image_width) {
00393 width += (image_width - (start.x + width));
00394 } else {
00395 width += margin;
00396 }
00397
00398 if ((start.y + height + margin) > image_height) {
00399 height += (image_height - (start.y + height));
00400 } else {
00401 height += margin;
00402 }
00403
00404 }
00405
00406
00407
00408
00409
00410
00411
00412
00413 ROI&
00414 ROI::operator+=(ROI &roi)
00415 {
00416
00417 if (roi.start.x < start.x) { width += start.x - roi.start.x; start.x = roi.start.x; }
00418 if (roi.start.y < start.y) { height += start.y - roi.start.y; start.y = roi.start.y; }
00419 if (roi.start.x + roi.width > start.x + width) { width += roi.start.x + roi.width - (start.x + width); }
00420 if (roi.start.y + roi.height > start.y + height) { height += roi.start.y + roi.height - (start.y + height); }
00421
00422 num_hint_points += roi.num_hint_points;
00423
00424 return *this;
00425 }
00426
00427
00428
00429
00430
00431
00432 bool
00433 ROI::operator<(const ROI &roi) const
00434 {
00435 return (num_hint_points < roi.num_hint_points);
00436 }
00437
00438
00439
00440
00441
00442
00443 bool
00444 ROI::operator>(const ROI &roi) const
00445 {
00446 return (num_hint_points > roi.num_hint_points);
00447 }
00448
00449
00450
00451
00452
00453
00454
00455
00456 bool
00457 ROI::operator==(const ROI &roi) const
00458 {
00459 return (start.x == roi.start.x) &&
00460 (start.y == roi.start.y) &&
00461 (width == roi.width) &&
00462 (height == roi.height) &&
00463 (image_width == roi.image_width) &&
00464 (image_height == roi.image_height) &&
00465 (line_step == roi.line_step) &&
00466 (pixel_step == roi.pixel_step) &&
00467 (hint == roi.hint) &&
00468 (num_hint_points == roi.num_hint_points);
00469 }
00470
00471
00472
00473
00474
00475
00476
00477
00478 bool
00479 ROI::operator!=(const ROI &roi) const
00480 {
00481 return (num_hint_points != roi.num_hint_points);
00482 }
00483
00484
00485
00486
00487
00488
00489 ROI&
00490 ROI::operator=(const ROI &roi)
00491 {
00492 this->start.x = roi.start.x;
00493 this->start.y = roi.start.y;
00494 this->width = roi.width;
00495 this->height = roi.height;
00496 this->image_width = roi.image_width;
00497 this->image_height = roi.image_height;
00498 this->line_step = roi.line_step;
00499 this->pixel_step = roi.pixel_step;
00500 this->hint = roi.hint;
00501 this->num_hint_points = roi.num_hint_points;
00502
00503 return *this;
00504 }
00505
00506
00507
00508
00509
00510
00511
00512 unsigned char*
00513 ROI::get_roi_buffer_start(unsigned char *buffer) const
00514 {
00515 return (buffer + (start.y * line_step) + (start.x * pixel_step));
00516 }
00517
00518
00519
00520
00521
00522
00523
00524
00525
00526
00527
00528
00529
00530
00531
00532
00533
00534
00535
00536 unsigned int
00537 ROI::get_num_hint_points() const
00538 {
00539 return num_hint_points;
00540 }
00541
00542
00543
00544
00545
00546
00547
00548
00549
00550
00551
00552
00553 ROI *
00554 ROI::full_image(unsigned int width, unsigned int height)
00555 {
00556 if (roi_full_image == NULL) {
00557 roi_full_image = new ROI();
00558 roi_full_image->start.x = 0;
00559 roi_full_image->start.y = 0;
00560 roi_full_image->pixel_step = 1;
00561 }
00562 roi_full_image->width = width;
00563 roi_full_image->height = height;
00564 roi_full_image->image_width = roi_full_image->width;
00565 roi_full_image->image_height = roi_full_image->height;
00566 roi_full_image->line_step = roi_full_image->width;
00567
00568 return roi_full_image;
00569 }
00570
00571 }