0

I have two images which i would like to compare and do different operations depending on what value the specific pixel has. The problem is that is is really slow and i need to speed the operations up alot, what could be done with the code?

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

Thanks for any help!

2 Answers 2

1

You don't need to loop - you can do stuff like:

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

btw. in your code using currentFrame(i,j) > backgroundImage(i,j) you are just comparing the first of the three color dimensions. Is this intended?

Sign up to request clarification or add additional context in comments.

4 Comments

No it was not intended, can I just do currentFrame(i,j, :) > backgroundImage(i,j, :) instead?
That works - but gives you a 1x1x3 dimensional logical vector. You'd then need to clarify the if. (is it enough when just one color is larger?)
Ye it works, but i get a problem when im doing the same thing when i'm computing the fusion image. The colors gets really messed up on some regions (it is a pretty dark image from the beginning but now I get colors like clear blue/green/pink. Here is the code: fusion = zeros(size(currentFrame)); indexes = diff > 20; fusion(indexes) = double(rgbimage(indexes));
Can only guess here - but I believe Matlab uses RGB values in range 0-1, and it sounds yours are between 0-255.
1

You can compare matrices in one operation. for example,

D = diff > 20;

matrix D will containe D(i,j) = 1 where diff(i,j) > 20, otherwise zero.

Then you can use it to set other matrices:

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

and the same with the first loop.

1 Comment

Thanks alot, got much faster, I'm using your code and 'bdecaf' but he posted first so he gets the "correct answer"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.