变换后图片
代码
// 以Center为极坐标原点,将RowFrom到RowTo的圆环,仅仅变换该范围内的点,忽略掉其他部分。
#include "polar_transeforme.hpp"
#include <string>
using namespace cv;
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;
}