Skip to content

Commit

Permalink
Implement MW-PCAG.
Browse files Browse the repository at this point in the history
  • Loading branch information
nathanntg committed Apr 22, 2015
1 parent df8faf9 commit 76ff37a
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 0 deletions.
17 changes: 17 additions & 0 deletions mwpcag.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
function im_result = mwpcag(img)
%MWPCAG Perform the MW-PCAG algorithm on img.

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

im_result = zeros(height, width);
for sz = 9:2:15
% run MW-PCAG
cb = @(block) mwpcag_block(block, floor((sz-2)^2*0.1), floor(sz*sz*0.45));
r = blockproc(img, [sz sz], cb, 'PadPartialBlocks', true, 'PadMethod', 'symmetric');

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

end
40 changes: 40 additions & 0 deletions mwpcag_block.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
function px = mwpcag_block(block_struct, min_size, max_size)
%MWPCAG_BLOCK Perform the MW-PCAG algorithm on img

% gap between pixels
diff_num = 2;

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

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

ret = zeros([size(vals, 1) 1]);

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

% forward
[fm, fm_i] = max(diff_span(s(min_size:max_size), diff_num));

% backward
[bm, bm_i] = max(diff_span(s(end - max_size:end - min_size), diff_num));

% largest
if fm > bm
fm_i = min_size - 1 + fm_i;
ret(s_i(1:fm_i)) = ret(s_i(1:fm_i)) + (fm * p_la(c));
else
bm_i = numel(s) - max_size - 1 + bm_i + diff_num;
ret(s_i(bm_i:end)) = ret(s_i(bm_i:end)) + (bm * p_la(c));
end
end

% normalize
% restore shape
px = reshape(ret / sum(p_la), 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 @@ -90,6 +90,13 @@ function run_script(scene_file)
save(fname, 'img_pcag');
end

% MW-PCAG
fname = sprintf('output/%s-%d-mwpcag.mat', scene_file, j);
if ~exist(fname, 'file')
img_mwpcag = mwpcag(img);
save(fname, 'img_mwpcag');
end

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

0 comments on commit 76ff37a

Please sign in to comment.