Skip to content

Instantly share code, notes, and snippets.

@oostendo
Created August 22, 2012 19:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save oostendo/3428425 to your computer and use it in GitHub Desktop.
Save oostendo/3428425 to your computer and use it in GitHub Desktop.
# -*- coding: utf-8 -*-
# <nbformat>3.0</nbformat>
# <codecell>
from random import random
from SimpleCV import Image, Color
from scipy import stats
# <codecell>
### This is the Gaussian data smoothing function I wrote ###
def smooth(list,degree=5):
window=degree*2-1
weight=numpy.array([1.0]*window)
weightGauss=[]
for i in range(window):
i=i-degree+1
frac=i/float(window)
gauss=1/(numpy.exp((4*(frac))**2))
weightGauss.append(gauss)
weight=numpy.array(weightGauss)*weight
smoothed=[0.0]*(len(list)-window)
for i in range(len(smoothed)):
smoothed[i]=sum(numpy.array(list[i:i+window])*weight)/sum(weight)
return np.array(smoothed)
# <codecell>
def _getValues(sample):
locations = []
# fake the first derivative on a binary image
d = np.where(sample[1:]-sample[:-1] < 10)[0]
if( len(d) > 0 ):
# count the distance between spaces
locations = d[1:]-d[:-1]
return locations
# <codecell>
id = 6
f = Frame.objects.order_by("-capturetime")[id]
img = f.image
self = f.features[0].feature
top = self.top[0][1]
bottom = self.lbs_line[0][1]
shaft_height = self.bottom[0][1]-bottom
head_height = bottom-top
#top = top + 0.2*head_height # move down the top this will fail with the tall bolts
#bottom = bottom #+ 0.05*shaft_height
head_left=self.lbs_left[0][0]
head_width=np.abs(self.lbs_line[0][0]-self.lbs_line[1][0])
b = img.findBlobsFromMask(img.threshold(20).dilate(3))
dest = Image((img.width,img.height))
dest = dest.blit(b[-1].blobImage(),mask=b[-1].blobMask(),pos=b[-1].topLeftCorner())
dest = dest.applyLayers()
# move ever so slightly down the shank
span = ((bottom-top)/2.0)
spanw = span*.9 # try to avoid the fillets
temp = dest.crop(head_left,top+span,head_width,spanw)
grain_region = temp
lines = []
meanlines = []
alllines = []
clip = 5
render = True
#oimg = grain_region.equalize().binarize(blocksize=11)
oimg = grain_region.equalize()#.binarize(blocksize=11)
temp = oimg.getGrayNumpy() # get the image -- only works on binary images
for i in range(0,oimg.height):
result = _getValues(temp[:,i])
if( len(result) > clip*2 ):
result = result[clip:-1*clip]
#lines.append(np.std(result))
lines.append(np.mean(result))
alllines.append( result ) # for each line get the length of the horizontal lines
degree = 4
window = 40
smlines = smooth(lines, degree)
curve = smooth(lines, window)
#plot(lines[degree:-degree])
oimg.dl().line((0,start), (oimg.width, start), color = Color.GREEN)
oimg.dl().line((0,end), (oimg.width, end), color = Color.RED)
oimg.save(display)
minavg = stats.scoreatpercentile(curve[:len(curve)/2], 10)
maxavg = stats.scoreatpercentile(smlines, 95)
end = np.where(smlines > maxavg)[0][0]
start = np.where(smlines[:end] < minavg)[0][-1]
while smlines[start] > smlines[start-1]:
start -= 1
start += degree
while smlines[end] < smlines[end+1]:
end += 1
end = end + degree
p = plot(smlines)
axvline(x = start - degree)
axvline(x = end - degree)
start, end
# <codecell>
plot(curve)
# <codecell>
p
# <codecell>
# <codecell>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment