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;
}

文章出处登录后可见!

已经登录?立即刷新

共计人评分,平均

到目前为止还没有投票!成为第一位评论此文章。

(0)
乘风的头像乘风管理团队
上一篇 2023年12月6日
下一篇 2023年12月6日

相关推荐