Recently, the matlab digital image processing course requires batch feature extraction of thousands of training set and test set images as input to SVM.
So you can use matlab to extract image feature vectors in batches and save them for subsequent use.
Batch extraction function:
% function return parameters % Category column vector Categorys, and feature vector matrix Features will also automatically save the two variables in the saveMatName file. % Usage example % Extract train training set features and classification % [Categorys,Features]=extractAndSaveFeatures('train.mat', sourceFolderPath, saveMatPath) % load('train.mat', '-mat'); % p_Trains_Categorys =Categorys; %Get category 1 column vector % p_Trains_Features =Features; %Get features. One row corresponds to all the features of a picture, and one column corresponds to one type of features. % Extract test test set features and classification % [Categorys,Features]=extractAndSaveFeatures('test.mat', sourceFolderPath, saveMatPath) % load('test.mat', '-mat'); % p_Test_Categorys =Categorys; %Get category 1 column vector % p_Test_Features =Features; %Get features. One row corresponds to all the features of a picture, and one column corresponds to one type of features. function [ Categorys, Features ]=extractAndSaveFeatures(saveMatName, sourceFolderPath, saveMatPath) addRootSonDir( ); % Make sure the dependent function has been added to the directory if nargin < 3 saveMatPath = ''; end % should be stored in the workspace directory % Function input parameters % saveMatName % The last result file name saved. The mat type file name is such as train.mat. % sourceFolderPath % original file path absolute path such as J:/test % savePath % Save mat file variable path absolute path % function return parameters % Category column vector Categorys, and feature vector matrix Features will also automatically save the two variables in the saveMatName file. %% parameter settings sizeA = 96; sizeB = 96; % Each picture is processed uniformly for this size, and the length and width are adjusted to a*b %% Processing starts, obtaining a list of all files fileList = dir(fullfile(sourceFolderPath, '*.png'));% Get a list of all files in the folder %% Read a single sheet first to determine the feature length; Read a single sheet first to determine the feature length; Read a single sheet first to determine the feature length; fileName = fileList(1).name; [~, name, ~] = fileparts(fileName); path = fullfile(sourceFolderPath, fileName); img = imread(path); img = imresize(img, [sizeA, sizeB]); HogFeatures = extractHOGFeatures(img); %Perform HOG feature extraction. Make sure the batch extraction is consistent with this. RGBFeatures = GetColorHist(img); %Perform RBG feature extraction HogLength = size(HogFeatures, 2); %The feature vector length of this picture is used below as the length of each picture RGBLength = size(RGBFeatures, 2); fprintf('Start extracting %s\ ',saveMatName); fprintf(' 1. Save file:%s\ ',fullfile(saveMatPath, saveMatName)); fprintf(' 2. The source directory of the extracted file: %s\ ', sourceFolderPath); fprintf(' 3. Batch processing image size: length * width = %d × %d\ ', sizeA, sizeB); fprintf(' 4. The length of the feature vector of each picture: Hog:%d RGB:%d\ ', HogLength, RGBLength); %% Batch extraction Batch extraction Batch extraction Batch extraction Batch extraction Batch extraction Batch extraction % Initialize the vector matrix to store data fileNums = length(fileList); Categories = zeros(fileNums, 1); Features = zeros(fileNums, HogLength + RGBLength); dispStr = sprintf('Progress:m/m', 0, 0); clearStr= [repmat('\b', 1, numel(dispStr)), '%s']; fprintf(dispStr); for i = 1:fileNums if mod(i, 150) == 0 fprintf(clearStr);fprintf('Progress:m/m', i, fileNums); end fileName = fileList(i).name; [~, name, ~] = fileparts(fileName); NameFirts_Part = strsplit(name, '_');%Cut out the first number of the file name as the classification number Categories(i, 1) = str2double(NameFirts_Part{<!-- -->1});% Collect categories for this picture img = imread(fullfile(sourceFolderPath, fileName)); img = imresize(img, [sizeA, sizeB]); % Read the image and resize it to 512x512 HogFeatures = extractHOGFeatures(img); % Execute HOG feature extraction RGBFeatures = GetColorHist(img); % Execute RBG feature extraction for j = 1:HogLength % collect HOG feature vectors Features(i, j) = HogFeatures(:, j);%Add end for j = 1:RGBLength % collect RGB feature vectors Features(i, HogLength + j) = RGBFeatures(:, j);%Add end end % for loop processes each picture end fprintf(clearStr);fprintf('Progress: m/m, 99%% completed.\ ', i, fileNums); save(fullfile(saveMatPath, saveMatName), 'Features', 'Categorys');% Save the data as a file, and directly load this callable variable in other .m files fprintf('This round of extraction is completed, %s was saved successfully\ ------\ ',saveMatName); end % If variables with the same name exist in multiple imported files, conflicts will occur. % When using the load function to load multiple files, if there are variables with the same name in the files, % Files loaded later will overwrite variables with the same name in files loaded earlier. % % To avoid variable name conflicts, you can use different variable names to store different variables when loading files. % For example, assume there are two files data1.mat and data2.mat, both of which contain a variable named x. % You can use the following code to load these two files and store them in the variables x1 and x2 respectively: % % load('data1.mat', '-mat'); % x1 = x; % % load('data2.mat', '-mat'); % x2 = x; % % In this way, variables x1 and x2 store the x variables in data1.mat and data2.mat respectively, avoiding variable name conflicts. function color_hist = GetColorHist(img) gray_img = rgb2gray(img); color_hist = imhist(gray_img) / numel(gray_img); color_hist = color_hist'; end function addRootSonDir(rootDir) % Automatically add the input root directory + subdirectories to the environment variables if nargin < 1 || isempty(rootDir) % If the parameter is empty, the directory where the file calling this function is located will be used as rootDir, and the environment variable will be added. stack = dbstack('-completenames'); callingScript = stack(2).file; % 1-the directory of the script function itself, 2-the directory where it is called [scriptDir, ~, ~] = fileparts(callingScript); rootDir = scriptDir; end subDirs = dir(rootDir); % disp(rootDir); subDirs = subDirs([subDirs.isdir]); subDirPath = fullfile(rootDir, subDirs(1).name); addpath(subDirPath); for i = 3:length(subDirs) % starts from 3, because 1 \. ; 2 \..; 3 subdirectory is the first; subDirPath = fullfile(rootDir, subDirs(i).name); addpath(subDirPath); end % disp('---||All added user library directories (excluding the software's default system library directory):'); % paths = strsplit(path, ';'); % for i = 1:length(paths) % if ~contains(paths{i}, '\Program Files\MATLAB') & amp; & amp; ~contains(paths{i}, '\AppData\Local\Temp') % disp(paths{i}); % end % end % disp('---||The user library directory management operation is completed.'); end
Mainly extracted
- HOG features
- Color characteristics?
- If necessary, you can refer to the modification directly and modify the function for extracting feature vectors.
- There is also the saving file type. Here it is saved directly as .mat. You can also modify it yourself and save it as .csv, excel table and other formats.
I can’t bear to look at the dependent function I wrote.
Here is an example of how to extract features:
clc,clear; %Add library file directory % ............ Feature extraction function used by extractAndSaveFeatures.m % HogFeatures = extractHOGFeatures(img); % Execute HOG feature extraction % RGBFeatures = GetColorHist(img); % Perform RBG feature extraction %% Please run in sections to avoid freezing; select the section and use the shortcut key ctrl + enter to run the section %% saveNameT = 'Train.mat'; % training set FolderPathT = 'Source image file path'; savePathT = 'save path'; [P_trainTypes,P_trainFeatures]=extractAndSaveFeatures(saveNameT,FolderPathT,savePathT); %% saveNameE = 'Test.mat'; % test set FolderPathE = 'Source image file path'; savePathE = 'save path'; [P_testTypes,P__testFeatures]=extractAndSaveFeatures(saveNameE,FolderPathE,savePathE); %% How to call the variables in the saved .mat in matlab % Extract train training set features and classification % [Categorys,Features]=extractAndSaveFeatures('train.mat', sourceFolderPath, saveMatPath) % load('train.mat', '-mat'); % p_Trains_Categorys =Categorys; %Get category 1 column vector % p_Trains_Features =Features; %Get features. One row corresponds to all the features of a picture, and one column corresponds to one type of features. % Extract test test set features and classification % [Categorys,Features]=extractAndSaveFeatures('test.mat', sourceFolderPath, saveMatPath) % load('test.mat', '-mat'); % p_Test_Categorys =Categorys; %Get category 1 column vector % p_Test_Features =Features; %Get features. One row corresponds to all the features of a picture, and one column corresponds to one type of features.