def update(self,im): """Update cell position with respect to a given image raddii are adjusted accordingly to the previous size """ self.path = npy.zeros((self.niter,2)) for iter in range(self.niter): #compute the shifts self.shift_halo = meanshift(im,self.tri_halo,0.0,0.0,lut = self.LutW) self.shift_soma = meanshift(im,self.tri_soma,0.0,0.0,lut = self.LutB) #update the position # halo centroid halo = self.shift_halo[:,0:2] # nucleus centroid soma = self.shift_soma[:,0:2] halo_mean = halo.mean(axis=0) soma_mean = soma.mean(axis=0) self.center[:] = (1.-self.alpha) * soma_mean + self.alpha * halo_mean self.path[iter,:] = self.center #update previous radii MaxRadius = self.radius_halo MinRadius = self.radius_soma self.prev_radii = npy.maximum(npy.minimum(npy.sqrt(npy.sum((halo-self.center)**2,axis=1)),MaxRadius),MinRadius) #filter previous radii r1 = npy.roll(self.prev_radii,-1) r2 = npy.roll(self.prev_radii,+1) self.prev_radii = .5*(self.prev_radii + .5*r1 + .5*r2) #update the triangles self.update_triangles()
def segment(self): algorithm = self.algorithmComboBox.currentText() imgPath = str(self.userpath.text()) index = self.featureComboBox.currentIndex() features = ["INTENSITY","INTENSITY+LOC","RGB","YUV","LM", "ILM","PCA"] if algorithm == "K-means": k = int(self.kText.text()) iterations = int(self.iterationsText.text()) epsilon = float(self.epsilonText.text()) print imgPath,index,k,iterations,epsilon org = cv.LoadImageM(imgPath) im = kmeans.kmeans(imgPath,features[index],k,iterations,epsilon) cv.ShowImage("original",org) cv.ShowImage("segmented",im) elif algorithm == "Mean Shift": if index == 4: QtGui.QMessageBox.information(self, 'Error', 'LM is not supported in mean shift') return print imgPath,features[index] org = cv.LoadImageM(imgPath) im = meanshift.meanshift(imgPath,features[index]) cv.ShowImage("original",org) cv.ShowImage("segmented",im)
def segment(self): algorithm = self.algorithmComboBox.currentText() imgPath = str(self.userpath.text()) index = self.featureComboBox.currentIndex() features = [ "INTENSITY", "INTENSITY+LOC", "RGB", "YUV", "LM", "ILM", "PCA" ] if algorithm == "K-means": k = int(self.kText.text()) iterations = int(self.iterationsText.text()) epsilon = float(self.epsilonText.text()) print imgPath, index, k, iterations, epsilon org = cv.LoadImageM(imgPath) im = kmeans.kmeans(imgPath, features[index], k, iterations, epsilon) cv.ShowImage("original", org) cv.ShowImage("segmented", im) elif algorithm == "Mean Shift": if index == 4: QtGui.QMessageBox.information( self, 'Error', 'LM is not supported in mean shift') return print imgPath, features[index] org = cv.LoadImageM(imgPath) im = meanshift.meanshift(imgPath, features[index]) cv.ShowImage("original", org) cv.ShowImage("segmented", im)
def update(self,im): """Update cell position with respect to a given image """ self.path = npy.zeros((self.niter,2)) for iter in range(self.niter): #compute the shifts self.shift_halo = meanshift(im,self.tri_halo,0.0,0.0,lut = self.LutW) self.shift_soma = meanshift(im,self.tri_soma,0.0,0.0,lut = self.LutB) #update the position # halo centroid halo = npy.asarray([sh[0:2] for sh in self.shift_halo]) # nucleus centroid soma = npy.asarray([sh[0:2] for sh in self.shift_halo]) self.center[:] = (1.-self.alpha) * soma.mean(axis=0) + self.alpha * halo.mean(axis=0) self.path[iter,:] = self.center #update the triangles self.build_triangles()
from meanshift import meanshift import matplotlib.pyplot as plt from sklearn.preprocessing import StandardScaler scl = StandardScaler() mean_01 = np.array([0.0, 0.0]) cov_01 = np.array([[1, 0.3], [0.3, 1]]) dist_01 = np.random.multivariate_normal(mean_01, cov_01, 100) mean_02 = np.array([6.0, 7.0]) cov_02 = np.array([[1.5, 0.3], [0.3, 1]]) dist_02 = np.random.multivariate_normal(mean_02, cov_02, 100) mean_03 = np.array([7.0, -5.0]) cov_03 = np.array([[1.2, 0.5], [0.5, 1, 3]]) dist_03 = np.random.multivariate_normal(mean_03, cov_01, 100) mean_04 = np.array([2.0, -7.0]) cov_04 = np.array([[1.2, 0.5], [0.5, 1, 3]]) dist_04 = np.random.multivariate_normal(mean_04, cov_01, 100) data = np.vstack((dist_01, dist_02, dist_03, dist_04)) np.random.shuffle(data) data_normalized = scl.fit_transform(data) labels = meanshift(data_normalized) plt.scatter(data[:, 0], data[:, 1], c=labels) plt.show()