figsize(12,8)
import tables
from collections import Counter
matplotlib.rc("image",cmap="gray")
matplotlib.rc("image",interpolation="nearest")
from scipy.ndimage import filters,morphology
from scipy.ndimage.filters import gaussian_filter as G
def crop_black(image):
gray = image
if gray.ndim==3: gray = sum(image,axis=2)
yr = find(sum(gray,axis=1)>0)
y0 = yr[0]
y1 = yr[-1]
xr = find(sum(gray,axis=0)>0)
x0 = xr[0]
x1 = xr[-1]
if image.ndim==3: return image[y0:y1,x0:x1,:]
else: return image[y0:y1,x0:x1]
hdf = tables.openFile("1k.h5")
images = hdf.root.icons
image = mean(crop_black(images[77]),2)
imshow(image)
--------------------------------------------------------------------------- IOError Traceback (most recent call last) <ipython-input-2-c2f00917c23a> in <module>() ----> 1 hdf = tables.openFile("1k.h5") 2 images = hdf.root.icons 3 image = mean(crop_black(images[77]),2) 4 imshow(image) /usr/lib/python2.7/dist-packages/tables/_past.pyc in oldfunc(*args, **kwargs) 33 def oldfunc(*args, **kwargs): 34 warn(warnmsg, DeprecationWarning, stacklevel=2) ---> 35 return obj(*args, **kwargs) 36 oldfunc.__doc__ = ( 37 obj.__doc__ or '') + "\n\n.. warning::\n\n " + warnmsg + "\n" /usr/lib/python2.7/dist-packages/tables/file.pyc in open_file(filename, mode, title, root_uep, filters, **kwargs) 316 317 # Finally, create the File instance, and return it --> 318 return File(filename, mode, title, root_uep, filters, **kwargs) 319 320 openFile = previous_api(open_file) /usr/lib/python2.7/dist-packages/tables/file.pyc in __init__(self, filename, mode, title, root_uep, filters, **kwargs) 789 790 # Now, it is time to initialize the File extension --> 791 self._g_new(filename, mode, **params) 792 793 # Check filters and set PyTables format version for new files. /usr/lib/python2.7/dist-packages/tables/hdf5extension.so in tables.hdf5extension.File._g_new (tables/hdf5extension.c:3812)() /usr/lib/python2.7/dist-packages/tables/utils.pyc in check_file_access(filename, mode) 155 # The file should be readable. 156 if not os.access(filename, os.F_OK): --> 157 raise IOError("``%s`` does not exist" % (filename,)) 158 if not os.path.isfile(filename): 159 raise IOError("``%s`` is not a regular file" % (filename,)) IOError: ``1k.h5`` does not exist
def harris(image,sigma=1.0,area=5.0):
dx = filters.gaussian_filter(image,sigma,(0,1))
dy = filters.gaussian_filter(image,sigma,(1,0))
sxx = filters.gaussian_filter(dx**2,area)
sxy = filters.gaussian_filter(dx*dy,area)
syy = filters.gaussian_filter(dy**2,area)
T = sxx+syy
D = sxx*syy-sxy**2
R = (T**2-4*D)**.5
hi = (T+R)/2
lo = (T-R)/2
return lo
subplot(121); imshow(image)
subplot(122); imshow(harris(image,2.0,6.0)**2)
smooth = filters.gaussian_filter(image,3.0)
subplot(121); imshow(smooth)
subplot(122); imshow(abs(smooth-filters.median_filter(smooth,10))**2)
I = filters.gaussian_filter(image,3.0)
s = 2.0
Ix = filters.gaussian_filter(image,s,(0,1))
Iy = filters.gaussian_filter(image,s,(1,0))
Ixx = filters.gaussian_filter(image,s,(0,2))
Ixy = filters.gaussian_filter(image,s,(1,1))
Iyy = filters.gaussian_filter(image,s,(2,0))
k = Ix**2*Iyy + Iy**2*Ixx - 2*Ix*Iy*Ixy
subplot(121); imshow(smooth)
subplot(122); imshow(abs(k))
--------------------------------------------------------------------------- NameError Traceback (most recent call last) <ipython-input-3-33d85c2061c0> in <module>() ----> 1 I = filters.gaussian_filter(image,3.0) 2 s = 2.0 3 Ix = filters.gaussian_filter(image,s,(0,1)) 4 Iy = filters.gaussian_filter(image,s,(1,0)) 5 Ixx = filters.gaussian_filter(image,s,(0,2)) NameError: name 'image' is not defined
I = filters.gaussian_filter(image,1.0)
s = 1.0
Ix = filters.gaussian_filter(image,s,(0,1))
Iy = filters.gaussian_filter(image,s,(1,0))
--------------------------------------------------------------------------- NameError Traceback (most recent call last) <ipython-input-4-e82b995efd37> in <module>() ----> 1 I = filters.gaussian_filter(image,1.0) 2 s = 1.0 3 Ix = filters.gaussian_filter(image,s,(0,1)) 4 Iy = filters.gaussian_filter(image,s,(1,0)) NameError: name 'image' is not defined
def between(x,a,b,r=0.03):
return 1.0/(1+exp((a-x)/r))/(1+exp((x-b)/r))
plot(between(linspace(0.0,1.0,100),0.2,0.4))
[<matplotlib.lines.Line2D at 0x7f3a45565b10>]
bins = []
for i,a in enumerate(linspace(0,pi,8)[:-1]):
p = abs(cos(a)*Ix+sin(a)*Iy)
p = between(p,0.6,1.0,0.05)
f = filters.maximum_filter(p,10)
f /= amax(f)
bins.append(f)
subplot(131); imshow(bins[0])
subplot(132); imshow(bins[2])
subplot(133); imshow(minimum(bins[0],bins[3]))
--------------------------------------------------------------------------- NameError Traceback (most recent call last) <ipython-input-6-e7a9bf748be4> in <module>() 1 bins = [] 2 for i,a in enumerate(linspace(0,pi,8)[:-1]): ----> 3 p = abs(cos(a)*Ix+sin(a)*Iy) 4 p = between(p,0.6,1.0,0.05) 5 f = filters.maximum_filter(p,10) NameError: name 'Ix' is not defined
ohist = transpose(bins,[1,2,0])
plot(ohist[5,80])
--------------------------------------------------------------------------- ValueError Traceback (most recent call last) <ipython-input-7-2b887e7c41b7> in <module>() ----> 1 ohist = transpose(bins,[1,2,0]) 2 plot(ohist[5,80]) /usr/lib/python2.7/dist-packages/numpy/core/fromnumeric.pyc in transpose(a, axes) 531 transpose = a.transpose 532 except AttributeError: --> 533 return _wrapit(a, 'transpose', axes) 534 return transpose(axes) 535 /usr/lib/python2.7/dist-packages/numpy/core/fromnumeric.pyc in _wrapit(obj, method, *args, **kwds) 41 except AttributeError: 42 wrap = None ---> 43 result = getattr(asarray(obj), method)(*args, **kwds) 44 if wrap: 45 if not isinstance(result, mu.ndarray): ValueError: axes don't match array
outside = zeros((8,8),'B')
inside = zeros((8,8),'B')
outside[:2,:] = 1; outside[:,:2] = 1
inside[-3:,-3:] = 1
subplot(121); imshow(outside)
subplot(122); imshow(inside)
<matplotlib.image.AxesImage at 0x7f3a454b02d0>
smooth = filters.median_filter(image,5)
imshow(smooth)
--------------------------------------------------------------------------- NameError Traceback (most recent call last) <ipython-input-9-d5bc24bcfc07> in <module>() ----> 1 smooth = filters.median_filter(image,5) 2 imshow(smooth) NameError: name 'image' is not defined
oimage = morphology.grey_opening(smooth,footprint=outside)
iimage = morphology.grey_closing(smooth,footprint=inside)
subplot(121); imshow(oimage)
subplot(122); imshow(iimage)
imshow(maximum(0,oimage-image)**2)