1

Description:

I have this data represented in a cartesian coordinate system with 256 columns and 640 rows. Each column represents an angle, theta, from -65 deg to 65 deg. Each row represents a range, r, from 0 to 20 m.

An example is given below:

Cartesian sample

With the following code I try to make a grid and transform each pixel location to the location it would have on a polar grid:

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

This returns an image as below:

polar_sample

Questions:

This is sort of what I actually want to achieve except from two things:

1) there seem to be some artifacts in the new image and also the mapping seems a bit coarse.

Does someone have a suggestion on how to interpolate to get rid of this?

2) The image remains in a Cartesian representation, meaning that I don't have any polar gridlines, nor can I visualize intervals of range/angle.

Anybody know how to visualize the polar grids with axis ticks in theta and range?

2
  • What's your question? Commented Aug 6, 2019 at 0:48
  • Should be clear now. Commented Aug 6, 2019 at 0:50

1 Answer 1

1

You can use pyplot.pcolormesh() to plot the converted mesh:

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

or you can use the remap() in OpenCV to convert it to a new image:

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)
Sign up to request clarification or add additional context in comments.

1 Comment

This was exactly was I was looking for! Thanks! On the OpenCV method - any idea as to how to introduce polar grid lines?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.