def Get_feat_test(ind,h,w, M_mean,nb_features): img=np.empty([h,w,3]) GRAY_IM=np.empty([h,w]) img=M_mean[:,:,:][ind] gray_img = rgb2gray(img) gray_img = (gray_img * 255 ).astype( np.uint8 ) GRAY_IM[:,:]=gray_img-np.mean(gray_img) GRAY_IM FV=np.empty([nb_features]) matrix = PyImageMatrix() matrix.allocate(h, w) numpy_matrix = matrix.as_ndarray() numpy_matrix[:] = GRAY_IM[:,:] fv = FeatureVector( name='stufff', long=True, original_px_plane=matrix ) t1 = time.time() fv.GenerateFeatures(quiet=True, write_to_disk=False) t2 = time.time() FV[:]=fv.values Names=fv.feature_names FV FV=FV.astype(float) return(FV)
def get_image_matrix(img_array): if len(img_array.shape) != 2: raise ValueError("array must be two-dimensional") image_matrix = PyImageMatrix() image_matrix.allocate(img_array.shape[1], img_array.shape[0]) numpy_matrix = image_matrix.as_ndarray() numpy_matrix[:] = img_array return image_matrix
def calc_features(img_arr, plane_tag, long=False): assert len(img_arr.shape) == 2 pychrm_matrix = PyImageMatrix() pychrm_matrix.allocate(img_arr.shape[1], img_arr.shape[0]) numpy_matrix = pychrm_matrix.as_ndarray() numpy_matrix[:] = img_arr signatures = FeatureVector(basename=plane_tag, long=long) signatures.original_px_plane = pychrm_matrix signatures.GenerateFeatures(write_to_disk=False) return signatures
def Features_calcul_np_GrayscaleIm_WND(ind, nb_features, w, h, GRAY): " calculate WND Charm Features from grayscale images (2919 features)" FV = np.empty([nb_features]) matrix = PyImageMatrix() matrix.allocate(h, w) numpy_matrix = matrix.as_ndarray() numpy_matrix[:] = GRAY[:, :][ind] fv = FeatureVector(name='stufff', long=True, original_px_plane=matrix) t1 = time.time() fv.GenerateFeatures(quiet=True, write_to_disk=False) t2 = time.time() FV[:] = fv.values Names = fv.feature_names FV return (FV, Names)
def test_allocate( self ): """make an empty pixel plane""" tempdir = mkdtemp() test_path = join( tempdir, "TEST_allocate.tif" ) num_rows, num_cols = shape = (123,456) try: origim = PyImageMatrix() # virtual void allocate (unsigned int w, unsigned int h); origim.allocate( num_cols, num_rows ) origim.SaveTiff( test_path ) pixels = plt.imread( test_path ) self.assertEqual( pixels.shape, shape ) finally: rmtree( tempdir )
def test_allocate(self): """make an empty pixel plane""" tempdir = mkdtemp() test_path = join(tempdir, "TEST_allocate.tif") num_rows, num_cols = shape = (123, 456) try: origim = PyImageMatrix() # virtual void allocate (unsigned int w, unsigned int h); origim.allocate(num_cols, num_rows) origim.SaveTiff(test_path) pixels = plt.imread(test_path) self.assertEqual(pixels.shape, shape) finally: rmtree(tempdir)
def Get_proba(ind, h, w, M_mean, nb_features): img = np.empty([h, w, 3]) GRAY_IM = np.empty([h, w]) img = M_mean[:, :, :][ind] gray_img = rgb2gray(img) gray_img = (gray_img * 255).astype(np.uint8) GRAY_IM[:, :] = gray_img - np.mean(gray_img) GRAY_IM FV = np.empty([nb_features]) matrix = PyImageMatrix() matrix.allocate(h, w) numpy_matrix = matrix.as_ndarray() numpy_matrix[:] = GRAY_IM[:, :] fv = FeatureVector(name='stufff', long=True, original_px_plane=matrix) t1 = time.time() fv.GenerateFeatures(quiet=True, write_to_disk=False) t2 = time.time() FV[:] = fv.values Names = fv.feature_names FV FV = FV.astype(float) pca = decomposition.PCA() RFC = RandomForestClassifier() estimators = [('reduce_dim', pca), ('Random_Forest', RFC)] pipe = Pipeline(estimators) params = dict(reduce_dim__n_components=90, Random_Forest__n_estimators=200, Random_Forest__random_state=0) filename_Features_two_blocs = projectpath + 'io/Output/Features_two_blocs.npy' FV_N = np.load(filename_Features_two_blocs) X = FV_N Data_FRAMES = pd.read_pickle(projectpath + 'io/Output/Dataframe_.pkl') yr = Get_true_y(Data_FRAMES) filename_yr = projectpath + 'io/Output/yr.npy' np.save(filename_yr, yr) yr = np.load(filename_yr) RFC = RandomForestClassifier(n_estimators=200, random_state=0) predictedVAL = cross_val_predict(RFC, X, yr, n_jobs=-1) metrics.accuracy_score(yr, predictedVAL) Conf_Mat = confusion_matrix(yr, predictedVAL) RFC.fit(X, yr) predict_probab = np.ones([M_mean.shape[0], 3]) predict_proba = RFC.predict_proba(FV) predict_probab[ind, 0] = predict_proba[:, 0] predict_probab[ind, 1] = predict_proba[:, 1] predict_probab[ind, 2] = predict_proba[:, 2] return (predict_probab)
def extractFeatures(ftb, ds, newOnly, chNames, imageId = None, im = None): message = '' tc = ftb.tc # dataset must be explicitly provided because an image can be linked to # multiple datasets in which case im.getDataset() doesn't work if not im: if not imageId: #raise Exception('No input image') raise omero.ServerError('No input image') im = ftb.conn.getObject('Image', imageId) if not im: return 'Image id:%d not found\n' % imageId else: imageId = im.getId() tid = WndcharmStorage.getAttachedTableFile(ftb.tc, ds) if tid: if not ftb.openTable(tid): return message + '\nERROR: Table not opened\n' version = unwrap(ftb.versiontag.getTextValue()) # version seems to be in unicode message += 'Opened table id:%d version:%s\n' % (tid, str(version)) if newOnly and ftb.tableContainsId(imageId): return message + 'Image id:%d features already in table' % imageId # FIXME: default is convert multichannel to greyscale unless user input # Calculate features for an image channel # Optionally prepend the channel label to each feature name and combine ftall = None for c in xrange( len( chNames ) ): wndcharm_matrix = PyImageMatrix() wndcharm_matrix.allocate( im.getSizeX(), im.getSizeY() ) numpy_matrix = wndcharm_matrix.as_ndarray() numpy_matrix[:] = im.getPrimaryPixels().getPlane(theZ=0,theC=c,theT=0) feature_plan = wndcharm.StdFeatureComputationPlans.getFeatureSet(); options = "" # This is where you can tell wnd-charm to normalize pixel intensities, # take ROIs etc. ... leave blank for now. ft = Signatures.NewFromFeatureComputationPlan( wndcharm_matrix, feature_plan, options ) ft.names = [WndcharmStorage.insert_channel_name( n, chNames[c]) for n in ft.names] ft.source_path = im.getName() if not ftall: ftall = ft else: ftall.names += ft.names ftall.values += ft.values # Save the features to a table if not tid: ftb.createTable(ftall.names, ft.version) version = unwrap(ftb.versiontag.getTextValue()) message += 'Created new table id:%d version:%s\n' % ( ftb.tc.tableId, version) message += WndcharmStorage.addFileAnnotationTo(tc, ds) if version != ft.version: return message + 'Incompatible version: Stored=%s Calculated=%s' % ( version, ft.version) ftb.saveFeatures(imageId, ftall) return message + 'Extracted features from Image id:%d\n' % imageId