快速访问八度中的像素

Oct*_*oob 1 image pixel octave

我正在使用Octave分析一些图像。现在,我使用两个嵌套的for循环访问每个像素,但这确实很慢。

我的代码是这样的:

for i = 1:size(im,1)
    for j = 1:size(im,2)
    p = im(i,j,1:3);
        if (classRGB(class, [p(1),p(2),p(3)]) > 0)
            ## Apply some function to that pixel here
        endif
    endfor
endfor
Run Code Online (Sandbox Code Playgroud)

没有八度中的循环,有没有办法做到这一点?

提前致谢。

Jai*_*oto 5

我建议采用更面向矩阵的方法。使用循环时,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]);
Run Code Online (Sandbox Code Playgroud)

我建议学习更多有关Octave中的矩阵操作,算术和寻址的信息。我提供了示例的原始图像和结果图像以供参考。 原始图片 结果图像