def convertToLogPolar(img, centerTrans, angleStep, logBase, mode = "nearest"):
if mode == "nearest":
# Step 1 - Initialize transformed image
transformedImage = np.zeros(img.shape, dtype = img.dtype)
# Step 2 - Apply reverse log polar transformation
for radius in range(img.shape[COLS_AXIS]): # start with radius, because calculating exponential power is time consuming
actRadius = logBase ** radius
for angle in range(img.shape[ROWS_AXIS]):
anglePi = angle * angleStep
# calculate euclidian coordinates (source: https://en.wikipedia.org/wiki/Log-polar_coordinates)
row = int(centerTrans[ROWS_AXIS] + actRadius * math.sin(anglePi))
col = int(centerTrans[COLS_AXIS] + actRadius * math.cos(anglePi))
# copy pixel from the location to log polar image
if 0 <= row < img.shape[ROWS_AXIS] and 0 <= col < img.shape[COLS_AXIS]:
transformedImage[angle, radius] = img[row, col]
return transformedImage
else:
print("Base: " + str(logBase))
# create matrix with angles
anglesMap = np.zeros(img.shape, dtype=np.float64)
# each column has 0 in its first row and -pi in its last row
anglesVector = -np.linspace(0, np.pi, img.shape[0], endpoint=False)
# initialize it by columns using the same vector
anglesMap.T[:] = anglesVector
# create matrix with radii
radiusMap = np.zeros(img.shape, dtype=np.float64)
# each line contains a vector with numbers from in (0, cols) to power logBase
radiusVector = np.power(logBase, np.arange(img.shape[1], dtype=np.float64)) - 1.0
# initialize it by rows using the same vector
radiusMap[:] = radiusVector
# calculate x coordinates (source: https://en.wikipedia.org/wiki/Log-polar_coordinates)
x = radiusMap * np.sin(anglesMap) + centerTrans[1]
# calculate y coordinates (source: https://en.wikipedia.org/wiki/Log-polar_coordinates)
y = radiusMap * np.cos(anglesMap) + centerTrans[0]
# initialize final image
outputImg = np.zeros(img.shape)
# use spline interpolation to map pixels from original image to calculated coordinates
ndii.map_coordinates(img, [x, y], output=outputImg)
return outputImg
# computes phase correlation and returns position of pixel with highest value (row, column)
评论列表
文章目录