I would like to use photoshop to create accurate residual images (image describing the difference between two images) and to apply such residual images to restore one of the images used to create the residual.
given two 8bit images (pixel values in the range [0, 255]) a residual image would be created by subtracting one image from the other:
Residual = Image A - Image B
as the potential range of this residual is [-255, 255] it must be mapped to [0, 255] to be saved as image:
Residual = (Image A - Image B) / 2 + 127
given the residual image and image B, image A can then be restored as
Image A = Image B + (Residual - 127) * 2
so my question is: are these two processes (creating a residual image and restoring image A) possible using blending modes, and if not, can i program it somehow? I already tried it using PixelBender, but the PixelBender photoshop plugin doesn't seem to work for pixel bender kernels that take two input images.
Try using a combination of the Levels command (not adjustment layer) and Image > Apply Image. Because you're compressing and then re-expanding the tonality, you will lose accuracy due to rounding errors. Maybe it could be more accurate using 32-bit images, but many of the operations in Apply Image don't work on these.
16 bits / channel could yield a happy medium.
If you want mathematical precision, switching to a linear color space would be helpful I think.
Difference blending can yield a result that shows you the difference between two images, but you lose the sign (i.e., your pixel values get the absolute value of the difference). You might be able to do something where you offset the pixel values in hte first image to middle gray before using Difference, then you'd retain the sign.
Overlay blending could potentially yield a result something like you're looking for in your second equation.
I've done stuff using these blending modes in Actions, for example in the process of isolating noise from an image so as to be able to cancel it out.
precision is not an issue. I intend to lose 1bit of precision by mapping from [-255,255] to [0,255].
The issue is that i cannot achieve the correct mathematical operations.
If i offset pixels first by adding a 127 gray, then all values above 127 in the source image get clamped.
while overlay blending is doing something similar, the mathematical equation applies multiplication and inverse multiplication instead of addition/subtraction.
I need exact results as this is for some paper. i can use an own program to calculate these things but for convenience i'd like to be able to do this directly in photoshop.
How exactly are you doing such things with actions? is it possible to iterate over pixels and apply mathematical equations on them using actions?
In order for 127 to be "middle gray" you'll need to go to a linear color space.
Actions just operate the Photoshop functions available to us. Armed with an understanding of how the data is represented, and what the various functions do, one can manipulate images with them. You may ultimately end up with an action that can repeat the steps you want to do on multiple images.
For example, if you want to offset image values by 127, rather than add a middle gray, you'll probably want to use a Curves function and pull up the black point to 127, leaving the white point alone. You lose another bit of precision, and as I mentioned before switching to 16 bits/channel mode early on in the process might be a good idea.
What I was thinking was to run Levels on the two images. On one, set the Black Point (down in th 'Ouput Levels' field) to 128, and in the other, set the White Point to 127. This does the division before the subtraction. This is algebraically the same: (A - B) /2 = (A / 2) - (B / 2) [Distributaive Law].
The image with the altered Whit Point will look dark, since its range has been compressed to 0 - 127. The image with the altered Black Point will look light, with a range fron 128 - 255. Note that the range is compressed, not clipped.
Then, subtract the darker from the lighter with Apply Image. Since the lightest pixel of the dark image is darker than the darkest pixel of the light image, the result will always be > 0.
Okay, I got it to work. After doing as I posted above, go to the darker image 'B'. Run Apply Image, using the residual image, with a blending mode of 'Add'. Then, run Levels, with the Shadows field in the 'Input levels' area set to 127 (or should it be 128).
Compare the two Histograms of the original and recovered images