我有两个图像,我想比较并根据特定像素的值进行不同的操作。问题是速度很慢,我需要加快操作速度,代码可以做些什么?如何在MATLAB中优化两个for循环
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
感谢您的帮助!
没有它没有打算,我可以''currentFrame(i,j, :)> backgroundImage(i,j,:)'而不是? – Jonas
工作原理 - 但给你一个1x1x3维的逻辑向量。那么你需要澄清if。 (只有一种颜色更大时,就足够了吗?) – bdecaf
它可以工作,但当我在计算融合图像时,我会做同样的事情时遇到问题。在某些区域颜色会变得非常混乱(从一开始这是一个非常暗的图像,但现在我得到的颜色就像清晰的蓝色/绿色/粉红色),下面是代码:fusion = zeros(size(currentFrame)); index = diff> 20; fusion(indexes)= double(rgbimage(indexes));' – Jonas