__init__.py
Go to the documentation of this file.
00001 import sys
00002 from PySide import QtGui, QtCore
00003 from PySide.QtGui import QPalette
00004 import numpy as np
00005 class CalibrationGrid(QtGui.QWidget):
00006         _width  = 700
00007         _height = 500
00008         padding = 100
00009         origin  = None
00010         corners = []
00011         
00012         key_handlers = dict()
00013         
00014         def __init__(self, nRows=5, nCols=5, origin=None, scale=1):
00015                 super(CalibrationGrid, self).__init__()
00016                 self.nRows = nRows
00017                 self.nCols = nCols
00018                 self.origin = origin
00019                 self.scale  = scale
00020                 self.initUI()
00021                 
00022         # @property
00023         # def width(self):
00024         #       return self.size().width()
00025         # 
00026         # @property
00027         # def height(self):
00028         #       return self.size().height()
00029                 
00030         def escHandler(self, e):
00031                 sys.exit(0)
00032                 
00033         def initUI(self):
00034                 self.addKeyHandler(16777216, self.escHandler)      
00035                 p = QPalette()
00036                 p.setColor(QPalette.Background, QtGui.QColor(255,255,255))
00037                 self.setPalette(p)
00038                 self.setGeometry(0, 0, self._width, self._height)
00039                 self.setWindowTitle('Projector Calibration')
00040                 self.show()
00041 
00042         def paintEvent(self, e):
00043                 qp = QtGui.QPainter()
00044                 qp.begin(self)
00045                 self.drawRectangles(qp)
00046                 qp.end()
00047                                 
00048         def keyPressEvent(self, e):
00049                 for key, fn in self.key_handlers.items():
00050                         if key == e.key():
00051                                 fn(e)
00052                 
00053         def addKeyHandler(self, key, fn):
00054                 self.key_handlers[key] = fn
00055                 
00056         def drawRectangles(self, qp):
00057                 black = False
00058                 cref = np.array((255,255,255))
00059                 square = None
00060                 if self.height() < self.width():
00061                         square = self.height()/self.nRows - 2*self.padding/self.nRows
00062                 else:
00063                         square = self.width()/self.nCols - 2*self.padding/self.nCols
00064                         
00065                 square *= self.scale
00066                         
00067                 grid_size_rows = square * self.nRows + 2 * self.padding
00068                 grid_size_cols = square * self.nCols + 2 * self.padding
00069                 
00070                 row_offset = (self.height() - grid_size_rows) / 2
00071                 col_offset = (self.width()  - grid_size_cols) / 2
00072                 
00073                 top0, left0 = (self.padding + row_offset, self.padding + col_offset)
00074                 if self.origin is not None:
00075                         top0, left0 = self.origin[1], self.origin[0]
00076         
00077                 del self.corners[:]
00078                 for row in range(self.nRows):
00079                         top = row*square + top0
00080                         for col in range(self.nCols):                   
00081                                 left = col*square + left0
00082                                 if (col < self.nCols - 1) and (row < self.nRows - 1):
00083                                         self.corners.append((top+square, left+square))
00084                                 color = QtGui.QColor(*(cref*black))
00085                                 qp.setPen(color)
00086                                 qp.setBrush(color)
00087                                 qp.drawRect(left, top, square, square)
00088                         
00089                                 black = (not black) 
00090                 
00091         def getPatternAsImage(self, im_type='PIL'):
00092                 pixmap = QtGui.QPixmap.grabWidget(self)
00093                 qimage = pixmap.toImage()
00094                 if im_type == 'PIL':
00095                         import Image
00096                         pil_im = Image.frombuffer('RGBA', (self.width(), self.height()), qimage.bits(), 'raw', 'RGBA', 0, 1).convert('L')
00097                         return pil_im
00098                 elif im_type == 'OPENCV':
00099                         import cv
00100                         cv_im = cv.CreateImageHeader((self.width(), self.height()), cv.IPL_DEPTH_8U, 4)
00101                         cv.SetData(cv_im, qimage.bits())
00102                         return cv_im


projector_calibration
Author(s): Daniel Lazewatsky
autogenerated on Mon Oct 6 2014 10:10:01