1 #########################################################################################
3 # IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
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.
10 # Intel License Agreement
11 # For Open Source Computer Vision Library
13 # Copyright (C) 2000, Intel Corporation, all rights reserved.
14 # Third party copyrights are property of their respective owners.
16 # Redistribution and use in source and binary forms, with or without modification,
17 # are permitted provided that the following conditions are met:
19 # * Redistribution's of source code must retain the above copyright notice,
20 # this list of conditions and the following disclaimer.
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.
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.
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.
40 #########################################################################################
42 """Matlab syntax for OpenCV
44 For those who have switched from Matlab, this module offers similar syntax to the basic
45 Matlab commands. I.e. you can invoke 'imread' to load images, 'imshow', etc.
49 from highgui import cvShowImage,cvNamedWindow,cvLoadImage,cvWaitKey
51 #__all__ = ['imagesc', 'display', 'imread', 'imshow', 'saveimage', 'loadimage', 'pause',
52 # 'Image', 'Image8', 'Image8c3', 'Image32s', 'Image32f', 'Image64f']
61 cvSet(mat, cvScalarAll(1.0))
66 cvSet(mat, cvScalarAll(0.0))
75 # nasty argument parsing
77 if isinstance(args[0],tuple) or isinstance(args[0],list) and len(args[0]) > 1:
89 # is the last argument the classname?
90 if args[nargs-1].__class__ == str:
91 classname = args[nargs-1]
98 if(classname=='double'):
100 elif(classname=='single'):
102 elif(classname=='int8'):
104 elif(classname=='uint8'):
106 elif(classname=='int16'):
108 elif(classname=='uint16'):
110 elif(classname=='int32' or classname=='uint32' or
111 classname=='int64' or classname=='uint64'):
115 depth = CV_MAKETYPE(depth, c)
116 return cvCreateMat(m,n,depth)
120 if hasattr(X, "type"):
121 sz = (X.rows, X.cols, CV_MAT_CN(X.type))
123 elif hasattr(X, "nChannels"):
124 sz = (X.height, X.width, X.nChannels)
133 def reshape(X, m, n=1, c=-1):
135 reshape will produce different results in matlab and python due to the
136 order of elements stored in the array (row-major vs. column major)
140 return cvReshape(X, c, m)
145 if CV_MAT_DEPTH(mat.type)==CV_32F:
148 im64f = array(size(im), 'float')
149 cvConvertScale(im, im64f, 1.0, 0.0)
154 if CV_MAT_DEPTH(mat.type)==CV_64F:
156 im64f = array(size(im), 'double')
157 cvConvertScale(im, im64f, 1.0, 0.0)
161 trans = [ [0.299, 0.596, 0.211], [0.587, -0.274, -0.523], [0.114, -0.322, 0.312] ];
165 ntscmap = rgb2ntsc (rgb);
166 graymap = ntscmap [:, 1] * ones (1, 3);
171 Wrapper class for some matlab/octave/scilab syntax image viewing functions
173 currentWindowName = ""
177 def imagesc(self,im, clims=None):
179 Display a normalized version of the image
181 if(self.currentWindow==-1):
184 # don't normalize multichannel image
186 # if(im.depth!=cv.IPL_DEPTH_8U):
187 # im2 = cvCreateImage( cvSize(im.width, im.height), cv.IPL_DEPTH_8U, im.nChannels)
190 # cvShowImage(self.currentWindowName, im)
191 # return self.currentWindow
197 [minv,maxv] = cvMinMaxLoc(im)
199 s = 255.0/(maxv-minv)
200 shift = 255*(-minv)/(maxv-minv)
205 im2 = array( size(im), 'uint8' )
206 cvConvertScale(im, im2, s, shift)
208 cvShowImage(self.currentWindowName, im2)
212 Display image as is -- probably not what you'd expect for FP or integer images
214 if(self.currentWindow==-1):
217 cvShowImage(self.currentWindowName,im)
218 return self.currentWindow
221 def display(self, index=-1):
226 self.maxWindow = self.maxWindow+1;
227 index= self.maxWindow;
229 if(index > self.maxWindow):
230 self.maxWindow = index;
232 self.currentWindow = index;
233 self.currentWindowName = "opencv-python window %d" % self.currentWindow
234 cvNamedWindow(self.currentWindowName,0)
235 return self.currentWindow
244 cvWaitKey(delay*1000)
253 return cvLoadImage(fname, -1)
257 def imsave(im, fname, format):
258 return cvSaveImage(fname, im)
267 cvSobel(F, Fx, 1, 0, CV_SCHARR)
268 cvSobel(F, Fy, 0, 1, CV_SCHARR)