diff --git a/openbr/plugins/crop.cpp b/openbr/plugins/crop.cpp index cc0c359..01b5167 100644 --- a/openbr/plugins/crop.cpp +++ b/openbr/plugins/crop.cpp @@ -113,6 +113,8 @@ BR_REGISTER(Transform, ROIFromPtsTransform) * \brief Resize the template * \author Josh Klontz \cite jklontz * \note Method: Area should be used for shrinking an image, Cubic for slow but accurate enlargment, Bilin for fast enlargement. + * \param preserveAspect If true, the image will be sized per specification, but + * a border will be applied to preserve aspect ratio. */ class ResizeTransform : public UntrainableTransform { @@ -131,13 +133,32 @@ private: Q_PROPERTY(int rows READ get_rows WRITE set_rows RESET reset_rows STORED false) Q_PROPERTY(int columns READ get_columns WRITE set_columns RESET reset_columns STORED false) Q_PROPERTY(Method method READ get_method WRITE set_method RESET reset_method STORED false) + Q_PROPERTY(bool preserveAspect READ get_preserveAspect WRITE set_preserveAspect RESET reset_preserveAspect STORED false) BR_PROPERTY(int, rows, -1) BR_PROPERTY(int, columns, -1) BR_PROPERTY(Method, method, Bilin) + BR_PROPERTY(bool, preserveAspect, false) void project(const Template &src, Template &dst) const { - resize(src, dst, Size((columns == -1) ? src.m().cols*rows/src.m().rows : columns, rows), 0, 0, method); + if (!preserveAspect) + resize(src, dst, Size((columns == -1) ? src.m().cols*rows/src.m().rows : columns, rows), 0, 0, method); + else { + float inRatio = (float) src.m().rows / src.m().cols; + float outRatio = (float) rows / columns; + dst = Mat::zeros(rows, columns, src.m().type()); + if (outRatio > inRatio) { + float heightAR = src.m().rows * inRatio / outRatio; + Mat buffer; + resize(src, buffer, Size(columns, heightAR), 0, 0, method); + buffer.copyTo(dst.m()(Rect(0, (rows - heightAR) / 2, columns, heightAR))); + } else { + float widthAR = src.m().cols / inRatio * outRatio; + Mat buffer; + resize(src, buffer, Size(widthAR, rows), 0, 0, method); + buffer.copyTo(dst.m()(Rect((columns - widthAR) / 2, 0, widthAR, rows))); + } + } } };