本文主要是介绍Opencv 极坐标变换,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
变换后图片
代码
// 以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;
}
这篇关于Opencv 极坐标变换的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!