def process(path_in): """Open/Process/Write the image in the given path. Args: path_in(str) """ arr_in = IO.read(path_in) arr_out = arr_in path_out = path_in IO.write(arr_out, path_out) logging.info('%s processed [Ok]' % path_in)
#!/usr/bin/env python import numpy as np import skfuzzy as fuzz from geotiff.io import IO # load original image DIR = 'C:/Data/Tewkesbury-LiDAR' img = IO.read(DIR + '/stack-lidar.data/Sigma0_HH_slv1_25Jul2007.img') # pixel intensities I = img.reshape((1, -1)) # params n_centers = 2 fuzziness_degree = 2 error = 0.005 maxiter = 1000 # fuzz c-means clustering centers, u, u0, d, jm, n_iters, fpc = fuzz.cluster.cmeans(I, c=n_centers, m=fuzziness_degree, error=error, maxiter=maxiter, init=None) img_clustered = np.argmax(u, axis=0).astype(float) # display clusters img_clustered.shape = img.shape IO.write(img_clustered, DIR + '/fuzzy-clustered.tif')
#!/usr/bin/env python import numpy as np from skimage.filters import threshold_otsu from geotiff.io import IO # load two images DIR = 'C:/Data/Tewkesbury-LiDAR' img_pre = IO.read(DIR + '/subset.data/Sigma0_HH_slv2_22Jul2008.img') img_post = IO.read(DIR + '/subset.data/Sigma0_HH_slv1_25Jul2007.img') # otsu thresholding of the two images threshold_pre = threshold_otsu(img_pre) img_thresholded_pre = img_pre < threshold_pre print 'Threshold for image pre:', threshold_pre IO.write(img_thresholded_pre, DIR + '/thresholded-pre.tif') threshold_post = threshold_otsu(img_post) img_thresholded_post = img_post < threshold_post print 'Threshold for image post:', threshold_post IO.write(img_thresholded_post, DIR + '/thresholded-post.tif') # difference between the two thresholded images img_difference = np.subtract(img_thresholded_post, img_thresholded_pre, dtype=np.float) IO.write(img_difference, DIR + '/difference.tif')
DIR = 'C:/Data/Tewkesbury-LiDAR' img = IO.read(DIR + '/stack-lidar.data/Sigma0_HH_slv1_25Jul2007.img') # gaussian mixture model # fit gaussian mixture model to the pixel intensities observation X = img.reshape((-1, 1)) g = mixture.GMM(n_components=2) g.fit(X) print '# of Observations:', X.shape print 'Gaussian Weights:', g.weights_ print 'Gaussian Means:', g.means_ # predict classes of pixel intensities from normal gmm img_clustered1 = g.predict(X) img_clustered1.shape = img.shape img_clustered1 = img_clustered1.astype(np.float) IO.write(img_clustered1, DIR + '/gmm.tif') # predict classes of pixel intensities by thresholding the gmm probabilities img_clustered2 = g.predict_proba(X) img_clustered2 = np.array(map(lambda x: False if x[1] > 0.1 else True, img_clustered2)) img_clustered2.shape = img.shape IO.write(img_clustered2, DIR + '/gmm-empirical.tif') # thresholding using average of estimated gaussian means threshold = np.average(g.means_) print 'Gaussian Means Average:', threshold img_thresholded = img > threshold IO.write(img_thresholded, DIR + '/gmm-thresholding.tif')
from skimage.filters import threshold_otsu from geotiff.io import IO # initialize driver DIR = 'C:/Data/Tewkesbury-LiDAR' img = IO.read(DIR + '/stack-lidar.data/Sigma0_HH_slv1_25Jul2007.img') # mean shift clustering of the image # cluster pixel intensities using k-means X = img.reshape((-1, 1)) bandwidth = estimate_bandwidth(X, quantile=.2, n_samples=500) mean_shift = MeanShift(bandwidth, bin_seeding=True) mean_shift.fit(X) # extract means of each cluster & clustered intensities population clusters_means = mean_shift.cluster_centers_.squeeze() X_clustered = mean_shift.labels_ print '# of Observations:', X.shape print 'Means:', clusters_means print 'Classified:', X_clustered # get clustered image from clustered intensities img_clustered = np.choose(X_clustered, clusters_means) img_clustered.shape = img.shape IO.write(img_clustered.astype(np.uint8), DIR + '/mean-shift.tif') # otsu thresholding of the binary image obtained threshold = threshold_otsu(img_clustered) img_thresholded = img_clustered > threshold IO.write(img_thresholded, DIR + '/mean-shift-thresholded.tif')
x_offset = 0 while x_offset < img_xsize: # estimate the otsu threshold for each split split_xsize = ( SPLIT_SIZE if x_offset + SPLIT_SIZE < img_xsize else img_xsize - x_offset ) split_ysize = ( SPLIT_SIZE if y_offset + SPLIT_SIZE < img_ysize else img_ysize - y_offset ) split = img[x_offset:x_offset + split_xsize, y_offset:y_offset + split_ysize] local_thresholds.append(threshold_otsu(split)) x_offset += SPLIT_SIZE y_offset += SPLIT_SIZE # average of local otsu thresholds local_threshold = sum(local_thresholds) / len(local_thresholds) print 'Local threshold:', local_threshold # global otsu threshold global_threshold = threshold_otsu(img) print 'Global threshold:', global_threshold # otsu thresholding using the average of splits thresholds img_thresholded = img > local_threshold IO.write(img_thresholded, 'C:/Data/namibia-flipped-split-thresholded.tif')
# gaussian mixture model # fit gaussian mixture model to the pixel intensities observation X = img.reshape((-1, 1)) g = mixture.GMM(n_components=2) g.fit(X) print '# of Observations:', X.shape print 'Gaussian Weights:', g.weights_ print 'Gaussian Means:', g.means_ # predict classes of pixel intensities from normal gmm img_clustered = g.predict(X) img_clustered.shape = img.shape img_clustered = img_clustered.astype(np.float) # save clustered image IO.write(img_clustered, DIR + '/gmm.tif') # image size img_gmm = np.empty_like(img_clustered) img_gmm[:] = img_clustered l = img_clustered.shape[1] h = img_clustered.shape[0] # consider neighbourhood during clustering step = 1 for i in xrange(1, h - 1): for j in xrange(1, l - 1): # counts of #elems/cluster in the neighbourhood c, f = np.unique(img_clustered[i - step:i + step + 1, j - step:j + step + 1], return_counts=True)
import numpy as np from skimage.filters import threshold_otsu from sklearn import cluster from geotiff.io import IO # load original image DIR = 'C:/Data/Tewkesbury-LiDAR' img = IO.read(DIR + '/stack-lidar.data/Sigma0_HH_slv1_25Jul2007.img') # k-means clustering of the image (population of pixels intensities) # cluster pixel intensities using k-means X = img.reshape((-1, 1)) k_means = cluster.KMeans(n_clusters=2) k_means.fit(X) # extract means of each cluster & clustered intensities population clusters_means = k_means.cluster_centers_.squeeze() X_clustered = k_means.labels_ print '# of Observations:', X.shape print 'Clusters Means:', clusters_means # get clustered image from clustered intensities img_clustered = np.choose(X_clustered, (0, 1)) img_clustered.shape = img.shape IO.write(img_clustered, DIR + '/kmeans.tif') # otsu thresholding of the binary image obtained threshold = threshold_otsu(img_clustered) img_thresholded = img_clustered > threshold IO.write(img_thresholded, DIR + '/kmeans-thresholding.tif')
#!/usr/bin/env python import numpy as np from skimage.feature import local_binary_pattern from sklearn import cluster from geotiff.io import IO # read image DIR = 'C:/Data/Tewkesbury-LiDAR' img = IO.read(DIR + '/stack-lidar.data/Sigma0_HH_slv1_25Jul2007.img') # extract texture using local binary pattern img_texture = local_binary_pattern(img, P=16, R=16, method='ror') IO.write(img_texture, DIR + '/texture.tif') # cluster texture using k-means X = img_texture.reshape((-1, 1)) k_means = cluster.KMeans(n_clusters=2) k_means.fit(X) # clustered intensities population X_clustered = k_means.labels_ # get clustered image from clustered intensities img_clustered = np.choose(X_clustered, [255, 0]) img_clustered.shape = img.shape IO.write(np.invert(img_clustered), DIR + '/texture-clustered.tif')