In [1]:
# Plot in this IPython Notebook instead of opening separate windows
%matplotlib inline
%config InlineBackend.figure_format = 'retina'
import scipy as sp
from skimage import io
In [2]:
import pattern_finder_gpu
from pattern_finder_gpu import center_roi_around, rotation_around
In [3]:
from ipywidgets import interact, interactive, fixed, interact_manual
In [4]:
from matplotlib import pyplot as plt
In [5]:
from skimage import transform
In [6]:
import logging
logging.basicConfig()
We create a test image where we place a cross at a certain position. We later on want to find the position.
In [7]:
test_image = sp.ones((50,20,3), dtype=sp.float32) -0.4
test_cross_rc = [30, 6]
test_image[test_cross_rc[0]-4:test_cross_rc[0]+5, test_cross_rc[1]] = (1.0, 0, 0)
test_image[test_cross_rc[0], test_cross_rc[1]-4:test_cross_rc[1]+5] = (0, 1.0, 0)
io.imshow(test_image)
Out[7]:
We define a target pattern that we use to find the cross in the test image. The target pattern is in the center.
In [8]:
test_target = sp.ones((7,11,4), dtype=sp.float32)
test_target_center_rc = sp.array(test_target.shape[:2]) / 2 - 0.5
# boder transparent
test_target_cross_rc = sp.around(test_target_center_rc).astype(sp.int32)
test_target[test_target_cross_rc[0]-3:test_target_cross_rc[0]+4, test_target_cross_rc[1], :] = (1.0, 0, 0, 1)
test_target[test_target_cross_rc[0], test_target_cross_rc[1]-3:test_target_cross_rc[1]+4, :] = (0, 1.0, 0, 1)
#test_target = transform.warp(test_target, rotation_around(10, test_target_cross_rc))
test_target[0,:,3] = 0
test_target[-1,:,3] = 0
test_target[:,0,3] = 0
test_target[:,-1,3] = 0
io.imshow(test_target)
Out[8]:
In [9]:
test_target[...,-1]
Out[9]:
In [10]:
test_roi = center_roi_around( (25,10), (21,11))
In [11]:
test_roi
Out[11]:
In [12]:
test_PF = pattern_finder_gpu.PatternFinder(partitions=2)
In [13]:
test_PF.set_image(test_image)
test_PF.set_pattern(test_target)
test_out, test_rc, test_val = test_PF.find(roi=test_roi)
print(test_val)
print(test_rc)
fig, ax = plt.subplots()
ax.imshow(test_out)
ax.plot(*(test_rc-test_roi[:2])[::-1], "rx")
Out[13]:
In [14]:
test_out, test_rc, test_val1 = test_PF.find()
print(test_val1)
In [15]:
test_out, test_rc, test_val2 = test_PF.find(test_target, test_image)
print(test_val2)
In [16]:
assert(test_val1 == test_val2)
In [17]:
fig, ax = plt.subplots()
ax.imshow(test_out)
ax.plot(*test_rc[::-1], "rx")
Out[17]:
In [18]:
test_rc
Out[18]:
In [19]:
test_cross_rc
Out[19]:
In [20]:
assert sp.allclose(test_rc, test_cross_rc)
In [21]:
true_rot = 4 #deg
true_trans = -sp.array([10,5])
true_transe = (
rotation_around(true_rot, around_rc=sp.asarray(test_cross_rc)) +
transform.AffineTransform(translation=true_trans[::-1])
)
In [22]:
true_transe.params
Out[22]:
In [23]:
test_image_transfromed = transform.warp(test_image,
true_transe,
)#output_shape=[test_target.shape[0], test_target.shape[1]])
In [24]:
io.imshow(test_image_transfromed)
Out[24]:
In [25]:
test_image_transfromed.shape
Out[25]:
In [ ]:
def propeller(deg, dx, dy):
T = (
transform.AffineTransform(translation=(-dx, -dy)) +
rotation_around(deg, around_rc=test_rc)
)
io.imshow(transform.warp(test_image, T))
interact(propeller, deg=(-100,100,1), dx=(-10,10,1), dy=(-10,10,1))
Out[ ]:
In [27]:
found_trans, found_value = pattern_finder_gpu.find_pattern_rotated(test_PF,
test_target,
test_image,
rotations=sp.linspace(-6,6,13),
roi_size_hw=(37,17),
roi_center_rc=(26,9),
plot='all')
In [28]:
found_trans.params
Out[28]:
In [29]:
io.imshow(transform.warp(test_image, found_trans))
Out[29]:
Let's use a nice image from the data that comes with skimage and show it
In [30]:
from skimage import data
In [31]:
test_image2 = data.coffee()
In [32]:
io.imshow(test_image2)
Out[32]:
Use a cutout of the test_image2 as the target and make it RGBA (as uint8)
In [33]:
test_image2.shape
Out[33]:
In [34]:
test_image2.dtype
Out[34]:
In [35]:
test_target2 = sp.ones((101, 201, 4), dtype='uint8') * 255
test_target2[:,:,:3] = test_image2[150:251,150:351,:]
test_target2.shape
Out[35]:
In [36]:
io.imshow(test_target2)
Out[36]:
In [37]:
test_image2.shape
Out[37]:
In [38]:
theta = 20
dxy = sp.array((-50,-100))
test_image2_transform = transform.AffineTransform(rotation=sp.deg2rad(theta), translation=dxy)
In [39]:
test_image2_rotated_translated = transform.warp(test_image2, test_image2_transform)
io.imshow(test_image2_rotated_translated)
Out[39]:
In [40]:
true_reverse_transform = (transform.AffineTransform(translation=-dxy+(150,150)) +
transform.AffineTransform(rotation=-sp.deg2rad(theta)))
true_reversed_test_image2 = transform.warp(test_image2_rotated_translated,
true_reverse_transform,
output_shape=test_target2.shape)
io.imshow(true_reversed_test_image2)
Out[40]:
We can see it fits perfectly. Now let's try if find_pattern_rotate can reach a similar result:
In [41]:
trans, value = pattern_finder_gpu.find_pattern_rotated(test_PF,
test_target2,
test_image2_rotated_translated,
rescale=1,
rotations=sp.linspace(-50, 50 , 50+50+1),
roi_size_hw=(191, 307),
roi_center_rc=(253, 300), # row, col
plot='all')
In [42]:
center_roi_around((330, 220), (221,221))
Out[42]:
In [43]:
sp.rad2deg(trans.rotation)
Out[43]:
In [44]:
trans.translation
Out[44]:
In [45]:
result = transform.warp(test_image2_rotated_translated, trans, output_shape=test_target2.shape)
io.imshow(result)
Out[45]:
In [46]:
io.imshow(test_target2)
Out[46]:
In [47]:
io.imshow(sp.absolute(test_target2[...,:3]/255 - result))
Out[47]:
In [48]:
In [48]: