diff --git a/scikits/image/opencv/opencv_cv.pyx b/scikits/image/opencv/opencv_cv.pyx index b7ed826a..66b67884 100644 --- a/scikits/image/opencv/opencv_cv.pyx +++ b/scikits/image/opencv/opencv_cv.pyx @@ -15,10 +15,12 @@ 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.') + raise RuntimeError('The opencv libraries were not found. ' + 'Please make sure they are installed and ' + 'available on the system path.') -################################### +################################### # opencv function declarations ################################### @@ -40,40 +42,48 @@ c_cvCanny = (ctypes.addressof(cv.cvCanny))[0] # cvPreCornerDetect ctypedef void (*cvPreCorneDetectPtr)(IplImage*, IplImage*, int) cdef cvPreCorneDetectPtr c_cvPreCornerDetect -c_cvPreCornerDetect = (ctypes.addressof(cv.cvPreCornerDetect))[0] +c_cvPreCornerDetect = ( + ctypes.addressof(cv.cvPreCornerDetect))[0] # cvCornerEigenValsAndVecs ctypedef void (*cvCornerEigenValsAndVecsPtr)(IplImage*, IplImage*, int, int) cdef cvCornerEigenValsAndVecsPtr c_cvCornerEigenValsAndVecs -c_cvCornerEigenValsAndVecs = (ctypes.addressof(cv.cvCornerEigenValsAndVecs))[0] +c_cvCornerEigenValsAndVecs = ( + ctypes.addressof(cv.cvCornerEigenValsAndVecs))[0] # cvCornerMinEigenVal ctypedef void (*cvCornerMinEigenValPtr)(IplImage*, IplImage*, int, int) cdef cvCornerMinEigenValPtr c_cvCornerMinEigenVal -c_cvCornerMinEigenVal = (ctypes.addressof(cv.cvCornerMinEigenVal))[0] +c_cvCornerMinEigenVal = ( + ctypes.addressof(cv.cvCornerMinEigenVal))[0] # cvCornerHarris ctypedef void (*cvCornerHarrisPtr)(IplImage*, IplImage*, int, int, double) cdef cvCornerHarrisPtr c_cvCornerHarris -c_cvCornerHarris = (ctypes.addressof(cv.cvCornerHarris))[0] +c_cvCornerHarris = ( + ctypes.addressof(cv.cvCornerHarris))[0] # cvFindCornerSubPix -ctypedef void (*cvFindCornerSubPixPtr)(IplImage*, CvPoint2D32f*, int, CvSize, CvSize, CvTermCriteria) +ctypedef void (*cvFindCornerSubPixPtr)(IplImage*, CvPoint2D32f*, int, + CvSize, CvSize, CvTermCriteria) cdef cvFindCornerSubPixPtr c_cvFindCornerSubPix -c_cvFindCornerSubPix = (ctypes.addressof(cv.cvFindCornerSubPix))[0] +c_cvFindCornerSubPix = ( + ctypes.addressof(cv.cvFindCornerSubPix))[0] # cvSmooth -ctypedef void (*cvSmoothPtr)(IplImage*, IplImage*, int, int, int, double, double) +ctypedef void (*cvSmoothPtr)(IplImage*, IplImage*, int, int, + int, double, double) cdef cvSmoothPtr c_cvSmooth c_cvSmooth = (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 = (ctypes.addressof(cv.cvGoodFeaturesToTrack))[0] +c_cvGoodFeaturesToTrack = ( + ctypes.addressof(cv.cvGoodFeaturesToTrack))[0] # cvResize ctypedef void (*cvResizePtr)(IplImage*, IplImage*, int) @@ -86,22 +96,22 @@ c_cvResize = (ctypes.addressof(cv.cvResize))[0] #################################### 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 +124,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 +165,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 +199,32 @@ 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): - +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 +232,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] * 6 - + outshape[1] = src.shape[1] * 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 @@ -351,104 +362,110 @@ def cvFindCornerSubPix(np.ndarray src, np.ndarray corners, int count, win, # that it is 2D cdef int nbytes = get_array_nbytes(corners) if nbytes != (count * 2 * 4): - raise ValueError('the number of declared points is different than exists in the array') - + 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 = win[0] cvwin.width = win[1] - + cdef CvSize cvzerozone cvzerozone.height = zero_zone[0] cvzerozone.width = 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): + +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]) + raise RuntimeError('In place operation not supported with this ' + 'filter') + + 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') - + 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, - double min_distance, np.ndarray mask=None, - int block_size=3, int use_harris=0, double k=0.04): +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 +473,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 = ( - PyMem_Malloc(corner_count * sizeof(CvPoint2D32f))) - - cdef int out_corner_count + 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, + + 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] = 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 - \ No newline at end of file diff --git a/scikits/image/opencv/tests/test_opencv_cv.py b/scikits/image/opencv/tests/test_opencv_cv.py index aef35d98..1907b591 100644 --- a/scikits/image/opencv/tests/test_opencv_cv.py +++ b/scikits/image/opencv/tests/test_opencv_cv.py @@ -9,11 +9,10 @@ try: except: OPENCV_LIBS_NOTFOUND = True -from scikits.image import data_dir +from scikits.image import data_dir -_opencv_skip = dec.skipif(OPENCV_LIBS_NOTFOUND, - 'Skipping OpenCV test because OpenCV' - 'libs were not found') +_opencv_skip = dec.skipif(OPENCV_LIBS_NOTFOUND, + 'OpenCV libraries not found') class OpenCVTest: # setup only works as a module level function @@ -21,25 +20,25 @@ class OpenCVTest: self.lena_RGB_U8 = np.load(os.path.join(data_dir, 'lena_RGB_U8.npy')) self.lena_GRAY_U8 = np.load(os.path.join(data_dir, 'lena_GRAY_U8.npy')) - + class TestSobel(OpenCVTest): @_opencv_skip def test_cvSobel(self): cvSobel(self.lena_GRAY_U8) - - + + class TestLaplace(OpenCVTest): @_opencv_skip def test_cvLaplace(self): cvLaplace(self.lena_GRAY_U8) - - + + class TestCanny(OpenCVTest): @_opencv_skip def test_cvCanny(self): cvCanny(self.lena_GRAY_U8) - + class TestPreCornerDetect(OpenCVTest): @_opencv_skip def test_cvPreCornerDetect(self): @@ -50,27 +49,27 @@ class TestCornerEigenValsAndVecs(OpenCVTest): @_opencv_skip def test_cvCornerEigenValsAndVecs(self): cvCornerEigenValsAndVecs(self.lena_GRAY_U8) - + class TestCornerMinEigenVal(OpenCVTest): @_opencv_skip def test_cvCornerMinEigenVal(self): cvCornerMinEigenVal(self.lena_GRAY_U8) - + class TestCornerHarris(OpenCVTest): @_opencv_skip def test_cvCornerHarris(self): cvCornerHarris(self.lena_GRAY_U8) - - + + class TestSmooth(OpenCVTest): @_opencv_skip def test_cvSmooth(self): - for st in (CV_BLUR_NO_SCALE, CV_BLUR, CV_GAUSSIAN, CV_MEDIAN, + for st in (CV_BLUR_NO_SCALE, CV_BLUR, CV_GAUSSIAN, CV_MEDIAN, CV_BILATERAL): cvSmooth(self.lena_GRAY_U8, None, st, 3, 0, 0, 0, False) - + class TestFindCornerSubPix: @_opencv_skip def test_cvFindCornersSubPix(self): @@ -83,27 +82,27 @@ class TestFindCornerSubPix: [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 TestGoodFeaturesToTrack(OpenCVTest): @_opencv_skip def test_cvGoodFeaturesToTrack(self): - cvGoodFeaturesToTrack(self.lena_GRAY_U8, 100, 0.1, 3) - - + 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()