【问题标题】:How can i get interpolated Polar coordinates?如何获得插值极坐标?
【发布时间】:2019-11-26 14:57:06
【问题描述】:

我在获取轮廓的离散序列时遇到问题。 我的想法:我想在图像的闭合轮廓的中间放置一个锚点,并使用极坐标来获取极坐标每个度数的长度。

我已经创建了一个固定长度 360 的向量,并遍历所有轮廓点(约 4000),长度为 l=contour.length/360。在这里,我沿着长度为 l 的轮廓得到 360 个值。但我希望从 1 到 360 的每个整数度都有一个离散值。

我可以对我的数组进行插值以将值固定在 1 到 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;
}

【问题讨论】:

    标签: c++ opencv graphics interpolation


    【解决方案1】:

    您似乎错过了一些 C++ 基础知识。例如

    1) 如果您使用at(),则会添加不必要的范围检查。当您循环到cnts[0].size() 时,您现在要这样做两次。

    2) 你不需要在void 函数中使用return

    3) 不要使用指针返回。这是 C++,而不是 C。使用引用或 std::tuple 返回类型。

    那么你实际上是在复制 std::complex 类型。

    代码可以很简单。

    #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() << ";";
        }
    }
    

    【讨论】:

    • 哇如何用 python 做到这一点?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多