Python Forum

Full Version: Image conversion form cartesian to polar and back to cartesian
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I am trying to convert Image from form Cartesian to polar and back to Cartesian but there is loss in the image, please help me to optimize my code

--------with scipy-----------------
import numpy as np
import cv2
from scipy.ndimage.interpolation import geometric_transform

def topolar(img, order=5):
max_radius = 0.5*np.linalg.norm( img.shape )
def transform(coords):
theta = 2.0*np.pi*coords[1] / (img.shape[1] - 1.)
radius = max_radius * coords[0] / img.shape[0]
i = 0.5*img.shape[0] - radius*np.sin(theta)
j = radius*np.cos(theta) + 0.5*img.shape[1]
return i,j

polar = geometric_transform(img, transform, order=order,mode='nearest',prefilter=True)
return polar

def tocart(img, order=5):
max_radius = 0.5*np.linalg.norm( img.shape )
def transform(coords):
xindex,yindex = coords
x0, y0 = (255,255)
x = xindex - x0
y = yindex - y0
r = np.sqrt(x ** 2.0 + y ** 2.0)*( img.shape[1]/max_radius)
theta = np.arctan2(y, x,where=True)
theta_index = (theta + np.pi) * img.shape[1] / (2 * np.pi)
return (r,theta_index)
polar = geometric_transform(img, transform, order=order,mode='nearest',prefilter=True)
return polar


img1=cv2.imread("/home/software/Desktop/nanotag/project4_fft/images/test_label.tif",0)
img = np.asarray(img1,dtype=np.float64)
pol = topolar(img)
res = tocart(pol)
pol1=pol/255
res = res/255
cv2.imshow('linearpolar2', pol1)
cv2.imshow('linearpolar3', res)
cv2.waitKey(0)

------with opencv------------
import cv2
import numpy as np

source = cv2.imread(IMAGE,0)
img64_float = source.astype(np.float64)

Mvalue = np.sqrt(((img64_float.shape[0]/2.0)**2.0)+((img64_float.shape[1]/2.0)**2.0))


ploar_image = cv2.linearPolar(img64_float,(img64_float.shape[0]/2, img64_float.shape[1]/2),Mvalue,cv2.WARP_FILL_OUTLIERS)

cartisian_image = cv2.linearPolar(ploar_image, (img64_float.shape[0]/2, img64_float.shape[1]/2),Mvalue, cv2.WARP_INVERSE_MAP)

cartisian_image = cartisian_image/200
ploar_image = ploar_image/255
cv2.imshow("log-polar1", ploar_image)
cv2.imshow("log-polar2", cartisian_image)

cv2.waitKey(0)
cv2.destroyAllWindows()
Please fix/repost your code using CODE tags and preserved indentation
--------with scipy-----------------
import numpy as np
import cv2
from scipy.ndimage.interpolation import geometric_transform

def topolar(img, order=5):
    max_radius = 0.5*np.linalg.norm( img.shape )
    def transform(coords):
        theta = 2.0*np.pi*coords[1] / (img.shape[1] - 1.)
        radius = max_radius * coords[0] / img.shape[0]
        i = 0.5*img.shape[0] - radius*np.sin(theta)
        j = radius*np.cos(theta) + 0.5*img.shape[1]
        return i,j

     polar = geometric_transform(img, transform, order=order,mode='nearest',prefilter=True)
return polar

"""convert polar to cartesian"""
def tocart(img, order=5):
    max_radius = 0.5*np.linalg.norm( img.shape )
    def transform(coords):
        xindex,yindex = coords
        x0, y0 = (255,255)
        x = xindex - x0
        y = yindex - y0
        r = np.sqrt(x ** 2.0 + y ** 2.0)*( img.shape[1]/max_radius)
        theta = np.arctan2(y, x,where=True)
        theta_index = (theta + np.pi) * img.shape[1] / (2 * np.pi)
        return (r,theta_index)
    polar = geometric_transform(img, transform, order=order,mode='nearest',prefilter=True)
return polar


img1=cv2.imread("Source Image ",0)
img = np.asarray(img1,dtype=np.float64)
pol = topolar(img)
res = tocart(pol)
pol1=pol/255
res = res/255
cv2.imshow('linearpolar2', pol1)
cv2.imshow('linearpolar3', res)
cv2.waitKey(0)
--------OPENCV METHOD----------------
import cv2
import numpy as np

source = cv2.imread("IMAGE",0)
img64_float = source.astype(np.float64)

Mvalue = np.sqrt(((img64_float.shape[0]/2.0)**2.0)+((img64_float.shape[1]/2.0)**2.0))


ploar_image = cv2.linearPolar(img64_float,(img64_float.shape[0]/2, img64_float.shape[1]/2),Mvalue,cv2.WARP_FILL_OUTLIERS)

cartisian_image = cv2.linearPolar(ploar_image, (img64_float.shape[0]/2, img64_float.shape[1]/2),Mvalue, cv2.WARP_INVERSE_MAP)

cartisian_image = cartisian_image/200
ploar_image = ploar_image/255
cv2.imshow("log-polar1", ploar_image)
cv2.imshow("log-polar2", cartisian_image)

cv2.waitKey(0)
cv2.destroyAllWindows()

Thank you buran, waiting for needful help from all

Source Image is 512*512