File: test_fourier_descriptors.cpp

package info (click to toggle)
opencv 4.5.1%2Bdfsg-5
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 268,248 kB
  • sloc: cpp: 969,170; xml: 682,525; python: 36,732; lisp: 30,170; java: 25,155; ansic: 7,927; javascript: 5,643; objc: 2,041; sh: 935; cs: 601; perl: 494; makefile: 145
file content (90 lines) | stat: -rw-r--r-- 3,643 bytes parent folder | download | duplicates (3)
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
// This file is part of OpenCV project.
// It is subject to the license terms in the LICENSE file found in the top-level directory
// of this distribution and at http://opencv.org/license.html.

#include "test_precomp.hpp"

namespace opencv_test { namespace {


TEST(ximgproc_fourierdescriptors,test_FD_AND_FIT)
{
    Mat fd;
    vector<Point2f> ctr(16);
    float Rx = 100, Ry = 100;
    Point2f g(0, 0);
    float angleOri = 0;
    for (int i = 0; i < static_cast<int>(ctr.size()); i++)
    {
        float theta = static_cast<float>(2 * CV_PI / static_cast<int>(ctr.size()) * i + angleOri);
        ctr[i] = Point2f(Rx * cos(theta) + g.x, Ry * sin(theta) + g.y);

    }
    ximgproc::fourierDescriptor(ctr, fd);
    CV_Assert(cv::norm(fd.at<Vec2f>(0, 0)) < ctr.size() * FLT_EPSILON && cv::norm(fd.at<Vec2f>(0, 1) - Vec2f(Rx, 0)) < ctr.size() * FLT_EPSILON);
    Rx = 100, Ry = 50;
    g = Point2f(50, 20);
    for (int i = 0; i < static_cast<int>(ctr.size()); i++)
    {
        float theta = static_cast<float>(2 * CV_PI / static_cast<int>(ctr.size()) * i + angleOri);
        ctr[i] = Point2f(Rx * cos(theta) + g.x, Ry * sin(theta) + g.y);
    }
    ximgproc::fourierDescriptor(ctr, fd);
    CV_Assert(cv::norm(fd.at<Vec2f>(0, 0) - Vec2f(g)) < 1 &&
        fabs(fd.at<Vec2f>(0, 1)[0] + fd.at<Vec2f>(0, static_cast<int>(ctr.size()) - 1)[0] - Rx) < 1 &&
        fabs(fd.at<Vec2f>(0, 1)[0] - fd.at<Vec2f>(0, static_cast<int>(ctr.size()) - 1)[0] - Ry) < 1);
    Rx = 70, Ry = 100;
    g = Point2f(30, 100);
    angleOri = static_cast<float>(CV_PI / 4);
    for (int i = 0; i < static_cast<int>(ctr.size()); i++)
    {
        float theta = static_cast<float>(2 * CV_PI / static_cast<int>(ctr.size()) * i + CV_PI / 4);
        ctr[i] = Point2f(Rx * cos(theta) + g.x, Ry * sin(theta) + g.y);
    }
    ximgproc::fourierDescriptor(ctr, fd);
    CV_Assert(cv::norm(fd.at<Vec2f>(0, 0) - Vec2f(g)) < 1);
    CV_Assert(cv::norm(Vec2f((Rx + Ry)*cos(angleOri) / 2, (Rx + Ry)*sin(angleOri) / 2) - fd.at<Vec2f>(0, 1)) < 1);
    CV_Assert(cv::norm(Vec2f((Rx - Ry)*cos(angleOri) / 2, -(Rx - Ry)*sin(angleOri) / 2) - fd.at<Vec2f>(0, static_cast<int>(ctr.size()) - 1)) < 1);

    RNG rAlea;
    g.x = 0; g.y = 0;
    ctr.resize(256);
    for (int i = 0; i < static_cast<int>(ctr.size()); i++)
    {
        ctr[i] = Point2f(rAlea.uniform(0.0F, 1.0F), rAlea.uniform(0.0F, 1.0F));
        g += ctr[i];
    }
    g.x = g.x / ctr.size();
    g.y = g.y / ctr.size();
    double rotAngle = 35;
    double s = 0.1515;
    Mat r = getRotationMatrix2D(g, rotAngle, 0.1515);
    vector<Point2f> unknownCtr;
    vector<Point2f> ctrShift;
    int valShift = 170;
    for (int i = 0; i < static_cast<int>(ctr.size()); i++)
        ctrShift.push_back(ctr[(i + valShift) % ctr.size()]);
    cv::transform(ctrShift, unknownCtr, r);
    ximgproc::ContourFitting fit;
    fit.setFDSize(16);
    Mat t;
    double dist;
    fit.estimateTransformation(unknownCtr, ctr, t, &dist, false);
    CV_Assert(fabs(t.at<double>(0, 0)*ctr.size() + valShift) < 10 || fabs((1 - t.at<double>(0, 0))*ctr.size() - valShift) < 10);
    CV_Assert(fabs(t.at<double>(0, 1) - rotAngle / 180.*CV_PI) < 0.1);
    CV_Assert(fabs(t.at<double>(0, 2) - 1 / s) < 0.1);
    ctr.resize(4);
    ctr[0] = Point2f(0, 0);
    ctr[1] = Point2f(16, 0);
    ctr[2] = Point2f(16, 16);
    ctr[3] = Point2f(0, 16);
    double squareArea = contourArea(ctr), lengthSquare = arcLength(ctr, true);
    Mat ctrs;
    ximgproc::contourSampling(ctr, ctrs, 64);
    CV_Assert(fabs(squareArea - contourArea(ctrs)) < FLT_EPSILON);
    CV_Assert(fabs(lengthSquare - arcLength(ctrs, true)) < FLT_EPSILON);
}



}} // namespace