Creating a meshgrid from [-1,1] to apply conformal maps to an image

Viewed 327

I am using a piece of code that performs a conformal mapping operation on an image (it performs a log in the complex plane).

I am not sure if (and why) complex numbers are the reason why one needs a meshgrid that goes specifically from [-1,1]. If anyone has pointers or any reading material it will be appreciated.

QUESTION 1: These are the lines that I don't understand (why it needs to go from -1 to 1?)

image = np.array(cv2.imread(inFile)) #reading an image from file
row, col = image.shape[0], image.shape[1]  #taking dimensions of image
vec1 = np.linspace(-1, 1, num=row, endpoint=True) #creating a vector of equally spaced points
vec2 = np.linspace(-1, 1, num=col, endpoint=True)
X, Y = np.meshgrid(vec2, vec1) #using the previous vectors to create a matrix of points.

QUESTION 2: Also, this part I don't understand, my guess it's that it's reversing the [-1,1] story from before

  def create_X_new_and_Y_new(x, y, row, col):
        Xnew = (x / np.max(np.abs(x)) + 1) * col / 2
        Ynew = (y / np.max(np.abs(y)) + 1) * row / 2
        Xnew = np.clip(Xnew, 0, col - 1)
        Ynew = np.clip(Ynew, 0, row - 1)
        Xnew = np.floor(Xnew).astype(int)
        Ynew = np.floor(Ynew).astype(int)
        return Xnew, Ynew

TO ADD MORE BACKGROUND INFO: The story goes like this:

  1. On Question 1 above I read an image and use its coordinates to create a meshgrid. I obtain the X, Y coordinate vectors from this grid. On a later step, I pass these coordinates into a function that creates a complex plane off them and applies the Log on this complex plane:

    def log_deformation(x,y):
        Z = X + iY
        lnz = np.log(Z)
        return np.real(lnz), np.imag(lnz)
    
  2. Then, the code calls the create_X_new_and_Y_new(x, y, row, col) (from Question 2 above) to create a new plane. I am not sure what is doing there, and why is needed.

  3. Then, to save the new image I map the image's pixels to the newly created plane, and save the results as a new image using: out = cv2.remap(image, x, y, interpolation=cv2.INTER_CUBIC)

The whole code goes like this:

  1. Read image from file, take its dimensions
  2. Create a Cartesian plane using a meshgrid from [-1,1] (not sure why)
  3. Transform the Cartesian plane into the complex plane ->log_deformation()
  4. Apply Log on the complex plane
  5. Apply the odd transformation: create_X_new_and_Y_new(x, y, row, col) (not sure why)
  6. Map the image from the original plane to the transformed plane above ->cv2.map()
  7. Save

Thank you very much!

1 Answers

The mapping of pixel coordinates to the [-1,1] coordinate system is not because the use of complex numbers, but because the complex logarithmic mapping used works best in this range. This mapping maps circles to straight lines, the origin of the coordinate system used is the origin of the circles being mapped, so the [-1,1] coordinate system sets the origin of the mapping in the middle of the image.

The second mapping maps the output of the complex logarithmic mapping to normal pixel coordinates, such that cv2.remap can move the pixels to all the right places.

Related