mirror of
https://github.com/wassname/scikit-image.git
synced 2026-07-08 04:18:37 +08:00
Added cvFindChessboardCorners
This commit is contained in:
@@ -1,17 +1,10 @@
|
||||
import ctypes
|
||||
import sys
|
||||
|
||||
# try to open the opencv libs
|
||||
# raise an exception if the libs are not found
|
||||
|
||||
# linux
|
||||
try:
|
||||
ctypes.CDLL('libcv.so')
|
||||
except:
|
||||
# windows
|
||||
try:
|
||||
ctypes.CDLL('cv.dll')
|
||||
except:
|
||||
raise RuntimeError('The opencv libraries were not found. Please make sure they are installed and available on the system path.')
|
||||
from _libimport import cv
|
||||
|
||||
from opencv_constants import *
|
||||
from opencv_cv import *
|
||||
|
||||
@@ -10,12 +10,12 @@ This module also removes the code duplication in __init__ and
|
||||
opencv_cv
|
||||
"""
|
||||
|
||||
__all__ = [ "import_opencv_lib" ]
|
||||
__all__ = [ "cv" ]
|
||||
|
||||
import ctypes
|
||||
import sys
|
||||
|
||||
def import_opencv_lib(which = "cv"):
|
||||
def _import_opencv_lib(which = "cv"):
|
||||
"""
|
||||
Try to import a shared library of OpenCV.
|
||||
|
||||
@@ -32,18 +32,22 @@ def import_opencv_lib(which = "cv"):
|
||||
raise RuntimeError('The opencv libraries were not found. Please make ' \
|
||||
'sure they are installed and available on the system path.')
|
||||
|
||||
return shared_lib
|
||||
|
||||
def _tryload_macosx(which):
|
||||
common_paths = [
|
||||
'/lib/',
|
||||
'/usr/lib/',
|
||||
'/usr/local/lib',
|
||||
'/usr/local/lib/',
|
||||
'/opt/local/lib/', # MacPorts
|
||||
'/sw/lib/', # Fink
|
||||
]
|
||||
shared_lib = None
|
||||
for path in common_paths:
|
||||
try:
|
||||
libpath =path + "lib" + which + '.dylib'
|
||||
shared_lib = ctypes.CDLL(path + "lib" + which + '.dylib')
|
||||
break
|
||||
except OSError, e:
|
||||
if "image not found" in e.args[0]:
|
||||
continue
|
||||
@@ -52,3 +56,5 @@ def _tryload_macosx(which):
|
||||
return shared_lib
|
||||
|
||||
|
||||
cv = _import_opencv_lib("cv")
|
||||
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -11,10 +11,10 @@ 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,13 +29,13 @@ 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...
|
||||
#-------------------------------------------------------------------------------
|
||||
@@ -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 = <char*>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 = <char*>NULL
|
||||
|
||||
cdef int validate_array(np.ndarray arr) except -1:
|
||||
@@ -90,47 +90,47 @@ 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 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)
|
||||
@@ -143,7 +143,7 @@ cdef int assert_not_sharing_data(np.ndarray arr1, np.ndarray arr2) except -1:
|
||||
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
|
||||
@@ -154,9 +154,9 @@ cdef np.ndarray new_array_like(np.ndarray arr):
|
||||
# need to incref because numpy will apprently steal a dtype reference
|
||||
Py_INCREF(<object>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(<object>dtype)
|
||||
return PyArray_Empty(arr.ndim, arr.shape, dtype, 0)
|
||||
|
||||
@@ -168,21 +168,21 @@ cdef np.npy_intp* clone_array_shape(np.ndarray arr):
|
||||
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 = <CvPoint2D32f*>arr.data
|
||||
return point2Darr
|
||||
|
||||
cdef CvTermCriteria get_cvTermCriteria(int iterations, double epsilon):
|
||||
cdef CvTermCriteria crit
|
||||
cdef CvTermCriteria crit
|
||||
if iterations and epsilon:
|
||||
crit.type = <int>(CV_TERMCRIT_ITER | CV_TERMCRIT_EPS)
|
||||
crit.max_iter = iterations
|
||||
@@ -196,10 +196,10 @@ cdef CvTermCriteria get_cvTermCriteria(int iterations, double epsilon):
|
||||
crit.max_iter = 0
|
||||
crit.epsilon = epsilon
|
||||
return crit
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
#############################################
|
||||
# Constants (need a better place for these)
|
||||
############################################
|
||||
|
||||
|
||||
|
||||
CV_BLUR_NO_SCALE = 0
|
||||
CV_BLUR = 1
|
||||
@@ -18,3 +18,15 @@ CV_INTER_NN = 0
|
||||
CV_INTER_LINEAR = 1
|
||||
CV_INTER_CUBIC = 2
|
||||
CV_INTER_AREA = 3
|
||||
|
||||
#########################
|
||||
# Calibration Constants #
|
||||
#########################
|
||||
CV_CALIB_USE_INTRINSIC_GUESS = 1
|
||||
CV_CALIB_FIX_ASPECT_RATIO = 2
|
||||
CV_CALIB_FIX_PRINCIPAL_POINT = 4
|
||||
CV_CALIB_ZERO_TANGENT_DIST = 8
|
||||
CV_CALIB_CB_ADAPTIVE_THRESH = 1
|
||||
CV_CALIB_CB_NORMALIZE_IMAGE = 2
|
||||
CV_CALIB_CB_FILTER_QUADS = 4
|
||||
|
||||
|
||||
+1347
-1217
File diff suppressed because it is too large
Load Diff
+202
-168
@@ -8,17 +8,13 @@ from opencv_backend import *
|
||||
from opencv_backend cimport *
|
||||
from opencv_constants import *
|
||||
|
||||
#one of these should work if the user imported the package properly
|
||||
try:
|
||||
cv = ctypes.CDLL('libcv.so')
|
||||
except:
|
||||
try:
|
||||
cv = ctypes.CDLL('cv.dll')
|
||||
except:
|
||||
raise RuntimeError('The opencv libraries were not found. Please make sure they are installed and available on the system path.')
|
||||
from _libimport import cv
|
||||
|
||||
from opencv_constants import *
|
||||
from opencv_cv import *
|
||||
|
||||
|
||||
###################################
|
||||
###################################
|
||||
# opencv function declarations
|
||||
###################################
|
||||
|
||||
@@ -63,6 +59,11 @@ ctypedef void (*cvFindCornerSubPixPtr)(IplImage*, CvPoint2D32f*, int, CvSize, Cv
|
||||
cdef cvFindCornerSubPixPtr c_cvFindCornerSubPix
|
||||
c_cvFindCornerSubPix = (<cvFindCornerSubPixPtr*><size_t>ctypes.addressof(cv.cvFindCornerSubPix))[0]
|
||||
|
||||
# cvFindChessboardCorners
|
||||
ctypedef void (*cvFindChessboardCornersPtr)(IplImage*, CvSize, CvPoint2D32f*, int*, int)
|
||||
cdef cvFindChessboardCornersPtr c_cvFindChessboardCorners
|
||||
c_cvFindChessboardCorners = (<cvFindChessboardCornersPtr*><size_t>ctypes.addressof(cv.cvFindChessboardCorners))[0]
|
||||
|
||||
# cvSmooth
|
||||
ctypedef void (*cvSmoothPtr)(IplImage*, IplImage*, int, int, int, double, double)
|
||||
cdef cvSmoothPtr c_cvSmooth
|
||||
@@ -70,7 +71,7 @@ c_cvSmooth = (<cvSmoothPtr*><size_t>ctypes.addressof(cv.cvSmooth))[0]
|
||||
|
||||
# cvGoodFeaturesToTrack
|
||||
ctypedef void (*cvGoodFeaturesToTrackPtr)(IplImage*, IplImage*, IplImage*,
|
||||
CvPoint2D32f*, int*, double, double,
|
||||
CvPoint2D32f*, int*, double, double,
|
||||
IplImage*, int, int, double)
|
||||
cdef cvGoodFeaturesToTrackPtr c_cvGoodFeaturesToTrack
|
||||
c_cvGoodFeaturesToTrack = (<cvGoodFeaturesToTrackPtr*><size_t>ctypes.addressof(cv.cvGoodFeaturesToTrack))[0]
|
||||
@@ -84,24 +85,57 @@ c_cvResize = (<cvResizePtr*><size_t>ctypes.addressof(cv.cvResize))[0]
|
||||
####################################
|
||||
# Function Implementations
|
||||
####################################
|
||||
def cvFindChessboardCorners(np.ndarray src, pattern_size, int flags = CV_CALIB_CB_ADAPTIVE_THRESH):
|
||||
"""
|
||||
Wrapper around the OpenCV cvFindChessboardCorners function.
|
||||
|
||||
src - Image to search for chessboard corners
|
||||
pattern_size - Tuple of inner corners (w,h)
|
||||
flags - directly passed through to OpenCV
|
||||
"""
|
||||
validate_array(src)
|
||||
|
||||
assert_nchannels(src, [1, 3])
|
||||
assert_dtype(src, [UINT8])
|
||||
|
||||
cdef np.npy_intp outshape[2]
|
||||
outshape[0] = <int> pattern_size[1]*pattern_size[0]
|
||||
outshape[1] = <int> 2 # pattern_size[0]
|
||||
|
||||
points = new_array(2, outshape, FLOAT32)
|
||||
points[:] = 0
|
||||
cdef CvPoint2D32f* cvpoints = array_as_cvPoint2D32f_ptr(points)
|
||||
|
||||
cdef CvSize cvpattern_size
|
||||
cvpattern_size.height = pattern_size[1]
|
||||
cvpattern_size.width = pattern_size[0]
|
||||
|
||||
cdef IplImage srcimg
|
||||
populate_iplimage(src, &srcimg)
|
||||
|
||||
cdef int ncorners_found
|
||||
c_cvFindChessboardCorners(&srcimg, cvpattern_size, cvpoints, &ncorners_found, flags)
|
||||
|
||||
return points[:ncorners_found]
|
||||
|
||||
def cvSobel(np.ndarray src, np.ndarray out=None, int xorder=1, int yorder=0,
|
||||
int aperture_size=3):
|
||||
|
||||
|
||||
"""
|
||||
better doc string needed.
|
||||
better doc string needed.
|
||||
for now:
|
||||
http://opencv.willowgarage.com/documentation/cvreference.html
|
||||
"""
|
||||
|
||||
|
||||
validate_array(src)
|
||||
assert_dtype(src, [UINT8, INT8, FLOAT32])
|
||||
assert_nchannels(src, [1])
|
||||
|
||||
|
||||
if (aperture_size != 3 and aperture_size != 5 and aperture_size != 7):
|
||||
raise ValueError('aperture_size must be 3, 5, or 7')
|
||||
|
||||
|
||||
if out is not None:
|
||||
validate_array(out)
|
||||
validate_array(out)
|
||||
assert_not_sharing_data(src, out)
|
||||
assert_same_shape(src, out)
|
||||
assert_nchannels(out, [1])
|
||||
@@ -114,35 +148,35 @@ def cvSobel(np.ndarray src, np.ndarray out=None, int xorder=1, int yorder=0,
|
||||
out = new_array_like_diff_dtype(src, INT16)
|
||||
else:
|
||||
out = new_array_like(src)
|
||||
|
||||
|
||||
cdef IplImage srcimg
|
||||
cdef IplImage outimg
|
||||
|
||||
|
||||
populate_iplimage(src, &srcimg)
|
||||
populate_iplimage(out, &outimg)
|
||||
|
||||
|
||||
c_cvSobel(&srcimg, &outimg, xorder, yorder, aperture_size)
|
||||
|
||||
return out
|
||||
|
||||
return out
|
||||
|
||||
def cvLaplace(np.ndarray src, np.ndarray out=None, int aperture_size=3):
|
||||
|
||||
|
||||
"""
|
||||
better doc string needed.
|
||||
better doc string needed.
|
||||
for now:
|
||||
http://opencv.willowgarage.com/documentation/cvreference.html
|
||||
"""
|
||||
|
||||
|
||||
validate_array(src)
|
||||
assert_dtype(src, [UINT8, INT8, FLOAT32])
|
||||
assert_nchannels(src, [1])
|
||||
|
||||
|
||||
if (aperture_size != 3 and aperture_size != 5 and aperture_size != 7):
|
||||
raise ValueError('aperture_size must be 3, 5, or 7')
|
||||
|
||||
|
||||
if out is not None:
|
||||
validate_array(out)
|
||||
validate_array(out)
|
||||
assert_not_sharing_data(src, out)
|
||||
assert_same_shape(src, out)
|
||||
assert_nchannels(out, [1])
|
||||
@@ -155,33 +189,33 @@ def cvLaplace(np.ndarray src, np.ndarray out=None, int aperture_size=3):
|
||||
out = new_array_like_diff_dtype(src, INT16)
|
||||
else:
|
||||
out = new_array_like(src)
|
||||
|
||||
|
||||
cdef IplImage srcimg
|
||||
cdef IplImage outimg
|
||||
|
||||
|
||||
populate_iplimage(src, &srcimg)
|
||||
populate_iplimage(out, &outimg)
|
||||
|
||||
c_cvLaplace(&srcimg, &outimg, aperture_size)
|
||||
|
||||
return out
|
||||
|
||||
def cvCanny(np.ndarray src, np.ndarray out=None, double threshold1=10,
|
||||
c_cvLaplace(&srcimg, &outimg, aperture_size)
|
||||
|
||||
return out
|
||||
|
||||
def cvCanny(np.ndarray src, np.ndarray out=None, double threshold1=10,
|
||||
double threshold2=50, int aperture_size=3):
|
||||
|
||||
|
||||
"""
|
||||
better doc string needed.
|
||||
better doc string needed.
|
||||
for now:
|
||||
http://opencv.willowgarage.com/documentation/cvreference.html
|
||||
"""
|
||||
|
||||
|
||||
validate_array(src)
|
||||
assert_nchannels(src, [1])
|
||||
|
||||
|
||||
if (aperture_size != 3 and aperture_size != 5 and aperture_size != 7):
|
||||
raise ValueError('aperture_size must be 3, 5, or 7')
|
||||
|
||||
|
||||
|
||||
if out is not None:
|
||||
validate_array(out)
|
||||
assert_nchannels(out, [1])
|
||||
@@ -189,31 +223,31 @@ def cvCanny(np.ndarray src, np.ndarray out=None, double threshold1=10,
|
||||
assert_not_sharing_data(src, out)
|
||||
else:
|
||||
out = new_array_like(src)
|
||||
|
||||
|
||||
cdef IplImage srcimg
|
||||
cdef IplImage outimg
|
||||
populate_iplimage(src, &srcimg)
|
||||
populate_iplimage(out, &outimg)
|
||||
|
||||
|
||||
c_cvCanny(&srcimg, &outimg, threshold1, threshold2, aperture_size)
|
||||
|
||||
|
||||
return out
|
||||
|
||||
def cvPreCornerDetect(np.ndarray src, np.ndarray out=None, int aperture_size=3):
|
||||
|
||||
|
||||
"""
|
||||
better doc string needed.
|
||||
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])
|
||||
|
||||
|
||||
if (aperture_size != 3 and aperture_size != 5 and aperture_size != 7):
|
||||
raise ValueError('aperture_size must be 3, 5, or 7')
|
||||
|
||||
|
||||
if out is not None:
|
||||
validate_array(out)
|
||||
assert_same_shape(src, out)
|
||||
@@ -221,128 +255,128 @@ def cvPreCornerDetect(np.ndarray src, np.ndarray out=None, int aperture_size=3):
|
||||
assert_not_sharing_data(src, out)
|
||||
else:
|
||||
out = new_array_like_diff_dtype(src, FLOAT32)
|
||||
|
||||
|
||||
cdef IplImage srcimg
|
||||
cdef IplImage outimg
|
||||
populate_iplimage(src, &srcimg)
|
||||
populate_iplimage(out, &outimg)
|
||||
|
||||
|
||||
c_cvPreCornerDetect(&srcimg, &outimg, aperture_size)
|
||||
|
||||
return out
|
||||
|
||||
def cvCornerEigenValsAndVecs(np.ndarray src, int block_size=3,
|
||||
|
||||
return out
|
||||
|
||||
def cvCornerEigenValsAndVecs(np.ndarray src, int block_size=3,
|
||||
int aperture_size=3):
|
||||
|
||||
|
||||
"""
|
||||
better doc string needed.
|
||||
better doc string needed.
|
||||
for now:
|
||||
http://opencv.willowgarage.com/documentation/cvreference.html
|
||||
"""
|
||||
|
||||
# no option for the out argument on this one. Its easier just
|
||||
# to make it for them as there is only 1 valid out array for any
|
||||
|
||||
# no option for the out argument on this one. Its easier just
|
||||
# to make it for them as there is only 1 valid out array for any
|
||||
# given source array
|
||||
|
||||
|
||||
validate_array(src)
|
||||
assert_nchannels(src, [1])
|
||||
assert_dtype(src, [UINT8, FLOAT32])
|
||||
|
||||
|
||||
if (aperture_size != 3 and aperture_size != 5 and aperture_size != 7):
|
||||
raise ValueError('aperture_size must be 3, 5, or 7')
|
||||
|
||||
cdef np.npy_intp outshape[2]
|
||||
|
||||
cdef np.npy_intp outshape[2]
|
||||
outshape[0] = src.shape[0]
|
||||
outshape[1] = src.shape[1] * <np.npy_intp>6
|
||||
|
||||
outshape[1] = src.shape[1] * <np.npy_intp>6
|
||||
|
||||
out = new_array(2, outshape, FLOAT32)
|
||||
|
||||
|
||||
cdef IplImage srcimg
|
||||
cdef IplImage outimg
|
||||
populate_iplimage(src, &srcimg)
|
||||
populate_iplimage(out, &outimg)
|
||||
|
||||
populate_iplimage(out, &outimg)
|
||||
|
||||
c_cvCornerEigenValsAndVecs(&srcimg, &outimg, block_size, aperture_size)
|
||||
|
||||
|
||||
return out.reshape(out.shape[0], -1, 6)
|
||||
|
||||
def cvCornerMinEigenVal(np.ndarray src, int block_size=3,
|
||||
|
||||
def cvCornerMinEigenVal(np.ndarray src, int block_size=3,
|
||||
int aperture_size=3):
|
||||
"""
|
||||
better doc string needed.
|
||||
better doc string needed.
|
||||
for now:
|
||||
http://opencv.willowgarage.com/documentation/cvreference.html
|
||||
"""
|
||||
# no option for the out argument on this one. Its easier just
|
||||
# to make it for them as there is only 1 valid out array for any
|
||||
"""
|
||||
# no option for the out argument on this one. Its easier just
|
||||
# to make it for them as there is only 1 valid out array for any
|
||||
# given source array
|
||||
|
||||
|
||||
validate_array(src)
|
||||
assert_nchannels(src, [1])
|
||||
assert_dtype(src, [UINT8, FLOAT32])
|
||||
|
||||
|
||||
if (aperture_size != 3 and aperture_size != 5 and aperture_size != 7):
|
||||
raise ValueError('aperture_size must be 3, 5, or 7')
|
||||
|
||||
|
||||
out = new_array_like_diff_dtype(src, FLOAT32)
|
||||
|
||||
|
||||
cdef IplImage srcimg
|
||||
cdef IplImage outimg
|
||||
populate_iplimage(src, &srcimg)
|
||||
populate_iplimage(out, &outimg)
|
||||
|
||||
populate_iplimage(out, &outimg)
|
||||
|
||||
c_cvCornerMinEigenVal(&srcimg, &outimg, block_size, aperture_size)
|
||||
|
||||
|
||||
return out
|
||||
|
||||
def cvCornerHarris(np.ndarray src, int block_size=3, int aperture_size=3,
|
||||
double k=0.04):
|
||||
"""
|
||||
better doc string needed.
|
||||
better doc string needed.
|
||||
for now:
|
||||
http://opencv.willowgarage.com/documentation/cvreference.html
|
||||
"""
|
||||
# no option for the out argument on this one. Its easier just
|
||||
# to make it for them as there is only 1 valid out array for any
|
||||
"""
|
||||
# no option for the out argument on this one. Its easier just
|
||||
# to make it for them as there is only 1 valid out array for any
|
||||
# given source array
|
||||
|
||||
|
||||
validate_array(src)
|
||||
assert_nchannels(src, [1])
|
||||
assert_dtype(src, [UINT8, FLOAT32])
|
||||
|
||||
|
||||
if (aperture_size != 3 and aperture_size != 5 and aperture_size != 7):
|
||||
raise ValueError('aperture_size must be 3, 5, or 7')
|
||||
|
||||
|
||||
out = new_array_like_diff_dtype(src, FLOAT32)
|
||||
|
||||
|
||||
cdef IplImage srcimg
|
||||
cdef IplImage outimg
|
||||
populate_iplimage(src, &srcimg)
|
||||
populate_iplimage(out, &outimg)
|
||||
|
||||
populate_iplimage(out, &outimg)
|
||||
|
||||
c_cvCornerHarris(&srcimg, &outimg, block_size, aperture_size, k)
|
||||
|
||||
return out
|
||||
|
||||
return out
|
||||
|
||||
def cvFindCornerSubPix(np.ndarray src, np.ndarray corners, int count, win,
|
||||
zero_zone=(-1, -1), int iterations=0,
|
||||
zero_zone=(-1, -1), int iterations=0,
|
||||
double epsilon=1e-5):
|
||||
|
||||
|
||||
"""
|
||||
better doc string needed.
|
||||
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_nchannels(corners, [1])
|
||||
assert_dtype(corners, [FLOAT32])
|
||||
|
||||
|
||||
# make sure the number of points
|
||||
# jives with the elements in the array
|
||||
# the shape of the array is irrelevant
|
||||
@@ -352,103 +386,103 @@ def cvFindCornerSubPix(np.ndarray src, np.ndarray corners, int count, win,
|
||||
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 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)
|
||||
|
||||
|
||||
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):
|
||||
"""
|
||||
better doc string needed.
|
||||
better doc string needed.
|
||||
for now:
|
||||
http://opencv.willowgarage.com/documentation/cvreference.html
|
||||
"""
|
||||
|
||||
|
||||
validate_array(src)
|
||||
if out is not None:
|
||||
validate_array(out)
|
||||
|
||||
|
||||
# there are restrictions that must be placed on the data depending on
|
||||
# the smoothing operation requested
|
||||
|
||||
|
||||
# CV_BLUR_NO_SCALE
|
||||
if smoothtype == CV_BLUR_NO_SCALE:
|
||||
|
||||
|
||||
if in_place:
|
||||
raise RuntimeError('In place operation not supported with this filter')
|
||||
|
||||
assert_dtype(src, [UINT8, INT8, FLOAT32])
|
||||
|
||||
assert_dtype(src, [UINT8, INT8, FLOAT32])
|
||||
assert_ndims(src, [2])
|
||||
|
||||
|
||||
if out is not None:
|
||||
if src.dtype == FLOAT32:
|
||||
assert_dtype(out, [FLOAT32])
|
||||
assert_dtype(out, [FLOAT32])
|
||||
else:
|
||||
assert_dtype(out, [INT16])
|
||||
assert_same_shape(src, out)
|
||||
assert_same_shape(src, out)
|
||||
else:
|
||||
if src.dtype == FLOAT32:
|
||||
out = new_array_like(src)
|
||||
else:
|
||||
out = new_array_like_diff_dtype(src, INT16)
|
||||
|
||||
# CV_BLUR and CV_GAUSSIAN
|
||||
|
||||
# CV_BLUR and CV_GAUSSIAN
|
||||
elif smoothtype == CV_BLUR or smoothtype == CV_GAUSSIAN:
|
||||
|
||||
|
||||
assert_dtype(src, [UINT8, INT8, FLOAT32])
|
||||
assert_nchannels(src, [1, 3])
|
||||
|
||||
|
||||
if in_place:
|
||||
out = src
|
||||
out = src
|
||||
elif out is not None:
|
||||
assert_like(src, out)
|
||||
assert_like(src, out)
|
||||
else:
|
||||
out = new_array_like(src)
|
||||
|
||||
|
||||
# CV_MEDIAN and CV_BILATERAL
|
||||
else:
|
||||
else:
|
||||
assert_dtype(src, [UINT8, INT8])
|
||||
assert_nchannels(src, [1, 3])
|
||||
|
||||
|
||||
if in_place:
|
||||
raise RuntimeError('In place operation not supported with this filter')
|
||||
|
||||
|
||||
if out is not None:
|
||||
assert_like(src, out)
|
||||
else:
|
||||
out = new_array_like(src)
|
||||
|
||||
|
||||
cdef IplImage srcimg
|
||||
cdef IplImage outimg
|
||||
populate_iplimage(src, &srcimg)
|
||||
populate_iplimage(out, &outimg)
|
||||
|
||||
c_cvSmooth(&srcimg, &outimg, smoothtype, param1, param2, param3, param4)
|
||||
|
||||
populate_iplimage(out, &outimg)
|
||||
|
||||
c_cvSmooth(&srcimg, &outimg, smoothtype, param1, param2, param3, param4)
|
||||
|
||||
return out
|
||||
|
||||
def cvGoodFeaturesToTrack(np.ndarray src, int corner_count, double quality_level,
|
||||
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.
|
||||
better doc string needed.
|
||||
for now:
|
||||
http://opencv.willowgarage.com/documentation/cvreference.html
|
||||
"""
|
||||
@@ -456,85 +490,85 @@ def cvGoodFeaturesToTrack(np.ndarray src, int corner_count, double quality_level
|
||||
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
|
||||
<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
|
||||
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 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]
|
||||
|
||||
cdef np.npy_intp cornershape[2]
|
||||
cornershape[0] = <np.npy_intp>out_corner_count
|
||||
cornershape[1] = 2
|
||||
|
||||
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
|
||||
|
||||
cornersarr[i,1] = corners[i].y
|
||||
|
||||
PyMem_Free(corners)
|
||||
|
||||
|
||||
return cornersarr
|
||||
|
||||
|
||||
def cvResize(np.ndarray src, height=None, width=None,
|
||||
|
||||
|
||||
def cvResize(np.ndarray src, height=None, width=None,
|
||||
int method=CV_INTER_LINEAR):
|
||||
"""
|
||||
better doc string needed.
|
||||
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 ndim = src.ndim
|
||||
|
||||
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)
|
||||
populate_iplimage(out, &outimg)
|
||||
|
||||
|
||||
c_cvResize(&srcimg, &outimg, method)
|
||||
|
||||
|
||||
return out
|
||||
|
||||
|
||||
|
||||
@@ -8,27 +8,27 @@ cdef struct _IplImage:
|
||||
int nChannels # number of channels 1, 2, 3 or 4
|
||||
int alphaChannel # ignored by opencv
|
||||
int depth # pixel depth in bits: IPL_DEPTH_8U, IPL_DEPTH_8S, IPL_DEPTH_16S, IPL_DEPTH_32S, IPL_DEPTH_32F, IPL_DEPTH_64F
|
||||
|
||||
|
||||
char colorModel[4] # ignored by opencv
|
||||
char channelSeq[4] # ignored by opencv
|
||||
int dataOrder # must be 0
|
||||
|
||||
|
||||
int origin # should be 0 for top-left origin
|
||||
|
||||
|
||||
int align # ignored by opencv
|
||||
|
||||
|
||||
int width # width in pixels
|
||||
int height # height in pixels
|
||||
|
||||
|
||||
void *roi # must be NULL
|
||||
void *maskROI # must be NULL
|
||||
void *maskROI # must be NULL
|
||||
void *imageId # must be NULL
|
||||
void *tileInfo # must be NULL
|
||||
int imageSize # image size in bytes
|
||||
|
||||
|
||||
char *imageData # pointer to the data
|
||||
int widthStep # row size in bytes (first stride of numpy array)
|
||||
int BorderMode[4] # ignored by opencv
|
||||
int BorderMode[4] # ignored by opencv
|
||||
int BorderConst[4] # ignored by opencv
|
||||
char* imageDataOrigin # pointer to origin of data. Used for deallocation, but python will handle this so we'll set it to void*
|
||||
|
||||
@@ -38,13 +38,13 @@ ctypedef _IplImage IplImage
|
||||
cdef struct CvPoint2D32f:
|
||||
float x
|
||||
float y
|
||||
|
||||
|
||||
cdef struct CvSize:
|
||||
int width
|
||||
int height
|
||||
|
||||
|
||||
cdef struct CvTermCriteria:
|
||||
int type
|
||||
int max_iter
|
||||
double epsilon
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user