Opencv 极坐标变换
变换后图片
代码
#include "polar_transeforme.hpp"
#include <string>
using namespace cv;
inline float distanceEuclidean(int r, int c, const Point2d& point)
{
return sqrtf(powf(point.x - c, 2) + powf(point.y - r, 2));
}
void magtitue_point(Size matSize, Point2d point)
{
Mat mag(matSize, CV_32FC1);
for (int r = 0; r < matSize.height; r++)
{
float* pRow = (float*)mag.data + r * matSize.width;
for (int c = 0; c < matSize.width; c++)
{
float* pCur = pRow + c;
*pCur = distanceEuclidean(r, c, point);
}
}
}
void calculate_map(int rouFrom, int rouTo, Point2d center, Mat& map)
{
int heightDst = map.size().height;
int widthDst = map.size().width;
float dTheta = 2 * PAI / heightDst;
for (int r = 0; r < heightDst; r++)
{
float* pRow = (float*)map.data + r * 2 * widthDst;
float curTheta = r * dTheta;
float cosTheta = cos(curTheta);
float sinTheta = sin(curTheta);
for (int c = 0; c < widthDst; c++)
{
float* pCur = pRow + c * 2;
int rou = c + rouFrom;
*pCur = rou * cosTheta + center.x;
pCur++;
*pCur = center.y - rou * sinTheta;
}
}
}
Mat polar_transeforme(Mat& oriImage, int rouFrom, int rouTo, Point2d center)
{
int heightDst = 2 * PAI * rouFrom;
int widthDst = rouTo - rouFrom;
Mat map(Size(widthDst, heightDst), CV_32FC2);
calculate_map(rouFrom, rouTo, center, map);
Mat dstMat;
remap(oriImage, dstMat, map, Mat(), INTER_CUBIC, 0);
return dstMat;
}
Point2d polar2Origin(Point2d p, Point2d center, int rouFrom, float dTheta)
{
float theta = p.y / rouFrom;
float rou = p.x;
float tempX = center.x + rou * sin(theta);
float tempY = center.y - rou * cos(theta);
return Point2d(tempX, tempY);
}
int main()
{
const std::string strImagePath = "示例图片.jpg";
Mat oriImg = imread(strImagePath, IMREAD_GRAYSCALE);
Point2d center(237, 237);
int rouFrom = 110;
int rouTo = 230;
Mat polarImg = polar_transeforme(oriImg, rouFrom, rouTo, center);
int test = 0;
return 0;
}