我建议采取更面向矩阵的方法。使用循环时,MATLAB/Octave非常慢。
例如,让我们说,我想创建RGB图像,其中其灰度转换值的像素(0.3 * R + 0.6 * G + 0.1 * B)小于或等于128被设置为零:
# 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中学习更多关于矩阵操作,算术和寻址的知识。我包含了我的示例的原始和结果图像以供参考。
据我所知,这不能在Octave中完成。不过谢谢。 – OctaveNoob 2010-11-01 09:55:35
出于性能原因,您总是可以与C++代码进行交互。 – Ross 2010-11-01 13:26:31
抱歉,该项目必须在Octave中完成。我目前的解决方案效果很好,但速度很慢。我认为我必须做一些代码向量化,但我只是不知道如何。 – OctaveNoob 2010-11-01 20:05:47