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:
On Question 1 above I read an image and use its coordinates to create a meshgrid. I obtain the
X, Ycoordinate 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)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.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:
- Read image from file, take its dimensions
- Create a Cartesian plane using a meshgrid from [-1,1] (not sure why)
- Transform the Cartesian plane into the complex plane ->
log_deformation() - Apply Log on the complex plane
- Apply the odd transformation:
create_X_new_and_Y_new(x, y, row, col)(not sure why) - Map the image from the original plane to the transformed plane above ->
cv2.map() - Save
Thank you very much!