321     0      0      0

     Principle Component Analysis (PCA) Matlab Code (ChatGPT)


    Amardip Ghosh  #Advanced Propulsion Systems (APSYS) Lab

     

    % PCA on an image set
    clc; clear; close all;

    % Set image directory
    imageDir = 'images/'; % Change this to your image folder path
    imageFiles = dir(fullfile(imageDir, '*.png')); % Change extension if needed (e.g., .jpg, .bmp)
    numImages = length(imageFiles);

    % Read first image to get dimensions
    firstImage = imread(fullfile(imageDir, imageFiles(1).name));
    if size(firstImage, 3) == 3
    firstImage = rgb2gray(firstImage); % Convert to grayscale if RGB
    end
    [rows, cols] = size(firstImage);
    imageSize = rows * cols;

    % Create data matrix where each column is an image vector
    dataMatrix = zeros(imageSize, numImages);

    for i = 1:numImages
    img = imread(fullfile(imageDir, imageFiles(i).name));
    if size(img, 3) == 3
    img = rgb2gray(img);
    end
    img = im2double(img); % Convert to double
    dataMatrix(:, i) = img(:); % Vectorize image
    end

    % Mean normalization
    meanImage = mean(dataMatrix, 2);
    X = dataMatrix - meanImage;

    % Compute covariance matrix
    C = cov(X');

    % Eigen decomposition
    [eigVectors, eigValues] = eig(C);
    eigValues = diag(eigValues);

    % Sort eigenvalues and vectors in descending order
    [~, idx] = sort(eigValues, 'descend');
    eigVectors = eigVectors(:, idx);

    % Project data onto principal components
    k = 10; % Number of principal components to keep
    eigVectors_k = eigVectors(:, 1:k);
    projectedData = eigVectors_k' * X;

    % Reconstruct an image using top k components
    reconstructed = eigVectors_k * projectedData meanImage;

    % Show original and reconstructed images
    figure;
    subplot(1, 2, 1);
    imshow(reshape(dataMatrix(:, 1), rows, cols), []);
    title('Original Image');

    subplot(1, 2, 2);
    imshow(reshape(reconstructed(:, 1), rows, cols), []);
    title(['Reconstructed with ', num2str(k), ' PCs']);

    disp('PCA completed successfully!');



     

RECENT POSTS

Posts arranged recent first


Flow Diagnostics Tools - Schlieren
Flame flashback in supersonic flowfields
Shocktrain Underexpanded nozzle
Setup alignment #zero to hero
Ex-ISRO Chairman visits APSYS Labs
Turbulent Combustion and High Speed Flows
BTP and MTP Project Topics - 2023 Batch
Flame Dynamics and Its Control
Heat Release Distribution
Thermodynamics and Aerospace Propulsion Systems Class
SCRAMJET Test Rig Instrumentation
Effect of Wall Divergence on the Flow Field Inside a Scramjet Engine
Showcase Our Work to ISRO Chairman S. Somanath and Dr. V. Narayanan
Teacher\'s day celebration 2024 in APSYS Lab
Instability of isolator shocks to fuel flow rate modulations in a strut-stabilised scramjet combustor
Research Areas
Insights into flame flashback phenomenon utilizing a Strut-Cavity flame holder inside scramjet combustor
Wavelets in Signal Processing
A Matlab code of how to do dynamic mode decomposition of a video sequence.
Principle Component Analysis (PCA) Matlab Code (ChatGPT)
Independent Component Analysis (ICA) for a video sequence. ChatGPT
Bychkov Limit (ChatGPT, Google AI)