Reconstruction of a RGB image after PCA is way darker than the original one
12 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
Hello, I'm currently struggling with PCA applied to RGB images.
I think that what I've done is correct, yet the reconstructed images are way darker than the original ones or even blueish.
I used DermaMNIST from MedMNIST and divided the dataset by 255.
Here's my code:
x_train_r = [];
x_train_g = [];
x_train_b = [];
for i = 1 : size(x_train, 1)
% red
sample = x_train(i,:,:,1);
sample = squeeze(sample);
sample = reshape(sample, [size(sample,1)*size(sample,2),1]);
x_train_r(:,i) = sample;
% green
sample = x_train(i,:,:,2);
sample = squeeze(sample);
sample = reshape(sample, [size(sample,1)*size(sample,2),1]);
x_train_g(:,i) = sample;
% blue
sample = x_train(i,:,:,3);
sample = squeeze(sample);
sample = reshape(sample, [size(sample,1)*size(sample,2),1]);
x_train_b(:,i) = sample;
end
x_val_r = [];
x_val_g = [];
x_val_b = [];
for i = 1 : size(x_val, 1)
% red
sample = x_val(i,:,:,1);
sample = squeeze(sample);
sample = reshape(sample, [size(sample,1)*size(sample,2),1]);
x_val_r(:,i) = sample;
% green
sample = x_val(i,:,:,2);
sample = squeeze(sample);
sample = reshape(sample, [size(sample,1)*size(sample,2),1]);
x_val_g(:,i) = sample;
% blue
sample = x_val(i,:,:,3);
sample = squeeze(sample);
sample = reshape(sample, [size(sample,1)*size(sample,2),1]);
x_val_b(:,i) = sample;
end
x_test_r = [];
x_test_g = [];
x_test_b = [];
for i = 1 : size(x_test, 1)
% red
sample = x_test(i,:,:,1);
sample = squeeze(sample);
sample = reshape(sample, [size(sample,1)*size(sample,2),1]);
x_test_r(:,i) = sample;
% green
sample = x_test(i,:,:,2);
sample = squeeze(sample);
sample = reshape(sample, [size(sample,1)*size(sample,2),1]);
x_test_g(:,i) = sample;
% blue
sample = x_test(i,:,:,3);
sample = squeeze(sample);
sample = reshape(sample, [size(sample,1)*size(sample,2),1]);
x_test_b(:,i) = sample;
end
%% PCA
n_components = 309;
% PCA canale red
dataset_r = [x_train_r, x_val_r, x_test_r];
[U_r, Z_r] = pca(dataset_r', 'NUMCOMPONENTS', n_components);
% PCA canale green
dataset_g = [x_train_g, x_val_g, x_test_g];
[U_g, Z_g] = pca(dataset_g', 'NUMCOMPONENTS', n_components);
% PCA canale blue
dataset_b = [x_train_b, x_val_b, x_test_b];
[U_b, Z_b] = pca(dataset_b', 'NUMCOMPONENTS', n_components);
%% Ricostruzione
Reconstruction_r = Z_r*U_r';
Reconstruction_g = Z_g*U_g';
Reconstruction_b = Z_b*U_b';
Reconstruction = cat(3, Reconstruction_r, Reconstruction_g, Reconstruction_b);
Reconstruction = double(Reconstruction);
%%
figure
idx = 1752; %1450 blue
img = squeeze(Reconstruction(idx,:,:));
img = reshape(img, 28, 28, 3);
imagesc(img)
figure
dataset = cat(1, x_train, x_val, x_test);
imagesc(squeeze(dataset(idx,:,:,:)))
This is what I get:
0 Commenti
Risposte (2)
Neha
il 11 Ott 2023
Hi Matteo,
I understand that you want to apply PCA for RGB images obtained from DermMNIST. Rather than applying PCA to individual channels and then reconstructing the image, you can reshape the images into a 2D array without separating the color channels where you will have 3 columns which correspond to the three color channels of the RGB image and the number of rows would be the number of pixels of the image. You can refer to the following MATLAB Answer for the code snippet:
Apart from PCA, you can also consider using GLCM (Gray-Level Co-occurence Matrix) to extract features from an image. For a skin disease dataset, texture can indeed be a very important feature and since skin diseases manifest as visible changes in the texture of the skin, such as roughness and scaliness. You can refer to the following documentation link for more information on GLCM:
Hope this helps!
Image Analyst
il 16 Ott 2023
Here's a demo that converts an RGB image into the PCA channels. Adapt as needed.
1 Commento
Vedere anche
Categorie
Scopri di più su Dimensionality Reduction and Feature Extraction in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!