matlab 所有像素求和_Matlab中图像之间绝对差异的总和

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!

解决方案

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...

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值