How to convert images from Cartesian coordinate system to polar coordinate system and back, using cv2.warpPolar
method, without cropping the view therefore losing details?
I observed that for images that are not perfect squares, in the resultant polar system, lines that are parallel to x-axis will become circles instead of ellipses, so a fair chunk of the image will be out of view, therefore information is lost.
I want the circles to become ellipses of the same aspect ratio as the original image so that all of the converted image is squeezed into the view and no information is lost.
For example, this produces a test image:
import numpy as np
import cv2
img = np.zeros(shape=(1080, 1920, 3), dtype=np.uint8)
img[:, :, 0] = np.linspace(0, 255, 1920, dtype=np.uint8)[np.newaxis, :]
img[:, :, 2] = np.linspace(0, 255, 1080, dtype=np.uint8)[:, np.newaxis]
img[0:180, 0:320, 1] = 255
img[900:1080, 0:320, 1] = 255
img[900:1080, 1600:1920, 1] = 255
img[0:180, 1600:1920, 1] = 255
cv2.imshow('test image', img); cv2.waitKey(0)
cv2.imwrite('D:/test_image.jpg', img)
This warps the test image to polar coordinates:
r = (1920*1920+1080*1080)**.5/2
polar = cv2.warpPolar(img, dsize=(1920, 1080), center=(960, 540), maxRadius=r, flags=cv2.WARP_INVERSE_MAP)
cv2.imshow('polar image', polar); cv2.waitKey(0)
cv2.imwrite('D:/polar_test_image.jpg', polar)
And this wraps it back to Cartesian:
linear = cv2.warpPolar(polar, dsize=(1920, 1080), center=(960, 540), maxRadius=r, flags=cv2.WARP_POLAR_LINEAR)
cv2.imshow('cartesian image', linear); cv2.waitKey(0)
cv2.imwrite('D:/cartesian_test_image.jpg', linear)
But what I want is this:
The above image is converted using PhotoShop CS6.
And wrapped back by PhotoShop CS6:
How do I generate the same results as PhotoShop?