WorldViz User Forum  

Go Back   WorldViz User Forum > Vizard

Reply
 
Thread Tools Rate Thread Display Modes
  #1  
Old 12-02-2014, 08:52 AM
VirtuallyInsane VirtuallyInsane is offline
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.theta = math.radians(self.ANGLE) # Converts angle x from degrees to radians.
        self.sigma_x = 1.0
        self.sigma_y = 30.0
        self.radius = 100
        
        self.M = np.zeros((self.radius*2, self.radius*2))
        # 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.theta = math.radians(self.ANGLE)
        x = -float(self.radius)
        for i in range(self.radius*2):
            y = -float(self.radius)
            for j in range(self.radius*2):
                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()
Reply With Quote
  #2  
Old 12-02-2014, 08:57 AM
VirtuallyInsane VirtuallyInsane is offline
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
Reply With Quote
Reply

Thread Tools
Display Modes Rate This Thread
Rate This Thread:

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 Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Vizard problem mizutani_jun Vizard 4 01-09-2011 03:39 PM
Vizard tech tip: Using the Python Imaging Library (PIL) Jeff Vizard 0 03-23-2009 11:13 AM
newbie problem getting vizard to work Colin Vizard 2 11-10-2006 10:54 AM
problem talking to usb device in Vizard Vbents Plug-in development 2 06-15-2006 03:08 PM
Vizard Crashes: causes are hard to determine, possible problem with the viz code vr_boyko Vizard 1 01-07-2005 10:52 AM


All times are GMT -7. The time now is 07:54 AM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Copyright 2002-2023 WorldViz LLC