简体   繁体   中英

How to properly convert image between Cartesian and polar coordinate systems using `cv2.warpPolar`?

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 warps 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 warped back by PhotoShop CS6:

在此处输入图像描述

How do I generate the same results as PhotoShop?


I thought I was clear enough but you didn't get it.

I want the warped image to not be a perfect square, it should have exactly the same resolution and aspect ratio as the input image instead.

And there should be no extra black portions. Just like the effect in PhotoShop.

The picture you "want" is easily achieved by rotating the input by 90 degrees. You want the green and cyan squares in the center? Then rotate counterclockwise by 90 degrees, so they're on the left side, before warping. Then they'll be in the center.

You have to make sure a circle of the given radius fits in the dimensions you specify in dsize .

Use dsize=(2*r,2*r) and center accordingly.

Either that or you have to use a different radius value.

适当的论据

并再次扭曲回来

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM