よりマトリックス指向のアプローチを取ることをお勧めします。ループを使用すると、MATLAB/Octave が非常に遅くなります。
たとえば、グレースケール変換値 (0.3*R + 0.6*G + 0.1*B) が 128 以下のピクセルがゼロに設定されている RGB 画像を作成するとします。
# Read a 512x512 RGB image.
# Resulting matrix size is [512 512 3]
im = imread('lena_rgb.png');
# Compute grayscale value (could be done more accurately with rgb2gray).
# Resulting matrix size is [512 512 1] (same as [512 512])
grayval = 0.3*im(:,:,1) + 0.6*im(:,:,2) + 0.1*im(:,:,3);
# Create a bitmask of grayscale values above 128
# Contains 0 if less than or equal than 128, 1 if greater than 128
# Resulting matrix size is [512 512 1] (same as [512 512])
mask = (grayval > 128);
# Element-wise multiply the mask with the input image to get the new RGB image
# Resulting matrix size is [512 512 3]
result = im.* repmat(mask, [1 1 3]);
Octaveで行列操作、算術演算、アドレス指定についてさらに学習することをお勧めします。参照用に、私の例の元の画像と結果の画像を含めました。
