WorldViz User Forum gabor patch - matplotlib vizard problem
 Register FAQ Search Today's Posts Mark Forums Read

#1
12-02-2014, 08:52 AM
 VirtuallyInsane Member Join Date: Jan 2013 Posts: 9
gabor patch - matplotlib vizard problem

Hello everyone,

just having troubles with a little class that makes a little gabor patch using some math, matplotlib, and vizmatplot. I would like to rotate the gabor patch (actually looks more like a bar) by pressing keys (left and right arrow) and update this every time a key was pressed.

However, "repainting" the gabor takes soooo much time... Has anyone some insight into what I am doing wrong and how to speed this up?

Many thanks in advance and kind regards!

Code:
```import matplotlib, sys
matplotlib.use("Agg")
import matplotlib.backends.backend_agg as agg
import pylab, math

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.cm as cm

import random, viz, vizmatplot, vizact

viz.go()

class GaborPatch():
""" http://www.science-emergence.com/ImageProcessing/ImageProcessingPython/PlotGaborFilterMatplotlib/"""
def __init__(self):
self.F = 0.0
self.ANGLE = 90
self.sigma_x = 1.0
self.sigma_y = 30.0

# viz.ON or viz.OFF

def onSensorDown(self, key):
if key == viz.KEY_RIGHT:
self.ANGLE += 10
print(self.ANGLE)
#pylab.clf()
self.Gabor()
if key == viz.KEY_LEFT:
self.ANGLE -= 10
print(self.ANGLE)
#pylab.clf()
self.Gabor()

def ChangeBase(self, x, y, theta):
self.x_theta = x * math.cos(self.theta) + y * math.sin(self.theta)
self.y_theta = y * math.cos(self.theta) - x * math.sin(self.theta)
return self.x_theta, self.y_theta

def GaborFunction(self, x, y, theta, f, sigma_x, sigma_y):
r1 = self.ChangeBase(x, y, theta)[0] / sigma_x
r2 = self.ChangeBase(x, y, theta)[1] / sigma_y
arg = - 0.5 * (r1**2 + r2**2)
return math.exp(arg) * math.cos(2*math.pi*f*self.ChangeBase(x, y, theta)[0])

def Gabor(self):
self.M[i, j] = self.GaborFunction(x, y, self.theta, self.F, self.sigma_x, self.sigma_y)
y = y + 1
x = x + 1
# Normalization from 0 to 255
self.M[:, :] = ((self.M[:, :] - self.M.min()) * 255) / (self.M.max() - self.M.min())

self.Plot()

def Plot(self):
# Create a figure with size 8 x 8 inches, resolution 80 dots per inch and
# set color of drawing background to black = 'k'
self.fig = pylab.figure(figsize=[8, 8], dpi=100, facecolor='k')
# gca stands for 'get current axis'
self.ax = self.fig.gca()
self.ax.axis('off')
plt.imshow(self.M, cmap=cm.Greys_r)
plt.hold(False)
#enable rendering of plot by passing the pyplot figure to vizmatplot
plt.grid()
matplot = vizmatplot.Show(self.fig)
#enable display of draw rate
matplot.showDrawRate(viz.OFF) # viz.ON or viz.OFF

def main():
patch = GaborPatch()
patch.Gabor()
viz.callback(viz.KEYDOWN_EVENT, patch.onSensorDown)
main()```
#2
12-02-2014, 08:57 AM
 VirtuallyInsane Member Join Date: Jan 2013 Posts: 9
sorry, the code needs the vizmatplot.py file that can be downloaded here in order to work:

http://kb.worldviz.com/articles/483

 Thread Tools Display Modes Rate This Thread Linear Mode Rate This Thread: 5 : Excellent 4 : Good 3 : Average 2 : Bad 1 : Terrible

 Posting Rules You may not post new threads You may not post replies You may not post attachments You may not edit your posts BB code is On Smilies are On [IMG] code is On HTML code is Off Forum Rules
 Forum Jump User Control Panel Private Messages Subscriptions Who's Online Search Forums Forums Home Announcements Vizard Precision Position Tracker (PPT) Plug-in development

 Similar Threads Thread Thread Starter Forum Replies Last Post mizutani_jun Vizard 4 01-09-2011 03:39 PM Jeff Vizard 0 03-23-2009 11:13 AM Colin Vizard 2 11-10-2006 10:54 AM Vbents Plug-in development 2 06-15-2006 03:08 PM vr_boyko Vizard 1 01-07-2005 10:52 AM

All times are GMT -7. The time now is 12:50 AM.