Commit 0c10fa8c369b7bd3381463103ffb342676b5a772
1 parent
0b967872
Transforms for building AFLW dataset
Showing
3 changed files
with
115 additions
and
3 deletions
openbr/plugins/imgproc/rndaffine.cpp
0 → 100644
| 1 | +#include <opencv2/imgproc/imgproc.hpp> | |
| 2 | + | |
| 3 | +#include <openbr/plugins/openbr_internal.h> | |
| 4 | +#include <openbr/core/opencvutils.h> | |
| 5 | + | |
| 6 | +using namespace cv; | |
| 7 | + | |
| 8 | +namespace br | |
| 9 | +{ | |
| 10 | + | |
| 11 | +class RndAffineTransform : public UntrainableMetaTransform | |
| 12 | +{ | |
| 13 | + Q_OBJECT | |
| 14 | + Q_PROPERTY(int numAffines READ get_numAffines WRITE set_numAffines RESET reset_numAffines STORED false) | |
| 15 | + Q_PROPERTY(int winWidth READ get_winWidth WRITE set_winWidth RESET reset_winWidth STORED false) | |
| 16 | + Q_PROPERTY(int winHeight READ get_winHeight WRITE set_winHeight RESET reset_winHeight STORED false) | |
| 17 | + Q_PROPERTY(float scaleFactor READ get_scaleFactor WRITE set_scaleFactor RESET reset_scaleFactor STORED false) | |
| 18 | + Q_PROPERTY(int maxAngle READ get_maxAngle WRITE set_maxAngle RESET reset_maxAngle STORED false) | |
| 19 | + BR_PROPERTY(int, numAffines, 0) | |
| 20 | + BR_PROPERTY(int, winWidth, 24) | |
| 21 | + BR_PROPERTY(int, winHeight, 24) | |
| 22 | + BR_PROPERTY(float, scaleFactor, 1.2) | |
| 23 | + BR_PROPERTY(int, maxAngle, 15) | |
| 24 | + | |
| 25 | + void project(const Template &src, Template &dst) const | |
| 26 | + { | |
| 27 | + TemplateList temp; | |
| 28 | + project(TemplateList() << src, temp); | |
| 29 | + if (!temp.isEmpty()) dst = temp.first(); | |
| 30 | + } | |
| 31 | + | |
| 32 | + void project(const TemplateList &src, TemplateList &dst) const | |
| 33 | + { | |
| 34 | + foreach (const Template &t, src) { | |
| 35 | + QPointF affine_0 = t.file.get<QPointF>("Affine_0"); | |
| 36 | + QPointF affine_1 = t.file.get<QPointF>("Affine_1"); | |
| 37 | + | |
| 38 | + // Append the original points | |
| 39 | + Template u = t; | |
| 40 | + u.file.setPoints(QList<QPointF>() << affine_0 << affine_1); | |
| 41 | + u.file.set("Affine_0", affine_0); | |
| 42 | + u.file.set("Affine_1", affine_1); | |
| 43 | + dst.append(u); | |
| 44 | + | |
| 45 | + const double IPD = sqrt(pow(affine_0.x() - affine_1.x(), 2) + pow(affine_0.y() - affine_1.y(), 2)); | |
| 46 | + for (int i = 0; i < numAffines; i++) { | |
| 47 | + int angle = (rand() % (2*maxAngle)) - maxAngle; | |
| 48 | + | |
| 49 | + int min = (int)(sqrt(1 / scaleFactor) * IPD); | |
| 50 | + int max = (int)(sqrt(scaleFactor) * IPD); | |
| 51 | + int dx = (rand() % (max - min)) + min; | |
| 52 | + int dy = (dx * sin(angle * M_PI / 180))/2; | |
| 53 | + | |
| 54 | + QPointF shiftedAffine_0 = QPointF(affine_1.x() - dx, affine_1.y() + dy); | |
| 55 | + | |
| 56 | + Template u = t; | |
| 57 | + u.file.setPoints(QList<QPointF>() << shiftedAffine_0 << affine_1); | |
| 58 | + u.file.set("Affine_0", shiftedAffine_0); | |
| 59 | + u.file.set("Affine_1", affine_1); | |
| 60 | + dst.append(u); | |
| 61 | + } | |
| 62 | + } | |
| 63 | + } | |
| 64 | +}; | |
| 65 | + | |
| 66 | +BR_REGISTER(Transform, RndAffineTransform) | |
| 67 | + | |
| 68 | +} // namespace br | |
| 69 | + | |
| 70 | +#include "imgproc/rndaffine.moc" | ... | ... |
openbr/plugins/io/write.cpp
| ... | ... | @@ -31,10 +31,10 @@ class WriteTransform : public TimeVaryingTransform |
| 31 | 31 | { |
| 32 | 32 | Q_OBJECT |
| 33 | 33 | Q_PROPERTY(QString outputDirectory READ get_outputDirectory WRITE set_outputDirectory RESET reset_outputDirectory STORED false) |
| 34 | - Q_PROPERTY(QString imageName READ get_imageName WRITE set_imageName RESET reset_imageName STORED false) | |
| 34 | + Q_PROPERTY(QString underscore READ get_underscore WRITE set_underscore RESET reset_underscore STORED false) | |
| 35 | 35 | Q_PROPERTY(QString imgExtension READ get_imgExtension WRITE set_imgExtension RESET reset_imgExtension STORED false) |
| 36 | 36 | BR_PROPERTY(QString, outputDirectory, "Temp") |
| 37 | - BR_PROPERTY(QString, imageName, "image") | |
| 37 | + BR_PROPERTY(QString, underscore, "") | |
| 38 | 38 | BR_PROPERTY(QString, imgExtension, "jpg") |
| 39 | 39 | |
| 40 | 40 | int cnt; |
| ... | ... | @@ -48,7 +48,8 @@ class WriteTransform : public TimeVaryingTransform |
| 48 | 48 | void projectUpdate(const Template &src, Template &dst) |
| 49 | 49 | { |
| 50 | 50 | dst = src; |
| 51 | - OpenCVUtils::saveImage(dst.m(), QString("%1/%2_%3.%4").arg(outputDirectory).arg(imageName).arg(cnt++, 5, 10, QChar('0')).arg(imgExtension)); | |
| 51 | + QString path = QString("%1/%2%3.%4").arg(outputDirectory).arg(dst.file.baseName()).arg(underscore.isEmpty() ? "" : "_" + underscore).arg(imgExtension); | |
| 52 | + OpenCVUtils::saveImage(dst.m(), path); | |
| 52 | 53 | } |
| 53 | 54 | |
| 54 | 55 | }; | ... | ... |
openbr/plugins/metadata/registerpointsasaffine.cpp
0 → 100644
| 1 | +#include <openbr/plugins/openbr_internal.h> | |
| 2 | + | |
| 3 | +namespace br | |
| 4 | +{ | |
| 5 | + | |
| 6 | +class RegisterPointsAsAffine : public UntrainableMetadataTransform | |
| 7 | +{ | |
| 8 | + Q_OBJECT | |
| 9 | + Q_PROPERTY(QList<int> pointsIdxs READ get_pointIdxs WRITE set_pointIdxs RESET reset_pointIdxs STORED false) | |
| 10 | + BR_PROPERTY(QList<int>, pointIdxs, QList<int>()) | |
| 11 | + | |
| 12 | + void projectMetadata(const File &src, File &dst) const | |
| 13 | + { | |
| 14 | + const int chin = 20; | |
| 15 | + | |
| 16 | + if (pointIdxs.size() != 2 && pointIdxs.size() != 3) | |
| 17 | + qFatal("Need 2 or 3 points for affine transform"); | |
| 18 | + | |
| 19 | + dst = src; | |
| 20 | + | |
| 21 | + QList<QPointF> points = src.points(); | |
| 22 | + | |
| 23 | + if (points[pointIdxs[0]] == QPointF(-1, -1) || | |
| 24 | + points[pointIdxs[1]] == QPointF(-1, -1)) | |
| 25 | + dst.fte = true; | |
| 26 | + | |
| 27 | + if (points[chin] == QPointF(-1, -1)) | |
| 28 | + dst.fte = true; | |
| 29 | + | |
| 30 | + dst.set("Affine_0", points[pointIdxs[0]]); | |
| 31 | + dst.set("Affine_1", points[pointIdxs[1]]); | |
| 32 | + if (pointIdxs.size() == 3) | |
| 33 | + dst.set("Affine_2", points[pointIdxs[2]]); | |
| 34 | + } | |
| 35 | +}; | |
| 36 | + | |
| 37 | +BR_REGISTER(Transform, RegisterPointsAsAffine) | |
| 38 | + | |
| 39 | +} // namespace br | |
| 40 | + | |
| 41 | +#include "metadata/registerpointsasaffine.moc" | ... | ... |