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