resize.cpp
2.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include <opencv2/imgproc/imgproc.hpp>
#include <openbr/plugins/openbr_internal.h>
using namespace cv;
namespace br
{
/*!
* \ingroup transforms
* \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
{
Q_OBJECT
Q_ENUMS(Method)
public:
/*!< */
enum Method { Near = INTER_NEAREST,
Area = INTER_AREA,
Bilin = INTER_LINEAR,
Cubic = INTER_CUBIC,
Lanczo = INTER_LANCZOS4};
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
{
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)));
}
}
}
};
BR_REGISTER(Transform, ResizeTransform)
} // namespace br
#include "resize.moc"