0

我有两张图像,我想根据特定像素的值进行比较并执行不同的操作。问题是它真的很慢,我需要加快操作速度,代码可以做什么?

currentFrame = rgbimage; %rgbimage is an 800x450x3 matrix

for i = 1:size(currentFrame, 1)

   for j = 1 : size(currentFrame,2) 

       if currentFrame(i,j) > backgroundImage(i,j) %backgroundimage is an equally sized image which i would like to compare with
          backgroundImage(i,j, :) = double(backgroundImage(i,j, :) +1); 

       elseif currentFrame(i,j) < backgroundImage(i,j)
          backgroundImage(i,j, :) = double(backgroundImage(i,j, :) -1);          
       end


   end

end

diff = abs(double(currentFrame) - double(backgroundImage)); %difference between my backgroundimage and my current frame
fusion = zeros(size(currentFrame)); % A fusion image

for i=1:size(backgroundImage,1)
    for j = 1:size(backgroundImage,2)

           if diff(i,j) > 20

            fusion(i,j, :) = double(currentFrame(i,j, :));

           else
             fusion(i,j, :) = 0;  

           end
    end 
end

谢谢你的帮助!

4

2 回答 2

1

您可以在一次操作中比较矩阵。例如,

D = diff > 20;

矩阵 D 将包含 D(i,j) = 1,其中 diff(i,j) > 20,否则为零。

然后你可以用它来设置其他矩阵:

fusion = zeros(size(currentFrame));
fusion(diff > 20) = double(currentFrame(diff > 20));

和第一个循环一样。

于 2012-06-19T08:59:30.040 回答
1

您不需要循环 - 您可以执行以下操作:

indexes = currentFrame > backgroundImage;
backgroundImage(indexes) = backgroundImage(indexes) + 1;

顺便提一句。在您的代码中使用currentFrame(i,j) > backgroundImage(i,j)您只是比较三个颜色维度中的第一个。这是故意的吗?

于 2012-06-19T09:00:35.790 回答