added morphology functions and support for IplConvKernel

This commit is contained in:
sccolbert
2009-10-23 20:40:05 +02:00
parent e44bb5bd00
commit c2bfc99818
8 changed files with 4385 additions and 1507 deletions
File diff suppressed because it is too large Load Diff
+7
View File
@@ -7,6 +7,7 @@ cdef extern from "Python.h":
cdef extern from "numpy/arrayobject.h":
object PyArray_Empty(int, np.npy_intp*, dtype, int)
bint PyArray_ISCONTIGUOUS(np.ndarray)
ctypedef np.uint8_t UINT8_t
ctypedef np.int8_t INT8_t
@@ -44,3 +45,9 @@ cdef np.npy_intp* clone_array_shape(np.ndarray arr)
#-------------------------------------------------------------------------------
cdef CvPoint2D32f* array_as_cvPoint2D32f_ptr(np.ndarray arr)
cdef CvTermCriteria get_cvTermCriteria(int, double)
cdef IplConvKernel* get_IplConvKernel_ptr_from_array(np.ndarray arr, anchor) except NULL
cdef void free_IplConvKernel(IplConvKernel* iplkernel)
#-------------------------------------------------------------------------------
# Other convienences
#-------------------------------------------------------------------------------
+61 -2
View File
@@ -4,7 +4,7 @@ cimport numpy as np
from python cimport *
from opencv_constants import *
from opencv_type cimport *
from _libimport import cxcore
from _libimport import cv, cxcore
# setup numpy tables for this module
np.import_array()
@@ -108,6 +108,7 @@ cdef CvMat* cvmat_ptr_from_iplimage(IplImage* arr):
return mat_hdr
cdef int validate_array(np.ndarray arr) except -1:
assert PyArray_ISCONTIGUOUS(arr), 'Array must be contiguous'
if arr.ndim != 2 and arr.ndim != 3:
raise ValueError('Arrays must have either 2 or 3 dimensions')
if arr.ndim == 3:
@@ -187,7 +188,7 @@ cdef np.ndarray new_array_like_diff_dtype(np.ndarray arr, 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
# make sure you call PyMem_Free after you're done with the shape
cdef int ndim = arr.ndim
cdef np.npy_intp* shape = <np.npy_intp*>PyMem_Malloc(
ndim * sizeof(np.npy_intp))
@@ -223,3 +224,61 @@ cdef CvTermCriteria get_cvTermCriteria(int iterations, double epsilon):
crit.max_iter = 0
crit.epsilon = epsilon
return crit
ctypedef IplConvKernel* (*cvCreateStructuringElementExPtr)(int, int, int, int,
int, int*)
cdef cvCreateStructuringElementExPtr c_cvCreateStructuringElementEx
c_cvCreateStructuringElementEx = (<cvCreateStructuringElementExPtr*><size_t>
ctypes.addressof(cv.cvCreateStructuringElementEx))[0]
ctypedef void (*cvReleaseStructuringElementPtr)(IplConvKernel**)
cdef cvReleaseStructuringElementPtr c_cvReleaseStructuringElement
c_cvReleaseStructuringElement = (<cvReleaseStructuringElementPtr*><size_t>
ctypes.addressof(cv.cvReleaseStructuringElement))[0]
cdef IplConvKernel* get_IplConvKernel_ptr_from_array(np.ndarray arr, anchor) \
except NULL:
# make sure you call free_IplConvKernel you're done with the kernel
validate_array(arr)
assert_ndims(arr, [2])
assert_dtype(arr, [INT32])
cdef int rows
cdef int cols
cdef int anchorx
cdef int anchory
if anchor is not None:
assert len(anchor) == 2, 'anchor must be (x, y) tuple'
anchorx = <int>anchor[0]
anchory = <int>anchor[1]
assert (anchorx < arr.shape[1]) and (anchorx >= 0) \
and (anchory < arr.shape[0]) and (anchory >= 0), \
'anchor point must be inside kernel'
else:
anchorx = <int>(arr.shape[1] / 2.)
anchory = <int>(arr.shape[0] / 2.)
rows = arr.shape[0]
cols = arr.shape[1]
cdef int* values = <int*>arr.data
# this function copies the data from the array into (i'm guessing)
# aligned memory. Since this is using opencv memory management
# the free_IplConvKernel function makes the appropriate calls to free it
cdef IplConvKernel* iplkernel = \
c_cvCreateStructuringElementEx(cols, rows, anchorx, anchory,
CV_SHAPE_CUSTOM, values)
return iplkernel
cdef void free_IplConvKernel(IplConvKernel* iplkernel):
c_cvReleaseStructuringElement(&iplkernel)
#-------------------------------------------------------------------------------
# Other convienences
#-------------------------------------------------------------------------------
+11
View File
@@ -22,6 +22,17 @@ CV_INTER_AREA = 3
CV_WARP_FILL_OUTLIERS = 8
CV_WARP_INVERSE_MAP = 16
CV_SHAPE_RECT = 0
CV_SHAPE_CROSS = 1
CV_SHAPE_ELLIPSE = 2
CV_SHAPE_CUSTOM = 100
CV_MOP_OPEN = 2
CV_MOP_CLOSE = 3
CV_MOP_GRADIENT = 4
CV_MOP_TOPHAT = 5
CV_MOP_BLACKHAT = 6
#########################
# Calibration Constants #
#########################
File diff suppressed because it is too large Load Diff
+155
View File
@@ -111,6 +111,28 @@ cdef cvWarpPerspectivePtr c_cvWarpPerspective
c_cvWarpPerspective = (<cvWarpPerspectivePtr*><size_t>
ctypes.addressof(cv.cvWarpPerspective))[0]
# cvLogPolar
ctypedef void (*cvLogPolarPtr)(IplImage*, IplImage*, CvPoint2D32f, double, int)
cdef cvLogPolarPtr c_cvLogPolar
c_cvLogPolar = (<cvLogPolarPtr*><size_t>ctypes.addressof(cv.cvLogPolar))[0]
# cvErode
ctypedef void (*cvErodePtr)(IplImage*, IplImage*, IplConvKernel*, int)
cdef cvErodePtr c_cvErode
c_cvErode = (<cvErodePtr*><size_t>ctypes.addressof(cv.cvErode))[0]
# cvDilate
ctypedef void (*cvDilatePtr)(IplImage*, IplImage*, IplConvKernel*, int)
cdef cvDilatePtr c_cvDilate
c_cvDilate = (<cvDilatePtr*><size_t>ctypes.addressof(cv.cvDilate))[0]
# cvMorphologyEx
ctypedef void (*cvMorphologyExPtr)(IplImage*, IplImage*, IplImage*,
IplConvKernel*, int, int)
cdef cvMorphologyExPtr c_cvMorphologyEx
c_cvMorphologyEx = (<cvMorphologyExPtr*><size_t>
ctypes.addressof(cv.cvMorphologyEx))[0]
# cvCalibrateCamera2
ctypedef void (*cvCalibrateCamera2Ptr)(CvMat*, CvMat*, CvMat*,
CvSize, CvMat*, CvMat*, CvMat*, CvMat*, int)
@@ -764,6 +786,139 @@ def cvWarpPerspective(np.ndarray src, np.ndarray warpmat,
return out
def cvLogPolar(np.ndarray src, center, double M,
int flags=CV_INTER_LINEAR+CV_WARP_FILL_OUTLIERS):
validate_array(src)
assert len(center) == 2
cdef np.ndarray out = new_array_like(src)
cdef CvPoint2D32f cv_center
cv_center.x = <float>center[0]
cv_center.y = <float>center[1]
cdef IplImage srcimg
cdef IplImage outimg
populate_iplimage(src, &srcimg)
populate_iplimage(out, &outimg)
c_cvLogPolar(&srcimg, &outimg, cv_center, M, flags)
return out
def cvErode(np.ndarray src, np.ndarray element=None, int iterations=1,
anchor=None, in_place=False):
validate_array(src)
cdef np.ndarray out
cdef IplConvKernel* iplkernel
if element == None:
iplkernel = NULL
else:
iplkernel = get_IplConvKernel_ptr_from_array(element, anchor)
if in_place:
out = src
else:
out = new_array_like(src)
cdef IplImage srcimg
cdef IplImage outimg
populate_iplimage(src, &srcimg)
populate_iplimage(out, &outimg)
c_cvErode(&srcimg, &outimg, iplkernel, iterations)
free_IplConvKernel(iplkernel)
if in_place:
return None
else:
return out
def cvDilate(np.ndarray src, np.ndarray element=None, int iterations=1,
anchor=None, in_place=False):
validate_array(src)
cdef np.ndarray out
cdef IplConvKernel* iplkernel
if element == None:
iplkernel = NULL
else:
iplkernel = get_IplConvKernel_ptr_from_array(element, anchor)
if in_place:
out = src
else:
out = new_array_like(src)
cdef IplImage srcimg
cdef IplImage outimg
populate_iplimage(src, &srcimg)
populate_iplimage(out, &outimg)
c_cvDilate(&srcimg, &outimg, iplkernel, iterations)
free_IplConvKernel(iplkernel)
if in_place:
return None
else:
return out
def cvMorphologyEx(np.ndarray src, np.ndarray element, int operation,
int iterations=1, anchor=None, in_place=False):
validate_array(src)
cdef np.ndarray out
cdef np.ndarray temp
cdef IplConvKernel* iplkernel
iplkernel = get_IplConvKernel_ptr_from_array(element, anchor)
if in_place:
out = src
else:
out = new_array_like(src)
cdef IplImage srcimg
cdef IplImage outimg
cdef IplImage tempimg
cdef IplImage* tempimgptr = &tempimg
populate_iplimage(src, &srcimg)
populate_iplimage(out, &outimg)
# determine if we need the tempimg
if operation == CV_MOP_OPEN or operation == CV_MOP_CLOSE:
tempimgptr = NULL
elif operation == CV_MOP_GRADIENT:
temp = new_array_like(src)
populate_iplimage(temp, &tempimg)
elif operation == CV_MOP_TOPHAT or operation == CV_MOP_BLACKHAT:
if in_place:
temp = new_array_like(src)
populate_iplimage(temp, &tempimg)
else:
tempimgptr = NULL
else:
raise RuntimeError('operation type not understood')
c_cvMorphologyEx(&srcimg, &outimg, tempimgptr, iplkernel, operation,
iterations)
free_IplConvKernel(iplkernel)
if in_place:
return None
else:
return out
def cvCalibrateCamera2(np.ndarray object_points, np.ndarray image_points,
np.ndarray point_counts, image_size):
+9
View File
@@ -66,4 +66,13 @@ cdef struct CvTermCriteria:
cdef struct CvScalar:
double val[4]
cdef struct _IplConvKernel:
int nCols
int nRows
int anchorX
int anchorY
int *values
int nShiftR
ctypedef _IplConvKernel IplConvKernel
+48 -7
View File
@@ -15,7 +15,7 @@ with warnings.catch_warnings():
opencv_skip = dec.skipif(cv is None,
'OpenCV libraries not found')
class OpenCVTest:
class OpenCVTest(object):
lena_RGB_U8 = np.load(os.path.join(data_dir, 'lena_RGB_U8.npy'))
lena_GRAY_U8 = np.load(os.path.join(data_dir, 'lena_GRAY_U8.npy'))
@@ -70,7 +70,7 @@ class TestSmooth(OpenCVTest):
cvSmooth(self.lena_GRAY_U8, None, st, 3, 0, 0, 0, False)
class TestFindCornerSubPix:
class TestFindCornerSubPix(object):
@opencv_skip
def test_cvFindCornersSubPix(self):
img = np.array([[1, 1, 1, 0, 0, 0, 1, 1, 1],
@@ -133,7 +133,45 @@ class TestWarpPerspective(OpenCVTest):
cvWarpPerspective(self.lena_RGB_U8, warpmat)
class TestFindChessboardCorners:
class TestLogPolar(OpenCVTest):
@opencv_skip
def test_cvLogPolar(self):
img = self.lena_RGB_U8
width = img.shape[1]
height = img.shape[0]
x = width / 2.
y = height / 2.
cvLogPolar(img, (x, y), 20)
class TestErode(OpenCVTest):
@opencv_skip
def test_cvErode(self):
kern = np.array([[0, 1, 0],
[1, 1, 1],
[0, 1, 0]], dtype='int32')
cvErode(self.lena_RGB_U8, kern, in_place=True)
class TestDilate(OpenCVTest):
@opencv_skip
def test_cvDilate(self):
kern = np.array([[0, 1, 0],
[1, 1, 1],
[0, 1, 0]], dtype='int32')
cvDilate(self.lena_RGB_U8, kern, in_place=True)
class TestMorphologyEx(OpenCVTest):
@opencv_skip
def test_cvMorphologyEx(self):
kern = np.array([[0, 1, 0],
[1, 1, 1],
[0, 1, 0]], dtype='int32')
cvMorphologyEx(self.lena_RGB_U8, kern, CV_MOP_TOPHAT, in_place=True)
class TestFindChessboardCorners(object):
@opencv_skip
def test_cvFindChessboardCorners(self):
chessboard_GRAY_U8 = np.load(os.path.join(data_dir,
@@ -141,7 +179,7 @@ class TestFindChessboardCorners:
pts = cvFindChessboardCorners(chessboard_GRAY_U8, (7, 7))
class TestDrawChessboardCorners:
class TestDrawChessboardCorners(object):
@opencv_skip
def test_cvDrawChessboardCorners(self):
chessboard_GRAY_U8 = np.load(os.path.join(data_dir,
@@ -150,9 +188,11 @@ class TestDrawChessboardCorners:
'chessboard_RGB_U8.npy'))
corners = cvFindChessboardCorners(chessboard_GRAY_U8, (7, 7))
cvDrawChessboardCorners(chessboard_RGB_U8, (7, 7), corners)
class TestCvCalibrateCamera2(object):
def test_cvCalibrateCamear2_Identity(self):
class TestCalibrateCamera2(object):
@opencv_skip
def test_cvCalibrateCamera2_Identity(self):
ys = xs = range(4)
image_points = np.array( [(4 * x, 4 * y) for x in xs for y in ys ],
@@ -177,8 +217,9 @@ class TestCvCalibrateCamera2(object):
assert_almost_equal( intrinsics[2,1], 0)
assert_almost_equal( intrinsics[2,2], 1)
@opencv_skip
@dec.slow
def test_cvCalibrateCamear2_KnownData(self):
def test_cvCalibrateCamera2_KnownData(self):
(object_points,points_count,image_points,intrinsics,distortions) =\
cPickle.load(open(os.path.join(
data_dir, "cvCalibrateCamera2TestData.pck"), "rb")