1

I want to implement sum of absolute difference in Matlab to establish a similarity metric between between one video frame and 5 frames either side of this frame (i.e. past and future frames). I only need the SAD value for the co-located pixel in each frame, rather than a full search routine, such as full search.

Obviously I could implement this as nested loops such as:

bs = 2; % block size
for (z_i = -bs:1:bs)
    for (z_j = -bs:1:bs) 

        I1(1+bs:end-bs,1+bs:end-bs) = F1(1+bs+z_i:end-bs+z_i, 1+bs+z_j:end-bs+z_j);
        I2(1+bs:end-bs,1+bs:end-bs) = F2(1+bs+z_i:end-bs+z_i, 1+bs+z_j:end-bs+z_j);

        sad(:,:) = sad(:,:) + abs( I1(:,:) - I2(:,:));

    end
end

However I'm wondering is there a more efficient way of doing it than this? At the very least I guess I should define the above code snippet as a function?

Any recommendations would be grateful accepted!

trican
  • 1,157
  • 4
  • 15
  • 24
  • I don't understand your code. `I1(1:bs-1,:)`,`I1(:,1:bs-1)`,`I1(end-bs+1:end,:)` and `I1(:,end-bs+1:end)` seems to be unused. Can you give a mathematical definition of what you are trying to implement? – Daniel May 14 '14 at 20:16
  • I1 and I2 are used in the calculation of the SAD on the subsequent line? In the above, the SAD calculation is vectorized so that the entire frame is calculated by shifting the frame around - therefore 25 frame level calculations in this case (block size is 5x5 here) – trican May 14 '14 at 20:25
  • That makes sense, but wouldn't this require to set I1 and I2 to zero in each iteration? – Daniel May 14 '14 at 20:27
  • I don't see why it would? Granted I haven't run the code snippet above, but have used something very similar previously – trican May 14 '14 at 20:33
  • In the first iteratin you are filling I1(1,:), then these values remain unchanged and you add them on each iteration. – Daniel May 14 '14 at 20:39

1 Answers1

2

You should use the command im2col in MATLAB you will be able to do so in Vectorized manner.
Just arrange each neighborhood in columns (For each frame).
Put them in 3D Matrix and apply you operation on the 3rd dimension.

Code Snippet

I used Wikipedia's definition of "Sum of Absolute Differences".

The demo script:

```

% Sum of Absolute Differences Demo

numRows = 10;
numCols = 10;

refBlockRadius = 1;
refBlockLength = (2 * refBlockRadius) + 1;

mImgSrc     = randi([0, 255], [numRows, numCols]);
mRefBlock   = randi([0, 255], [refBlockLength, refBlockLength]);

mSumAbsDiff = SumAbsoluteDifferences(mImgSrc, mRefBlock);

```

The Function SumAbsoluteDifferences:

```

function [ mSumAbsDiff ] = SumAbsoluteDifferences( mInputImage, mRefBlock )
%UNTITLED2 Summary of this function goes here
%   Detailed explanation goes here

numRows = size(mInputImage, 1);
numCols = size(mInputImage, 2);

blockLength = size(mRefBlock, 1);
blockRadius = (blockLength - 1) / 2;

mInputImagePadded = padarray(mInputImage, [blockRadius, blockRadius], 'replicate', 'both');

mBlockCol = im2col(mInputImagePadded, [blockLength, blockLength], 'sliding');

mSumAbsDiff = sum(abs(bsxfun(@minus, mBlockCol, mRefBlock(:))));

mSumAbsDiff = col2im(mSumAbsDiff, [blockLength, blockLength], [(numRows + blockLength - 1), (numCols + blockLength - 1)]);


end

```

Enjoy...

Royi
  • 4,640
  • 6
  • 46
  • 64
  • thats very interesting - I've never used im2col before, I'll give it try now. Presumably then there is a reverse function to transform the result back into a 2D array? – trican May 14 '14 at 20:13
  • You can have a look at the function page I linked. `col2im` does the inverse. – Royi May 14 '14 at 20:15
  • I think the way I vectorized the code might be as fast if not faster than using im2col? Any thoughts? – trican May 15 '14 at 13:06
  • I see loops with no vectorization. Time the different solutions and tell us. – Royi May 15 '14 at 13:26
  • Perhaps I'm writing the im2col inefficiently - could you amend your answer with a relevant code snippet that I could use? – trican May 15 '14 at 13:28
  • I will if you give the definition of the math. I can't really understand it by your code. I just can see you're doing "Block" math. – Royi May 15 '14 at 15:58
  • I added a code snippet to evaluate "Sum of Absolute differences" according to Wikipedia with a predefined block. – Royi May 15 '14 at 16:50