Implementation:Scikit learn Scikit learn ImageFeatureExtraction
| Knowledge Sources | |
|---|---|
| Domains | Computer Vision, Feature Extraction |
| Last Updated | 2026-02-08 15:00 GMT |
Overview
Concrete tool for extracting features from images including patch extraction and graph construction provided by scikit-learn.
Description
The image module provides utilities to extract features from images. It includes PatchExtractor for extracting patches from collections of images, functions for converting images to graph representations (img_to_graph and grid_to_graph), and utilities for extracting and reconstructing 2D patches (extract_patches_2d and reconstruct_from_patches_2d). These tools are useful for image-based machine learning tasks.
Usage
Use the image feature extraction module when working with image data in machine learning pipelines. PatchExtractor is useful for extracting local image patches for patch-based learning. The graph construction functions are useful for image segmentation tasks where pixel connectivity information is needed.
Code Reference
Source Location
- Repository: scikit-learn
- File: sklearn/feature_extraction/image.py
Signature
class PatchExtractor(TransformerMixin, BaseEstimator):
def __init__(self, *, patch_size=None, max_patches=None, random_state=None):
def extract_patches_2d(image, patch_size, *, max_patches=None, random_state=None):
def reconstruct_from_patches_2d(patches, image_size):
def img_to_graph(img, *, mask=None, return_as=sparse.coo_matrix, dtype=None):
def grid_to_graph(n_x, n_y, n_z=1, *, mask=None, return_as=sparse.coo_matrix, dtype=int):
Import
from sklearn.feature_extraction.image import PatchExtractor
from sklearn.feature_extraction.image import extract_patches_2d
from sklearn.feature_extraction.image import img_to_graph
from sklearn.feature_extraction.image import grid_to_graph
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| patch_size | tuple of int (patch_height, patch_width) | No | The dimensions of one patch. Default is None. |
| max_patches | int or float | No | Maximum number of patches per image to extract. If float, it is the fraction of total possible patches. |
| random_state | int or RandomState | No | Random state for reproducible patch extraction when max_patches is specified. |
| image | ndarray of shape (image_height, image_width) or (image_height, image_width, n_channels) | Yes | The original image to extract patches from (for extract_patches_2d). |
| img | ndarray of shape (height, width) or (height, width, channel) | Yes | Input image for graph construction (for img_to_graph). |
Outputs
| Name | Type | Description |
|---|---|---|
| patches | ndarray of shape (n_patches, patch_height, patch_width) | Array of extracted image patches. |
| graph | sparse matrix | The pixel graph representation of the image with edge weights based on gradient. |
Usage Examples
Basic Usage
from sklearn.feature_extraction.image import extract_patches_2d, reconstruct_from_patches_2d
import numpy as np
# Create a sample image
image = np.arange(16).reshape(4, 4).astype(float)
# Extract 2x2 patches
patches = extract_patches_2d(image, (2, 2), max_patches=4, random_state=0)
print(patches.shape)
# (4, 2, 2)
# Reconstruct from patches
from sklearn.feature_extraction.image import PatchExtractor
one_image = np.arange(16).reshape((1, 4, 4))
pe = PatchExtractor(patch_size=(2, 2))
patches = pe.fit_transform(one_image)
print(patches.shape)