Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added texture synthesis code for 'polka dots' and 'bricks' stimulus.
- Loading branch information
1 parent
0f32290
commit 8379812
Showing
7 changed files
with
493 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
""" | ||
Generation of texture stimuli | ||
Generation of various texture stimuli | ||
""" | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
# -*- coding: utf-8 -*- | ||
""" | ||
Generate stimulus resembling Todd (2010) paper, bricks on a wall. | ||
@author: G. Aguilar, Jul 2014 | ||
""" | ||
|
||
import numpy as np | ||
import cairo | ||
import Image | ||
import random | ||
from math import pi | ||
|
||
def draw_rounded(cr, area, radius): | ||
""" draws rectangles with rounded (circular arc) corners """ | ||
|
||
a,b,c,d=area | ||
cr.arc(a + radius, c + radius, radius, 2*(pi/2), 3*(pi/2)) | ||
cr.arc(b - radius, c + radius, radius, 3*(pi/2), 4*(pi/2)) | ||
cr.arc(b - radius, d - radius, radius, 0*(pi/2), 1*(pi/2)) # ;o) | ||
cr.arc(a + radius, d - radius, radius, 1*(pi/2), 2*(pi/2)) | ||
cr.close_path() | ||
|
||
|
||
def draw(rootname, w=500, h=2500, bh=30, bw=45, sep=7, roundrad=5, lum=(0.0, 0.65)): | ||
""" | ||
Generate 'bricks' stimulus | ||
Parameters | ||
---------- | ||
rootname : string | ||
filename used as output | ||
w, h: ints | ||
width and height of desired stimulus, in pix. | ||
bh, bw: ints | ||
width and height of bricks, in pix. | ||
sep : int | ||
separation between bricks, in pix. | ||
rounrad : int | ||
radius for rounded corners, in pix. | ||
lum : tuple (double, double) | ||
min and max grayvalues for bricks, in the range [0, 1] | ||
Luminance for each brick is randomly taken from uniform distribution | ||
from lum min and max. Bricks separation is always maximum (white). | ||
""" | ||
|
||
outputpng = "%s.png" % rootname | ||
lummin, lummax = lum | ||
|
||
####### getting bricks positions | ||
x= np.arange(-2*bw, w+2*bw, bw+sep ) + random.random() * (bw+sep) | ||
y= np.arange(-2*bh, h+2*bh, bh+sep ) + random.random() * (bh+sep) | ||
|
||
X,Y=np.meshgrid(x,y) | ||
|
||
# offseting every other row | ||
X[0::2] = X[0::2] + bw/2.0 | ||
|
||
####### drawing with cairo | ||
data = np.zeros( (w, h, 4), dtype=np.uint8) | ||
surface = cairo.ImageSurface.create_for_data(data, cairo.FORMAT_ARGB32, w, h) | ||
cr = cairo.Context(surface) | ||
|
||
# fill with solid white | ||
cr.set_source_rgb(1.0, 1.0, 1.0) | ||
cr.paint() | ||
|
||
# drawing | ||
for i in range( X.shape[0]): | ||
for j in range ( X.shape[1]): | ||
|
||
#cr.rectangle( X[i,j], Y[i,j] , bw, bh) # x, y of top left, width and height | ||
|
||
# (top, bottom, left, right) edges in absolute coordinates: | ||
inside_area = (X[i,j], X[i,j]+ bw, Y[i,j], Y[i,j] + bh) | ||
draw_rounded(cr, inside_area, roundrad) | ||
|
||
cr.set_line_width(0) # width | ||
lum = random.random() * ( lummax- lummin) | ||
cr.set_source_rgb( lum, lum, lum) # r,g,b in [0,1] | ||
cr.fill() | ||
cr.stroke() | ||
|
||
|
||
# write output | ||
surface.write_to_png( outputpng ) | ||
del surface | ||
|
||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
# -*- coding: utf-8 -*- | ||
""" | ||
Example script that generate a 'bricks' texture | ||
@author: G. Aguilar, 2014 | ||
""" | ||
import sys | ||
sys.path.append('..') | ||
|
||
import bricks | ||
|
||
# | ||
bricks.draw('test_bricks', w=500, h=1500) | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
# -*- coding: utf-8 -*- | ||
""" | ||
Example script that generate a texture of random dots using | ||
stochastic geometry. It creates the 'polka dots' texture, that consist of | ||
randomly placed circles with an inhibition distance, and a voronoi | ||
tesselation. Both are computed using a 'hardcore' point process in R. | ||
These functions generate the centers and draws them immediately to a png file. | ||
Dependencies: R with the 'spatstat' package. | ||
in python: PIL, cairo and rpy2. | ||
@author: G. Aguilar, 2013-14 | ||
""" | ||
import sys | ||
sys.path.append('..') | ||
|
||
from spatialprocess import hardcore | ||
|
||
# Generates one instance of 3500 number of circles / centroids, using | ||
# default parameters of size and inhibition distance. It also creates a | ||
# tesselation using those points as centroids. | ||
|
||
rootname = "test" | ||
x,y, files = hardcore.generate_hardcore(rootname, N=3500, tess=True) | ||
|
||
|
||
# from the image files, it creates 5 different samples at random locations. | ||
hardcore.sample(files[0], n=5) | ||
hardcore.sample(files[1], n=5) | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
import hardcore |
Oops, something went wrong.