【发布时间】:2015-03-07 06:58:18
【问题描述】:
我想计算由窗口(虚线)创建的子区域的均值和标准微分,并以识别的像素红色为中心(称为局部均值和标准微分)。这是描述它的图
我们可以通过使用蒙版卷积图像来做到这一点。但是,这需要很长时间,因为我只关心服务器点的均值和标准推导,而卷积计算的是图像中的整个点。您能否有一种更快的方法来解决它,只计算已识别像素处的均值和标准导数?我是用matlab做的。这是我的卷积函数代码
I=[18 36 70 33 64 40 62 76 71 37 5
82 49 86 45 96 29 74 7 60 56 45
25 32 55 48 25 30 12 82 95 77 8
24 18 78 74 19 57 67 59 16 46 78
28 9 59 2 29 11 7 31 75 15 25
83 26 96 8 82 26 85 12 11 28 19
81 64 78 70 26 33 17 72 81 16 54
75 39 78 34 59 31 77 31 61 81 89
89 84 29 99 79 25 26 35 65 56 76
93 90 45 7 61 13 34 24 11 34 92
88 82 91 81 100 4 88 70 85 8 19];
identified_position=[30 36 84 90] %indices of pixel 78, 48,72 60
mask=1/9.*ones(3,3);
mean_all=imfilter(I,mask,'same');
%Mean of identified pixels
mean_all(identified_position)
% Compute the variance
std_all=stdfilt(I,ones(3));
%std of identified pixels
std_all(identified_position)
这是对比代码
function compare_mean(dimx,dimy)
I=randi(100,[dimx,dimy]);
rad=3;
identified_position=randi(max(I(:)),[1,5]);% Get 5 random position
function way1()
mask=ones(rad,rad);
mask=mask./sum(mask(:));
mean_all=conv2(I,mask,'same');
mean_out =mean_all(identified_position);
end
function way2()
box_size = rad; %// Edit your window size here (an odd number is preferred)
bxr = floor(box_size/2); %// box radius
%// Get neighboring indices and those elements for all identified positions
off1 = bsxfun(@plus,[-bxr:bxr]',[-bxr:bxr]*size(I,1)); %//'#neighborhood offsets
idx = bsxfun(@plus,off1(:),identified_position); %// all absolute offsets
I_selected_neigh = I(idx); %// all offsetted elements
mean_out = mean(I_selected_neigh,1); %// mean output
end
way2()
time_way1=@()way1();timeit(time_way1)
time_way2=@()way2();timeit(time_way2)
end
有时way2有错误
Subscript indices must either be real positive integers or logicals.
Error in compare_mean/way2 (line 18)
I_selected_neigh = I(idx); %// all offsetted elements
Error in compare_mean (line 22)
way2()
【问题讨论】:
-
您会只使用 3x3 的盒子还是想要任何尺寸?这将导致不同的方法。
-
其实我想要任何尺寸。 3x3 仅用于简单的可视化
-
最后一件事:你想如何管理边界?只需平均/标准最少数量的像素?
-
好问题。我知道边界问题。我们可以添加填充点。但是,边界点对我的方法影响不大。因此,我们可以忽略它们。只需在卷积中使用选项“相同”
-
我很好奇您为什么要这样做?你愿意解释一下吗?
标签: performance matlab image-processing matrix