ctypes-opencv

Python wrapper for OpenCV using ctypes
Download

ctypes-opencv Ranking & Summary

Advertisement

  • Rating:
  • License:
  • BSD License
  • Price:
  • FREE
  • Publisher Name:
  • Minh-Tri Pham
  • Publisher web site:
  • http://code.google.com/u/pmtri80/

ctypes-opencv Tags


ctypes-opencv Description

Python wrapper for OpenCV using ctypes ctypes-opencv is a library that brings Intel's Open Source Computer Vision Library (OpenCV) to Python. OpenCV is a collection of algorithms and sample code for various computer vision problems. The goal of ctypes-opencv is to provide Python access to all documented functionality of OpenCV.Advantages:* Complete interface to OpenCV's CXCORE, CV, HighGUI components.* Pythonic interface. OpenCV's objects are safely deleted when not used. No need to call cvRelease...().* Pure Python package. Neither C/C++ compiler nor OpenCV's source code is needed.* Support on both versions 1.0 and 1.1 of OpenCV.* Cross platform, running on any OS that OpenCV can be installed, including: Windows, Linux, and Mac OS X. Disadvantage:* No support for OpenCV's ML component yet. This is a limitation of the current ctypes package. The ML component contains mostly C++ classes. ctypes at the moment cannot wrap C++ classes and their member functions. This limitation will be overcome in the future. Work has already begun.Example:C code#ifdef _CH_#pragma package #endif#ifndef _EiC#include "cv.h"#include "highgui.h"#endifint main( int argc, char** argv ){ #define MAX_CLUSTERS 5 CvScalar color_tab; IplImage* img = cvCreateImage( cvSize( 500, 500 ), 8, 3 ); CvRNG rng = cvRNG(-1); CvPoint ipt; color_tab = CV_RGB(255,0,0); color_tab = CV_RGB(0,255,0); color_tab = CV_RGB(100,100,255); color_tab = CV_RGB(255,0,255); color_tab = 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)00 + 1; CvMat* points = cvCreateMat( sample_count, 1, CV_32FC2 ); CvMat* clusters = cvCreateMat( sample_count, 1, CV_32SC1 ); /* 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 ); } cvKMeans2( points, cluster_count, clusters, cvTermCriteria( CV_TERMCRIT_EPS+CV_TERMCRIT_ITER, 10, 1.0 )); cvZero( img ); for( i = 0; i < sample_count; i++ ) { int cluster_idx = clusters->data.i; ipt.x = (int)points->data.fl; ipt.y = (int)points->data.fl; cvCircle( img, ipt, 2, color_tab, 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;}Python codefrom opencv import *from random import randintMAX_CLUSTERS=5if __name__ == "__main__": color_tab = img = cvCreateImage( cvSize( 500, 500 ), 8, 3 ) rng = cvRNG(-1) cvNamedWindow( "clusters", 1 ) while True: cluster_count = randint(2, MAX_CLUSTERS) sample_count = randint(1, 1000) points = cvCreateMat( sample_count, 1, CV_32FC2 ) clusters = cvCreateMat( sample_count, 1, CV_32SC1 ) # generate random sample from multigaussian distribution for k in range(cluster_count): center = CvPoint(cvRandInt(rng)%img.width, cvRandInt(rng)%img.height) first = k*sample_count/cluster_count last = sample_count if k != cluster_count: last = (k+1)*sample_count/cluster_count if first >= last: continue point_chunk = cvGetRows(points, first, last) 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 cvRandShuffle( points, rng ) cvKMeans2( points, cluster_count, clusters, cvTermCriteria( CV_TERMCRIT_EPS+CV_TERMCRIT_ITER, 10, 1.0 )) cvZero( img ) for i in range(sample_count): cluster_idx = clusters pt = points pt = cvPoint(cvRound(pt), cvRound(pt)) cvCircle( img, pt, 2, color_tab, CV_FILLED, CV_AA, 0 ) cvShowImage( "clusters", img ) key = cvWaitKey(0) if( key == 'x1b' or key == 'q' or key == 'Q' ): # 'ESC' break Requirements: · Python


ctypes-opencv Related Software