zhangyl112
zhangyl112
2017-11-26 15:57

opencv下笛卡尔坐标图片转化为极坐标图片无法显示彩色图片

  • c
  • 图片

uchar interpolate_bilinear(cv::Mat& mat_src, double ri, int rf, int rc, double ti, int tf, int tc)
{
double inter_value = 0.0;

if (rf == rc && tc == tf)
{
    inter_value = mat_src.ptr<uchar>(rc)[tc];
}
else if (rf == rc)
{
    inter_value = (ti - tf) * mat_src.ptr<uchar>(rf)[tc] + (tc - ti) * mat_src.ptr<uchar>(rf)[tf];
}
else if (tf == tc)
{
    inter_value = (ri - rf) * mat_src.ptr<uchar>(rc)[tf] + (rc - ri) * mat_src.ptr<uchar>(rf)[tf];
}
else
{
    double inter_r1 = (ti - tf) * mat_src.ptr<uchar>(rf)[tc] + (tc - ti) * mat_src.ptr<uchar>(rf)[tf];
    double inter_r2 = (ti - tf) * mat_src.ptr<uchar>(rc)[tc] + (tc - ti) * mat_src.ptr<uchar>(rc)[tf];

    inter_value = (ri - rf) * inter_r2 + (rc - ri) * inter_r1;
}

return (uchar)inter_value;

}

bool cartesian_to_polar(cv::Mat& mat_c, cv::Mat& mat_p, int img_d)
{
int color_num = mat_c.channels();
cout << "color_num=" << color_num << endl;

    mat_p = cv::Mat::zeros(img_d, img_d, CV_8UC1);


//mat_p = cv::Mat::zeros(img_d, img_d, CV_8UC3);
//mat_p = cv::Mat::zeros(img_d, img_d, color_num);
int line_len = mat_c.rows;
int line_num = mat_c.cols;

double delta_r = (2.0*line_len) / (img_d - 1); //半径因子
double delta_t = 2.0 * PI / line_num; //角度因子

double center_x = (img_d - 1) / 2.0;
double center_y = (img_d - 1) / 2.0;

for (int i = 0; i < img_d; i++)

{
    for (int j = 0; j < img_d; j++)

    {
        double rx = j - center_x; //图像坐标转世界坐标
        double ry = center_y - i; //图像坐标转世界坐标

        double r = std::sqrt(rx*rx + ry*ry);

        if (r <= (img_d - 1) / 2.0)

        {
            double ri = r * delta_r;
            int rf = (int)std::floor(ri);
            int rc = (int)std::ceil(ri);

            if (rf < 0)
            {
                rf = 0;
            }
            if (rc >(line_len - 1))
            {
                rc = line_len - 1;
            }

            double t = std::atan2(ry, rx);

            if (t < 0)
            {
                t = t + 2.0 * PI;
            }

            double ti = t / delta_t;
            int tf = (int)std::floor(ti);
            int tc = (int)std::ceil(ti);

            if (tf < 0)
            {
                tf = 0;
            }
            if (tc >(line_num - 1))
            {
                tc = line_num - 1;
            }

            mat_p.ptr<uchar>(i)[j] = interpolate_bilinear(mat_c, ri, rf, rc, ti, tf, tc);

            /*
            mat_p.ptr<Vec3b>(i, j)[0] = interpolate_bilinear(mat_c, ri, rf, rc, ti, tf, tc);
            mat_p.ptr<Vec3b>(i, j)[1] = interpolate_bilinear(mat_c, ri, rf, rc, ti, tf, tc);
            mat_p.ptr<Vec3b>(i, j)[2] = interpolate_bilinear(mat_c, ri, rf, rc, ti, tf, tc);
            */
        }
    }
}

return true;

}

  • 点赞
  • 回答
  • 收藏
  • 复制链接分享

1条回答

为你推荐

换一换