aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/TCC/Tango.TCC.CardDetector/ArucoUtils.cpp
blob: 52721368ae58af1f849ba260f18f7a3b57e8288c (plain)
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
#include "ArucoUtils.h"

using namespace Tango::TCC::CardDetector;

Ptr<Dictionary> dictionary;

ArucoUtils::ArucoUtils()
{
	dictionary = getPredefinedDictionary(aruco::DICT_4X4_50);
}


ArucoUtils::~ArucoUtils()
{
}

vector<Point> ArucoUtils::getArcusVertices(Mat image)
{
	vector<Point> vertices;

	vector<int> ids;
	vector<vector<Point2f>> corners;

	aruco::DetectorParameters* params = new aruco::DetectorParameters();
	params->cornerRefinementMethod = aruco::CORNER_REFINE_SUBPIX;
	params->perspectiveRemovePixelPerCell = 50;


	aruco::detectMarkers(image, dictionary, corners, ids, &(*params));
	if (corners.size() == 4)
	{
		//aruco::drawDetectedMarkers(image, corners, ids);

		vertices =
		{
			Point(0,0),
			Point(0,0),
			Point(0,0),
			Point(0,0)
		};

		InputArrayOfArrays _corners = corners;
		InputArray _ids = ids;

		int nMarkers = (int)_corners.total();

		for (int i = 0; i < nMarkers; i++) {

			Mat currentMarker = _corners.getMat(i);
			int id = _ids.getMat().ptr< int >(0)[i] - 1;

			switch (id)
			{
			case 0:
				vertices[0] = currentMarker.ptr< Point2f >(0)[0];
				break;
			case 1:
				vertices[1] = currentMarker.ptr< Point2f >(0)[1];
				break;
			case 2:
				vertices[2] = currentMarker.ptr< Point2f >(0)[2];
				break;
			case 3:
				vertices[3] = currentMarker.ptr< Point2f >(0)[3];
				break;
			}
		}
	}

	return vertices;
}

Mat ArucoUtils::applyHomography(Mat image, vector<cv::Point> vertices, Size destination_size)
{
	Mat im_dst = Mat::zeros(destination_size, CV_8UC3);

	vector<Point2f> pts_dst;

	pts_dst.push_back(Point2f(0, 0));
	pts_dst.push_back(Point2f(destination_size.width - 1, 0));
	pts_dst.push_back(Point2f(destination_size.width - 1, destination_size.height - 1));
	pts_dst.push_back(Point2f(0, destination_size.height - 1));

	Mat tform = findHomography(vertices, pts_dst);
	warpPerspective(image, im_dst, tform, destination_size);
	return im_dst;
}