Skip to main content

Posts

Showing posts with the label scipy

SciPy-Simulator

For the last few weeks I have been working on a Simulink / Labview / Ptolemy II like program for modelling... all sorts of stuff!! I am interested in modeling with different systems of computation, and of course some engineering problems, mechanical and electrical control etc. Ptolemey II is an open source java based system which a whole lot of people from Berkley have been working on for a number of years - I am not trying to match what they have - or get anywhere near the breadth that the commercial products Simulink and Labview have. But I do think something useful is very achievable - and of course leveraging what I can from Ptolemy II and basing mine on Python - heavily using SciPy , NumPy and MatPlotLib . So far I have been working on the semantics - in essence creating a new language for defining models by using blocks (or Actors) that carry out a very specific function. An example actor might be a Ramp Source or a Sin Function . Each of these actors run in their own thre...

Kiwi Pycon

I have been looking at harris feature detection lately. Implementing it side by side in OpenCV and SciPy, luckily for me a SciPy implementation by Jan Solem was found on this blog . As I was going through the code, I wanted to get my head around what was happening. So using these two lines: from IPython.Shell import IPShellEmbed IPShellEmbed()() This piece of magic can be put anywhere, right deep inside a nested loop, inside a function called from X via Y via Z etc. And it obviously pops you right into the brilliant IPython shell, with the full normal IPython luxuries like timeit, history, autocomplete, pylab plotting... So I plotted a few images midway through processing, just to see what the program sees. First is the grayscale image taken from my webcam. No I'm not colour blind - I realize I have plotted it in colour.... Second and thirdly the two gaussian derivatives of the image, one in X and one in Y. And a bit later after getting the thing going - the final output! Pretty ...

Gaussian Blur

Trying to work out why the gaussian blur in OpenCV is different from that of SciPy... The differences are too small to see, but are still there. Following is an imshow of each channel of the difference image. And by looking at a singe row, we see that the difference spikes the whole intensity range. Edit: That was pretty much staring me in the face wasn't it... uint8's are prone to integer overflow! Comparing the OpenCV implementation with two versions in SciPy now gives: Doing a pixel for pixel comparison on each channel between the SciPy and OpenCV examples: The second one is comparing an IIR filter implementation to a ndfilt.

Decoration to the rescue!

import numpy as np from scipy import signal from opencv import adaptors class scipyFromOpenCV ( object ): """This decorator can be used to wrap a function that takes and returns a numpy array into one that takes and retuns an opencv CvMat. """ def __init__ ( self , f ): self . f = f def __call__ ( self , image ): # Convert CvMat to ndarray np_image = adaptors . Ipl2NumPy ( image ) # Call the original function np_image_filtered = self . f ( np_image ) # Convert back to CvMat return adaptors . NumPy2Ipl ( np_image_filtered ) @scipyFromOpenCV def slowGaussianBlur ( matrix ): """Manual gaussian blur - Very very very slow!""" filterSize = 3 filt = gauss_kern ( filterSize ) r = signal . convolve ( matrix [:,:, 0 ], filt , 'same' ) g = signal . convolve ( matrix [:,:, 1 ], filt ...

Spyder

Enter the new matlab contender, Spyder: This would have been pretty handy over the last summer! All the pylab, scipy and numpy docs. The matplotlib integration is key. Eventually it would be awesome to see pydb and stepping through python scripts like you can for m files in matlab. But this is a huge step in the right direction! This is also my first post from Windows, good to see that Python(x,y) has been coming along nicely since I last looked at it. I will see how this Pydee (renamed to Spyder) goes on all three platforms that I now use.

Weave

Weave is a python module that includes a method of including C and C++ code inline with python. Has a slightly bigger overhead than the swig approach but is a lot easier to implement. And the code can be any dynamically created string. For example this block of python code calculates the fixed point multiplication of var1 and var2. The code is modified from a matlab algorithm worked on during my summer internship at Tait Electronics . from scipy.weave import inline as inlineC r = rounding and 1 or 0 # Blatent hack to convert from pythons True/False to 1/0 for C/C++ exp = """ signed int K,result; signed long int temp; temp = (long int)var1 * (long int)var2; signed int sign = temp/abs(temp); if(r) { K = 1 > numBits); if(result > fp_Max){ result = fp_Max; } if(result result = inlineC(exp,[ 'var1' , 'var2' , 'numBits' , 'fp_Min' , 'fp_Max' , 'r' ]...

Current state of the art

Hello and welcome to my dev blog! So this blog is to track what I am up to in my developing... I am a python convert - when ever I can I now use python. At this very moment I am working as an engineering intern for Tait Electronics . My research project here is "Using Python as a replacement to Matlab in the field of signal processing". To this end I am using scipy and matplotlib daily. I have a iBook G4 (ppc) and run linux - precompiled binaries for closed source apps like skype and adobe flash simply don't get realeased for the powerpc. This makes life far more interesting! Work environment Windows 2000 - eek Eclipse (Version: 3.4.1) Pydev (1.4) Pydev Extensions svn mylyn Enthought Python Distribution (Py 2.5 + scipy + numpy + matplotlib) Python 2.3 -> 3.0 (and svn trunk) Home environment Ubuntu 8.10 (ppc) Eclipse Pydev svn mylyn django (svn) django-command-extensions (svn) Python 2.5, 2.6, 3.0 scipy, numpy and matplotlib, pil... gammu, python-gammu (No wammu ...