极坐标和直角坐标是两种不同的坐标系,用于描述平面上的点的位置。它们之间的转换涉及线性代数的一些基本概念和数学原理。
直角坐标系(笛卡尔坐标系)
直角坐标系使用直角(垂直和水平)的参考线来表示点的位置。在二维直角坐标系中,一个点的位置由两个坐标值(x,y)表示,其中x表示点在水平方向上的位置,y表示点在垂直方向上的位置。直角坐标系中的点可以通过平移和旋转来变换其位置。
极坐标系
极坐标系使用极径(向量的长度)和极角(向量与参考线的夹角)来表示点的位置。在二维极坐标系中,一个点的位置由两个坐标值(r,θ)表示,其中r表示点到原点的距离,θ表示点与参考线的夹角。极坐标系中的点可以通过平移和旋转来变换其位置。
直角坐标系与极坐标系的互相转换
- 直角坐标转极坐标:给定直角坐标系中的点的坐标(x,y),可以使用以下公式将其转换为极坐标系中的坐标(r,θ):
r = x 2 + y 2 r = \sqrt{x^2 + y^2} r=x2+y2计算点到原点的距离(极径r)。
θ = arctan2 ( y , x ) \theta = \text{arctan2}(y, x) θ=arctan2(y,x)计算点与参考线的夹角(极角),使用反正切函数 arctan2(y, x) 来确定正确的象限。 - 极坐标转直角坐标:给定极坐标系中的点的坐标(r,θ),可以使用以下公式将其转换为直角坐标系中的坐标(x,y):
x = r ⋅ cos ( θ ) x = r \cdot \cos(\theta) x=r⋅cos(θ)计算点在水平方向上的位置。
y = r ⋅ sin ( θ ) y = r \cdot \sin(\theta) y=r⋅sin(θ)计算点在垂直方向上的位置。
作用和意义
极坐标和直角坐标在不同的应用中具有不同的作用和意义:
- 极坐标可以更直观地描述点相对于原点的位置和方向,适用于圆形图案边缘上的文字排列等场景。
- 直角坐标可以方便地进行几何计算和图像处理,例如图像的平移、旋转、缩放等操作。
在图像处理中,使用极坐标和直角坐标之间的转换可以方便地处理圆形特征、文字识别和检测等任务。通过将图像从直角坐标系转换为极坐标系,可以将圆形图案边缘上的文字排列在新图像的边缘,使得文字的识别和检测更加方便。反之,将极坐标转换为直角坐标可以方便地进行后续的图像处理和分析。总而言之,极坐标和直角坐标的互相转换提供了在不同坐标系之间进行方便的变换和处理的能力,可以在图像处理、几何计算和其他领域中发挥重要作用。
代码实现过程如下所示:
import cv2
import numpy as np
import matplotlib.pyplot as plt
# class Trans:
# def __init__(self,x,y,n):
# self.x=x
# self.y=y
# self.n=n
#
# def Trs(self):
# r,theta=cv2.cartToPolar(x,y,angleInDegrees=True)
# xr,yr=cv2.polarToCart(r,theta,angleInDegrees=1)
# print(xr,yr)
#
# plt.figure(figsize=(9, 5))
# plt.subplot(121), plt.title("Cartesian coordinate"), plt.plot(x, y, 'o')
# for i, txt in enumerate(n):
# plt.annotate(txt, (x[i], y[i]))
# plt.subplot(122), plt.title("Polar coordinate"), plt.plot(r, theta, 'o')
# for i, txt in enumerate(n):
# plt.annotate(txt, (r[i], theta[i]))
# plt.show()
#
#
# x = np.float32([0, 1, 2, 0, 1, 2, 0, 1, 2]) - 1
# y = np.float32([0, 0, 0, 1, 1, 1, 2, 2, 2]) - 1
# n = np.arange(9)
# to=Trans(x,y,n)
# to.Trs()
class Trans:
def __init__(self,image_path):
self.image_path=image_path
def Trs(self):
img=cv2.imread(self.image_path)
if img is None:
print('Unable to load image!')
else:
h,w=img.shape[:2]
cx,cy=int(w/2),int(h/2)
maxR=max(cx,cy)
imgPolar = cv2.linearPolar(img, (cx, cy), maxR, cv2.INTER_LINEAR)
imgPR = cv2.rotate(imgPolar, cv2.ROTATE_90_COUNTERCLOCKWISE)
self.show_image(img,imgPR)
def show_image(self,img,imgPR):
plt.figure(figsize=(10, 6))
plt.subplot(121), plt.imshow(cv2.cvtColor(img, cv2.COLOR_BGR2RGB)), plt.title("Original"), plt.axis('off')
plt.subplot(122), plt.imshow(cv2.cvtColor(imgPR, cv2.COLOR_BGR2RGB)), plt.title("PolarTrans"), plt.axis('off')
plt.show()
imgfile="Images/Atest1.jpg"
to=Trans(imgfile)
to.Trs()