如何在每个超像素上提取Gabor纹理特征?(含Matlab代码)
Hey there! Let's walk through how to extract Gabor features for each superpixel and create independent variables for your clustered categories—building right off the work you've already completed.
First, a quick recap of your existing work
You've successfully run superpixel segmentation and clustered superpixels based on mean color. Here's your code and results organized clearly:
Superpixel Segmentation Code
A = imread('kobi.png'); [L,N] = superpixels(A,5); figure BW = boundarymask(L); figure;imshow(imoverlay(A,BW,'cyan'),'InitialMagnification',67)
Superpixel Segmentation Result:
Superpixel Clustering Code
idx=label2idx(L); meanColor = zeros(N,3); [m,n] = size(L); for i = 1:N meanColor(i,1) = mean(A(idx{i})); meanColor(i,2) = mean(A(idx{i}+m*n)); meanColor(i,3) = mean(A(idx{i}+2*m*n)); end numColors = 6; [pidx,cmap] = kmeans(meanColor,numColors,'replicates',2); cmap = lab2rgb(cmap); Lout = zeros(size(A,1),size(A,2)); for i = 1:N Lout(idx{i}) = pidx(i); end imshow(label2rgb(Lout))
Clustering Result:
Extracting Gabor Features for Each Superpixel
Gabor filters are perfect for capturing texture across different scales and orientations. Here's how to compute them for each of your superpixels:
1. Generate a Gabor Filter Bank
First, create a set of multi-scale, multi-directional Gabor filters using MATLAB's built-in gabor function:
% Define parameters: 4 scales, 6 orientations (adjust these as needed!) num_scales = 4; num_orientations = 6; gabor_filters = gabor([0.5 1 2 4], 0:pi/num_orientations:pi-pi/num_orientations);
2. Compute Gabor Responses for the Image
Convert your RGB image to grayscale (Gabor works best with single-channel data) and calculate responses for each filter:
gray_img = rgb2gray(A); gabor_responses = imgaborfilt(gray_img, gabor_filters);
3. Extract Statistical Features per Superpixel
Use the label2idx(L) you already used to get pixel indices for each superpixel, then pull out key stats (mean and variance are standard choices) from the Gabor responses:
superpixel_idx = label2idx(L); total_filters = num_scales * num_orientations; % Each superpixel gets 2 features per filter (mean + variance) gabor_features = zeros(N, total_filters * 2); for i = 1:N pix_indices = superpixel_idx{i}; % Pixels in current superpixel for j = 1:total_filters resp = gabor_responses(:,:,j); resp_pixels = resp(pix_indices); % Store mean and variance for this filter gabor_features(i, 2*j-1) = mean(resp_pixels); gabor_features(i, 2*j) = var(resp_pixels); end end
Now gabor_features is an N-row matrix where each row holds the full texture feature set for one superpixel.
Creating Independent Variables for Each Clustered Category
Based on your clustering results (pidx for superpixel labels, Lout for image-level labels), here are three practical ways to create independent variables:
Option 1: Category Mask Variables
Generate a binary mask for each category—pixels in the category are 1, others are 0. Great for visualization or region-specific analysis:
num_classes = numColors; % You used 6 clusters category_masks = cell(num_classes, 1); for c = 1:num_classes category_masks{c} = (Lout == c); % Mask for category c end
Call category_masks{3} to get the mask for your 3rd cluster anytime.
Option 2: One-Hot Encoded Variables
Convert superpixel category labels into one-hot vectors—ideal for feeding into machine learning models:
one_hot_features = zeros(N, num_classes); for i = 1:N one_hot_features(i, pidx(i)) = 1; end
For example, if superpixel 5 belongs to cluster 2, one_hot_features(5,:) will be [0 1 0 0 0 0].
Option 3: Structured Category Info
Store detailed info for each category (like which superpixels belong to it, average features) in a struct for easy access:
category_info = struct(); for c = 1:num_classes % Get all superpixels in this category class_superpixels = find(pidx == c); % Store metadata and aggregated features category_info.(['cluster', num2str(c)]) = struct(... 'superpixel_indices', class_superpixels, ... 'avg_gabor_features', mean(gabor_features(class_superpixels, :)) ... ); end
You can then pull up details like category_info.cluster1.avg_gabor_features to get the average Gabor features for all superpixels in cluster 1.
内容的提问来源于stack exchange,提问作者Aadnan Farooq A

