SLProject  4.2.000
A platform independent 3D computer graphics framework for desktop OS, Android, iOS and online in web browsers
WAICompassAlignment.cpp
Go to the documentation of this file.
1 #include <WAICompassAlignment.h>
2 #include <opencv2/imgproc.hpp>
3 #include <Profiler.h>
4 
5 #include <iostream>
6 
7 void WAICompassAlignment::setTemplate(cv::Mat& templateImage, double latitudeDEG, double longitudeDEG, double altitudeM)
8 {
9  _template.image = templateImage.clone();
10  _template.latitudeDEG = latitudeDEG;
11  _template.longitudeDEG = longitudeDEG;
12  _template.altitudeM = altitudeM;
13 }
14 
15 void WAICompassAlignment::update(const cv::Mat& frameGray,
16  cv::Mat& resultImage,
17  const float hFov,
18  double latitudeDEG,
19  double longitudeDEG,
20  double altitudeM,
21  cv::Point& vecCurForward)
22 {
24 
25  // TODO(dgj1): handle error if no template has been set
26 
27  cv::matchTemplate(frameGray, _template.image, resultImage, cv::TM_CCOEFF_NORMED);
28 
29  double minVal, maxVal;
30  cv::Point minLoc, maxLoc;
31  cv::minMaxLoc(resultImage, &minVal, &maxVal, &minLoc, &maxLoc);
32 
33  std::cout << "maxLoc: " << maxLoc << std::endl;
34 
35  cv::Point imCenter = cv::Point((int)(frameGray.cols * 0.5f),
36  (int)(frameGray.rows * 0.5f));
37  cv::Point tplCenter = cv::Point((int)(_template.image.cols * 0.5f),
38  (int)(_template.image.rows * 0.5f));
39  cv::Point tplMatchCenter = maxLoc + tplCenter;
40 
41  cv::Point offset = tplMatchCenter - imCenter;
42 
43  float degPerPixelH = hFov / frameGray.cols;
44 
45  // Angle between camera forward and template match center
46  // If GPS is absolutely precise, the difference between this angle and
47  // the angle between GPS forward and template match center should be 0
48  float angleCenterTemplateDegH = offset.x * degPerPixelH;
49 
50  // TODO(dgj1): actually calculate this
51  cv::Point vecCurTpl = cv::Point((int)(_template.latitudeDEG - latitudeDEG),
52  (int)(_template.longitudeDEG - longitudeDEG));
53  vecCurTpl /= cv::norm(vecCurTpl);
54  vecCurForward /= cv::norm(vecCurForward);
55  float angleGPSTemplateDegH = (float)vecCurTpl.dot(vecCurForward);
56 
57  _rotAngDEG = angleGPSTemplateDegH - angleCenterTemplateDegH;
58 }
#define PROFILE_FUNCTION()
Definition: Instrumentor.h:41
void update(const cv::Mat &frameGray, cv::Mat &resultImage, const float hFov, double latitudeDEG, double longitudeDEG, double altitudeM, cv::Point &forwardPoint)
void setTemplate(cv::Mat &templateImage, double latitudeDEG, double longitudeDEG, double altitudeM)