Skip to content

Instantly share code, notes, and snippets.

@nzjrs
Last active December 16, 2015 19:08
Show Gist options
  • Save nzjrs/5482449 to your computer and use it in GitHub Desktop.
Save nzjrs/5482449 to your computer and use it in GitHub Desktop.
Python Point Detect
import cv2
import numpy as np
import scipy.ndimage.measurements
cv2.startWindowThread()
for iname,t in (("img",45),("img2",230), ("img3",230), ("img4",250)):
i = cv2.imread("%s.png" % iname)
cv2.namedWindow(iname)
valid = i >= t
cv2.imshow(iname, i)
i[~valid] = 0
lbls,npts = scipy.ndimage.measurements.label(i)
slcs = scipy.ndimage.measurements.find_objects(lbls)
#according to the docs, passing None as the last argument should
#return all centre of mass. But this doesnt work. So assume the labels start
#at 1 and increase linearly
#http://projects.scipy.org/scipy/ticket/1839
#
#lets also assume that they are returned in the same order as the slices
for n in range(1,npts+1):
slc = slcs[n-1]
r,c,_ = scipy.ndimage.measurements.center_of_mass(i,lbls,n)
b = i[slc]
cv2.namedWindow("%spatch%d" % (iname,n))
cv2.imshow("%spatch%d" % (iname,n), b)
print n, " = ", b.sum()/np.count_nonzero(b),"@",r,",",c
i[r-20:r+20,c,2] = 255
i[r,c-20:c+20,2] = 255
cv2.imshow(iname, i)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment