added cvCornerSubPix and cvResize impementations. Added a bash build script.

This commit is contained in:
sccolbert
2009-10-12 19:10:21 +02:00
parent 2e87575625
commit 14f588e0fa
8 changed files with 2335 additions and 862 deletions
File diff suppressed because it is too large Load Diff
+3 -1
View File
@@ -32,4 +32,6 @@ cdef int assert_not_sharing_data(np.ndarray arr1, np.ndarray arr2) except -1
cdef np.ndarray new_array(int ndim, np.npy_intp* shape, dtype)
cdef np.ndarray new_array_like(np.ndarray arr)
cdef np.ndarray new_array_like_diff_dtype(np.ndarray arr, dtype)
cdef CvPoint2D32f* as_2Dpoint_array(np.ndarray arr)
cdef np.npy_intp get_array_nbytes(np.ndarray arr)
cdef CvPoint2D32f* array_as_cvPoint2D32f_ptr(np.ndarray arr)
cdef CvTermCriteria get_cvTermCriteria(int, double)
+29 -3
View File
@@ -1,7 +1,9 @@
import numpy as np
cimport numpy as np
from opencv_constants import *
from opencv_type cimport *
np.import_array()
#-------------------------------------------------------------------------------
@@ -153,11 +155,35 @@ cdef np.ndarray new_array_like_diff_dtype(np.ndarray arr, dtype):
# need to incref because numpy will apprently steal a dtype reference
Py_INCREF(<object>dtype)
return PyArray_Empty(arr.ndim, arr.shape, dtype, 0)
cdef CvPoint2D32f* as_2Dpoint_array(np.ndarray arr):
cdef CvPoint2D32f* array_as_cvPoint2D32f_ptr(np.ndarray arr):
cdef CvPoint2D32f* point2Darr
point2Darr = <CvPoint2D32f*>arr.data
return point2Darr
cdef np.npy_intp get_array_nbytes(np.ndarray arr):
cdef np.npy_intp nbytes = np.PyArray_NBYTES(arr)
return nbytes
cdef CvTermCriteria get_cvTermCriteria(int iterations, double epsilon):
cdef CvTermCriteria crit
if iterations and epsilon:
crit.type = <int>(CV_TERMCRIT_ITER | CV_TERMCRIT_EPS)
crit.max_iter = iterations
crit.epsilon = epsilon
elif iterations and not epsilon:
crit.type = <int>CV_TERMCRIT_ITER
crit.max_iter = iterations
crit.epsilon = 0.
else:
crit.type = <int>CV_TERMCRIT_EPS
crit.max_iter = 0
crit.epsilon = epsilon
return crit
+10 -1
View File
@@ -8,4 +8,13 @@ CV_BLUR_NO_SCALE = 0
CV_BLUR = 1
CV_GAUSSIAN = 2
CV_MEDIAN = 3
CV_BILATERAL = 4
CV_BILATERAL = 4
CV_TERMCRIT_NUMBER = 1
CV_TERMCRIT_ITER = 1
CV_TERMCRIT_EPS = 2
CV_INTER_NN = 0
CV_INTER_LINEAR = 1
CV_INTER_CUBIC = 2
CV_INTER_AREA = 3
File diff suppressed because it is too large Load Diff
+85 -13
View File
@@ -55,18 +55,22 @@ ctypedef void (*cvCornerHarrisPtr)(IplImage*, IplImage*, int, int, double)
cdef cvCornerHarrisPtr c_cvCornerHarris
c_cvCornerHarris = (<cvCornerHarrisPtr*><size_t>ctypes.addressof(cv.cvCornerHarris))[0]
'''
# cvFindCornerSubPix
ctypedef void (*cvFindCornerSubPixPtr)(IplImage*, CvPoint32f*, int, CvSize, CvSize, CvTermCriteria):
ctypedef void (*cvFindCornerSubPixPtr)(IplImage*, CvPoint2D32f*, int, CvSize, CvSize, CvTermCriteria)
cdef cvFindCornerSubPixPtr c_cvFindCornerSubPix
c_cvFindCornerSubPix = (<cvFindCornerSubPixPtr*><size_t><ctypes.addressof(cv.cvFindCornerSubPix))[0]
'''
c_cvFindCornerSubPix = (<cvFindCornerSubPixPtr*><size_t>ctypes.addressof(cv.cvFindCornerSubPix))[0]
# cvSmooth
ctypedef void (*cvSmoothPtr)(IplImage*, IplImage*, int, int, int, double, double)
cdef cvSmoothPtr c_cvSmooth
c_cvSmooth = (<cvSmoothPtr*><size_t>ctypes.addressof(cv.cvSmooth))[0]
# cvResize
ctypedef void (*cvResizePtr)(IplImage*, IplImage*, int)
cdef cvResizePtr c_cvResize
c_cvResize = (<cvResizePtr*><size_t>ctypes.addressof(cv.cvResize))[0]
####################################
# Function Implementations
@@ -311,31 +315,55 @@ def cvCornerHarris(np.ndarray src, int block_size=3, int aperture_size=3,
return out
""" not quite finished with this one
def cvFindCornerSubPix(np.ndarray src, np.ndarray corners, int count, win,
zero_zone, criteria):
zero_zone=(-1, -1), int iterations=0,
double epsilon=1e-5):
"""
better doc string needed.
for now:
http://opencv.willowgarage.com/documentation/cvreference.html
"""
validate_array(src)
validate_array(corners)
assert_nchannels(src, [1])
assert_dtype(src, UINT8)
assert_dtype(src, [UINT8])
assert_nchannels(corners, [1])
assert_dtype(corners, FLOAT32)
assert_dtype(corners, [FLOAT32])
# make sure the number of points
# jives with the elements in the array
# the shape of the array is irrelevant
# because opencv will index it as if it were
# flat anyway
if a.nbytes != (count * 2 * 4):
# flat anyway, but regardless, the validate_array function ensures
# that it is 2D
cdef int nbytes = <int> get_array_nbytes(corners)
if nbytes != (count * 2 * 4):
raise ValueError('the number of declared points is different than exists in the array')
cdef CvPoint32f* cvCorners = as_2Dpoint_array(corners)
cdef CvPoint2D32f* cvcorners = array_as_cvPoint2D32f_ptr(corners)
cdef CvSize cvwin
cvwin.height = <int> win[0]
cvwin.width = <int> win[1]
"""
cdef CvSize cvzerozone
cvzerozone.height = <int> zero_zone[0]
cvzerozone.width = <int> zero_zone[1]
cdef IplImage srcimg
populate_iplimage(src, &srcimg)
cdef CvTermCriteria crit
crit = get_cvTermCriteria(iterations, epsilon)
c_cvFindCornerSubPix(&srcimg, cvcorners, count, cvwin, cvzerozone, crit)
return None
def cvSmooth(np.ndarray src, np.ndarray out=None, int smoothtype=CV_GAUSSIAN, int param1=3,
int param2=0, double param3=0, double param4=0, bool in_place=False):
"""
@@ -406,3 +434,47 @@ def cvSmooth(np.ndarray src, np.ndarray out=None, int smoothtype=CV_GAUSSIAN, in
c_cvSmooth(&srcimg, &outimg, smoothtype, param1, param2, param3, param4)
return out
def cvResize(np.ndarray src, height=None, width=None,
int method=CV_INTER_LINEAR):
"""
better doc string needed.
for now:
http://opencv.willowgarage.com/documentation/cvreference.html
"""
validate_array(src)
if not height or not width:
raise ValueError('width and height must not be none')
cdef int ndims = src.ndim
# we need a copy of the shape in case it has more than one channel
# what follows is a hack because i dont want to mess with malloc right
# now, and it doesnt waste a ton of memory
cdef np.npy_intp* shape2[2]
cdef np.npy_intp* shape3[3]
cdef np.npy_intp* shape
if ndims == 2:
shape = <np.npy_intp*>shape2
shape[0] = <np.npy_intp>height
shape[1] = <np.npy_intp>width
else:
shape = <np.npy_intp*>shape3
shape[0] = <np.npy_intp>height
shape[1] = <np.npy_intp>width
shape[2] = src.shape[2]
cdef np.ndarray out = new_array(ndims, shape, src.dtype)
validate_array(out)
cdef IplImage srcimg
cdef IplImage outimg
populate_iplimage(src, &srcimg)
populate_iplimage(out, &outimg)
c_cvResize(&srcimg, &outimg, method)
return out
+8 -2
View File
@@ -35,10 +35,16 @@ cdef struct _IplImage:
ctypedef _IplImage IplImage
ctypedef struct CvPoint2D32f:
cdef struct CvPoint2D32f:
float x
float y
ctypedef struct CvSize:
cdef struct CvSize:
int width
int height
cdef struct CvTermCriteria:
int type
int max_iter
double epsilon
@@ -69,6 +69,33 @@ class TestSmooth(OpenCVTest):
CV_BILATERAL):
cvSmooth(self.lena_GRAY_U8, None, st, 3, 0, 0, 0, False)
class TestFindCornerSubPix:
@_opencv_skip
def test_cvFindCornersSubPix(self):
img = np.array([[1, 1, 1, 0, 0, 0, 1, 1, 1],
[1, 1, 1, 0, 0, 0, 1, 1, 1],
[1, 1, 1, 0, 0, 0, 1, 1, 1],
[0, 0, 0, 1, 1, 1, 0, 0, 0],
[0, 0, 0, 1, 1, 1, 0, 0, 0],
[0, 0, 0, 1, 1, 1, 0, 0, 0],
[1, 1, 1, 0, 0, 0, 1, 1, 1],
[1, 1, 1, 0, 0, 0, 1, 1, 1],
[1, 1, 1, 0, 0, 0, 1, 1, 1]], dtype='uint8')
corners = np.array([[2, 2],
[2, 5],
[5, 2],
[5, 5]], dtype='float32')
cvFindCornerSubPix(img, corners, 4, (2, 2))
class TestResize(OpenCVTest):
@_opencv_skip
def test_cvResize(self):
cvResize(self.lena_RGB_U8, height=50, width=50, method=CV_INTER_LINEAR)
cvResize(self.lena_RGB_U8, height=200, width=200, method=CV_INTER_CUBIC)
if __name__ == '__main__':
run_module_suite()