Extracting texture features from images

Texture is the spatial and visual quality of an image. In this recipe, we will take a look at Haralick texture features. These features are based on the co-occurrence matrix (11.5) defined as follows:

Extracting texture features from images

In equation 11.5, i and j are intensities, while p and q are positions. The Haralick features are 13 metrics derived from the co-occurrence matrix, some of them given in equation 11.6. For a more complete list, refer to http://murphylab.web.cmu.edu/publications/boland/boland_node26.html (retrieved December 2015).

We will calculate the Haralick features with the mahotas API and apply them to the handwritten digits dataset of scikit-learn.

Getting ready

Install mahotas as follows:

$ pip install mahotas

I tested the code with mahotas 1.4.0.

How to do it...

  1. The imports are as follows:
    import mahotas as mh
    import numpy as np
    from sklearn.datasets import load_digits
    import matplotlib.pyplot as plt
    from tpot import TPOT
    from sklearn.cross_validation import train_test_split
    import dautil as dl
  2. Load the scikit-learn digits data as follows:
    digits = load_digits()
    X = digits.data.copy()
  3. Create Haralick features and add them:
    for i, img in enumerate(digits.images):
        np.append(X[i], mh.features.haralick(
            img.astype(np.uint8)).ravel())
  4. Fit and score a model with TPOT (or my fork, as discussed in Chapter 9, Ensemble Learning and Dimensionality Reduction):
    X_train, X_test, y_train, y_test = train_test_split(
        X, digits.target, train_size=0.75)
    
    tpot = TPOT(generations=6, population_size=101,
                random_state=46, verbosity=2)
    tpot.fit(X_train, y_train)
    
    print('Score {:.2f}'.format(tpot.score(X_train, y_train, X_test, y_test)))
  5. Plot the first original image as follows:
    dl.plotting.img_show(plt.gca(), digits.images[0])
    plt.title('Original Image')
  6. Plot the core features for that image:
    plt.figure()
    dl.plotting.img_show(plt.gca(), digits.data[0].reshape((8, 8)))
    plt.title('Core Features')
  7. Plot the Haralick features for that image too:
    plt.figure()
    dl.plotting.img_show(plt.gca(), mh.features.haralick(
        digits.images[0].astype(np.uint8)))
    plt.title('Haralick Features')

Refer to the following screenshot for the end result:

How to do it...

The code is in the extracting_texture.ipynb file in this book's code bundle.

See also

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset