def segment_ch4(self, segment_fn, segment_transform):
segs = np.zeros_like(self.ch4_images, dtype=np.float32)
ims = np.copy(self.ch4_images).reshape(-1, 1, self.ch4_images.shape[1],
self.ch4_images.shape[2])
ims = segment_transform(ims)
for i in xrange(self.ch4_images.shape[0]):
segs[i:i+1] = segment_fn(ims[i:i+1])
_,sb = cv2.threshold(np.copy(segs[i])*255, 127, 255, cv2.THRESH_BINARY)
patches = get_patches(sb)
sb = np.zeros_like(sb, dtype=np.uint8)
if len(patches) > 0:
patch = next(p for p in patches if p.shape[0] == max(p1.shape[0]
for p1 in patches))
for x,y in patch:
sb[x,y]=255
pca = decomposition.PCA(n_components=2)
pca.fit(patch)
mean, major = pca.mean_, pca.components_[0]
middle = sb.shape[0]/2
sb = cv2.warpAffine(sb, np.float32([[1,0,middle-mean[1]],
[0,1,middle-mean[0]]]), sb.shape)
sb = scipy.misc.imrotate(sb, np.arctan2(*major)*180/np.pi)
segs[i:i+1]=sb
self.ch4seg = segs
self.ch4counts = np.array([np.count_nonzero(s) for s in self.ch4seg]).reshape(1,-1)
评论列表
文章目录