麻烦在Matlab中使用一个简单的循环

时间:2011-12-03 07:22:17

标签: matlab for-loop

我有一个像素e1的逻辑掩码需要“混合”。无论我为k赋予什么值,循环执行k次,但图像只受影响一次。 代码:

for k=1:5 %I want to do it 5 times but it does the action only once.
    for i=2:rows-1
        for j=2:cols-1
            if e1(i, j) == 1 %if mask == 1, I take "marked" pixel, 8 pixel surrounding it, and avg them.
                I2(i, j, 1) = (I2(i, j-1, 1) + I2(i, j+1, 1) + I2(i-1, j, 1) + I2(i-1, j-1, 1) + I2(i-1, j+1, 1) + I2(i+1, j, 1) + I2(i+1, j+1, 1) + I2(i+1, j-1, 1))/8;
                I2(i, j, 2) = (I2(i, j-1, 2) + I2(i, j+1, 2) + I2(i-1, j, 2) + I2(i-1, j-1, 2) + I2(i-1, j+1, 2) + I2(i+1, j, 2) + I2(i+1, j+1, 2) + I2(i+1, j-1, 2))/8;
                I2(i, j, 3) = (I2(i, j-1, 3) + I2(i, j+1, 3) + I2(i-1, j, 3) + I2(i-1, j-1, 3) + I2(i-1, j+1, 3) + I2(i+1, j, 3) + I2(i+1, j+1, 3) + I2(i+1, j-1, 3))/8;
            end
        end
    end
end

1 个答案:

答案 0 :(得分:0)

我认为你应该尝试:

for k=1:5
  I2=convn(I2,[1 1 1;1 0 1;1 1 1]/8);
end