Click here to Skip to main content
15,891,657 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have two images two images which are 8 bit indexed, I am trying to perform the following calculations on them:

1. Multiply both images
2. Total of all pixel values from result of step 1.

SrcBitmap1
http://i.stack.imgur.com/xQe1B.png[^]

SrcBitmap2
http://i.stack.imgur.com/9mBHi.png[^]

ResultBitmap:

http://i.stack.imgur.com/PFTna.png[^]



below is my code, and for some reason, I am getting color images after multiplying two gray images. Can't figure out what is going on. Any advise is greatly appreciated.

C#
public Bitmap MaskImage(Bitmap SrcBitmap1, Bitmap SrcBitmap2)
    {
        int width;
        int height;

        //Message = "Impossible.";

        if (SrcBitmap1.Width < SrcBitmap2.Width)
            width = SrcBitmap1.Width;
        else
            width = SrcBitmap2.Width;

        if (SrcBitmap1.Height < SrcBitmap2.Height)
            height = SrcBitmap1.Height;
        else
            height = SrcBitmap2.Height;

        Bitmap bitmap = new Bitmap(width, height);

        try
        {
            BitmapData Src1Data = SrcBitmap1.LockBits(new Rectangle(0, 0, SrcBitmap1.Width, SrcBitmap1.Height), ImageLockMode.ReadOnly, PixelFormat.Format8bppIndexed);

            BitmapData Src2Data = SrcBitmap2.LockBits(new Rectangle(0, 0, SrcBitmap2.Width, SrcBitmap2.Height), ImageLockMode.ReadOnly, PixelFormat.Format8bppIndexed);

            BitmapData DestData = bitmap.LockBits(new Rectangle(0, 0, bitmap.Width, bitmap.Height), ImageLockMode.WriteOnly, PixelFormat.Format8bppIndexed);

            unsafe
            {
                //Following is list of offset for different bit images
                //8 bit : 1
                //16 bit : 2
                //24 bit : 3 and
                //32 bit : 4
                int xOffset = 1;

                for (int col = 0; col < bitmap.Height - 1; col++)
                {
                    byte* Src1Ptr = (byte*)Src1Data.Scan0 + col * Src1Data.Stride;
                    byte* Src2Ptr = (byte*)Src2Data.Scan0 + col * Src2Data.Stride;
                    byte* DestPtr = (byte*)DestData.Scan0 + col * DestData.Stride;

                    for (int row = 0; row < bitmap.Width - 1; row++)
                    {
                        byte i1 = Src1Ptr[row * xOffset];
                        bbyte i2 = Src2Ptr[row * xOffset];
                        DestPtr[row * xOffset + 0] = (byte)((((ushort)(i1) * (ushort)(i2)) >> 8));
                        DestPtr[row * xOffset + 1] = (byte)((ushort)(((ushort)(i1) * (ushort)(i2)) >> 8));
                        DestPtr[row * xOffset + 2] = (byte)((ushort)(((ushort)(i1) * (ushort)(i2)) >> 8));
                    }
                }
            }

            bitmap.UnlockBits(DestData);
            SrcBitmap1.UnlockBits(Src1Data);
            SrcBitmap2.UnlockBits(Src2Data);

            SrcBitmap1.Dispose();
            SrcBitmap2.Dispose();
        }
        catch (Exception ex)
        {
            Console.Write(ex);
        }

        return bitmap;
    }
Posted

1 solution

I don't think you can multiply images directly in indexed mode. I would advise to convert the image to some additive color model, perform the operation, and, if you really need indexed representation, re-index the resulting image. Please see:
https://en.wikipedia.org/wiki/Color_model[^],
https://en.wikipedia.org/wiki/Additive_color[^].

—SA
 
Share this answer
 
Comments
Member 11823791 13-Jul-15 21:12pm    
http://softwarebydefault.com/2013/04/27/image-arithmetic/

It can be done. I have used this code for test, cant reproduce the same results for the same Images. Cant understand why.
Sergey Alexandrovich Kryukov 13-Jul-15 23:38pm    
Not clear what can you reproduce and what not. And how would you define multiplication on indices. You formally multiply some pixel values. But does it produce something reasonable? And how you know that the result is correct or not?
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900