Image Processing: Classification

Aus HSHL Mechatronik
Version vom 4. Februar 2026, 21:26 Uhr von Ajay.paul@stud.hshl.de (Diskussion | Beiträge) (Die Seite wurde neu angelegt: „= Classification: Geometric Feature Extraction = '''Image Classification''' in the context of classic image processing involves categorizing objects into classes based on measurable geometric properties (descriptors). Unlike Deep Learning, which learns features automatically, this method relies on '''Feature Engineering'''—mathematically defining properties like Area, Perimeter, or Orientation and using logical thresholds to sort objects. This article…“)
(Unterschied) ← Nächstältere Version | Aktuelle Version (Unterschied) | Nächstjüngere Version → (Unterschied)
Zur Navigation springen Zur Suche springen

Classification: Geometric Feature Extraction

Image Classification in the context of classic image processing involves categorizing objects into classes based on measurable geometric properties (descriptors). Unlike Deep Learning, which learns features automatically, this method relies on Feature Engineering—mathematically defining properties like Area, Perimeter, or Orientation and using logical thresholds to sort objects.

This article demonstrates how to classify objects using the MATLAB function regionprops.

Theoretical Background

The Workflow

  1. Segmentation: The image is converted to a binary mask where objects are white (1) and the background is black (0).
  2. Connected Component Analysis (CCA): The algorithm scans the binary mask to identify distinct, disconnected "blobs" or objects.
  3. Feature Extraction: For every identified object, specific mathematical properties are calculated.
  4. Decision Rule: A logical classifier (e.g., an If-Else statement or a Decision Tree) assigns a label to the object based on its feature values.

Geometric Descriptors

The primary tool for this in MATLAB is regionprops. Common descriptors include:

  • Area: The scalar count of pixels in the region. (Useful for classifying by size).
  • Orientation: The angle (in degrees ranging from -90 to 90) between the x-axis and the major axis of the ellipse that fits the region. (Useful for alignment detection).
  • Eccentricity: A value between 0 and 1 indicating how elongated the shape is (0 is a circle, 1 is a line).
  • Solidity: The proportion of the pixels in the convex hull that are also in the region.

MATLAB Implementation

The following script performs two classification tasks:

  1. Classifying Coins by Size (Large vs. Small).
  2. Classifying Rice Grains by Orientation (Horizontal vs. Vertical).

Code Example

%% Task 6: Geometric Classification
clc; clear; close all;

%% --- CASE 1: Classification by Size (Area) ---

% 1. Load and Preprocess
I_coins = imread('coins.png');
% Otsu's thresholding for binarization
level = graythresh(I_coins);
BW_coins = imbinarize(I_coins, level);
% Fill holes so the Area calculation represents the solid coin
BW_coins = imfill(BW_coins, 'holes');

% 2. Feature Extraction
% Measure Area, Centroid (for labeling), and BoundingBox (for drawing)
stats_coins = regionprops(BW_coins, 'Area', 'Centroid', 'BoundingBox');

% 3. Classification Logic and Visualization
figure('Name', 'Task 6: Classification by Size', ...
       'NumberTitle', 'off', 'Position', [100, 500, 600, 500]);
imshow(I_coins); 
title('Classifying Coins: Red=Large, Green=Small');
hold on;

% Threshold determined by observing the data
area_threshold = 2000; 

for k = 1:length(stats_coins)
    % Get features for the k-th object
    current_area = stats_coins(k).Area;
    bbox = stats_coins(k).BoundingBox;
    centroid = stats_coins(k).Centroid;
    
    % --- THE CLASSIFIER ---
    if current_area > area_threshold
        % Class 1: Large
        color = 'r';
        label = 'L';
    else
        % Class 2: Small
        color = 'g';
        label = 'S';
    end
    
    % Draw bounding box
    rectangle('Position', bbox, 'EdgeColor', color, 'LineWidth', 2);
    % Place label
    text(centroid(1), centroid(2), label, 'Color', 'k', ...
         'FontSize', 12, 'FontWeight', 'bold', 'BackgroundColor', 'w');
end

%% --- CASE 2: Classification by Orientation ---

% 1. Load and Preprocess (Rice requires background subtraction)
I_rice = imread('rice.png');
background = imopen(I_rice, strel('disk', 15));
I_rice_adj = I_rice - background;
BW_rice = imbinarize(I_rice_adj);

% 2. Feature Extraction
% Measure Orientation and MajorAxisLength (to draw lines)
stats_rice = regionprops(BW_rice, 'Orientation', 'Centroid', 'MajorAxisLength');

% 3. Visualization
figure('Name', 'Task 6: Classification by Orientation', ...
       'NumberTitle', 'off', 'Position', [750, 500, 600, 500]);
imshow(I_rice); 
title('Rice Orientation: Cyan=Horizontal, Magenta=Vertical');
hold on;

for k = 1:length(stats_rice)
    angle = stats_rice(k).Orientation;
    centroid = stats_rice(k).Centroid;
    
    % --- THE CLASSIFIER ---
    % MATLAB orientation is between -90 and 90.
    % < 45 degrees is horizontal-ish. > 45 degrees is vertical-ish.
    if abs(angle) < 45
        color = 'c'; % Cyan for Horizontal
    else
        color = 'm'; % Magenta for Vertical
    end
    
    % Draw orientation lines (Geometric calculation)
    len = stats_rice(k).MajorAxisLength;
    theta = deg2rad(-angle);
    x1 = centroid(1) - (len/2) * cos(theta);
    y1 = centroid(2) - (len/2) * sin(theta);
    x2 = centroid(1) + (len/2) * cos(theta);
    y2 = centroid(2) + (len/2) * sin(theta);
    
    plot([x1 x2], [y1 y2], 'Color', color, 'LineWidth', 2);
end
hold off;

Detailed Code Logic

1. Preprocessing

Before features can be extracted, the image must be binary.

  • For the coins, simple Otsu thresholding (`graythresh`) works well because the background is uniform.
  • For the rice, Top-hat filtering (Background subtraction using `imopen`) is used first because the lighting is uneven.

2. regionprops

This is the workhorse function.

stats = regionprops(BW, 'Property1', 'Property2', ...);

It returns a structure array where `stats(1)` refers to the first object found, `stats(2)` the second, and so on.

3. The Decision Boundary

In classical classification, the engineer must manually determine the cutoff point.

  • Coins: By inspecting the Area values, we see nickels have an area 2500 pixels and dimes have an area 1800 pixels. A threshold of 2000 cleanly separates the classes.
  • Rice: Orientation ranges from -90 to 90. The decision boundary is naturally 45 degrees.

Common Issues

  1. Under-Segmentation: If two objects touch, `regionprops` sees them as one single object.
    • Impact: The Area will be double the expected size, causing a misclassification (e.g., two small coins classified as one large coin).
    • Fix: Use `imopen` with a disk structuring element or Watershed segmentation to break connections before feature extraction.
  2. Scale Sensitivity: Features like Area and MajorAxisLength depend on the image resolution. If the camera zooms in, the "Area" changes.
    • Fix: Use scale-invariant dimensionless features like Eccentricity or Circularity (4πA/P2) if the camera distance varies.