Why is my Hotelling Transform implementation producing garbage?
3 次查看(过去 30 天)
显示 更早的评论
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.
0 个评论
采纳的回答
yanqi liu
2022-1-4
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
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Mathematics and Optimization 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!