Multiply two Images


I want to calculate the variance over 10 Images. For that I need two multiply two GRAYSACLE Images mathematical and pixel wise. So for example you have the two pixel with the same coordinates and their values 10 and 12, you get the new pixel value 120. If the result is higher than 255 (maximum of the 8 Bit GRAYSCALE Format), the pixel value should be 255. Unluckily the Image.mul() function does not calculate that. Is there an efficient way to solve that Problem? Without two for-loops with Image.get_pixel / Image.set_pixel.

Thanks in Advance

Um, just think about what you want to do a little bit more… Most pixel values are like 150-200. You will immediately saturate the image to 255 per pixel. The way multiply works right now is like it is because unless you make the image 16-bits per pixel or more there’s no way to store the accumulation.

So… are you sure what you want trying to do is the right way? Is there another way to do what you want?

It’s not a huge problem if the values can’t get higher than 255. The reason is the formula of the variance. I use a special formula so I don’t need to store to many pictures [variance: Sk = Sk-1 + (xk – Mk-1)*(xk – Mk)]. As you can see I subtract the mean of the picture xk and afterwards I multiply them. So most values of the pixel will be very low at the multiplication and the rest of it will be very high or at the maximum. And that’s totally fine.
And if I subtract the old variance with the new one I get a binary picture. With that I can get the movements of the video and run some algorithm over. Just a little comment on what I’m doing.
I tried it out on matlab and it worked perfect.

So is there an efficient way to calculate the “mathematical” multiplication pixel wise on an 8 bit picture?
You don’t need to worry about saturation. And if a counter overflow occurs it should stay at the maximum.
Or do you have another way to calculate the variance?

thanks in advance

Hi, so, we have ulab onboard and I think we just need to make some more minor modifications to the library before you can just use it to do any math on images.

However, the current multiply method just multiplies images by treating the pixel values as 0-1.

I.e it scales each pixel from 0-255 to 0-1, then does the multiply, then scales the result back to 0-255.

This of course never overflows as a number less than 1 multiplied by another number less than 1 is always less than 1.

Anyway, this is a linear operation that just accumulates a set of constants each time the multiply happens. So, if you just keep track of how many times you’ve done a multiply of images you will know how many of these scaling constants were added in and you can undo that effect mathematically.