Matlab中图像之间绝对差异的总和 [英] Sum of Absolute differences between images in Matlab

查看:333
本文介绍了Matlab中图像之间绝对差异的总和的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在Matlab中实现绝对差值之和,以在该帧的任一侧(即过去和未来帧)之间建立一个视频帧与5帧之间的相似性度量。我只需要每帧中共同定位像素的SAD值,而不是完整的搜索例程,例如完全搜索。

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!

推荐答案

您应该使用命令 im2col 在MATLAB中你可以用矢量化的方式这样做。

Just在列中排列每个邻域(对于每个帧)。

将它们放在3D矩阵中并在第3维上应用操作。

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.

我使用维基百科的定义绝对差异总和

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

演示脚本:

```

% 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);

```

函数 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

```

享受......

这篇关于Matlab中图像之间绝对差异的总和的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆