| 1 | #include "MCaos.h"
|
|---|
| 2 |
|
|---|
| 3 | #include <fstream>
|
|---|
| 4 | #include <iostream>
|
|---|
| 5 | #include <iomanip>
|
|---|
| 6 | #include <math.h>
|
|---|
| 7 |
|
|---|
| 8 | #include "Led.h"
|
|---|
| 9 | #include "FilterLed.h"
|
|---|
| 10 |
|
|---|
| 11 | using namespace std;
|
|---|
| 12 |
|
|---|
| 13 | void MCaos::ReadResources(const char *name)
|
|---|
| 14 | {
|
|---|
| 15 | ifstream fin(name);
|
|---|
| 16 | if (!fin)
|
|---|
| 17 | {
|
|---|
| 18 | cout << "ERROR - Cannot open " << name << endl;
|
|---|
| 19 | return;
|
|---|
| 20 | }
|
|---|
| 21 |
|
|---|
| 22 | fPositions.clear();
|
|---|
| 23 |
|
|---|
| 24 | cout << " Reading " << name << ":" << endl;
|
|---|
| 25 | cout << "------------------------------" << endl;
|
|---|
| 26 | while (1)
|
|---|
| 27 | {
|
|---|
| 28 | double px, py, phi;
|
|---|
| 29 | fin >> px >> py >> phi;
|
|---|
| 30 | if (!fin)
|
|---|
| 31 | break;
|
|---|
| 32 |
|
|---|
| 33 | cout << " Led #" << fPositions.size() << ": ";
|
|---|
| 34 | cout << setw(3) << px << " ";
|
|---|
| 35 | cout << setw(3) << py << " (";
|
|---|
| 36 | cout << setw(3) << phi << ")\n";
|
|---|
| 37 | AddPosition(px, py, phi);
|
|---|
| 38 | }
|
|---|
| 39 | cout << "Found " << fPositions.size() << " leds." << endl;
|
|---|
| 40 | }
|
|---|
| 41 |
|
|---|
| 42 | void MCaos::CalcCenters(const vector<Led> &leds, float min, float max)
|
|---|
| 43 | {
|
|---|
| 44 | fRings.clear();
|
|---|
| 45 |
|
|---|
| 46 | const int nPoints = leds.size();
|
|---|
| 47 |
|
|---|
| 48 | // A minimum of at least 3 points is mandatory!
|
|---|
| 49 | if (nPoints<fMinNumberLeds || nPoints<3)
|
|---|
| 50 | return;
|
|---|
| 51 |
|
|---|
| 52 | // ofstream fout("rings.txt", ios::app);
|
|---|
| 53 |
|
|---|
| 54 | for (int i=0; i<nPoints-2; i++)
|
|---|
| 55 | for (int j=i+1; j<nPoints-1; j++)
|
|---|
| 56 | for (int k=j+1; k<nPoints; k++)
|
|---|
| 57 | {
|
|---|
| 58 | Ring ring;
|
|---|
| 59 | if (!ring.CalcCenter(leds[i], leds[j], leds[k]))
|
|---|
| 60 | continue;
|
|---|
| 61 |
|
|---|
| 62 | // fout << i+j*10+k*100 << " " << ring.GetR() << " " << ring.GetX() << " " << ring.GetY() << endl;
|
|---|
| 63 |
|
|---|
| 64 | //
|
|---|
| 65 | //filter and remove rings with too big or too small radius
|
|---|
| 66 | //
|
|---|
| 67 | if ((min>=0&&ring.GetR()<min) || (max>=0&&ring.GetR()>max))
|
|---|
| 68 | continue;
|
|---|
| 69 |
|
|---|
| 70 | fRings.push_back(ring);
|
|---|
| 71 | }
|
|---|
| 72 | }
|
|---|
| 73 |
|
|---|
| 74 | int32_t MCaos::CalcRings(std::vector<Led> &leds, float min, float max)
|
|---|
| 75 | {
|
|---|
| 76 | CalcCenters(leds, min, max);
|
|---|
| 77 |
|
|---|
| 78 | fCenter.InterpolCenters(fRings);
|
|---|
| 79 |
|
|---|
| 80 | for (auto it=leds.begin(); it!=leds.end(); it++)
|
|---|
| 81 | it->CalcPhi(fCenter);
|
|---|
| 82 |
|
|---|
| 83 | return fRings.size();
|
|---|
| 84 | }
|
|---|
| 85 |
|
|---|
| 86 | Ring MCaos::Run(uint8_t *img)
|
|---|
| 87 | {
|
|---|
| 88 | fLeds.clear();
|
|---|
| 89 |
|
|---|
| 90 | // img width height radius sigma
|
|---|
| 91 | FilterLed f(img, 768, 576, fSizeBox, fSizeBox, fCut);
|
|---|
| 92 |
|
|---|
| 93 | for (auto it=fPositions.begin(); it!=fPositions.end(); it++)
|
|---|
| 94 | {
|
|---|
| 95 | std::vector<Led> arr;
|
|---|
| 96 |
|
|---|
| 97 | // Try to find Led in this area
|
|---|
| 98 | f.Execute(arr, floor(it->GetX()), floor(it->GetY()));
|
|---|
| 99 |
|
|---|
| 100 | // Loop over newly found Leds
|
|---|
| 101 | for (auto jt=arr.begin(); jt!=arr.end(); jt++)
|
|---|
| 102 | {
|
|---|
| 103 | // Add Offset to Led
|
|---|
| 104 | //jt->AddOffset(it->GetDx(), it->GetDy());
|
|---|
| 105 |
|
|---|
| 106 | // Remember the expected phi for each detected led
|
|---|
| 107 | jt->SetPhi(it->GetPhi());
|
|---|
| 108 |
|
|---|
| 109 | // Mark Led in image (FIXME: Move to MStarguider)
|
|---|
| 110 | f.MarkPoint(jt->GetX(), jt->GetY(), jt->GetMag());
|
|---|
| 111 | }
|
|---|
| 112 |
|
|---|
| 113 | fLeds.insert(fLeds.end(), arr.begin(), arr.end());
|
|---|
| 114 | }
|
|---|
| 115 |
|
|---|
| 116 | fNumDetectedRings = CalcRings(fLeds, fMinRadius, fMaxRadius);
|
|---|
| 117 |
|
|---|
| 118 | double sumphi = 0;
|
|---|
| 119 | for (auto it=fLeds.begin(); it!=fLeds.end(); it++)
|
|---|
| 120 | {
|
|---|
| 121 | //cout << it->CalcPhi(fCenter) << "|" << it->GetPhi() << " ";
|
|---|
| 122 | double dphi = it->CalcPhi(fCenter) - it->GetPhi();
|
|---|
| 123 | if (dphi>M_PI)
|
|---|
| 124 | dphi -= 2*M_PI;
|
|---|
| 125 | if (dphi<-M_PI)
|
|---|
| 126 | dphi += 2*M_PI;
|
|---|
| 127 |
|
|---|
| 128 | sumphi += dphi;
|
|---|
| 129 | }
|
|---|
| 130 | //cout << endl;
|
|---|
| 131 |
|
|---|
| 132 | fCenter.SetPhi(sumphi/fLeds.size());
|
|---|
| 133 |
|
|---|
| 134 | return fCenter;
|
|---|
| 135 | }
|
|---|