This project has moved and is read-only. For the latest updates, please go here.

Channel Operations

Jun 1, 2015 at 11:13 PM
I need to apply this formula: (blue channel - red channel) / (blue channel + red channel) to some images. The problem is that this formula ranges from -1 to 1, and i have somehow to offset this range to positive values and map it to a full grayscale from 0 to max of ushort.
I managed to achieve the result by iterating pixel by pixel and applying all the required calculations but i dont think it is efficient and i was wondering if it was possible to do direct calculations between the channels. Something like this:
            using(MagickImage img = new MagickImage(imgPath))
                using(MagickImage imgBlue = img.Separate(Channels.Blue).First())
                using (MagickImage imgRed = img.Separate(Channels.Red).First())
            using(MagickImage imgBlue = new MagickImage(bluePath))
            using(MagickImage imgRed = new MagickImage(redPath))
                imgBlue.Composite(imgRed, CompositeOperator.MinusDst);
            using (MagickImage imgBlue = new MagickImage(bluePath))
            using (MagickImage imgRed = new MagickImage(redPath))
                imgBlue.Composite(imgRed, CompositeOperator.Plus);
            using (MagickImage imgBlue_add_Red = new MagickImage(blue_add_redPath))
            using (MagickImage imgBlue_sub_Red = new MagickImage(blue_sub_redPath))
                imgBlue_sub_Red.Composite(imgBlue_add_Red, CompositeOperator.DivideDst);
The problem with this is that all negative values are truncated to 0.
Is it possible to offset the negative values so that -1 corresponds to 0 and 1 to the maxium quantum range?
Jun 2, 2015 at 7:37 AM
You cannot have out of range values. Have you tried using Fx? You can find some information about that here:
Jun 2, 2015 at 9:58 AM
dlemstra wrote:
You cannot have out of range values. Have you tried using Fx? You can find some information about that here:
yes i did, but with the same results: negative values are truncated to 0. I used the following fx: (u.b-u.r)/(u.b+u.r). But the fx way is extremely slow and resource consuming, even compared with the pixel-by-pixel analysis.

Well, i guess i have to stick to the pixel-by-pixel iteration.

Jun 2, 2015 at 6:41 PM
You might also want to take a look at the hdri build. It uses a lit of memory but it can have out of bounds values. It will be clamped when the image is written.