Skip to content

Commit

Permalink
Added comments to LRMC and PCA
Browse files Browse the repository at this point in the history
  • Loading branch information
DEVANSH-DVJ committed May 14, 2021
1 parent 656956c commit bf6cd44
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 0 deletions.
26 changes: 26 additions & 0 deletions algos/LRMC/LRMC.m
Original file line number Diff line number Diff line change
@@ -1,5 +1,20 @@
function [recon, filtered] = LRMC(noisy, frameno, tau, kmax, tol, variant)
% Input:
% noisy : noisy video, [dim1 dim2 nframes] (uint8)
% frameno : frame number to be denoised
% tau : step size
% kmax : maximum iterations
% tol : tolerance
% variant : two binary digits representing
% { whether second subset of missing pixels to be taken and
% replace in patchArr after every iteration }
% Output:
% recon : denoised video, [dim1 dim2 nframes] (uint8)
% filtered : video after applying adaptive median filter, [dim1 dim2 nframes] (uint8)
% Brief:
% Using Low Rank Matrix Completion to denoise video

% Switch case on variant
switch variant
case '00'
sec_missing = false;
Expand All @@ -19,13 +34,16 @@

[dim1, dim2, nframes] = size(noisy);

% Applying adaptive median filter
filtered = zeros([dim1 dim2 nframes], 'uint8');
for i=1:nframes
filtered(:,:,i) = adapmedfilt(noisy(:,:,i), 11);
end

% Finding out the first subset of missing pixels
missing = (filtered ~= noisy);

% Generating patch array and missing array
patchArr = zeros([64 (dim1/4 - 1) (dim2/4 - 1) nframes], 'uint8');
missingArr = false(size(patchArr));

Expand All @@ -38,34 +56,41 @@
end
end

% Iteratively denoising each patch
denoisedpatchArr = zeros(size(patchArr), 'double');
for refj=1:(dim1/4 - 1)
for refk=1:(dim2/4 - 1)

% Getting indices of matching patches
indices = patchmatcher(patchArr, refj, refk, frameno);

% Creating patch matrix and Omega
patchMat = zeros(64, size(indices, 2), 'uint8');
patchOmega = false(64, size(indices, 2));
for i=1:size(indices,2)
patchMat(:,i) = patchArr(:, indices(1,i), indices(2,i), indices(3,i));
patchOmega(:, i) = ~missingArr(:, indices(1,i), indices(2,i), indices(3,i));
end

% Running our SVT algorithm
[denoisedpatchMat, ~] = svti(cast(patchMat, 'double'), patchOmega, tau, kmax, tol, sec_missing);

% Finding the index of the reference patch in patch matrix
for selfind=1+5*(frameno-1):5*frameno
if (indices(:, selfind) == [refj; refk; frameno;])
break;
end
end

% Updating the reference patch
if replace
patchArr(:, refj, refk, frameno) = denoisedpatchMat(:, selfind);
end
denoisedpatchArr(:, refj, refk, frameno) = denoisedpatchMat(:, selfind);
end
end

% Averaged reconstruction from denoised patch array
recon = zeros(size(filtered), 'double');
weight = recon;
for i=1:nframes
Expand All @@ -77,5 +102,6 @@
end
end
recon = recon ./ weight;
% Casting it to uint8 for consistency
recon = cast(recon, 'uint8');
end
21 changes: 21 additions & 0 deletions algos/PCA/PCA.m
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
function [recon, filtered] = PCA(noisy, frameno, C, variant)
% Input:
% noisy : noisy video, [dim1 dim2 nframes] (uint8)
% frameno : frame number to be denoised
% C : reduced dimension for PCA
% variant : one binary digit representing replace in patchArr after every iteration
% Output:
% recon : denoised video, [dim1 dim2 nframes] (uint8)
% filtered : video after applying adaptive median filter, [dim1 dim2 nframes] (uint8)
% Brief:
% Using Principal Component Analysis to denoise video

% Switch case on variant
switch variant
case '0'
replace = false;
Expand All @@ -11,11 +22,13 @@

[dim1, dim2, nframes] = size(noisy);

% Applying adaptive median filter
filtered = zeros([dim1 dim2 nframes], 'uint8');
for i=1:nframes
filtered(:,:,i) = adapmedfilt(noisy(:,:,i), 11);
end

% Generating patch array
patchArr = zeros([64 (dim1/4 - 1) (dim2/4 - 1) nframes], 'uint8');

for i=1:nframes
Expand All @@ -26,32 +39,39 @@
end
end

% Iteratively denoising each patch
denoisedpatchArr = zeros(size(patchArr), 'double');
for refj=1:(dim1/4 - 1)
for refk=1:(dim2/4 - 1)

% Getting indices of matching patches
indices = patchmatcher(patchArr, refj, refk, frameno);

% Creating patch matrix
patchMat = zeros(64, size(indices, 2), 'uint8');
for i=1:size(indices,2)
patchMat(:,i) = patchArr(:, indices(1,i), indices(2,i), indices(3,i));
end

% Running our PCA algorithm
[denoisedpatchMat] = pcai(cast(patchMat, 'double'), C);

% Finding the index of the reference patch in patch matrix
for selfind=1+5*(frameno-1):5*frameno
if (indices(:, selfind) == [refj; refk; frameno;])
break;
end
end

% Updating the reference patch
if replace
patchArr(:, refj, refk, frameno) = denoisedpatchMat(:, selfind);
end
denoisedpatchArr(:, refj, refk, frameno) = denoisedpatchMat(:, selfind);
end
end

% Averaged reconstruction from denoised patch array
recon = zeros(size(filtered), 'double');
weight = recon;
for i=1:nframes
Expand All @@ -63,5 +83,6 @@
end
end
recon = recon ./ weight;
% Casting it to uint8 for consistency
recon = cast(recon, 'uint8');
end

0 comments on commit bf6cd44

Please sign in to comment.