Skip to content

Commit

Permalink
Merge branch 'master' of github-scienceopen:scienceopen/cv-phantom-gen
Browse files Browse the repository at this point in the history
  • Loading branch information
scienceopen committed Feb 3, 2017
2 parents 305c5bb + c6cb8e9 commit a9fb654
Show file tree
Hide file tree
Showing 12 changed files with 55 additions and 57 deletions.
4 changes: 0 additions & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ matrix:
include:
# - os: osx
# osx_image: xcode8.2
# env:
# - osv=MacOSX
# compiler: gcc
# before_install:
# - brew update && brew upgrade
Expand All @@ -24,8 +22,6 @@ matrix:
- os: linux
sudo: required
dist: trusty
env:
- osv=Linux
before_install:
- sudo add-apt-repository ppa:octave/stable -y
- sudo apt update -qq
Expand Down
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion makeSwirl.m → Matlab/makeSwirl.m
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
% function makeSwirl(I,x0,y0,strength,radius,showPlot,fillValue,BitDepth)
%
% inputs:
% I: image data (default is checkerboard 512x512 pixels)
% I: image data
% x0: x-pixel of swirl center
% y0: y-pixel of swirl center
% strength: factor indicating how strong twisting is
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
12 changes: 8 additions & 4 deletions NarrowArcBlur.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,21 @@
'rowcol': (512,512),
'dxy': (1,1),
'xy': (0,0), # displacement
'nframe': 1,
'nframe': 100,
'fwidth': 5,
'fstep': 1,
'gausssigma':10,
'texture': 'vertsine',
'motion': 'horizslide',
'fmt': None,
'motion': 'swirl',
'fmt': None,
'x0': [256], #swirl centers
'y0': [256], #swirl centers
'strength': 10, #swirl
'radius' : 30, #swirl
}
#%% computing
bg = phantomTexture(U)
bg = bg + shift(bg,[0,15]) # this line can wrap values if you overlap
#bg = bg + shift(bg,[0,15]) # this line can wrap values if you overlap

data = translateTexture(bg,U)
#%% plotting / saving
Expand Down
2 changes: 2 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
.. image:: https://zenodo.org/badge/31153745.svg
:target: https://zenodo.org/badge/latestdoi/31153745
.. image:: https://travis-ci.org/scienceopen/cv-phantom-gen.svg?branch=master
:target: https://travis-ci.org/scienceopen/cv-phantom-gen
.. image:: https://coveralls.io/repos/github/scienceopen/cv-phantom-gen/badge.svg?branch=master
Expand Down
81 changes: 38 additions & 43 deletions cvphantom/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,16 @@
from scipy.ndimage.filters import gaussian_filter,gaussian_laplace
from scipy.ndimage.interpolation import rotate,affine_transform
from scipy.ndimage import shift
from skimage.data import checkerboard
from skimage.transform import swirl

def phantomTexture(U:dict):
nrow,ncol = U['rowcol']
fwidth = U['fwidth']
dtype = U['dtype']
if 'dtype' in U and U['dtype'] is not None:
U['dtype'] = U['dtype']
else:
U['dtype'] = 'uint8'

bgmax = valmax(U['dtype'])
bstep = round(bgmax/(nrow/2))
Expand All @@ -16,42 +21,43 @@ def phantomTexture(U:dict):
texture = U['texture'].lower()

if texture == 'wall':
bg = bgmax * ones(U['rowcol'],dtype)
bg = bgmax * ones(U['rowcol'], U['dtype'])
elif texture == 'uniformrandom':
bg = (bgmax * rand(U['rowcol'])).astype(dtype)
bg = (bgmax * rand(U['rowcol'])).astype(U['dtype'])
elif texture == 'gaussian':
bg = zeros(U['rowcol'],float)
bg[nrow//2,ncol//2] = 1.
bg = gaussian_filter(bg,U['gausssigma'])
bg = bgmax * bg / bg.max()# normalize to full bit range
bg = bg.astype(dtype)
bg = bg.astype(U['dtype'])
assert bg[nrow//2,ncol//2] == bgmax,'did you wrap value?'
elif texture == 'vertsine':
bgr = zeros(ncol,float) #yes, double to avoid quantization
bg = zeros(U['rowcol'],dtype)
bgr[ncol//2-fwidth//2:ncol//2+fwidth//2+1] = sin(radians(linspace(0,180,fwidth))) #don't do int8 yet or you'll quantize it to zero!
bg = zeros(U['rowcol'], U['dtype'])
s = slice(ncol//2-fwidth//2,ncol//2+fwidth//2+1)
bgr[s] = sin(radians(linspace(0,180,len(bgr[s])))) #don't do int8 yet or you'll quantize it to zero!
rowind = range(nrow//4,nrow*3//4)
bg[rowind,:] = bgmax * bgr
bg = bg.astype(dtype) # needs to be its own line
bg = bg.astype(U['dtype']) # needs to be its own line
elif texture == 'laplacian':
bg = zeros(U['rowcol'],float)
bg[nrow//2,ncol//2] = 1.
bg = -gaussian_laplace(bg,U['gausssigma'])
bg -= bg.min()
bg = bgmax * bg / bg.max() # normalize to full bit range
elif texture == 'xtriangle':
bg = zeros(ncol,dtype)
bg[:ncol//2] = arange(0,rmax, bstep, dtype)
bg = zeros(ncol, U['dtype'])
bg[:ncol//2] = arange(0,rmax, bstep, U['dtype'])
bg[ncol//2:] = bg[:ncol//2][::-1]
bg = tile(bg[None,:],[nrow,1]);
elif texture == 'ytriangle':
bg = zeros(nrow,dtype)
bg[:nrow//2] = arange(0,rmax, bstep, dtype)
bg = zeros(nrow, U['dtype'])
bg[:nrow//2] = arange(0,rmax, bstep, U['dtype'])
bg[nrow//2:] = bg[:nrow//2][::-1]
bg = tile(bg[:,None],[1,ncol])
elif texture in ('pyramid','pyramid45'):
bg = zeros(U['rowcol'],dtype)
temp = arange(0, rmax, bstep, dtype)
bg = zeros(U['rowcol'], U['dtype'])
temp = arange(0, rmax, bstep, U['dtype'])
for i in range(1,nrow//2):
bg[i,i:-i] = temp[i] #north face
bg[i:-i,i] = temp[i] #west face
Expand All @@ -64,14 +70,16 @@ def phantomTexture(U:dict):
bg = zeros(U['rowcol']) # float to avoid overflow
bg[nrow//2-fwidth//2:nrow//2 + fwidth//2, :ncol] = bgmax #horizontal line
bg[:nrow, ncol//2 - fwidth//2:ncol//2 + fwidth//2] = bgmax #vertical line
bg = clip(bg + rotate(bg,45,reshape=False),0,bgmax).astype(dtype) #diagonal line
bg = clip(bg + rotate(bg,45,reshape=False),0,bgmax).astype(U['dtype']) #diagonal line
elif texture== 'vertbar': #vertical bar, starts center of image
bg = zeros(U['rowcol'],dtype)
bg = zeros(U['rowcol'], U['dtype'])

#bg(1:nRow,end-4:end) = bgMaxVal; %vertical line, top to bottom

# vertical bar starts 1/4 from bottom and 1/4 from top of image
bg[nrow*1//4:nrow*3//4, ncol//2 - fwidth//2:ncol//2 + fwidth//2] = bgmax
elif texture == 'checkerboard':
bg = checkerboard() # no input options, it's loading an image
else:
raise TypeError('unspecified texture {} selected'.format(U['texture']))

Expand All @@ -92,23 +100,20 @@ def valmax(dtype):


def translateTexture(bg,U:dict):
# function data = translateTexture(bg,data,swirlParam,U)
# to make multiple simultaneous phantoms, call this function repeatedly and sum the result

"""
to make multiple simultaneous phantoms, call this function repeatedly and sum the result
"""
if not 'motion' in U:
U['motion']=None
if not 'nframe' in U:
U['nframe']=1
if not 'fstep' in U or not U['fstep']:
U['fstep']=1

nrow,ncol = U['rowcol']
nrow,ncol = bg.shape
nframe = U['nframe']
data = zeros((nframe,nrow,ncol), bg.dtype) #initialize all frame

# try:
# swx0 = swirlParam.x0; swy0 = swirlParam.y0;
# swstr = swirlParam.strength; swrad = swirlParam.radius;
#%% indices for frame looping
I = range(0,nframe,U['fstep'])

Expand All @@ -117,30 +122,17 @@ def translateTexture(bg,U:dict):
else:
motion = U['motion']

#%% implement motion
if motion in (None,'none'):
for i in I:
data[i,...] = bg
# elif motion == 'swirlstill': #currently, swirl starts off weak, and increases in strength
# swirlParam.x0(1:length(swirlParam.x0)) = U.fwidth; #FIXME
# print('The Swirl algorithm is alpha-testing--needs manual positioning help--try vertbar texture')
# for i in range(I):
# data[i,...] = makeSwirl(bg,...
# swx0,swy0,...
# swstr * (i-1), swrad,...
# false,fillValue,U.bitdepth);
# elif motion == 'shearrightswirl':
# print('swirl location not matching shear right now? shear going wrong way?')
# swx0(1:length(swy0)) = U.fwidth; %FIXME
# # swirl, then shear
# for i in range(I):
#
# #Step 1: swirl
# dataFrame = makeSwirl(bg,...
# swx0,swy0,...
# swstr * (i-1), swrad,...
# false,fillValue,U.bitdepth);
# #step 2: shear
# data[i,...] = doShearRight(dataFrame,RA,i,nFrame,U.dxy(1),U.rowcol,fillValue);
elif motion == 'swirl':
for i in I:
strength = i/nframe*U['strength']
for x,y in zip(U['x0'], U['y0']): #for each swirl center...
data[i,...] = swirl(bg, (x,y), strength,
radius=U['radius'], rotation=0, clip=True, preserve_range=True)

elif motion == 'rotate360ccw':
for i in I:
q = (nframe-i)/nframe*360 #degrees
Expand Down Expand Up @@ -172,6 +164,9 @@ def translateTexture(bg,U:dict):
return data

def doShearRight(bg,i,U):
"""
see also http://scikit-image.org/docs/dev/api/skimage.transform.html#affinetransform
"""
nframe = U['nframe']

T = [[1,0,0],
Expand Down
2 changes: 1 addition & 1 deletion cvphantom/plots.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def playwrite(imgs,U):
figure(1).clf()
fg = figure(1)
ax=fg.gca()
hi = ax.imshow(imgs[0,...], origin='bottom')
hi = ax.imshow(imgs[0,...], origin='bottom', interpolation="none")
fg.colorbar(hi,ax=ax)

if U['fmt'] == 'avi' and videoWriter is not None: #output video requested
Expand Down
9 changes: 5 additions & 4 deletions tests/test.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#!/usr/bin/env python
from cvphantom import genphantom
from cvphantom import phantomTexture
#from numpy.testing import assert_allclose

U = {'bitdepth': 16,
U = {'dtype': 'uint16',
'rowcol': (128,128),
'dxy': (1,1),
'nframe': 1,
Expand All @@ -11,6 +11,7 @@
'texture': 'vertsine',
}

imgs = genphantom(U)
imgs = phantomTexture(U)

assert imgs[31,6]==42125
assert imgs.dtype == U['dtype']
assert imgs[62,62]==53018

0 comments on commit a9fb654

Please sign in to comment.