【问题标题】:Converting Cartesian Coordinates To Polar Coordinates将笛卡尔坐标转换为极坐标
【发布时间】:2021-02-28 22:57:30
【问题描述】:

我正在尝试将图像从极坐标转换为笛卡尔坐标,但在应用公式后,我得到浮点坐标(r 和 teta),但我不知道如何使用 x 和 y 的浮点数来表示空间中的点.可能有一种方法可以将它们转换为 int 数字并仍然保留分布,但我不知道如何。我知道 OpenCV 中有类似 warpPolar 的功能,但我想自己实现它。任何想法都会有所帮助:)

这是我的代码:

struct Value
{
    double r;
    double teta;
    int value;  // pixel intensity
};
void irisNormalization(Mat img, Circle pupilCircle, Circle irisCircle, int &matrixWidth, int &matrixHeight)
{
    int w = img.size().width;
    int h = img.size().height;
    int X, Y;
    double r, teta;
    int rayOfIris = irisCircle.getRay();
    std::vector<Value> polar;
    // consider the rectangle the iris circle is confined in
    int xstart = irisCircle.getA() - rayOfIris;
    int ystart = irisCircle.getB() - rayOfIris;
    int xfinish = irisCircle.getA() + rayOfIris;
    int yfinish = irisCircle.getB() + rayOfIris;
    for (int x = xstart; x < xfinish; x++)
        for (int y = ystart; y < yfinish; y++)
        {
            X = x - xstart - rayOfIris;
            Y = y - ystart - rayOfIris;
            r = sqrt(X * X + Y * Y);
            if (X != 0)
            {
                teta = (atan(abs(Y / X)) * double(180 / M_PI));
                if (X > 0 && Y > 0) // quadrant 1
                    teta = teta;
                if (X > 0 && Y < 0)
                    teta = 360 - teta; // quadrant 4
                if (X < 0 && Y > 0) // quadrant 2
                    teta = 180 - teta;
                if (X < 0 && Y < 0) // quadrant 3
                    teta = 180 + teta;
                if (r < rayOfIris)
                {
                    polar.push_back({ r, teta, int(((Scalar)(img.at<uchar>(Point(x, y)))).val[0]) });
                }
            }
        }
    std::sort(polar.begin(), polar.end(), [](const Value &left, const Value &right) {
        return left.r < right.r && left.teta < right.teta;
    });
    for (std::vector<Value>::const_iterator i = polar.begin(); i != polar.end(); ++i)
        std::cout << i->r << ' ' << i->teta << endl;

【问题讨论】:

  • 您可以查看warpPolar 的实现以了解它是如何完成的。几何变形的最佳实现方法是获取输出像素的坐标,进行逆映射以找到输入中的(浮动)位置,然后在输入图像中进行插值。

标签: c++ opencv image-processing polar-coordinates cartesian


【解决方案1】:

您的实现尝试用极坐标表示给定圆内的每个整数坐标点。但是,在这种方式中,您会以一个坐标数组和一个值结束。

如果您想对图像进行几何变换,您应该:

  • 创建具有适当宽度(rho 分辨率)和高度(theta 分辨率)的目标图像;
  • 循环遍历目标图像的每个像素,并通过逆变换将其映射回原始图像;
  • 通过最终对近值进行插值,将反向变换点的值转换为原始图像。

对于插值,可以使用不同的方法。一份非详尽的清单包括:

【讨论】:

  • 非常感谢,但我仍然不明白如何在输出图像中使用浮点坐标插入点
  • @MariaNoaptes 我已经添加了一些建议。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-04-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多