diff --git a/scikits/image/opencv/opencv_backend.pyx b/scikits/image/opencv/opencv_backend.pyx index 78f6f543..ac2f1716 100644 --- a/scikits/image/opencv/opencv_backend.pyx +++ b/scikits/image/opencv/opencv_backend.pyx @@ -7,14 +7,14 @@ from opencv_type cimport * np.import_array() -#------------------------------------------------------------------------------- +#----------------------------------------------------------------------------- # Data Type Handling -#------------------------------------------------------------------------------- +#----------------------------------------------------------------------------- -# for some reason these have to declared as dtype objects rather than just the +# for some reason these have to declared as dtype objects rather than just the # dtype itself.... -UINT8 = np.dtype('uint8') -INT8 = np.dtype('int8') +UINT8 = np.dtype('uint8') +INT8 = np.dtype('int8') INT16 = np.dtype('int16') INT32 = np.dtype('int32') FLOAT32 = np.dtype('float32') @@ -29,16 +29,16 @@ cdef int IPL_DEPTH_32F = 32 cdef int IPL_DEPTH_64F = 64 -# I'd like a better to associate the IPL data type flag to the proper numpy +# I'd like a better to associate the IPL data type flag to the proper numpy # types without using a dictionary. -_ipltypes = {UINT8: IPL_DEPTH_8U, INT8: IPL_DEPTH_8S, INT16: IPL_DEPTH_16S, - INT32: IPL_DEPTH_32S, FLOAT32: IPL_DEPTH_32F, - FLOAT64: IPL_DEPTH_64F} +_ipltypes = {UINT8: IPL_DEPTH_8U, INT8: IPL_DEPTH_8S, INT16: IPL_DEPTH_16S, + INT32: IPL_DEPTH_32S, FLOAT32: IPL_DEPTH_32F, + FLOAT64: IPL_DEPTH_64F} - -#------------------------------------------------------------------------------- + +#----------------------------------------------------------------------------- # Utility functions for IplImage creation, array validation, etc... -#------------------------------------------------------------------------------- +#----------------------------------------------------------------------------- cdef int IPLIMAGE_SIZE = sizeof(IplImage) @@ -48,7 +48,7 @@ cdef void populate_iplimage(np.ndarray arr, IplImage* img): # function before using this function. # This function assumes that the array has successfully passed # validation - + # everything that will never change img.nSize = IPLIMAGE_SIZE img.ID = 0 @@ -58,29 +58,29 @@ cdef void populate_iplimage(np.ndarray arr, IplImage* img): img.maskROI = NULL img.imageId = NULL img.tileInfo = NULL - + cdef int channels cdef int ndim = arr.ndim cdef np.npy_intp* shape = arr.shape - cdef np.npy_intp* strides = arr.strides - + cdef np.npy_intp* strides = arr.strides + # nChannels is essentially the value of np.shape[2] of a 3D numpy array # for a 2D array, nChannels is 1 if ndim == 2: img.nChannels = 1 else: img.nChannels = shape[2] - + img.depth = _ipltypes[arr.dtype] img.width = shape[1] - img.height = shape[0] + img.height = shape[0] img.imageSize = arr.nbytes img.imageData = arr.data img.widthStep = strides[0] - - # really doesn't matter what this is set to, because opencv only uses it to - # deallocate images, but it will never attempt to deallocate images we - # create ourselves. + + # really doesn't matter what this is set to, because opencv only uses it to + # deallocate images, but it will never attempt to deallocate images we + # create ourselves. img.imageDataOrigin = NULL cdef int validate_array(np.ndarray arr) except -1: @@ -90,47 +90,49 @@ cdef int validate_array(np.ndarray arr) except -1: if arr.shape[2] > 4: raise ValueError('A 3D array must have 4 or less channels') if arr.dtype not in _ipltypes: - raise ValueError('Arrays must have one of the following dtypes: uint8, int8, int16, int32, float32, float64') + raise ValueError('Arrays must have one of the following dtypes: ' + 'uint8, int8, int16, int32, float32, float64') return 1 - + cdef int assert_dtype(np.ndarray arr, dtypes) except -1: if arr.dtype not in dtypes: raise ValueError('Unsupported dtype for this operation. \ Supported dtypes are %s' % str(dtypes)) return 1 - + cdef int assert_ndims(np.ndarray arr, dims) except -1: if arr.ndim not in dims: raise ValueError('Incorrect number of dimensions') return 1 - + cdef int assert_nchannels(np.ndarray arr, channels) except -1: cdef int nchannels if arr.ndim == 2: nchannels = 1 else: - nchannels = arr.shape[2] + nchannels = arr.shape[2] if nchannels not in channels: raise ValueError('Incorrect number of channels') return 1 - + cdef int assert_same_dtype(np.ndarray arr1, np.ndarray arr2) except -1: if arr1.dtype != arr2.dtype: raise ValueError('dtypes not same') return 1 - + cdef int assert_same_shape(np.ndarray arr1, np.ndarray arr2) except -1: if not np.PyArray_SAMESHAPE(arr1, arr2): raise ValueError('arrays not same shape') return 1 - -cdef int assert_same_width_and_height(np.ndarray arr1, np.ndarray arr2) except -1: + +cdef int assert_same_width_and_height(np.ndarray arr1, np.ndarray arr2) \ + except -1: cdef np.npy_intp* shape1 = arr1.shape cdef np.npy_intp* shape2 = arr2.shape if (shape1[0] != shape2[0]) or (shape1[1] != shape2[1]): raise ValueError('Arrays must have same width and height') return 1 - + cdef int assert_like(np.ndarray arr1, np.ndarray arr2) except -1: assert_same_dtype(arr1, arr2) assert_same_shape(arr1, arr2) @@ -142,9 +144,9 @@ cdef int assert_not_sharing_data(np.ndarray arr1, np.ndarray arr2) except -1: the out array is not just a view of src array') return 1 -#------------------------------------------------------------------------------- -# NumPy array convienences -#------------------------------------------------------------------------------- +#----------------------------------------------------------------------------- +# NumPy array convienences +#----------------------------------------------------------------------------- cdef np.ndarray new_array(int ndim, np.npy_intp* shape, dtype): # need to incref because numpy will apprently steal a dtype reference Py_INCREF(dtype) @@ -154,35 +156,36 @@ cdef np.ndarray new_array_like(np.ndarray arr): # need to incref because numpy will apprently steal a dtype reference Py_INCREF(arr.dtype) return PyArray_Empty(arr.ndim, arr.shape, arr.dtype, 0) - + cdef np.ndarray new_array_like_diff_dtype(np.ndarray arr, dtype): - # need to incref because numpy will apprently steal a dtype reference + # need to incref because numpy will apprently steal a dtype reference Py_INCREF(dtype) return PyArray_Empty(arr.ndim, arr.shape, dtype, 0) cdef np.npy_intp* clone_array_shape(np.ndarray arr): # make sure you call PyMem_Free after your done with the shape cdef int ndim = arr.ndim - cdef np.npy_intp* shape = PyMem_Malloc(ndim * sizeof(np.npy_intp)) + cdef np.npy_intp* shape = PyMem_Malloc( + ndim * sizeof(np.npy_intp)) cdef int i for i in range(ndim): shape[i] = arr.shape[i] return shape - + cdef np.npy_intp get_array_nbytes(np.ndarray arr): cdef np.npy_intp nbytes = np.PyArray_NBYTES(arr) return nbytes - -#------------------------------------------------------------------------------- + +#----------------------------------------------------------------------------- # OpenCV convienences -#------------------------------------------------------------------------------- +#----------------------------------------------------------------------------- cdef CvPoint2D32f* array_as_cvPoint2D32f_ptr(np.ndarray arr): - cdef CvPoint2D32f* point2Darr + cdef CvPoint2D32f* point2Darr point2Darr = arr.data return point2Darr cdef CvTermCriteria get_cvTermCriteria(int iterations, double epsilon): - cdef CvTermCriteria crit + cdef CvTermCriteria crit if iterations and epsilon: crit.type = (CV_TERMCRIT_ITER | CV_TERMCRIT_EPS) crit.max_iter = iterations @@ -196,10 +199,3 @@ cdef CvTermCriteria get_cvTermCriteria(int iterations, double epsilon): crit.max_iter = 0 crit.epsilon = epsilon return crit - - - - - - -