using Mean Square Error to create an index matrix matlab -
the mean square error(mse), method used define difference in between 2 blocks, , can calculated follow: a , b 2 blocks equal size
mse = sqrt(sum(sum((a-b).^2)))/size(a or b) if mse less given threshold, 2 blocks not diffrent.
given 2 matrix a , b, purpose divide 2 matrix blocks of given size, extract first block , let a, search block b b mean square error between a , b less given threshold, return position of block b matrix b. , on. , here example:
given 2 matrix a , b where:
a= [1 1 4 4 2 2 1 1 4 4 2 2 2 2 9 9 5 5 2 2 9 9 5 5 3 3 4 4 9 9 3 3 4 4 9 9]; b = [ 2 2 4 4 9 9 2 2 4 4 9 9]; the threshold 2
the first block a obtained matrix a is:
1 1 1 1 the block b obtained matrix b msr between a , b less threshold is:
2 2 2 2 therefore return position of block b in matrix b 1
the second block a obtained matrix a is:
4 4 4 4 the block b obtained matrix b msr between a , b less threshold is:
4 4 4 4 therefore return position of block b in matrix b 2. , on.
the final result should follow
res= [1 2 1 1 3 2 1 2 3]; is there faster way?
here's vectorized approach bsxfun.
first define data:
a = [1 1 4 4 2 2 1 1 4 4 2 2 2 2 9 9 5 5 2 2 9 9 5 5 3 3 4 4 9 9 3 3 4 4 9 9]; %// data: b = [2 2 4 4 9 9 2 2 4 4 9 9]; %// data: b m = 2; %// number of rows per block n = 2; %// number of cols per block then apply following steps:
- reshape matrices each block row (inspired this great answer).
- compute mse pairs of blocks.
- find argmin of mse respect blocks of
b(for each block ofa). note if there several minimizing blocks inbfinds first. - reshape result matrix.
code:
a2 = reshape(permute(reshape(a, size(a, 1), n, []), [2 1 3]), n*m, []); %// step 1 b2 = reshape(permute(reshape(b, size(b, 1), n, []), [2 1 3]), n*m, []); mse = squeeze(sum(bsxfun(@minus, a2, permute(b2,[1 3 2])).^2, 1)); %// step 2 [~, result] = min(mse, [], 2); %// step 3 result = reshape(result, size(a,1)/m, size(a,2)/n); %// step 4
Comments
Post a Comment