Commit dfc6f1cff693d4f9c025a584a5c90f80babdc8ac
1 parent
eb20e848
Contraining bounding boxes to be a valid size when using roi
Showing
2 changed files
with
27 additions
and
7 deletions
openbr/plugins/crop.cpp
| @@ -49,6 +49,24 @@ class CropTransform : public UntrainableTransform | @@ -49,6 +49,24 @@ class CropTransform : public UntrainableTransform | ||
| 49 | 49 | ||
| 50 | BR_REGISTER(Transform, CropTransform) | 50 | BR_REGISTER(Transform, CropTransform) |
| 51 | 51 | ||
| 52 | + | ||
| 53 | +static QRectF constrainRect(int height, int width, const QRectF &rect) | ||
| 54 | +{ | ||
| 55 | + QRectF newRect = rect; | ||
| 56 | + | ||
| 57 | + // roi.x + roi.width <= m.cols && roi.y + roi.height <= m.rows | ||
| 58 | + | ||
| 59 | + if (rect.x() < 0) newRect.setX(0); | ||
| 60 | + if (rect.y() < 0) newRect.setY(0); | ||
| 61 | + | ||
| 62 | + if ((newRect.y() + rect.width()) > width) newRect.setWidth(width-newRect.y()); | ||
| 63 | + if ((newRect.x() + rect.height()) > height) newRect.setHeight(height-newRect.x()); | ||
| 64 | + | ||
| 65 | + qDebug() << ((newRect.x() + newRect.width()) <= width) << ((newRect.y() + newRect.height()) <= height); | ||
| 66 | + | ||
| 67 | + return newRect; | ||
| 68 | +} | ||
| 69 | + | ||
| 52 | /*! | 70 | /*! |
| 53 | * \ingroup transforms | 71 | * \ingroup transforms |
| 54 | * \brief Crops the rectangular regions of interest. | 72 | * \brief Crops the rectangular regions of interest. |
| @@ -63,11 +81,13 @@ class ROITransform : public UntrainableTransform | @@ -63,11 +81,13 @@ class ROITransform : public UntrainableTransform | ||
| 63 | void project(const Template &src, Template &dst) const | 81 | void project(const Template &src, Template &dst) const |
| 64 | { | 82 | { |
| 65 | if (!propName.isEmpty()) { | 83 | if (!propName.isEmpty()) { |
| 66 | - QRectF rect = src.file.get<QRectF>(propName); | 84 | + QRectF rect = constrainRect(src.m().rows, src.m().cols, src.file.get<QRectF>(propName)); |
| 67 | dst += src.m()(OpenCVUtils::toRect(rect)); | 85 | dst += src.m()(OpenCVUtils::toRect(rect)); |
| 68 | } else if (!src.file.rects().empty()) { | 86 | } else if (!src.file.rects().empty()) { |
| 69 | - foreach (const QRectF &rect, src.file.rects()) | ||
| 70 | - dst += src.m()(OpenCVUtils::toRect(rect)); | 87 | + foreach (const QRectF &rect, src.file.rects()) { |
| 88 | + QRectF newRect = constrainRect(src.m().rows, src.m().cols, rect); | ||
| 89 | + dst += src.m()(OpenCVUtils::toRect(newRect)); | ||
| 90 | + } | ||
| 71 | } else if (src.file.contains(QStringList() << "X" << "Y" << "Width" << "Height")) { | 91 | } else if (src.file.contains(QStringList() << "X" << "Y" << "Width" << "Height")) { |
| 72 | dst += src.m()(Rect(src.file.get<int>("X"), | 92 | dst += src.m()(Rect(src.file.get<int>("X"), |
| 73 | src.file.get<int>("Y"), | 93 | src.file.get<int>("Y"), |
openbr/plugins/landmarks.cpp
| @@ -372,7 +372,7 @@ BR_REGISTER(Transform, ReadLandmarksTransform) | @@ -372,7 +372,7 @@ BR_REGISTER(Transform, ReadLandmarksTransform) | ||
| 372 | * \brief Name a point/rect | 372 | * \brief Name a point/rect |
| 373 | * \author Scott Klum \cite sklum | 373 | * \author Scott Klum \cite sklum |
| 374 | */ | 374 | */ |
| 375 | -class NameLandmarkTransform : public UntrainableMetadataTransform | 375 | +class NameLandmarksTransform : public UntrainableMetadataTransform |
| 376 | { | 376 | { |
| 377 | Q_OBJECT | 377 | Q_OBJECT |
| 378 | Q_PROPERTY(bool point READ get_point WRITE set_point RESET reset_point STORED false) | 378 | Q_PROPERTY(bool point READ get_point WRITE set_point RESET reset_point STORED false) |
| @@ -406,14 +406,14 @@ class NameLandmarkTransform : public UntrainableMetadataTransform | @@ -406,14 +406,14 @@ class NameLandmarkTransform : public UntrainableMetadataTransform | ||
| 406 | } | 406 | } |
| 407 | }; | 407 | }; |
| 408 | 408 | ||
| 409 | -BR_REGISTER(Transform, NameLandmarkTransform) | 409 | +BR_REGISTER(Transform, NameLandmarksTransform) |
| 410 | 410 | ||
| 411 | /*! | 411 | /*! |
| 412 | * \ingroup transforms | 412 | * \ingroup transforms |
| 413 | * \brief Remove a name from a point/rect | 413 | * \brief Remove a name from a point/rect |
| 414 | * \author Scott Klum \cite sklum | 414 | * \author Scott Klum \cite sklum |
| 415 | */ | 415 | */ |
| 416 | -class AnonymizeLandmarkTransform : public UntrainableMetadataTransform | 416 | +class AnonymizeLandmarksTransform : public UntrainableMetadataTransform |
| 417 | { | 417 | { |
| 418 | Q_OBJECT | 418 | Q_OBJECT |
| 419 | Q_PROPERTY(bool point READ get_point WRITE set_point RESET reset_point STORED false) | 419 | Q_PROPERTY(bool point READ get_point WRITE set_point RESET reset_point STORED false) |
| @@ -435,7 +435,7 @@ class AnonymizeLandmarkTransform : public UntrainableMetadataTransform | @@ -435,7 +435,7 @@ class AnonymizeLandmarkTransform : public UntrainableMetadataTransform | ||
| 435 | } | 435 | } |
| 436 | }; | 436 | }; |
| 437 | 437 | ||
| 438 | -BR_REGISTER(Transform, AnonymizeLandmarkTransform) | 438 | +BR_REGISTER(Transform, AnonymizeLandmarksTransform) |
| 439 | 439 | ||
| 440 | } // namespace br | 440 | } // namespace br |
| 441 | 441 |