Debugging/Testing PatternFinder


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)


/Users/sam/anaconda3/envs/hacking/lib/python3.7/site-packages/skimage/io/_plugins/matplotlib_plugin.py:150: UserWarning: Low image data range; displaying image with stretched contrast.
  lo, hi, cmap = _get_display_range(image)
Out[7]:
<matplotlib.image.AxesImage at 0x11c23d470>

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]:
<matplotlib.image.AxesImage at 0x11c330860>

In [9]:
test_target[...,-1]


Out[9]:
array([[0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
       [0., 1., 1., 1., 1., 1., 1., 1., 1., 1., 0.],
       [0., 1., 1., 1., 1., 1., 1., 1., 1., 1., 0.],
       [0., 1., 1., 1., 1., 1., 1., 1., 1., 1., 0.],
       [0., 1., 1., 1., 1., 1., 1., 1., 1., 1., 0.],
       [0., 1., 1., 1., 1., 1., 1., 1., 1., 1., 0.],
       [0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.]], dtype=float32)

In [10]:
test_roi = center_roi_around( (25,10), (21,11))

In [11]:
test_roi


Out[11]:
(15, 5, 36, 16)

In [12]:
test_PF = pattern_finder_gpu.PatternFinder(partitions=2)


INFO:PatternFinder:<pyopencl.Context at 0x7f97dbe75790 on <pyopencl.Device 'AMD Radeon Pro 560 Compute Engine' on 'Apple' at 0x7f97de7d0e90>>

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")


DEBUG:PatternFinder:Execution PatternFinder.find took 0.000008 ms
38.719997
[30  6]
Out[13]:
[<matplotlib.lines.Line2D at 0x11c23d828>]

In [14]:
test_out, test_rc, test_val1 = test_PF.find()
print(test_val1)


DEBUG:PatternFinder:Execution PatternFinder.find took 0.000002 ms
38.719997

In [15]:
test_out, test_rc, test_val2 = test_PF.find(test_target, test_image)
print(test_val2)


DEBUG:PatternFinder:Execution PatternFinder.find took 0.000003 ms
38.719997

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]:
[<matplotlib.lines.Line2D at 0x11dc774a8>]

In [18]:
test_rc


Out[18]:
array([30,  6])

In [19]:
test_cross_rc


Out[19]:
[30, 6]

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]:
array([[  0.99756405,  -0.06975647,  -2.89269009],
       [  0.06975647,   0.99756405, -10.34546035],
       [  0.        ,   0.        ,   1.        ]])

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]:
<matplotlib.image.AxesImage at 0x11c98c9b0>

find_pattern_rotated example


In [25]:
test_image_transfromed.shape


Out[25]:
(50, 20, 3)

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[ ]:
<function __main__.propeller(deg, dx, dy)>

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')


INFO:find_pattern_rotated:Rescaling image and target by scale=1.0.
    image (row, columns): (50, 20) px --> [50. 20.] px.
INFO:find_pattern_rotated:ROI center_rc=[26  9], in unscaled image.
     (height, width) = (37, 17) in scaled image.
INFO:find_pattern_rotated:Trying rotations: [-6. -5. -4. -3. -2. -1.  0.  1.  2.  3.  4.  5.  6.].
DEBUG:PatternFinder:Execution PatternFinder.find took 0.000001 ms
DEBUG:PatternFinder:Execution PatternFinder.find took 0.000001 ms
DEBUG:PatternFinder:Execution PatternFinder.find took 0.000001 ms
DEBUG:PatternFinder:Execution PatternFinder.find took 0.000001 ms
DEBUG:PatternFinder:Execution PatternFinder.find took 0.000001 ms
DEBUG:PatternFinder:Execution PatternFinder.find took 0.000001 ms
DEBUG:PatternFinder:Execution PatternFinder.find took 0.000001 ms
DEBUG:PatternFinder:Execution PatternFinder.find took 0.000001 ms
DEBUG:PatternFinder:Execution PatternFinder.find took 0.000001 ms
DEBUG:PatternFinder:Execution PatternFinder.find took 0.000002 ms
DEBUG:PatternFinder:Execution PatternFinder.find took 0.000001 ms
DEBUG:PatternFinder:Execution PatternFinder.find took 0.000001 ms
DEBUG:PatternFinder:Execution PatternFinder.find took 0.000001 ms
INFO:find_pattern_rotated:took 0.04048609733581543 seconds.
INFO:find_pattern_rotated:best_angle: -6.0 deg, best_coord (row,column): [30.  6.] in input image

In [28]:
found_trans.params


Out[28]:
array([[ 0.9945219 , -0.10452846,  1.34097591],
       [ 0.10452846,  0.9945219 , 26.493792  ],
       [ 0.        ,  0.        ,  1.        ]])

In [29]:
io.imshow(transform.warp(test_image, found_trans))


Out[29]:
<matplotlib.image.AxesImage at 0x11d5a2c50>

Test Rotation and Translation

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]:
<matplotlib.image.AxesImage at 0x11c9408d0>

Use a cutout of the test_image2 as the target and make it RGBA (as uint8)


In [33]:
test_image2.shape


Out[33]:
(400, 600, 3)

In [34]:
test_image2.dtype


Out[34]:
dtype('uint8')

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]:
(101, 201, 4)

In [36]:
io.imshow(test_target2)


Out[36]:
<matplotlib.image.AxesImage at 0x11d6144e0>

In [37]:
test_image2.shape


Out[37]:
(400, 600, 3)

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]:
<matplotlib.image.AxesImage at 0x11dccdfd0>

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]:
<matplotlib.image.AxesImage at 0x11d5ad438>

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')


INFO:find_pattern_rotated:Rescaling image and target by scale=1.
    image (row, columns): (400, 600) px --> [400 600] px.
INFO:find_pattern_rotated:ROI center_rc=[253 300], in unscaled image.
     (height, width) = (191, 307) in scaled image.
INFO:find_pattern_rotated:Trying rotations: [-50. -49. -48. -47. -46. -45. -44. -43. -42. -41. -40. -39. -38. -37.
 -36. -35. -34. -33. -32. -31. -30. -29. -28. -27. -26. -25. -24. -23.
 -22. -21. -20. -19. -18. -17. -16. -15. -14. -13. -12. -11. -10.  -9.
  -8.  -7.  -6.  -5.  -4.  -3.  -2.  -1.   0.   1.   2.   3.   4.   5.
   6.   7.   8.   9.  10.  11.  12.  13.  14.  15.  16.  17.  18.  19.
  20.  21.  22.  23.  24.  25.  26.  27.  28.  29.  30.  31.  32.  33.
  34.  35.  36.  37.  38.  39.  40.  41.  42.  43.  44.  45.  46.  47.
  48.  49.  50.].
DEBUG:PatternFinder:Execution PatternFinder.find took 0.000246 ms
DEBUG:PatternFinder:Execution PatternFinder.find took 0.000248 ms
DEBUG:PatternFinder:Execution PatternFinder.find took 0.000252 ms
DEBUG:PatternFinder:Execution PatternFinder.find took 0.000252 ms
DEBUG:PatternFinder:Execution PatternFinder.find took 0.000255 ms
DEBUG:PatternFinder:Execution PatternFinder.find took 0.000258 ms
DEBUG:PatternFinder:Execution PatternFinder.find took 0.000258 ms
DEBUG:PatternFinder:Execution PatternFinder.find took 0.000257 ms
DEBUG:PatternFinder:Execution PatternFinder.find took 0.000259 ms
DEBUG:PatternFinder:Execution PatternFinder.find took 0.000259 ms
DEBUG:PatternFinder:Execution PatternFinder.find took 0.000268 ms
DEBUG:PatternFinder:Execution PatternFinder.find took 0.000274 ms
DEBUG:PatternFinder:Execution PatternFinder.find took 0.000268 ms
DEBUG:PatternFinder:Execution PatternFinder.find took 0.000268 ms
/Users/sam/HoerSys/HS01_Company/03_Projects/stackalign/pattern_finder_gpu/pattern_finder_gpu/brute_force_convolve.py:264: PatternAtROIBorderWarning: PatternFinder: Minimal value at border of ROI! This hints at a too small ROI. Actual minimum might be outside of the ROI.
  PatternAtROIBorderWarning)
DEBUG:PatternFinder:Execution PatternFinder.find took 0.000281 ms
DEBUG:PatternFinder:Execution PatternFinder.find took 0.000274 ms
DEBUG:PatternFinder:Execution PatternFinder.find took 0.000275 ms
DEBUG:PatternFinder:Execution PatternFinder.find took 0.000277 ms
DEBUG:PatternFinder:Execution PatternFinder.find took 0.000279 ms
DEBUG:PatternFinder:Execution PatternFinder.find took 0.000281 ms
DEBUG:PatternFinder:Execution PatternFinder.find took 0.000283 ms
DEBUG:PatternFinder:Execution PatternFinder.find took 0.000287 ms
DEBUG:PatternFinder:Execution PatternFinder.find took 0.000287 ms
DEBUG:PatternFinder:Execution PatternFinder.find took 0.000288 ms
DEBUG:PatternFinder:Execution PatternFinder.find took 0.000291 ms
DEBUG:PatternFinder:Execution PatternFinder.find took 0.000293 ms
DEBUG:PatternFinder:Execution PatternFinder.find took 0.000297 ms
DEBUG:PatternFinder:Execution PatternFinder.find took 0.000297 ms
DEBUG:PatternFinder:Execution PatternFinder.find took 0.000301 ms
DEBUG:PatternFinder:Execution PatternFinder.find took 0.000301 ms
DEBUG:PatternFinder:Execution PatternFinder.find took 0.000303 ms
DEBUG:PatternFinder:Execution PatternFinder.find took 0.000306 ms
DEBUG:PatternFinder:Execution PatternFinder.find took 0.000310 ms
DEBUG:PatternFinder:Execution PatternFinder.find took 0.000312 ms
DEBUG:PatternFinder:Execution PatternFinder.find took 0.000314 ms
DEBUG:PatternFinder:Execution PatternFinder.find took 0.000317 ms
DEBUG:PatternFinder:Execution PatternFinder.find took 0.000318 ms
DEBUG:PatternFinder:Execution PatternFinder.find took 0.000322 ms
DEBUG:PatternFinder:Execution PatternFinder.find took 0.000324 ms
DEBUG:PatternFinder:Execution PatternFinder.find took 0.000325 ms
DEBUG:PatternFinder:Execution PatternFinder.find took 0.000328 ms
DEBUG:PatternFinder:Execution PatternFinder.find took 0.000333 ms
DEBUG:PatternFinder:Execution PatternFinder.find took 0.000334 ms
DEBUG:PatternFinder:Execution PatternFinder.find took 0.000341 ms
DEBUG:PatternFinder:Execution PatternFinder.find took 0.000341 ms
DEBUG:PatternFinder:Execution PatternFinder.find took 0.000345 ms
DEBUG:PatternFinder:Execution PatternFinder.find took 0.000345 ms
DEBUG:PatternFinder:Execution PatternFinder.find took 0.000349 ms
DEBUG:PatternFinder:Execution PatternFinder.find took 0.000357 ms
DEBUG:PatternFinder:Execution PatternFinder.find took 0.000358 ms
DEBUG:PatternFinder:Execution PatternFinder.find took 0.000358 ms
DEBUG:PatternFinder:Execution PatternFinder.find took 0.000353 ms
DEBUG:PatternFinder:Execution PatternFinder.find took 0.000357 ms
DEBUG:PatternFinder:Execution PatternFinder.find took 0.000351 ms
DEBUG:PatternFinder:Execution PatternFinder.find took 0.000346 ms
DEBUG:PatternFinder:Execution PatternFinder.find took 0.000343 ms
DEBUG:PatternFinder:Execution PatternFinder.find took 0.000348 ms
DEBUG:PatternFinder:Execution PatternFinder.find took 0.000337 ms
DEBUG:PatternFinder:Execution PatternFinder.find took 0.000334 ms
DEBUG:PatternFinder:Execution PatternFinder.find took 0.000332 ms
DEBUG:PatternFinder:Execution PatternFinder.find took 0.000330 ms
DEBUG:PatternFinder:Execution PatternFinder.find took 0.000326 ms
DEBUG:PatternFinder:Execution PatternFinder.find took 0.000324 ms
DEBUG:PatternFinder:Execution PatternFinder.find took 0.000321 ms
DEBUG:PatternFinder:Execution PatternFinder.find took 0.000324 ms
DEBUG:PatternFinder:Execution PatternFinder.find took 0.000322 ms
DEBUG:PatternFinder:Execution PatternFinder.find took 0.000314 ms
DEBUG:PatternFinder:Execution PatternFinder.find took 0.000312 ms
DEBUG:PatternFinder:Execution PatternFinder.find took 0.000310 ms
DEBUG:PatternFinder:Execution PatternFinder.find took 0.000308 ms
DEBUG:PatternFinder:Execution PatternFinder.find took 0.000305 ms
DEBUG:PatternFinder:Execution PatternFinder.find took 0.000301 ms
DEBUG:PatternFinder:Execution PatternFinder.find took 0.000300 ms
DEBUG:PatternFinder:Execution PatternFinder.find took 0.000298 ms
DEBUG:PatternFinder:Execution PatternFinder.find took 0.000299 ms
DEBUG:PatternFinder:Execution PatternFinder.find took 0.000292 ms
DEBUG:PatternFinder:Execution PatternFinder.find took 0.000290 ms
DEBUG:PatternFinder:Execution PatternFinder.find took 0.000288 ms
DEBUG:PatternFinder:Execution PatternFinder.find took 0.000287 ms
DEBUG:PatternFinder:Execution PatternFinder.find took 0.000288 ms
DEBUG:PatternFinder:Execution PatternFinder.find took 0.000283 ms
DEBUG:PatternFinder:Execution PatternFinder.find took 0.000282 ms
DEBUG:PatternFinder:Execution PatternFinder.find took 0.000280 ms
DEBUG:PatternFinder:Execution PatternFinder.find took 0.000276 ms
DEBUG:PatternFinder:Execution PatternFinder.find took 0.000277 ms
DEBUG:PatternFinder:Execution PatternFinder.find took 0.000272 ms
DEBUG:PatternFinder:Execution PatternFinder.find took 0.000270 ms
DEBUG:PatternFinder:Execution PatternFinder.find took 0.000268 ms
DEBUG:PatternFinder:Execution PatternFinder.find took 0.000274 ms
DEBUG:PatternFinder:Execution PatternFinder.find took 0.000268 ms
DEBUG:PatternFinder:Execution PatternFinder.find took 0.000262 ms
DEBUG:PatternFinder:Execution PatternFinder.find took 0.000266 ms
DEBUG:PatternFinder:Execution PatternFinder.find took 0.000259 ms
DEBUG:PatternFinder:Execution PatternFinder.find took 0.000256 ms
DEBUG:PatternFinder:Execution PatternFinder.find took 0.000257 ms
DEBUG:PatternFinder:Execution PatternFinder.find took 0.000253 ms
DEBUG:PatternFinder:Execution PatternFinder.find took 0.000258 ms
DEBUG:PatternFinder:Execution PatternFinder.find took 0.000247 ms
DEBUG:PatternFinder:Execution PatternFinder.find took 0.000248 ms
DEBUG:PatternFinder:Execution PatternFinder.find took 0.000247 ms
DEBUG:PatternFinder:Execution PatternFinder.find took 0.000242 ms
INFO:find_pattern_rotated:took 30.394640922546387 seconds.
INFO:find_pattern_rotated:best_angle: 20.0 deg, best_coord (row,column): [179. 384.] in input image

In [42]:
center_roi_around((330, 220), (221,221))


Out[42]:
(220, 110, 441, 331)

In [43]:
sp.rad2deg(trans.rotation)


Out[43]:
-20.0

In [44]:
trans.translation


Out[44]:
array([272.92973076, 166.21738329])

In [45]:
result = transform.warp(test_image2_rotated_translated, trans, output_shape=test_target2.shape)
io.imshow(result)


Out[45]:
<matplotlib.image.AxesImage at 0x1255fc0f0>

In [46]:
io.imshow(test_target2)


Out[46]:
<matplotlib.image.AxesImage at 0x125668400>

In [47]:
io.imshow(sp.absolute(test_target2[...,:3]/255 - result))


Out[47]:
<matplotlib.image.AxesImage at 0x11d79ba58>

In [48]:


In [48]: