opencv C++极坐标转换


#include  
#include   
#include  //srand()��rand()���� 
#include  //time()���� 
#include  
#include  
#include   
#include  
#include   
#include
#include 

using namespace std;
using namespace cv;
using namespace ml;
#define  PI      3.1415926 //Բ����

//˫�����Բ�ֵ
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)
{
	mat_p = cv::Mat::zeros(img_d, img_d, CV_8UC1);

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

	return true;
}

bool polar_to_cartesian(cv::Mat& mat_p, cv::Mat& mat_c, int rows_c, int cols_c)
{
	mat_c = cv::Mat::zeros(rows_c, cols_c, CV_8UC1);

	int polar_d = mat_p.cols;
	double polar_r = polar_d / 2.0; // Բͼ�뾶

	double delta_r = polar_r / rows_c; //�뾶����
	double delta_t = 2.0*PI / cols_c;  //�Ƕ�����

	double center_polar_x = (polar_d - 1) / 2.0;
	double center_polar_y = (polar_d - 1) / 2.0;

	for (int i = 0; i < cols_c; i++)
	{
		double theta_p = i * delta_t; //��ͼ��i����Բͼ��Ӧ�ߵĽǶ�
		double sin_theta = std::sin(theta_p);
		double cos_theta = std::cos(theta_p);

		for (int j = 0; j < rows_c; j++)
		{
			double temp_r = j * delta_r; //��ͼ��j����Բͼ�϶�Ӧ�İ뾶����

			int polar_x = (int)(center_polar_x + temp_r * cos_theta);
			int polar_y = (int)(center_polar_y - temp_r * sin_theta);

			mat_c.ptr<uchar>(j)[i] = mat_p.ptr<uchar>(polar_y)[polar_x];
		}
	}
	return true;
}
int main()
{
	//��ȡͼƬ��ʹ��ͼƬ�ľ���·����
	Mat srcImage;
	Mat src = imread("./test/test.jpg");
	namedWindow("src", 0);
	imshow("src", src);
	resize(src, src, Size(650, 650));
	cvtColor(src, srcImage, CV_BGR2GRAY);
	srcImage = srcImage >60; // ��ֵ��
	imshow("��ֵ��", srcImage);
	Mat dst;
	//cartesian_to_polar(srcImage,dst , 650);
	polar_to_cartesian(srcImage, dst, 400, 1000);
	namedWindow("dst", 0);
	imshow("dst", dst);

	return 0;
}

你可能感兴趣的:(c++,opencv,算法)