Example #1
0
    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)
    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)
Example #3
0
#!/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')
Example #4
0
#!/usr/bin/env python
import matplotlib.pyplot as plt
from geotiff.io import IO

# load original image
input_file = '/'.join(('C:', 'Data', 'Tewkesbury-LiDAR', 'subset.data',
                       'Sigma0_HH_slv1_25Jul2007.img'))
img = IO.read(input_file)

# histogram of the image
plt.hist(img.flatten(), bins=10, range=[0, 10])
plt.show()
#!/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')
Example #6
0
#!/usr/bin/env python
import numpy as np
from sklearn import mixture
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')

# 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')
Example #7
0
#!/usr/bin/env python
import numpy as np
from sklearn.cluster import estimate_bandwidth, MeanShift
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)
#!/usr/bin/env python
"""Split-based thresholding of a GeoTIFF image."""
from skimage.filters import threshold_otsu
from geotiff.io import IO


# constants
SPLIT_SIZE = 100
PATH = 'C:/Data'
IN_PATH = PATH + '/namibia-flipped.data/Sigma0_HH.img'
OUT_PATH = PATH + '/namibia-splits/serbia-split-(%s,%s).tiff'

# load original image
img = IO.read(IN_PATH)
img_xsize = img.shape[1]
img_ysize = img.shape[0]

# extract splits iteratively & their thresholds
local_thresholds = []
y_offset = 0
while y_offset < img_ysize:
    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
#!/usr/bin/env python
import matplotlib.pyplot as plt
from geotiff.io import IO


# load original image
input_file = '/'.join(('C:', 'Data', 'Tewkesbury-LiDAR', 'subset.data',
                       'Sigma0_HH_slv1_25Jul2007.img'))
img = IO.read(input_file)

# histogram of the image
plt.hist(img.flatten(), bins=10, range=[0, 10])
plt.show()
#!/usr/bin/env python
import numpy as np
from sklearn import mixture
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')

# 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]
        if sqterm < 0:
            print "MinError(I): not converging. (1)"
            return t

        # The updated threshold is the integer part of the solution of the
        # quadratic equation
        t_prev = t
        temp = (w1 + np.sqrt(sqterm)) / w0

        if isnan(temp):
            print "MinError(I): not converging. (2)"
            t = t_prev
        else:
            t = int(floor(temp))

    return t


# open image, convert to dB, and shift to positive values
DIR = 'C:/Data/Tewkesbury-LiDAR'
img = IO.read(DIR + '/subset.data/Sigma0_HH_slv1_25Jul2007.img')
img_db = 10 * np.log10(img)
img_db_int = np.round(img_db).astype(int)
img_db_int_pos = img_db_int + abs(np.min(img_db_int))

# calculate threshold
n = np.max(img_db_int_pos)  # 255
threshold = min_error_thresholding(img_db_int_pos.ravel(), n)
print 'threshold:', threshold
Example #12
0
#!/usr/bin/env python
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
from skimage.filters import threshold_otsu
from geotiff.io import IO


# load original image
img = IO.read('C:/Data/namibia-flipped.data/Sigma0_HH.img')

# otsu thresholding of the original image
threshold = threshold_otsu(img)
img_thresholded = img > threshold
IO.write(img_thresholded, 'C:/Data/namibia-flipped-thresholded.tif')
print 'Threshold for original image:', threshold
#!/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')