In [20]:
import numpy as np
import scipy as sp
import scipy.stats as stats
from skimage import img_as_float, io
# import matplotlib.pylab as plt
import sys, os
import psyutils as pu
from psyutils.image import show_im
import pandas as pd
# use ipython magic function to make sure psyutils is reloaded (for debugging):
%load_ext autoreload
%autoreload 2
# plots inline:
%pylab inline
# %config InlineBackend.figure_format = 'svg'
In [2]:
show_im(pu.misc.fixation_cross())
In [33]:
# pix per deg:
pu.misc.pix_per_deg(60, (1920, 1200), (48.4, 30.2), average_wh=False)
Out[33]:
In [36]:
pu.misc.pix_per_deg(50, (1920, 1200), (48.4, 30.2))
Out[36]:
In [4]:
im_size = 256
im = np.random.uniform(size=(im_size, im_size))
show_im(im)
In [5]:
filt = pu.image.make_filter_lowpass(im_size, cutoff=8)
show_im(filt)
In [6]:
blah = np.fft.fftshift(filt)
show_im(blah)
In [7]:
im2 = pu.image.filter_image(im, filt)
show_im(im2)
In [8]:
filt = pu.image.make_filter_highpass(im_size, cutoff=64)
im2 = pu.image.filter_image(im, filt)
show_im(im2)
In [9]:
filt = pu.image.make_filter_log_exp(im_size, peak=8, width=0.5)
show_im(filt)
In [10]:
im2 = pu.image.filter_image(im, filt)
show_im(im2)
In [11]:
filt = pu.image.make_filter_gaussian(im_size, peak=8, width=2)
show_im(filt)
In [12]:
im2 = pu.image.filter_image(im, filt)
show_im(im2)
In [13]:
filt = pu.image.make_filter_log_gauss(im_size, peak=8, width=.5)
show_im(filt)
In [14]:
im2 = pu.image.filter_image(im, filt)
show_im(im2)
In [15]:
filt = pu.image.make_filter_log_cosine(im_size, peak=8)
show_im(filt)
In [16]:
im2 = pu.image.filter_image(im, filt)
show_im(im2)
In [17]:
filt = pu.image.make_filter_alpha_over_f(im_size, 1)
show_im(filt)
In [18]:
im2 = pu.image.filter_image(im, filt)
show_im(im2)
In [19]:
filt = pu.image.make_filter_orientation_gaussian(im_size, peak=135, width=10)
show_im(filt)
In [20]:
im2 = pu.image.filter_image(im, filt)
show_im(im2)
In [21]:
sloans = pu.im_data.sloan_letters()
show_im(sloans["H"])
In [36]:
rect_a = sloans["H"]
rect_b = np.ones((512, 512))
new_rect = pu.image.put_rect_in_rect(rect_a, rect_b, midpoints=200)
show_im(new_rect)
In [59]:
rect_a = np.zeros((2, 2))
rect_b = np.ones((4, 4))
new = pu.image.put_rect_in_rect(rect_a, rect_b, midpoints=(3, 2))
show_im(new)
In [73]:
im = np.array([[1., 1., 1., 1.],
[1., 1., 0., 0.],
[1., 1., 0., 0.],
[1., 1., 1., 1.]])
res = pu.image.cutout_patch(im, size=(2, 2), midpoints=(3, 2))
res
Out[73]:
In [2]:
# 1D cosine window:
win = pu.image.cos_win_1d(200, ramp=45)
plot(win), plt.box('off')
Out[2]:
In [3]:
# 2D cosine window:
win = pu.image.cos_win_2d(128, ramp=0.3)
show_im(win)
In [4]:
# Specify using pixels (note the initial zero is included):
win = pu.image.cos_win_2d(16, ramp=4, ramp_type='pixels')
show_im(win)
In [5]:
win[7, :]
Out[5]:
I've written a wrapper function to save images as either 8- or 16-bit. Depending on the dimensions of the input, these will be saved as either greyscale images (MxN), RGB images (MxNx3) or RGBA images (MxNx4). Note that as of scikit-image version 0.10.1 there is a bug in saving to 16 bit, where the dimensions are all messed up. Should be fixed in a future version (see issue 1101 on their github).
The input array will be converted to float using skimage's img_to_float function.
In [7]:
im = np.random.uniform(size=(256, 256))
im[:128, :128] = 1
show_im(im)
In [8]:
# save as uint8, check reload:
fname = 'test_1_8_bit.png'
pu.image.save_im(fname, im, bitdepth=8)
test = io.imread(fname)
show_im(test)
In [9]:
# save as uint16, check reload:
fname = 'test_1_16_bit.png'
pu.image.save_im(fname, im, bitdepth=16)
test = io.imread(fname)
show_im(test)
In [10]:
im = np.random.uniform(size=(256, 256, 4))
im[..., 3] = pu.image.cos_win_2d(256, ramp=32, ramp_type='pixels') # fill cosine window into alpha level.
im[:128, :128, :3] = 1
show_im(im)
In [11]:
# save as uint8, check reload:
fname = 'test_2_8_bit.png'
pu.image.save_im(fname, im, bitdepth=8)
test = io.imread(fname)
show_im(test)
In [12]:
# save as uint16, check reload:
fname = 'test_2_16_bit.png'
pu.image.save_im(fname, im, bitdepth=16)
test = io.imread(fname)
show_im(test)
In [13]:
im = np.random.uniform(size=(256, 256, 3))
im[:128, :128] = 1
show_im(im)
In [14]:
# save as uint8, check reload:
fname = 'test_3_8_bit.png'
pu.image.save_im(fname, im, bitdepth=8)
test = io.imread(fname)
show_im(test)
In [15]:
# save as uint16, check reload:
fname = 'test_3_16_bit.png'
pu.image.save_im(fname, im, bitdepth=16)
test = io.imread(fname)
show_im(test)
psyutils internalsThe backend of the image submodule of psyutils generates filters by applying some distributions to some axes. There are basically four axes needed for the filtering operations, which are returned as 2D numpy arrays: cartesian x and y, radial distance r (i.e. distance from the axis centre) and polar angle a. All angles are specified in radians.
psyutils provides functions to generate such axes, as well as a couple of others (e.g. log-spaced axes).
In [15]:
x, y = pu.image.axes_cart(size=(12, 12), axes_limits=(-3, 10))
show_im(x)
In [16]:
r, a = pu.image.axes_polar(size=101)
show_im(r)
In [17]:
show_im(a)
In [18]:
x, y = pu.image.axes_semilogx_cart(size=10, axes_limits=(-2, 2))
show_im(x)
In [19]:
show_im(y)
In [21]:
r, a = pu.image.axes_logradial_polar(size=100)
show_im(r)
In [22]:
a = pu.image.axes_angular_distance(size=100)
show_im(a)