我有兩個圖像,我想比較並根據特定像素的值進行不同的操作。問題是速度很慢,我需要加快操作速度,代碼可以做些什麼?如何在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