matlab中邻域窗求和,matlab – 计算像素邻域的最快方法

我有一个由正值和负值组成的矩阵.我需要做这些事情.

设u(i,j)表示矩阵u的像素.

>计算过零像素.如果u(i-1,j)和u(i 1,j)具有相反的符号或者u(i,j-1)和u(i,j 1)具有相反的符号,则这些是网格中的像素.

>然后我需要计算这些零交叉像素周围的窄带.对于每个像素,窄带的宽度是(2r 1)×(2r 1).我正在考虑r = 1因此我必须实际获得每个零交叉像素的8个邻域像素.

我在一个程序中完成了这个.请看下面的内容.

%// calculate the zero crossing pixels

front = isfront(u);

%// calculate the narrow band of around the zero crossing pixels

band = isband(u,front,1);

我还附加了isfront和isband函数.

function front = isfront( phi )

%// grab the size of phi

[n, m] = size(phi);

%// create an boolean matrix whose value at each pixel is 0 or 1

%// depending on whether that pixel is a front point or not

front = zeros( size(phi) );

%// A piecewise(Segmentation) linear approximation to the front is contructed by

%// checking each pixels neighbour. Do not check pixels on border.

for i = 2 : n - 1;

for j = 2 : m - 1;

if (phi(i-1,j)*phi(i+1,j)<0) || (phi(i,j-1)*phi(i,j+1)<0)

front(i,j) = 100;

else

front(i,j) = 0;

end

end

end

function band = isband(phi, front, width)

%// grab size of phi

[m, n] = size(phi);

%// width=r=1;

width = 1;

[x,y] = find(front==100);

%// create an boolean matrix whose value at each pixel is 0 or 1

%// depending on whether that pixel is a band point or not

band = zeros(m, n);

%// for each pixel in phi

for ii = 1:m

for jj = 1:n

for k = 1:size(x,1)

if (ii==x(k)) && (jj==y(k))

band(ii-1,jj-1) = 100; band(ii-1,jj) = 100; band(ii-1,jj+1) = 100;

band(ii ,jj-1) = 100; band(ii ,jj) = 100; band(ii,jj+1) = 100;

band(ii+1,jj-1) = 100; band(ii+1,jj) = 100; band(ii+1,jj+1) = 100;

end

end

end

end

输出如下:以及计算时间:

%// Computation time

%// for isfront function

Elapsed time is 0.003413 seconds.

%// for isband function

Elapsed time is 0.026188 seconds.

当我运行代码时,我确实得到了正确的答案,但是任务的计算太多了,不管我喜欢什么.有没有更好的方法呢?特别是isband功能?如何进一步优化我的代码?

提前致谢.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值