我有两个想要比较的图像,并根据特定像素的值来执行不同的操作。问题是,这真的很慢,我需要加快操作速度,可以用代码做什么?
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
感谢您的帮助!
答案 0 :(得分:1)
您可以在一次操作中比较矩阵。例如,
D = diff > 20;
矩阵D将包含D(i,j)= 1,其中diff(i,j)> 20,否则为零。
然后你可以用它来设置其他矩阵:
fusion = zeros(size(currentFrame));
fusion(diff > 20) = double(currentFrame(diff > 20));
与第一个循环相同。
答案 1 :(得分:1)
您不需要循环 - 您可以执行以下操作:
indexes = currentFrame > backgroundImage;
backgroundImage(indexes) = backgroundImage(indexes) + 1;
顺便说一句。在使用currentFrame(i,j) > backgroundImage(i,j)
的代码中,您只是比较三个颜色维度中的第一个。这是为了吗?