Created
January 27, 2016 15:54
-
-
Save sirpercival/c49f91576a591fc8f487 to your computer and use it in GitHub Desktop.
trying to extend ndimage.interpolation.geometric_transform with Cython
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
cimport numpy as np | |
from cpython.cobject cimport PyCObject_FromVoidPtrAndDesc | |
from scipy.ndimage import geometric_transform | |
from astropy.modeling import models, fitting | |
cdef int _shift_function(double* output_coordinates, double* input_coordinates, | |
int output_rank, int input_rank, double* shift_data): | |
cdef np.ndarray[double, ndim=1] shift = np.asarray(<double[:3]> shift_data) | |
cdef Py_ssize_t ii | |
for ii in range(input_rank): | |
if ii == 0: input_coordinates[ii] = output_coordinates[ii] | |
else: | |
input_coordinates[ii] = output_coordinates[ii] + (shift[0] + \ | |
shift[1]*output_coordinates[0] + \ | |
shift[2]*pow(output_coordinates[0],2)) | |
return 1 | |
def undistort_function(np.ndarray[double, ndim=1] shift): | |
"""This is the function callable from python.""" | |
cdef double* shift_data = <double*> shift.data | |
return PyCObject_FromVoidPtrAndDesc(&_shift_function, | |
shift_data, | |
NULL) | |
#my code calls geometric_transform from within a method to a cdef class that includes | |
#a bunch of irrelevant stuff, so this is more of an adaptation than an accurate reproduction | |
def undistort_image(np.ndarray[double, ndim=2] image, | |
np.ndarray[double, ndim=1] trace_x, | |
np.ndarray[double, ndim=1] trace_y, | |
int fit_degree=2): | |
cdef object poly = models.Polynomial1D(fit_degree) | |
cdef object fitter = fitting.LinearLSQFitter() | |
poly = fitter(poly, trace_x, trace_y) | |
return geometric_transform(image, undistort_function(poly.parameters)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment