Click here to Skip to main content
15,885,127 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C#
public Bitmap GetAveragedBitmap()
    {

        int numberOfImages = _imageCollection.Count;
        Bitmap bmpresult = null;


        if (numberOfImages > 1)
        {
            Stopwatch clock = new Stopwatch();
            clock.Start();
            BitmapData[] bmpData = new BitmapData[numberOfImages];
            Bitmap[] bit = new Bitmap[numberOfImages];
            int width = 0;
            int height = 0;

            for (int index = 0; index < numberOfImages; index++)
            {
                bmpData[index] = _imageCollection[index].LockBits(new Rectangle(0, 0, _imageCollection[index].Width, _imageCollection[index].Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
                if (index > 0)
                {
                    if (bmpData[index].Width > width)
                    {
                        width = bmpData[index].Width;
                    }
                    if (bmpData[index].Height > height)
                    {
                        height = bmpData[index].Height;
                    }
                }
                else
                {
                    width = bmpData[0].Width;
                    height = bmpData[0].Height;
                }

                _imageCollection[index].UnlockBits(bmpData[index]);




            }//end of for loop
            for (int index = 0; index < numberOfImages; index++)
            {
                bit[index] = new Bitmap(_imageCollection[index], width, height);

            }

            bmpresult = new Bitmap(width, height);
            BitmapData[] data = new BitmapData[numberOfImages];
            for (int index = 0; index < numberOfImages; index++)
            {
                data[index] = bit[index].LockBits(new Rectangle(0, 0, bit[index].Width, bit[index].Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
            }
            data[1].Reserved = 1;
            BitmapData dataResult = bmpresult.LockBits(new Rectangle(0, 0, bmpresult.Width, bmpresult.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);

            unsafe
            {
                int[] remain = new int[numberOfImages];
                byte*[] ptr = new byte*[numberOfImages];
                long totalIntensity = 0;

                for (int index = 0; index < numberOfImages; index++)
                {
                    ptr[index] = (byte*)data[index].Scan0;
                    remain[index] = data[index].Stride - data[index].Width * 3;
                }
                byte* ptrResult = (byte*)dataResult.Scan0;
                //for resultant image
                int remainResult = dataResult.Stride - dataResult.Width * 3;


                for (int i = 0; i < height; i++)
                    for (int j = 0; j < width * 3; j++)
                    {
                        for (int index = 0; index < numberOfImages; index++)
                        {
                            totalIntensity += (int)(ptr[index][0]);
                            ptr[index]++;
                        }
                    }

                int average = (int)totalIntensity / (numberOfImages * height * width);
                //reset the pointer
                for (int index = 0; index < numberOfImages; index++)
                {
                    ptr[index] = (byte*)data[index].Scan0;
                }

                for (int i = 0; i < height; i++)
                {
                    for (int j = 0; j < width * 3; j++)
                    {

                        int result = 1;
                        for (int k = 0; k < numberOfImages; k++)
                        {

                            result = (ptr[k][0] * result);

                        }

                       // result = ptr[0][0] + result * (2 - 1);

                        ptrResult[0] = (byte)(result / average);

                        for (int index = 0; index < numberOfImages; index++)
                        {
                            ptr[index]++;
                        }
                        ptrResult++;

                    }
                    for (int index = 0; index < numberOfImages; index++)
                    {
                        ptr[index] += remain[index];

                    }

                    ptrResult += remainResult;

                }

                //  }//end of for loop

            }//end of unsafe

            for (int index = 0; index < numberOfImages; index++)
            {
                bit[index].UnlockBits(data[index]);
            }
            bmpresult.UnlockBits(dataResult);
            clock.Stop();
            Debug.WriteLine("Time for average " + clock.ElapsedMilliseconds.ToString());

        }
        return bmpresult;



    }


What I have tried:

I have tried the above code and getting the wrong result(IncorrectOutput.jpg).I am attaching collection of images.And also I have attached expected image(Expected Output.jpg).
Link for the images
Collection Of Images
Posted
Updated 3-Jun-16 1:14am
v6
Comments
Bernhard Hiller 2-Jun-16 4:13am    
What do you mean by "average of bitmaps"? I do not understand that.
BillWoodruff 2-Jun-16 4:57am    
Performing any arbitrary computation on various bitmap images data is straightforward, but the question here is: what are you trying to do ?
Member 12299560 2-Jun-16 6:23am    
we have collection of images. From those collection of images we want to display single image by calculating the average of collection of all bitmap images
Member 12299560 2-Jun-16 7:10am    
This code is static for 5 images.But I want to do it for multiple images dynamically.
Actually I have implemented the code for dynamic but if I take images more than 12 then it gives wrong image
Patrice T 2-Jun-16 11:18am    
Are you asking for help on this code because you have a problem on another code ?

if this Code does what you want (whatever this is - the "avarage" of a Bitmap...), just Change the Input Parameter to an Bitmap Array or list, and instead of doing everything 5 times do it in a Loop - I can't imagine what can be your problem with that - can you eleborate? Other from that I would recomend to split this up a Llttle: First calculate "biggest" image needed, then calculate Pixels, ... whatever you do here...
[EDIT: and get rid of your Magic numbers... replace with meaningful constants)
 
Share this answer
 
v2
The average of some values is sum of values divided by number of values.
At best this code is weird:
C#
ptrResult[0] = (byte)(((ptr1[0] + ptr2[0] + ptr3[0]) + (2 - 1) * ptr4[0] * ptr5[0]) / average);

replacing with
C#
ptrResult[0] = (byte)((ptr1[0] + ptr2[0] + ptr3[0] + ptr4[0] + ptr5[0]) / 5);

should be better.
 
Share this answer
 
Comments
Member 12299560 6-Jun-16 6:17am    
No its not a feasible solution

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