extended cv.KMeans2 API in Python (ticket #414; thanks to hogelog). fixed failures in the opencv python tests.

This commit is contained in:
Vadim Pisarevsky 2010-11-30 10:11:38 +00:00
parent 53e362e403
commit 8754cafffb
4 changed files with 83 additions and 32 deletions

View File

@ -766,11 +766,14 @@ CheckArr int
int flags 0
double min_val 0
double max_val 0
KMeans2
KMeans2 double
CvArr samples
int nclusters
CvArr labels
CvTermCriteria termcrit
CvTermCriteria termcrit
int attempts 1
int flags 0
CvArr centers NULL
# Gradients, Edges, Corners and Features
Sobel
@ -1648,7 +1651,7 @@ HOGDetectMultiScale CvSeq*
int nbins 9
int gammaCorrection 1
grabCut
GrabCut
CvArr image
CvArr mask
CvRect rect
@ -1700,11 +1703,11 @@ CreateHist CvHistogram /doconly
ranges ranges None
int uniform 1
CreateImageHeader IplImage* /doconly
int size
CvSize size
int depth
int channels
CreateImage IplImage* /doconly
int size
CvSize size
int depth
int channels
CreateMatHeader CvMat /doconly

View File

@ -3800,7 +3800,7 @@ static CvSeq* cvHOGDetectMultiScale( const CvArr* image, CvMemStorage* storage,
return seq;
}
static void cvgrabCut(CvArr *image,
static void cvGrabCut(CvArr *image,
CvArr *mask,
CvRect rect,
CvArr *bgdModel,
@ -3845,6 +3845,22 @@ static int zero = 0;
cvSnakeImage(image, points, length, a, b, g, coeff_usage, win, criteria, calc_gradient); \
} while (0)
static double cppKMeans(const CvArr* _samples, int cluster_count, CvArr* _labels,
CvTermCriteria termcrit, int attempts, int flags, CvArr* _centers)
{
cv::Mat data = cv::cvarrToMat(_samples), labels = cv::cvarrToMat(_labels), centers;
if( _centers )
centers = cv::cvarrToMat(_centers);
CV_Assert( labels.isContinuous() && labels.type() == CV_32S &&
(labels.cols == 1 || labels.rows == 1) &&
labels.cols + labels.rows - 1 == data.rows );
return cv::kmeans(data, cluster_count, labels, termcrit, attempts,
flags, _centers ? &centers : 0 );
}
#define cvKMeans2(samples, nclusters, labels, termcrit, attempts, flags, centers) \
cppKMeans(samples, nclusters, labels, termcrit, attempts, flags, centers)
#include "generated0.i"
#if PYTHON_USE_NUMPY

View File

@ -305,25 +305,25 @@
#define CV_EVENT_FLAG_SHIFTKEY 16
#define CV_EVENT_FLAG_ALTKEY 32
#define CV_MAX_DIM 32
include/highgui.h:#define CV_CAP_PROP_POS_MSEC 0
include/highgui.h:#define CV_CAP_PROP_POS_FRAMES 1
include/highgui.h:#define CV_CAP_PROP_POS_AVI_RATIO 2
include/highgui.h:#define CV_CAP_PROP_FRAME_WIDTH 3
include/highgui.h:#define CV_CAP_PROP_FRAME_HEIGHT 4
include/highgui.h:#define CV_CAP_PROP_FPS 5
include/highgui.h:#define CV_CAP_PROP_FOURCC 6
include/highgui.h:#define CV_CAP_PROP_FRAME_COUNT 7
include/highgui.h:#define CV_CAP_PROP_FORMAT 8
include/highgui.h:#define CV_CAP_PROP_MODE 9
include/highgui.h:#define CV_CAP_PROP_BRIGHTNESS 10
include/highgui.h:#define CV_CAP_PROP_CONTRAST 11
include/highgui.h:#define CV_CAP_PROP_SATURATION 12
include/highgui.h:#define CV_CAP_PROP_HUE 13
include/highgui.h:#define CV_CAP_PROP_GAIN 14
include/highgui.h:#define CV_CAP_PROP_EXPOSURE 15
include/highgui.h:#define CV_CAP_PROP_CONVERT_RGB 16
include/highgui.h:#define CV_CAP_PROP_WHITE_BALANCE 17
include/highgui.h:#define CV_CAP_PROP_RECTIFICATION 18
#define CV_CAP_PROP_POS_MSEC 0
#define CV_CAP_PROP_POS_FRAMES 1
#define CV_CAP_PROP_POS_AVI_RATIO 2
#define CV_CAP_PROP_FRAME_WIDTH 3
#define CV_CAP_PROP_FRAME_HEIGHT 4
#define CV_CAP_PROP_FPS 5
#define CV_CAP_PROP_FOURCC 6
#define CV_CAP_PROP_FRAME_COUNT 7
#define CV_CAP_PROP_FORMAT 8
#define CV_CAP_PROP_MODE 9
#define CV_CAP_PROP_BRIGHTNESS 10
#define CV_CAP_PROP_CONTRAST 11
#define CV_CAP_PROP_SATURATION 12
#define CV_CAP_PROP_HUE 13
#define CV_CAP_PROP_GAIN 14
#define CV_CAP_PROP_EXPOSURE 15
#define CV_CAP_PROP_CONVERT_RGB 16
#define CV_CAP_PROP_WHITE_BALANCE 17
#define CV_CAP_PROP_RECTIFICATION 18
#define CV_CN_SHIFT 3
#define CV_IMWRITE_JPEG_QUALITY 1
#define CV_IMWRITE_PNG_COMPRESSION 16
@ -333,7 +333,9 @@ include/highgui.h:#define CV_CAP_PROP_RECTIFICATION 18
#define CV_PI
#define CV_GAUSSIAN_5x5
#define CV_CN_MAX
../include/opencv/highgui.h:#define CV_WINDOW_AUTOSIZE 1
../include/opencv/highgui.h:#define CV_WINDOW_NORMAL 0
../include/opencv/highgui.h:#define CV_WINDOW_FULLSCREEN 1
../include/opencv/highgui.h:#define HG_AUTOSIZE CV_WINDOW_AUTOSIZE
#define CV_WINDOW_AUTOSIZE 1
#define CV_WINDOW_NORMAL 0
#define CV_WINDOW_FULLSCREEN 1
#define HG_AUTOSIZE CV_WINDOW_AUTOSIZE
#define CV_CVTIMG_FLIP 1
#define CV_CVTIMG_SWAP_RB 2

View File

@ -383,8 +383,8 @@ class FunctionTests(OpenCVTests):
m = cv.CreateMat(rows, cols, t)
self.assertEqual(cv.GetElemType(m), t)
self.assertEqual(m.type, t)
self.assertRaises(cv.error, lambda: cv.CreateMat(0, 100, cv.CV_8SC4))
self.assertRaises(cv.error, lambda: cv.CreateMat(100, 0, cv.CV_8SC4))
self.assertRaises(cv.error, lambda: cv.CreateMat(-1, 100, cv.CV_8SC4))
self.assertRaises(cv.error, lambda: cv.CreateMat(100, -1, cv.CV_8SC4))
self.assertRaises(cv.error, lambda: cv.cvmat())
def test_DrawChessboardCorners(self):
@ -632,7 +632,7 @@ class FunctionTests(OpenCVTests):
tmp1 = cv.CreateMat(1, 13 * 5, cv.CV_32FC1)
tmp2 = cv.CreateMat(1, 13 * 5, cv.CV_32FC1)
mask = cv.CreateMat(image.rows, image.cols, cv.CV_8UC1)
cv.grabCut(image, mask, (10,10,200,200), tmp1, tmp2, 10, cv.GC_INIT_WITH_RECT)
cv.GrabCut(image, mask, (10,10,200,200), tmp1, tmp2, 10, cv.GC_INIT_WITH_RECT)
def test_HoughLines2_PROBABILISTIC(self):
li = cv.HoughLines2(self.yield_line_image(),
@ -806,6 +806,36 @@ class FunctionTests(OpenCVTests):
r2 = cv.SnakeImage(cv.GetImage(src), pts, w, w, w, (7,7), (cv.CV_TERMCRIT_ITER, 100, 0.1))
self.assertEqual(r, r2)
def test_KMeans2(self):
size = 500
samples = cv.CreateMat(size, 1, cv.CV_32FC3)
labels = cv.CreateMat(size, 1, cv.CV_32SC1)
centers = cv.CreateMat(2, 3, cv.CV_32FC1)
cv.Zero(samples)
cv.Zero(labels)
cv.Zero(centers)
cv.Set(cv.GetSubRect(samples, (0, 0, 1, size/2)), (255, 255, 255))
compact = cv.KMeans2(samples, 2, labels, (cv.CV_TERMCRIT_ITER, 100, 0.1), 1, 0, centers)
self.assertEqual(int(compact), 0)
random.seed(0)
for i in range(50):
index = random.randrange(size)
if index < size/2:
self.assertEqual(samples[index, 0], (255, 255, 255))
self.assertEqual(labels[index, 0], 1)
else:
self.assertEqual(samples[index, 0], (0, 0, 0))
self.assertEqual(labels[index, 0], 0)
for cluster in (0, 1):
for channel in (0, 1, 2):
self.assertEqual(int(centers[cluster, channel]), cluster*255)
def test_Sum(self):
for r in range(1,11):
for c in range(1, 11):