%% PC_calculator %{ Version: v4 Description: This code calculate PCs from image data of different heat loads. Last modified: 2025/10/07 Author: Lige Zhang Reference: International Journal of Heat and Mass Transfer 255 (2026): 127894. %} %% ------------------- Setup ------------------- clear; clc; close all; format long; %% ------------------- Configuration ------------------- % Define the main data directory where high-speed images are stored. MainData_DIR = "\path\to\your\data"; % <-- IMPORTANT: Set your actual data path here % Define subfolders for each heat load heatLoads = ["15W", "30W"]; % Create a main directory for saving results outputDir = fullfile(MainData_DIR, "PC_Results"); if ~exist(outputDir, 'dir') mkdir(outputDir); end % --- Parameters to define for PCA calculation --- totalImages = 3000; % Total number of images to process per heat load (N) imageFileFormat = '%05d.jpg'; % The file name format that the high speed images are saved. numSubsets = 10; % Number of subsets to divide the data into numPCs = 10; % Number of principal components to calculate and save savePCdata = true; % Set to true to save results, false to just run fprintf('PCA Calculation Started...\n'); %% ------------------- Main Processing Loop ------------------- % Loop over each heat load condition for i = 1:length(heatLoads) currentHeatLoad = heatLoads(i); imageDataDir = fullfile(MainData_DIR, currentHeatLoad); fprintf('\nProcessing Heat Load: %s\n', currentHeatLoad); % Create a specific output folder for this heat load's results heatLoadOutputDir = fullfile(outputDir, currentHeatLoad); if ~exist(heatLoadOutputDir, 'dir') mkdir(heatLoadOutputDir); end imagesPerSubset = totalImages / numSubsets; % (n) % Loop over each of the 10 subsets for s = 1:numSubsets fprintf(' Calculating for Subset %d/%d...\n', s, numSubsets); % --- 1. Load, Resize, and Vectorize Images for the Subset --- % Determine the first and last image index for the current subset firstImageIdx = (s - 1) * imagesPerSubset; lastImageIdx = s * imagesPerSubset - 1; % Pre-allocate matrix to hold image data for efficiency % First, read one image to get its dimensions after resizing sampleImPath = fullfile(imageDataDir, sprintf(imageFileFormat, firstImageIdx)); I_sample = imread(sampleImPath); [m, n] = size(I_sample); % Get dimensions of the image % Now pre-allocate the data matrix: p-by-n (pixels-by-images) imageData = zeros(m * n, imagesPerSubset); % Load all images for the current subset for k = 1:imagesPerSubset imgIndex = firstImageIdx + (k - 1); imFile = sprintf(imageFileFormat, imgIndex); % Read, convert to double for math, and resize I = double(imread(fullfile(imageDataDir, imFile))); % I_resized = imresize(I, 0.1); % Normalize (0-255 -> 0-1) and flatten into a column vector imageData(:, k) = reshape(I / 255.0, [m * n, 1]); end % --- 2. Perform PCA using SVD --- % Calculate the mean image (vector) of the subset meanImageVector = mean(imageData, 2); % Subtract the mean from every image (mean-centering) meanCenteredData = imageData - meanImageVector; % Perform SVD on the transpose of the mean-centered data % 'econ' is crucial for efficiency, as it calculates the "economy size" SVD [U, S, V] = svd(meanCenteredData', 'econ'); % --- 3. Calculate and Save Principal Component Scores --- % The PC scores are simply U*S. all_PC_scores = U * S; % Extract the first 10 PCs PC_scores_to_save = all_PC_scores(:, 1:numPCs); % Save the results to a CSV file if requested if savePCdata % Define a descriptive filename fileName = sprintf('Subset_%02d_PCs.csv', s); csvFilePath = fullfile(heatLoadOutputDir, fileName); % Write the matrix of PC scores to the file writematrix(PC_scores_to_save, csvFilePath); end end end fprintf('\nPCA Calculation Complete. Results saved in: %s\n', outputDir);