This package takes a completely new and different approach in wrapping OpenCV from traditional swig-based and ctypes-based approaches. It is intended to be a successor of ctypes-opencv and to provide Python bindings for OpenCV 2.0. ctypes-based approaches like ctypes-opencv, while being very flexible at wrapping functions and structures, are weak at wrapping OpenCV's C++ interface. On the other hand, swig-based approaches flatten C++ classes and create countless memory management issues. In PyOpenCV, we use Boost.Python, a C++ library which enables seamless interoperability between C++ and Python. PyOpenCV will offer a better solution than both ctypes-based and swig-based wrappers:
- Provide a Python interface similar to the new C++ interface of OpenCV 2.0, including features that are available in the existing C interface but not in the C++ interface,
- Preserve C++ data structures and avoid memory management issues,
- Run at a speed nearer to OpenCV's native speed than existing wrappers.
In addition, we use NumPy to provide fast indexing and slicing functionality to OpenCV's dense data types like Vec-like, Point-like, Scalar, Mat, and MatND, and to offer the user an option to work with their multi-dimensional arrays in NumPy. It is well-known that NumPy is one of the best packages (if not the best) for dealing with multi-dimensional arrays in Python. OpenCV 2.0 provides a new C++ generic programming approach for matrix manipulation (i.e. MatExpr). It is a good attempt in C++. However, in Python, a package like NumPy is without a doubt a better solution. By incorporating NumPy into PyOpenCV to replace OpenCV 2.0's MatExpr approach, we seek to bring OpenCV and NumPy closer together, and offer a package that inherits the best of both world: fast computer vision functionality (OpenCV) and fast multi-dimensional array computation (NumPy).
At the moment, the development of PyOpenCV is at the beta stage. I am the sole author/developer of the project. I am using Py++ to generate the source code, but I can only work in my spare time. Hence, the development speed is rather low. I constantly look for partners to develop the project. If you would like to join in, please let me know.
Installation
General instructions for installing PyOpenCV can be found at the Installation page.
Example
Python demo code for K-Means Clustering
from pyopencv import *
import numpy.random as NR
MAX_CLUSTERS=5
if __name__ == "__main__":
color_tab = [CV_RGB(255,0,0),CV_RGB(0,255,0),CV_RGB(100,100,255), CV_RGB(255,0,255),CV_RGB(255,255,0)]
img = Mat(Size(500, 500), CV_8UC3)
rng = RNG()
namedWindow( "clusters", 1 )
while True:
cluster_count = rng.as_unsigned()%(MAX_CLUSTERS-1) + 2
# generate random sample from multigaussian distribution
points = NR.randn(cluster_count, rng.as_unsigned()%200 + 1, 2)*(img.cols, img.rows)*0.1
for k in range(cluster_count):
points[k] += (rng.as_unsigned()%img.cols, rng.as_unsigned()%img.rows)
sample_count = points.size/2
points = asMat(points.reshape(sample_count, 1, 2).astype('float32'))
randShuffle( points )
# K Means Clustering
clusters = Mat(points.size(), CV_32SC1)
compact, centers = kmeans(points, cluster_count, clusters,
TermCriteria(TermCriteria.EPS+TermCriteria.MAX_ITER, 10, 1.0), 3, KMEANS_RANDOM_CENTERS)
img.setTo(0)
pts = points[:].reshape(sample_count, 2).astype('int32')
for i in range(sample_count):
circle(img, asPoint(pts[i]), 2, color_tab[clusters[i,0]], CV_FILLED, CV_AA, 0)
imshow( "clusters", img )
if '%c' % (waitKey(0) & 255) in ['\x1b','q','Q']: # 'ESC'
break(Longer) Equivalent C demo code for K-Means Clustering
#ifdef _CH_
#pragma package <opencv>
#endif
#define CV_NO_BACKWARD_COMPATIBILITY
#ifndef _EiC
#include "cv.h"
#include "highgui.h"
#include <stdio.h>
#endif
int main( int argc, char** argv )
{
#define MAX_CLUSTERS 5
CvScalar color_tab[MAX_CLUSTERS];
IplImage* img = cvCreateImage( cvSize( 500, 500 ), 8, 3 );
CvRNG rng = cvRNG(-1);
CvPoint ipt;
color_tab[0] = CV_RGB(255,0,0);
color_tab[1] = CV_RGB(0,255,0);
color_tab[2] = CV_RGB(100,100,255);
color_tab[3] = CV_RGB(255,0,255);
color_tab[4] = CV_RGB(255,255,0);
cvNamedWindow( "clusters", 1 );
for(;;)
{
char key;
int k, cluster_count = cvRandInt(&rng)%MAX_CLUSTERS + 1;
int i, sample_count = cvRandInt(&rng)%1000 + 1;
CvMat* points = cvCreateMat( sample_count, 1, CV_32FC2 );
CvMat* clusters = cvCreateMat( sample_count, 1, CV_32SC1 );
cluster_count = MIN(cluster_count, sample_count);
/* generate random sample from multigaussian distribution */
for( k = 0; k < cluster_count; k++ )
{
CvPoint center;
CvMat point_chunk;
center.x = cvRandInt(&rng)%img->width;
center.y = cvRandInt(&rng)%img->height;
cvGetRows( points, &point_chunk, k*sample_count/cluster_count,
k == cluster_count - 1 ? sample_count :
(k+1)*sample_count/cluster_count, 1 );
cvRandArr( &rng, &point_chunk, CV_RAND_NORMAL,
cvScalar(center.x,center.y,0,0),
cvScalar(img->width*0.1,img->height*0.1,0,0));
}
/* shuffle samples */
for( i = 0; i < sample_count/2; i++ )
{
CvPoint2D32f* pt1 = (CvPoint2D32f*)points->data.fl + cvRandInt(&rng)%sample_count;
CvPoint2D32f* pt2 = (CvPoint2D32f*)points->data.fl + cvRandInt(&rng)%sample_count;
CvPoint2D32f temp;
CV_SWAP( *pt1, *pt2, temp );
}
printf( "iterations=%d\n", cvKMeans2( points, cluster_count, clusters,
cvTermCriteria( CV_TERMCRIT_EPS+CV_TERMCRIT_ITER, 10, 1.0 ),
5, 0, 0, 0, 0 ));
cvZero( img );
for( i = 0; i < sample_count; i++ )
{
int cluster_idx = clusters->data.i[i];
ipt.x = (int)points->data.fl[i*2];
ipt.y = (int)points->data.fl[i*2+1];
cvCircle( img, ipt, 2, color_tab[cluster_idx], CV_FILLED, CV_AA, 0 );
}
cvReleaseMat( &points );
cvReleaseMat( &clusters );
cvShowImage( "clusters", img );
key = (char) cvWaitKey(0);
if( key == 27 || key == 'q' || key == 'Q' ) // 'ESC'
break;
}
cvDestroyWindow( "clusters" );
return 0;
}
#ifdef _EiC
main(1,"kmeans.c");
#endifCurrent Wrapping Status
CxCore
- C Interface: almost done
- C++ Interface : almost done
Cv
- C Interface : almost done
- C++ Interface : almost done
HighGui
- C Interface : done
- C++ Interface : done
CxFLANN
- C++ Interface : done
ML
- C Interface : almost done
- C++ Interface : almost done
CvAux
- C Interface : started
- C++ Interface : in progress
CvVidSurv
- C Interface : started
Bugs and Commentary
Please send information on issues of usage to Minh-Tri Pham <pmtri80@gmail.com>, post a message to PyOpenCV and ctypes-opencv's discussion group, or create an issue in the Issues pannel.