added cvGoodFeaturesToTrack. Added clone_array_shape function.

I should at one point turn that into a type that does its own memory management.
This commit is contained in:
sccolbert
2009-10-13 00:31:40 +02:00
parent 1076de0949
commit 73f85b0d3a
6 changed files with 1961 additions and 1197 deletions
File diff suppressed because it is too large Load Diff
+9 -1
View File
@@ -18,7 +18,6 @@ ctypedef np.float64_t FLOAT64_t
#-------------------------------------------------------------------------------
# Utility functions for IplImage creation, array validation, etc...
#-------------------------------------------------------------------------------
cdef void populate_iplimage(np.ndarray arr, IplImage* img)
cdef int validate_array(np.ndarray arr) except -1
cdef int assert_dtype(np.ndarray arr, dtypes) except -1
@@ -29,9 +28,18 @@ cdef int assert_same_shape(np.ndarray arr1, np.ndarray arr2) except -1
cdef int assert_same_width_and_height(np.ndarray arr1, np.ndarray arr2) except -1
cdef int assert_like(np.ndarray arr1, np.ndarray arr2) except -1
cdef int assert_not_sharing_data(np.ndarray arr1, np.ndarray arr2) except -1
#-------------------------------------------------------------------------------
# NumPy convienences
#-------------------------------------------------------------------------------
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 np.npy_intp get_array_nbytes(np.ndarray arr)
cdef np.npy_intp* clone_array_shape(np.ndarray arr)
#-------------------------------------------------------------------------------
# OpenCV convienences
#-------------------------------------------------------------------------------
cdef CvPoint2D32f* array_as_cvPoint2D32f_ptr(np.ndarray arr)
cdef CvTermCriteria get_cvTermCriteria(int, double)
+22 -6
View File
@@ -1,5 +1,6 @@
import numpy as np
cimport numpy as np
from python cimport *
from opencv_constants import *
from opencv_type cimport *
@@ -140,7 +141,10 @@ cdef int assert_not_sharing_data(np.ndarray arr1, np.ndarray arr2) except -1:
raise ValueError('In place operation not supported. Make sure \
the out array is not just a view of src array')
return 1
#-------------------------------------------------------------------------------
# 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(<object>dtype)
@@ -156,15 +160,27 @@ cdef np.ndarray new_array_like_diff_dtype(np.ndarray arr, dtype):
Py_INCREF(<object>dtype)
return PyArray_Empty(arr.ndim, arr.shape, dtype, 0)
cdef CvPoint2D32f* array_as_cvPoint2D32f_ptr(np.ndarray arr):
cdef CvPoint2D32f* point2Darr
point2Darr = <CvPoint2D32f*>arr.data
return point2Darr
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 = <np.npy_intp*>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
point2Darr = <CvPoint2D32f*>arr.data
return point2Darr
cdef CvTermCriteria get_cvTermCriteria(int iterations, double epsilon):
cdef CvTermCriteria crit
if iterations and epsilon:
File diff suppressed because it is too large Load Diff
+80 -20
View File
@@ -1,6 +1,8 @@
import ctypes
import numpy as np
cimport numpy as np
from python cimport *
#from stdlib cimport *
from opencv_type cimport *
from opencv_backend import *
from opencv_backend cimport *
@@ -66,6 +68,13 @@ ctypedef void (*cvSmoothPtr)(IplImage*, IplImage*, int, int, int, double, double
cdef cvSmoothPtr c_cvSmooth
c_cvSmooth = (<cvSmoothPtr*><size_t>ctypes.addressof(cv.cvSmooth))[0]
# cvGoodFeaturesToTrack
ctypedef void (*cvGoodFeaturesToTrackPtr)(IplImage*, IplImage*, IplImage*,
CvPoint2D32f*, int*, double, double,
IplImage*, int, int, double)
cdef cvGoodFeaturesToTrackPtr c_cvGoodFeaturesToTrack
c_cvGoodFeaturesToTrack = (<cvGoodFeaturesToTrackPtr*><size_t>ctypes.addressof(cv.cvGoodFeaturesToTrack))[0]
# cvResize
ctypedef void (*cvResizePtr)(IplImage*, IplImage*, int)
cdef cvResizePtr c_cvResize
@@ -435,6 +444,69 @@ def cvSmooth(np.ndarray src, np.ndarray out=None, int smoothtype=CV_GAUSSIAN, in
return out
def cvGoodFeaturesToTrack(np.ndarray src, int corner_count, double quality_level,
double min_distance, np.ndarray mask=None,
int block_size=3, int use_harris=0, double k=0.04):
"""
better doc string needed.
for now:
http://opencv.willowgarage.com/documentation/cvreference.html
"""
validate_array(src)
assert_dtype(src, [UINT8, FLOAT32])
assert_nchannels(src, [1])
cdef np.ndarray eig = new_array_like_diff_dtype(src, FLOAT32)
cdef np.ndarray temp = new_array_like(eig)
cdef CvPoint2D32f* corners = (
<CvPoint2D32f*>PyMem_Malloc(corner_count * sizeof(CvPoint2D32f)))
cdef int out_corner_count
out_corner_count = corner_count
cdef IplImage srcimg
cdef IplImage eigimg
cdef IplImage tempimg
cdef IplImage *maskimg
populate_iplimage(src, &srcimg)
populate_iplimage(eig, &eigimg)
populate_iplimage(temp, &tempimg)
if mask is None:
maskimg = NULL
else:
validate_array(mask)
assert_nchannels(mask, [1])
populate_iplimage(mask, maskimg)
c_cvGoodFeaturesToTrack(&srcimg, &eigimg, &tempimg, corners, &out_corner_count,
quality_level, min_distance, maskimg, block_size,
use_harris, k)
# since the maximum allowed corners may not have been found
# the array might be too long, we create a new array and copy
# the the data into it
#
# It would be nice to use the numpy C-Api for this, but I couldn't quite
# get it to work
cdef np.npy_intp cornershape[2]
cornershape[0] = <np.npy_intp>out_corner_count
cornershape[1] = 2
cdef np.ndarray cornersarr = new_array(2, cornershape, FLOAT32)
cdef int i
for i in range(out_corner_count):
cornersarr[i,0] = corners[i].x
cornersarr[i,1] = corners[i].y
PyMem_Free(corners)
return cornersarr
def cvResize(np.ndarray src, height=None, width=None,
int method=CV_INTER_LINEAR):
"""
@@ -447,28 +519,16 @@ def cvResize(np.ndarray src, height=None, width=None,
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)
cdef int ndim = src.ndim
cdef np.npy_intp* shape = clone_array_shape(src)
shape[0] = height
shape[1] = width
cdef np.ndarray out = new_array(ndim, shape, src.dtype)
validate_array(out)
PyMem_Free(shape)
cdef IplImage srcimg
cdef IplImage outimg
populate_iplimage(src, &srcimg)
+8 -2
View File
@@ -89,13 +89,19 @@ class TestFindCornerSubPix:
cvFindCornerSubPix(img, corners, 4, (2, 2))
class TestGoodFeaturesToTrack(OpenCVTest):
@_opencv_skip
def test_cvGoodFeaturesToTrack(self):
cvGoodFeaturesToTrack(self.lena_GRAY_U8, 100, 0.1, 3)
class TestResize(OpenCVTest):
@_opencv_skip
def test_cvResize(self):
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()