PageRenderTime 155ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/opencv_source/interfaces/swig/python/build/lib.win32-2.6/opencv/matlab_syntax.py

https://bitbucket.org/m4271n/opencv
Python | 269 lines | 193 code | 5 blank | 71 comment | 6 complexity | aeb5f26707e23ccce463993af624628f MD5 | raw file
Possible License(s): BSD-3-Clause
  1. #########################################################################################
  2. #
  3. # IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
  4. #
  5. # By downloading, copying, installing or using the software you agree to this license.
  6. # If you do not agree to this license, do not download, install,
  7. # copy or use the software.
  8. #
  9. #
  10. # Intel License Agreement
  11. # For Open Source Computer Vision Library
  12. #
  13. # Copyright (C) 2000, Intel Corporation, all rights reserved.
  14. # Third party copyrights are property of their respective owners.
  15. #
  16. # Redistribution and use in source and binary forms, with or without modification,
  17. # are permitted provided that the following conditions are met:
  18. #
  19. # * Redistribution's of source code must retain the above copyright notice,
  20. # this list of conditions and the following disclaimer.
  21. #
  22. # * Redistribution's in binary form must reproduce the above copyright notice,
  23. # this list of conditions and the following disclaimer in the documentation
  24. # and/or other materials provided with the distribution.
  25. #
  26. # * The name of Intel Corporation may not be used to endorse or promote products
  27. # derived from this software without specific prior written permission.
  28. #
  29. # This software is provided by the copyright holders and contributors "as is" and
  30. # any express or implied warranties, including, but not limited to, the implied
  31. # warranties of merchantability and fitness for a particular purpose are disclaimed.
  32. # In no event shall the Intel Corporation or contributors be liable for any direct,
  33. # indirect, incidental, special, exemplary, or consequential damages
  34. # (including, but not limited to, procurement of substitute goods or services;
  35. # loss of use, data, or profits; or business interruption) however caused
  36. # and on any theory of liability, whether in contract, strict liability,
  37. # or tort (including negligence or otherwise) arising in any way out of
  38. # the use of this software, even if advised of the possibility of such damage.
  39. #
  40. #########################################################################################
  41. """Matlab syntax for OpenCV
  42. For those who have switched from Matlab, this module offers similar syntax to the basic
  43. Matlab commands. I.e. you can invoke 'imread' to load images, 'imshow', etc.
  44. """
  45. from cv import *
  46. from highgui import cvShowImage,cvNamedWindow,cvLoadImage,cvWaitKey
  47. #__all__ = ['imagesc', 'display', 'imread', 'imshow', 'saveimage', 'loadimage', 'pause',
  48. # 'Image', 'Image8', 'Image8c3', 'Image32s', 'Image32f', 'Image64f']
  49. def eye(*args):
  50. mat = array(*args)
  51. cvSetIdentity(mat);
  52. return mat
  53. def ones(*args):
  54. mat = array(*args)
  55. cvSet(mat, cvScalarAll(1.0))
  56. return mat
  57. def zeros(*args):
  58. mat = array(*args)
  59. cvSet(mat, cvScalarAll(0.0))
  60. return mat
  61. def array(*args):
  62. m=1
  63. n=1
  64. c=1
  65. classname='single'
  66. nargs = len(args)
  67. # nasty argument parsing
  68. if nargs>0:
  69. if isinstance(args[0],tuple) or isinstance(args[0],list) and len(args[0]) > 1:
  70. m=args[0][0]
  71. n=args[0][1]
  72. if len(args[0])>2:
  73. c=args[0][2]
  74. if len(args)>1:
  75. classname = args[1]
  76. else:
  77. m=args[0]
  78. if nargs == 1:
  79. n=args[0]
  80. elif nargs > 1:
  81. # is the last argument the classname?
  82. if args[nargs-1].__class__ == str:
  83. classname = args[nargs-1]
  84. nargs-=1
  85. if nargs > 1:
  86. n = args[1]
  87. if nargs > 2:
  88. c = args[2]
  89. if(classname=='double'):
  90. depth=cv.CV_64F
  91. elif(classname=='single'):
  92. depth=cv.CV_32F
  93. elif(classname=='int8'):
  94. depth=cv.CV_8S
  95. elif(classname=='uint8'):
  96. depth=cv.CV_8U
  97. elif(classname=='int16'):
  98. depth=cv.CV_16S
  99. elif(classname=='uint16'):
  100. depth=cv.CV_16U
  101. elif(classname=='int32' or classname=='uint32' or
  102. classname=='int64' or classname=='uint64'):
  103. depth=cv.CV_32S
  104. else:
  105. depth=cv.CV_32F
  106. depth = CV_MAKETYPE(depth, c)
  107. return cvCreateMat(m,n,depth)
  108. def size(X,dim=-1):
  109. # CvMat
  110. if hasattr(X, "type"):
  111. sz = (X.rows, X.cols, CV_MAT_CN(X.type))
  112. # IplImage
  113. elif hasattr(X, "nChannels"):
  114. sz = (X.height, X.width, X.nChannels)
  115. # CvMatNd
  116. else:
  117. sz = cvGetDims(X)
  118. if dim is -1:
  119. return sz
  120. return sz[dim]
  121. def reshape(X, m, n=1, c=-1):
  122. '''
  123. reshape will produce different results in matlab and python due to the
  124. order of elements stored in the array (row-major vs. column major)
  125. '''
  126. if c==-1:
  127. c = CV_MAT_CN(X)
  128. return cvReshape(X, c, m)
  129. def im2float(im):
  130. mat = cvGetMat(im);
  131. if CV_MAT_DEPTH(mat.type)==CV_32F:
  132. return mat
  133. im64f = array(size(im), 'float')
  134. cvConvertScale(im, im64f, 1.0, 0.0)
  135. return im64f
  136. def im2double(im):
  137. mat = cvGetMat(im);
  138. if CV_MAT_DEPTH(mat.type)==CV_64F:
  139. return mat
  140. im64f = array(size(im), 'double')
  141. cvConvertScale(im, im64f, 1.0, 0.0)
  142. return im64f
  143. def rgb2ntsc (rgb):
  144. trans = [ [0.299, 0.596, 0.211], [0.587, -0.274, -0.523], [0.114, -0.322, 0.312] ];
  145. return rgb * trans;
  146. def rgb2gray(rgb):
  147. ntscmap = rgb2ntsc (rgb);
  148. graymap = ntscmap [:, 1] * ones (1, 3);
  149. return graymap
  150. class cvImageViewer:
  151. """
  152. Wrapper class for some matlab/octave/scilab syntax image viewing functions
  153. """
  154. currentWindowName = ""
  155. currentWindow = -1
  156. maxWindow = -1
  157. def imagesc(self,im, clims=None):
  158. """
  159. Display a normalized version of the image
  160. """
  161. if(self.currentWindow==-1):
  162. self.display()
  163. # don't normalize multichannel image
  164. #if(im.nChannels>1):
  165. # if(im.depth!=cv.IPL_DEPTH_8U):
  166. # im2 = cvCreateImage( cvSize(im.width, im.height), cv.IPL_DEPTH_8U, im.nChannels)
  167. # cvScale(im, im2)
  168. # im = im2
  169. # cvShowImage(self.currentWindowName, im)
  170. # return self.currentWindow
  171. # normalize image
  172. if clims:
  173. [minv, maxv] = clims
  174. else:
  175. [minv,maxv] = cvMinMaxLoc(im)
  176. if maxv != minv:
  177. s = 255.0/(maxv-minv)
  178. shift = 255*(-minv)/(maxv-minv)
  179. else:
  180. s = 1.0
  181. shift = -maxv
  182. im2 = array( size(im), 'uint8' )
  183. cvConvertScale(im, im2, s, shift)
  184. cvShowImage(self.currentWindowName, im2)
  185. def image(self, im):
  186. """
  187. Display image as is -- probably not what you'd expect for FP or integer images
  188. """
  189. if(self.currentWindow==-1):
  190. self.display()
  191. cvShowImage(self.currentWindowName,im)
  192. return self.currentWindow
  193. def display(self, index=-1):
  194. """
  195. open a new window
  196. """
  197. if(index==-1):
  198. self.maxWindow = self.maxWindow+1;
  199. index= self.maxWindow;
  200. if(index > self.maxWindow):
  201. self.maxWindow = index;
  202. self.currentWindow = index;
  203. self.currentWindowName = "opencv-python window %d" % self.currentWindow
  204. cvNamedWindow(self.currentWindowName,0)
  205. return self.currentWindow
  206. def drawnow():
  207. cvWaitKey(10)
  208. def pause(delay=-1):
  209. if delay<0:
  210. cvWaitKey(-1)
  211. else:
  212. cvWaitKey(delay*1000)
  213. c = cvImageViewer()
  214. imagesc = c.imagesc
  215. display = c.display
  216. image = c.image
  217. imshow = c.image
  218. def imread(fname):
  219. return cvLoadImage(fname, -1)
  220. loadimage = imread
  221. imload = imread
  222. def imsave(im, fname, format):
  223. return cvSaveImage(fname, im)
  224. saveimage = imsave
  225. def gradient(F):
  226. F = im2float(F)
  227. Fx = array(size(F))
  228. Fy = array(size(F))
  229. # new images
  230. cvSobel(F, Fx, 1, 0, CV_SCHARR)
  231. cvSobel(F, Fy, 0, 1, CV_SCHARR)
  232. return (Fx, Fy)