Why is my Hotelling Transform implementation producing garbage?

I have to implement a Karhunen-Loeve / Hotelling Transform and i seemingly get garbage output. I have no errors and seemed to have respected the algorithm taught in class. I tested it using the b&w Lena image and the output is just a black and white mess. Code and testing steps provided below.
function [B] = TKL2D(image)
[numberOfLines, numberOfColumns] = size(image);
% mean value computation
imageMean = mean2(image);
stationaryImg = image - imageMean;
% autoCorr Matrix
autoCorrMat = (transpose(stationaryImg) * stationaryImg ) / size(stationaryImg, 1);
% spectral component
[V, D] = eig(autoCorrMat);
% sorting the eigenvalues from largest to smallest, rearranging the indices
% of the V matrix and searching for the critical point between the primary
% and secondary components (using findCriticalPoint)
diagValues = diag(D);
[sortedD, indices] = sort(diagValues, 'descend');
V = V(:, indices);
% bar(sortedD)
criticalPoint = findCriticalPoint(sortedD, 0.1); % 10% error
fprintf('%i\n', criticalPoint);
slicedV = V(:, 1: criticalPoint);
% TKL2D => transformed matrix
processedImage = transpose(slicedV) * transpose(stationaryImg);
% reconstructed original matrix
B = slicedV * processedImage + imageMean;
end
function [i] = findCriticalPoint(sortedArray, baseError)
error = baseError * sortedArray(1);
% error is a percentage of the largest value in the ordered array of
% eigenvalues
for i = 1 : ( length(sortedArray) - 1)
if (sortedArray(i) - sortedArray(i + 1)) > error
break;
% stop when the diff between 2 consecutive values is larger
% than the previously computed error (i always get 1 or 2
% primary components)
end
end
end
Testing script attached below
>> image = imread('lena.png');
>> greyImg = im2double(rgb2grey(image));
>> greyImg = im2double(rgb2gray(image));
>> restored = TKL2D(greyImg);
1
>> imshow(restored)
>>
As almost always, it seems that there's only 1 critical point.
Can any obvious flaw be identified? Thanks.

 Réponse acceptée

yanqi liu
yanqi liu le 4 Jan 2022
yes,sir,please check
criticalPoint = findCriticalPoint(sortedD, 0.1);
it is 1,if modify it to
criticalPoint = max(20,findCriticalPoint(sortedD, 0.1));
you can get different result

2 commentaires

Thanks, it seems to work better now. How can i improve the overall restored quality? It seems that even if i hardcode large values i still get quite the pixelated image.
yes,sir,may be increase error threshold

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Mathematics and Optimization dans Centre d'aide et File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by