Skip to content

Commit

Permalink
Add PCAD algorithm.
Browse files Browse the repository at this point in the history
  • Loading branch information
nathanntg committed Apr 24, 2015
1 parent ff66346 commit 960e965
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 0 deletions.
13 changes: 13 additions & 0 deletions pcad.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
function im_result = pcad(img)
%PCAD Perform the PCAD algorithm on img.

height = size(img, 1);
width = size(img, 2);

cb = @(block) pcad_block(block, 50);
r = blockproc(img, [15 15], cb, 'PadPartialBlocks', true, 'PadMethod', 'symmetric');

% MAX
im_result = r(1:height, 1:width);

end
27 changes: 27 additions & 0 deletions pcad_block.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
function px = pcad_block(block_struct, d)
%PCAD_BLOCK Perform the PCAD algorithm on img

% square dimension
block_size = size(block_struct.data);
channels = size(block_struct.data, 3);

% PCA
vals = reshape(block_struct.data, [], channels);
[~, p_sc, p_la] = pca(vals, 'Algorithm', 'svd', 'Centered', false);

% resuable values
pad = nan(d, 1);
p_nm = size(p_sc, 2);
ret = zeros(size(p_sc));

for c = 1:p_nm
[s, s_i] = sort(p_sc(:, c));

ret(s_i, c) = max(s - [pad; s(1:end-d)], [s(1 + d:end); pad] - s);
end

% Euclidian distance
% restore shape
px = reshape(sqrt(sum(ret .^ 2, 2)), block_size(1), block_size(2));

end
7 changes: 7 additions & 0 deletions run_script.m
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,13 @@ function run_script(scene_file)
save(fname, 'img_mwpcag');
end

% PCAD
fname = sprintf('output/%s-%d-pcad.mat', scene_file, j);
if ~exist(fname, 'file')
img_pcad = pcad(img);
save(fname, 'img_pcad');
end

% KNNA
fname = sprintf('output/%s-%d-knna.mat', scene_file, j);
if ~exist(fname, 'file')
Expand Down

0 comments on commit 960e965

Please sign in to comment.