Python: 在极坐标图中显示笛卡尔图像
Python: Show cartesian image in polar plot
描述:
我将此数据表示在具有 256 列和 640 行的笛卡尔坐标系中。每列代表一个角度,theta,从 -65 度到 65 度。每行代表一个范围 r,从 0 到 20 米。
例子如下:
使用以下代码,我尝试制作一个网格并将每个像素位置转换为它在极坐标网格上的位置:
def polar_image(image, bearings):
(h,w) = image.shape
x_max = (np.ceil(np.sin(np.deg2rad(np.max(bearings)))*h)*2+1).astype(int)
y_max = (np.ceil(np.cos(np.deg2rad(np.min(np.abs(bearings))))*h)+1).astype(int)
blank = np.zeros((y_max,x_max,1), np.uint8)
for i in range(w):
for j in range(h):
X = (np.sin(np.deg2rad( bearings[i]))*j)
Y = (-np.cos(np.deg2rad(bearings[i]))*j)
blank[(Y+h).astype(int),(X+562).astype(int)] = image[h-1-j,w-1-i]
return blank
这张returns一张图片如下:
问题:
除了两件事之外,这是我真正想要实现的目标:
1) 新图像中似乎有一些伪影,而且贴图似乎有点粗糙。
有人对如何通过插值来摆脱这个有什么建议吗?
2) 图像保持笛卡尔表示,这意味着我没有任何极坐标网格线,也无法可视化 range/angle.
的间隔
有人知道如何用 theta 和范围内的轴刻度来可视化极坐标网格吗?
您可以使用pyplot.pcolormesh()
绘制转换后的网格:
import numpy as np
import pylab as pl
img = pl.imread("c:/tmp/Wnov4.png")
angle_max = np.deg2rad(65)
h, w = img.shape
angle, r = np.mgrid[-angle_max:angle_max:h*1j, 0:20:w*1j]
x = r * np.sin(angle)
y = r * np.cos(angle)
fig, ax = pl.subplots()
ax.set_aspect("equal")
pl.pcolormesh(x, y, img, cmap="gray");
或者你可以使用 OpenCV 中的 remap()
将其转换为新图像:
import cv2
import numpy as np
from PIL import Image
img = cv2.imread(r"c:/tmp/Wnov4.png", cv2.IMREAD_GRAYSCALE)
angle_max = np.deg2rad(65)
r_max = 20
x = np.linspace(-20, 20, 800)
y = np.linspace(20, 0, 400)
y, x = np.ix_(y, x)
r = np.hypot(x, y)
a = np.arctan2(x, y)
map_x = r / r_max * img.shape[1]
map_y = a / (2 * angle_max) * img.shape[0] + img.shape[0] * 0.5
img2 = cv2.remap(img, map_x.astype(np.float32), map_y.astype(np.float32), cv2.INTER_CUBIC)
Image.fromarray(img2)
描述:
我将此数据表示在具有 256 列和 640 行的笛卡尔坐标系中。每列代表一个角度,theta,从 -65 度到 65 度。每行代表一个范围 r,从 0 到 20 米。
例子如下:
使用以下代码,我尝试制作一个网格并将每个像素位置转换为它在极坐标网格上的位置:
def polar_image(image, bearings):
(h,w) = image.shape
x_max = (np.ceil(np.sin(np.deg2rad(np.max(bearings)))*h)*2+1).astype(int)
y_max = (np.ceil(np.cos(np.deg2rad(np.min(np.abs(bearings))))*h)+1).astype(int)
blank = np.zeros((y_max,x_max,1), np.uint8)
for i in range(w):
for j in range(h):
X = (np.sin(np.deg2rad( bearings[i]))*j)
Y = (-np.cos(np.deg2rad(bearings[i]))*j)
blank[(Y+h).astype(int),(X+562).astype(int)] = image[h-1-j,w-1-i]
return blank
这张returns一张图片如下:
问题:
除了两件事之外,这是我真正想要实现的目标:
1) 新图像中似乎有一些伪影,而且贴图似乎有点粗糙。
有人对如何通过插值来摆脱这个有什么建议吗?
2) 图像保持笛卡尔表示,这意味着我没有任何极坐标网格线,也无法可视化 range/angle.
的间隔有人知道如何用 theta 和范围内的轴刻度来可视化极坐标网格吗?
您可以使用pyplot.pcolormesh()
绘制转换后的网格:
import numpy as np
import pylab as pl
img = pl.imread("c:/tmp/Wnov4.png")
angle_max = np.deg2rad(65)
h, w = img.shape
angle, r = np.mgrid[-angle_max:angle_max:h*1j, 0:20:w*1j]
x = r * np.sin(angle)
y = r * np.cos(angle)
fig, ax = pl.subplots()
ax.set_aspect("equal")
pl.pcolormesh(x, y, img, cmap="gray");
或者你可以使用 OpenCV 中的 remap()
将其转换为新图像:
import cv2
import numpy as np
from PIL import Image
img = cv2.imread(r"c:/tmp/Wnov4.png", cv2.IMREAD_GRAYSCALE)
angle_max = np.deg2rad(65)
r_max = 20
x = np.linspace(-20, 20, 800)
y = np.linspace(20, 0, 400)
y, x = np.ix_(y, x)
r = np.hypot(x, y)
a = np.arctan2(x, y)
map_x = r / r_max * img.shape[1]
map_y = a / (2 * angle_max) * img.shape[0] + img.shape[0] * 0.5
img2 = cv2.remap(img, map_x.astype(np.float32), map_y.astype(np.float32), cv2.INTER_CUBIC)
Image.fromarray(img2)