【问题标题】:Python: Show cartesian image in polar plotPython:在极坐标图中显示笛卡尔图像
【发布时间】:2019-12-13 12:11:11
【问题描述】:

说明:

我将这些数据表示在具有 256 列和 640 行的笛卡尔坐标系中。每列代表一个角度,theta,从 -65 度到 65 度。每行代表一个范围 r,从 0 到 20 m。

下面给出一个例子:

使用以下代码,我尝试制作一个网格并将每个像素位置转换为它在极坐标网格上的位置:

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

这会返回如下图像:

问题:

这是我真正想要实现的目标,除了两件事:

1) 新图像中似乎有一些伪影,而且映射似乎有点粗糙。

有人对如何进行插值以摆脱这种情况提出建议吗?

2) 图像仍以笛卡尔表示,这意味着我没有任何极坐标网格线,也无法可视化范围/角度的间隔。

有人知道如何用 theta 和范围内的轴刻度来可视化极坐标网格吗?

【问题讨论】:

  • 你有什么问题?
  • 现在应该清楚了。

标签: python numpy math image-processing signal-processing


【解决方案1】:

您可以使用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)

【讨论】:

  • 这正是我要找的!谢谢!关于 OpenCV 方法 - 关于如何引入极坐标网格线的任何想法?
猜你喜欢
  • 1970-01-01
  • 2013-08-05
  • 1970-01-01
  • 1970-01-01
  • 2016-04-14
  • 2013-10-10
  • 2015-03-13
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多