Osz*_*kar 5 matlab image-processing
我正在制作一个从图像中提取信息的算法 - 这些信息中的大部分都是类似能量的数据.这基本上是通过使用内核运行图像(大小作为参数给出)来获得,并获得该内核中的平方值.
这是在三个尺度中完成的,其中三个内核(补丁)大小(此时):smallestPatchSize,smallestPatchSize*3,smallestPatchSize*9(在第二和第三种情况下具有重叠内核).这是针对几种颜色通道,梯度滤波器等完成的(共有17种颜色通道).
我的问题是,是否可以对下面的代码进行矢量化; 很明显,这部分代码需要比其他代码更多的时间来运行.我是Matlab的初学者,仍然试图获得矢量化的一席之地但是这个让我很开心:)
for dim = 1:17
for i = 1:smallestPatchSize:(size(image,1) - currentPatchSize)
for j = 1:smallestPatchSize:(size(image,2) - currentPatchSize)
% calculate the position in the energy matrix
% which has different dimensions than the input pictures
iPosition = (i - 1 + smallestPatchSize) / smallestPatchSize;
jPosition = (j - 1 + smallestPatchSize) / smallestPatchSize;
% calculate the energy values and save them into the energy matrix
energy(iPosition, jPosition, dim) = sum(sum(abs(...
filters(i:i+currentPatchSize, j:j+currentPatchSize,dim)))) ^ 2;
end
end
end
Run Code Online (Sandbox Code Playgroud)
提前谢谢 - 这是我的第一个问题@ StackOverflow :)
您正在获取块的局部值和。求和过滤器是可分离的,即如果您想对 m×n 求和,可以将其分解为首先取 m×1 和,然后对结果求 1×n 和。请注意,使用全卷积,您会更频繁地求和,但它仍然比使用blkproc或更新的blockproc.
因此,对于 Even smallestPatchSize,您可以将内部循环编写为:
tmp = conv2(...
conv2(abs(filter),ones(currentPatchSize,1),'same'),...
ones(1,currentPatchSize,'same').^2;
%# tmp contains the energy for a sliding window filter.
energy = tmp((currentPatchSize/2+1):(currentPatchSize+1):size(image,1)-(currentPatchSize/2+1),...
(currentPatchSize/2+1):(currentPatchSize+1):size(image,2)-(currentPatchSize/2+1));
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
847 次 |
| 最近记录: |