fuhsy
fuhsy

Reputation: 21

How can i get interpolated Polar coordinates?

I have a problem to get a discrete Sequence of a contour. My idea: I want to place an anchor in a middle of a closed contour in an image and use polar coordinates to get a length for each degree of the polar coordinates.

I already have created a vector of fixed length 360 and iterate through all contour points(ca. 4000) with length l=contour.length/360. Here i get 360 values along the contour with length l. But i want to have a discrete value for each integer degree from 1 to 360.

Can i interpolate my array to fix values from 1 to 360?

vector<cv::Point> cn;
double theta = 0;
double dis = 0;
int polsize = 360;
int psize = 0;
for (int k = 0; k < cnts[0].size(); k++) {
    cn.push_back(cnts[0].at(k));
}

double pstep = cn.size() / polsize;
for (int m = 1; m < polsize; m++) {
    psize = (int)(m * pstep);
    polar(cn[psize].x, cn[psize].y, &dis, &theta);
    outputFile << theta << "/" << dis << ";";
}

void polar(int x, int y, double* r, double* theta)
{
   double toDegrees = 180 / 3.141593;
   *r = sqrt((pow(x, 2)) + (pow(y, 2)));
   double xt = x, yt = y;
   yt = 1024 - yt;
   if (xt == 0) xt = 0.1;
   if (yt == 0) yt = 0.1;
   *theta = atan(yt / xt) * toDegrees;
   if (*theta < 0) *theta = *theta+180;
   return;
}

Upvotes: 1

Views: 244

Answers (1)

JHBonarius
JHBonarius

Reputation: 11261

You seem to miss some of the C++ basics. E.g.

1) If you use at() you add unnecessary range checking. As you are looping until cnts[0].size() you're doing that twice now.

2) you don't need to use return in void functions.

3) don't use pointers for return. This is C++, not C. Use references, or std::tuple return type.

Then you're actually replicating the std::complex type.

The code can be really simple.

#include <vector>
//#include <algorithm> // if using std::copy
#include <cmath>
#include <sstream> // only for the temporary output.

static constexpr auto toDeg = 180 / 3.141593;

struct Point{
    double x,y;

    double abs() const { 
        return std::sqrt(std::pow(x,2) + std::pow(y,2));
    }

    double arg() const {
        return std::atan2(y, x) * toDeg;
    }
};

int main(){
    std::vector<std::vector<Point>> cnts = {{{1,1}}};

    // copy constructor
    std::vector<Point> cn(cnts[0]);
    // range-based constructor
    //std::vector<Point> cn(std::cbegin(cnts[0]), std::cend(cnts[0]));
    // or copy-insert
    //std::vector<Point> cn
    //cn.reserve(cnts[0].size());
    //std::copy(std::cbegin(cnts[0]), std::cend(cnts[0]), std::back_inserter(cn));

    std::stringstream outputFile; // temp

    for (auto const& el : cn) {
        outputFile << el.arg() << "/" << el.abs() << ";";
    }
}

Upvotes: 1

Related Questions