added threshold and adpative threshold

This commit is contained in:
sccolbert
2009-10-24 17:49:49 +02:00
committed by Stefan van der Walt
parent 31f81bff15
commit 4763ceeb74
5 changed files with 2530 additions and 1565 deletions
+1 -1
View File
@@ -1,4 +1,4 @@
/* Generated by Cython 0.11.3 on Sat Oct 24 16:06:03 2009 */
/* Generated by Cython 0.11.3 on Sat Oct 24 17:38:28 2009 */
#define PY_SSIZE_T_CLEAN
#include "Python.h"
+10 -1
View File
@@ -3,7 +3,6 @@
# Image Processing Constants
############################################
CV_BLUR_NO_SCALE = 0
CV_BLUR = 1
CV_GAUSSIAN = 2
@@ -27,6 +26,16 @@ CV_SHAPE_CROSS = 1
CV_SHAPE_ELLIPSE = 2
CV_SHAPE_CUSTOM = 100
CV_THRESH_BINARY = 0
CV_THRESH_BINARY_INV = 1
CV_THRESH_TRUNC = 2
CV_THRESH_TOZERO = 3
CV_THRESH_TOZERO_INV = 4
CV_THRESH_MASK = 7
CV_THRESH_OTSU = 8 # this must be combined with other flags
CV_ADAPTIVE_THRESH_MEAN_C = 0
CV_ADAPTIVE_THRESH_GAUSSIAN_C = 1
CV_MOP_OPEN = 2
CV_MOP_CLOSE = 3
CV_MOP_GRADIENT = 4
File diff suppressed because it is too large Load Diff
+70
View File
@@ -233,6 +233,18 @@ ctypedef void (*cvCvtColorPtr)(IplImage*, IplImage*, int)
cdef cvCvtColorPtr c_cvCvtColor
c_cvCvtColor = (<cvCvtColorPtr*><size_t>ctypes.addressof(cv.cvCvtColor))[0]
# cvThreshold
ctypedef double (*cvThresholdPtr)(IplImage*, IplImage*, double, double, int)
cdef cvThresholdPtr c_cvThreshold
c_cvThreshold = (<cvThresholdPtr*><size_t>ctypes.addressof(cv.cvThreshold))[0]
# cvAdaptiveThreshold
ctypedef void (*cvAdaptiveThresholdPtr)(IplImage*, IplImage*, double, int, int,
int, double)
cdef cvAdaptiveThresholdPtr c_cvAdaptiveThreshold
c_cvAdaptiveThreshold = (<cvAdaptiveThresholdPtr*><size_t>
ctypes.addressof(cv.cvAdaptiveThreshold))[0]
# cvCalibrateCamera2
ctypedef void (*cvCalibrateCamera2Ptr)(CvMat*, CvMat*, CvMat*,
CvSize, CvMat*, CvMat*, CvMat*, CvMat*, int)
@@ -1163,6 +1175,64 @@ def cvCvtColor(np.ndarray src, int code):
return out
def cvThreshold(np.ndarray src, double threshold, double max_value=255,
int threshold_type=CV_THRESH_BINARY):
validate_array(src)
assert_nchannels(src, [1])
assert_dtype(src, [UINT8, FLOAT32])
use_otsu = (threshold_type & CV_THRESH_OTSU) != 0
if use_otsu:
assert_dtype(src, [UINT8])
cdef np.ndarray out = new_array_like(src)
cdef IplImage srcimg
cdef IplImage outimg
populate_iplimage(src, &srcimg)
populate_iplimage(out, &outimg)
threshold = c_cvThreshold(&srcimg, &outimg, threshold, max_value,
threshold_type)
if use_otsu:
return (out, threshold)
else:
return out
def cvAdaptiveThreshold(np.ndarray src, double max_value,
int adaptive_method=CV_ADAPTIVE_THRESH_MEAN_C,
int threshold_type=CV_THRESH_BINARY,
int block_size=3, double param1=5):
validate_array(src)
assert_nchannels(src, [1])
assert_dtype(src, [UINT8])
if (adaptive_method!=CV_ADAPTIVE_THRESH_MEAN_C and
adaptive_method!=CV_ADAPTIVE_THRESH_GAUSSIAN_C):
raise ValueError('Invalid adaptive method')
if (threshold_type!=CV_THRESH_BINARY and
threshold_type!=CV_THRESH_BINARY_INV):
raise ValueError('Invalid threshold type')
if (block_size % 2 != 1 or block_size <= 1):
raise ValueError('block size must be and odd number and greater than 1')
cdef np.ndarray out = new_array_like(src)
cdef IplImage srcimg
cdef IplImage outimg
populate_iplimage(src, &srcimg)
populate_iplimage(out, &outimg)
c_cvAdaptiveThreshold(&srcimg, &outimg, max_value, adaptive_method,
threshold_type, block_size, param1)
return out
def cvCalibrateCamera2(np.ndarray object_points, np.ndarray image_points,
np.ndarray point_counts, image_size):
@@ -200,6 +200,23 @@ class TestCvtColor(OpenCVTest):
cvCvtColor(self.lena_GRAY_U8, CV_GRAY2BGR555)
class TestThreshold(OpenCVTest):
@opencv_skip
def test_cvThreshold(self):
cvThreshold(self.lena_GRAY_U8, 100, 255, CV_THRESH_BINARY)
cvThreshold(self.lena_GRAY_U8, 100, 255, CV_THRESH_BINARY_INV)
cvThreshold(self.lena_GRAY_U8, 100, threshold_type=CV_THRESH_TRUNC)
cvThreshold(self.lena_GRAY_U8, 100, threshold_type=CV_THRESH_TOZERO)
cvThreshold(self.lena_GRAY_U8, 100, threshold_type=CV_THRESH_TOZERO_INV)
cvThreshold(self.lena_GRAY_U8, 100, 1, CV_THRESH_BINARY+CV_THRESH_OTSU)
class TestAdaptiveThreshold(OpenCVTest):
@opencv_skip
def test_cvAdaptiveThreshold(self):
cvAdaptiveThreshold(self.lena_GRAY_U8, 100)
class TestFindChessboardCorners(object):
@opencv_skip
def test_cvFindChessboardCorners(self):