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:

  1. reshape matrices each block row (inspired this great answer).
  2. compute mse pairs of blocks.
  3. find argmin of mse respect blocks of b (for each block of a). note if there several minimizing blocks in b finds first.
  4. 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

Popular posts from this blog

php - failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request -

java - How to filter a backspace keyboard input -

java - Show Soft Keyboard when EditText Appears -