From 76ff37a9dc3c16d5c1751a2263d54f0efe1d2108 Mon Sep 17 00:00:00 2001 From: Nathan Perkins Date: Wed, 22 Apr 2015 09:05:04 -0400 Subject: [PATCH] Implement MW-PCAG. --- mwpcag.m | 17 +++++++++++++++++ mwpcag_block.m | 40 ++++++++++++++++++++++++++++++++++++++++ run_script.m | 7 +++++++ 3 files changed, 64 insertions(+) create mode 100644 mwpcag.m create mode 100644 mwpcag_block.m diff --git a/mwpcag.m b/mwpcag.m new file mode 100644 index 0000000..08605e7 --- /dev/null +++ b/mwpcag.m @@ -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 diff --git a/mwpcag_block.m b/mwpcag_block.m new file mode 100644 index 0000000..64be95a --- /dev/null +++ b/mwpcag_block.m @@ -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 diff --git a/run_script.m b/run_script.m index 9bce466..0fb7470 100644 --- a/run_script.m +++ b/run_script.m @@ -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')