import cv2
import numpy as np

img = cv2.imread('rainbow2.jpg')


centre = img.shape[0]//2, img.shape[1]//2
##
##radius = (centre[0]**2 + centre[1]**2)**0.5
##
##res = cv2.linearPolar(img, centre, radius, 8)
##
##cv2.imshow('polar',res)
##
####
###cv2.imwrite('polar.png', res)
##
####cv2.imshow('src',img)
##
##
##logPolar = cv2.logPolar(img, centre, 80, 8)
####cv2.imshow('logPOlar', logPolar)


#log transform
def log():
    map_x = np.zeros(img.shape[:-1])
    map_y = np.zeros(img.shape[:-1])

    cols = img.shape[1]
    rows = img.shape[0]

    LOG_BASE = 1.017

    for i in range(rows):
        for j in range(cols):
            map_x[i,j] = LOG_BASE**j
            map_y[i,j] = LOG_BASE**i

    map_x = map_x.astype(np.float32)
    map_y = map_y.astype(np.float32)



    log = cv2.remap(img, map_x, map_y, 1)
    log_res = log[:355,:363]

    # cv2.CV_WARP_FILL_OUTLIERS = 8
    # CV_INTER_LINEAR = 1

    ##cv2.imshow('log',log_res)
    ##cv2.imwrite('log.png', log_res)

def myPolar():
    cols = img.shape[1]
    rows = img.shape[0]

    map_x = np.zeros(img.shape[:-1])
    map_y = np.zeros(img.shape[:-1])

    theta_d = 2*np.pi / rows
    theta = 0

    for i in range(rows):
        for j in range(cols):
            map_x[i,j] = np.sin(theta)*j + centre[1]
            map_y[i,j] = np.cos(theta)*j + centre[0]
        theta += theta_d

    map_x = map_x.astype(np.float32)
    map_y = map_y.astype(np.float32)

    pol = cv2.remap(img, map_x, map_y, 1)
    cv2.imshow('bbb', pol)
    cv2.imwrite('myPolar.png', pol)


cols = img.shape[1]
rows = img.shape[0]

map_x = np.zeros(img.shape[:-1])
map_y = np.zeros(img.shape[:-1])

d = (centre[0]**2 + centre[1]**2)**0.5
K = 10**(np.log10(d) / rows)

theta_d = 2*np.pi / rows
theta = 0

for i in range(rows):
    for j in range(cols):
        radius = (K**j)
        map_x[i,j] = np.sin(theta)*radius + centre[1]
        map_y[i,j] = np.cos(theta)*radius + centre[0]
    theta += theta_d

map_x = map_x.astype(np.float32)
map_y = map_y.astype(np.float32)

logPol = cv2.remap(img, map_x, map_y, 1)
cv2.imshow('bbb', logPol)
cv2.imwrite('myLogPol.png', logPol)






cv2.waitKey(10000)



