簡體   English   中英

Python:在極坐標圖中顯示笛卡爾圖像

[英]Python: Show cartesian image in polar plot

描述:

我將這些數據表示在具有 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

這將返回如下圖像:

polar_sample

問題:

這是我真正想要實現的,除了兩件事:

1) 新圖像中似乎有一些偽影,而且映射似乎有點粗糙。

有人有關於如何插值以擺脫這種情況的建議嗎?

2)圖像保持笛卡爾表示,這意味着我沒有任何極坐標網格線,也無法可視化范圍/角度的間隔。

有誰知道如何用θ和范圍內的軸刻度來可視化極坐標網格?

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM