我建議採取更面向矩陣的方法。使用循環時,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