私は正と負の値からなる行列を持っています。私はこれらのことをする必要があります。ピクセルの近傍を計算する最も簡単な方法
u(i,j)
は、u
のピクセルを示します。
- ゼロクロッシングピクセルを計算します。これらは、
u(i-1,j)
とu(i+1,j)
が反対の符号であるか、u(i,j-1)
とu(i,j+1)
が反対の符号である場合、グリッドのピクセルです。 - 次に、これらのゼロクロスピクセルの周りの狭いバンドを計算する必要があります。狭帯域の幅は、各画素について
(2r+1)X(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
の機能は?コードをさらに最適化するにはどうすればよいですか?
ありがとうございます。
あなたは形態学的操作を考えがありますが、[ 'bwmorph'](http://www.mathworks.com/help/images/ref/bwmorph.html)言うの? –
注意: 'isband'の右下の2つの隣接ピクセルへのアクセスは不正確です。あなたはおそらくコピーペーストし、-1を+1と+0に訂正するのを忘れたでしょう。 –
@RodyOldenhuisコメントをいただきありがとうございます。うん、私はそれをコピーして貼り付けたので間違いだった。それを修正するために私の質問を編集しました。 – roni