comparison python/cvutils.py @ 151:4af774bb186d

wrote a simple script to compute homography from point correspondences and display the reprojection for visual verification
author Nicolas Saunier <nicolas.saunier@polymtl.ca>
date Tue, 06 Sep 2011 17:55:06 -0400
parents 404f3cade05f
children 74b1fc68d4df
comparison
equal deleted inserted replaced
150:404f3cade05f 151:4af774bb186d
1 #! /usr/bin/env python 1 #! /usr/bin/env python
2 '''Image/Video utilities''' 2 '''Image/Video utilities'''
3 3
4 import Image, ImageDraw # PIL 4 import Image, ImageDraw # PIL
5 try: 5 try:
6 import cv,cv2 6 import cv2
7 opencvExists = True 7 opencvExists = True
8 except ImportError: 8 except ImportError:
9 print('OpenCV library could not be loaded') 9 print('OpenCV library could not be loaded')
10 opencvExists = False 10 opencvExists = False
11 from sys import stdout 11 from sys import stdout
12 12
13 import utils
14
13 #import aggdraw # agg on top of PIL (antialiased drawing) 15 #import aggdraw # agg on top of PIL (antialiased drawing)
14 #import utils 16 #import utils
15 17
16 __metaclass__ = type 18 __metaclass__ = type
19
20 cvRed = (0,0,255)
21 cvGreen = (0,255,0)
22 cvBlue = (255,0,0)
23 cvColors = utils.PlottingPropertyValues([cvRed,
24 cvGreen,
25 cvBlue])
17 26
18 def drawLines(filename, origins, destinations, w = 1, resultFilename='image.png'): 27 def drawLines(filename, origins, destinations, w = 1, resultFilename='image.png'):
19 '''Draws lines over the image ''' 28 '''Draws lines over the image '''
20 29
21 img = Image.open(filename) 30 img = Image.open(filename)
31 #out = utils.openCheck(resultFilename) 40 #out = utils.openCheck(resultFilename)
32 img.save(resultFilename) 41 img.save(resultFilename)
33 42
34 def computeHomography(srcPoints, dstPoints, method=0, ransacReprojThreshold=0.0): 43 def computeHomography(srcPoints, dstPoints, method=0, ransacReprojThreshold=0.0):
35 '''Returns the homography matrix mapping from srcPoints to dstPoints (dimension Nx2)''' 44 '''Returns the homography matrix mapping from srcPoints to dstPoints (dimension Nx2)'''
36 cvSrcPoints = arrayToCvMat(srcPoints); 45 #cvSrcPoints = arrayToCvMat(srcPoints);
37 cvDstPoints = arrayToCvMat(dstPoints); 46 #cvDstPoints = arrayToCvMat(dstPoints);
38 H = cv.CreateMat(3, 3, cv.CV_64FC1) 47 #H = cv.CreateMat(3, 3, cv.CV_64FC1)
39 cv.FindHomography(cvSrcPoints, cvDstPoints, H, method, ransacReprojThreshold) 48 H, mask = cv2.findHomography(srcPoints, dstPoints, method, ransacReprojThreshold)
40 return H 49 return H
41 50
42 def cvMatToArray(cvmat): 51 def cvMatToArray(cvmat):
43 '''Converts an OpenCV CvMat to numpy array.''' 52 '''Converts an OpenCV CvMat to numpy array.'''
44 from numpy.core.multiarray import zeros 53 from numpy.core.multiarray import zeros