Segmenting images with spectral clustering

Spectral clustering is a clustering technique that can be used to segment images. The scikit-learn spectral_clustering() function implements the normalized graph cuts spectral clustering algorithm. This algorithm represents an image as a graph of units. "Graph" here is the same mathematical concept as in Chapter 8, Text Mining and Social Network Analysis. The algorithm tries to partition the image, while minimizing segment size and the ratio of intensity gradient along cuts.

How to do it...

  1. The imports are as follows:
    import numpy as np import matplotlib.pyplot as plt from sklearn.feature_extraction.image import img_to_graph from sklearn.cluster import spectral_clustering from sklearn.datasets import load_digits ...

Get Python Data Analysis Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.