Python Forum
Image conversion form cartesian to polar and back to cartesian
Thread Rating:
  • 2 Vote(s) - 2 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Image conversion form cartesian to polar and back to cartesian
#1
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()
Reply
#2
Please fix/repost your code using CODE tags and preserved indentation
Reply
#3
--------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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Use pandas to obtain cartesian product between a dataframe of int and equations? haihal 0 1,091 Jan-06-2023, 10:53 PM
Last Post: haihal
Question How can I save cartesian products to a dataframe? noahverner1995 1 1,609 Dec-27-2021, 09:15 AM
Last Post: noahverner1995
  Image conversion help KJDVolc 0 1,776 Dec-18-2018, 12:37 PM
Last Post: KJDVolc

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020