2

I have 2 pictures, containing part of each other:

enter image description here

now I want to combine them together, but not repeating the common part, to have something like that:

enter image description here

What is the best way to do it?

@edit: That images are only examples of what I want to do with any two images, that contain part of each other, and that part is at the bottom of the first image, and at the top of the second.

Com Piler
  • 257
  • 5
  • 14
  • we need more information about those pics , do they always contain 3 solid colors like this ? if you tell me more about them I can help you take a look at this question I've answered : http://stackoverflow.com/questions/13548962/crop-remove-unwanted-space-at-the-edges-of-image – Mehran Jun 25 '13 at 07:49
  • ok, I've just updated my question – Com Piler Jun 25 '13 at 08:05
  • How are they supposed to extend and merge ? In your example there is more red in the fusion than in the original image, is it normal ? – Alex Jun 25 '13 at 08:45
  • sorry, Imageshack automatically resized the first image. Each image should have the same width. They shouldn't extend. – Com Piler Jun 25 '13 at 09:03

1 Answers1

2

here's a solution for your question :

I defined a function which takes 2 Bitmaps and returns the Combined Bitmap , in this function , I store first 2 rows of the second bitmap in an array of byte so I can compare them with the first bitmap in the next step , then I start looking for the matched rows in the first bitmap whenever I fined the matched row in that , I store the y position , now I combine those bitmaps according to the y I've already found !

Here's the Demo Project : Download

Here's the Solution File : Download

here's the results for :

  • Example 1

enter image description here

  • Example 2

enter image description here

and here's the function :

private Bitmap CombineImages(Bitmap bmp_1, Bitmap bmp_2)
{
    if (bmp_1.Width == bmp_2.Width)
    {
        int bmp_1_Height, bmpWidth, bmp_2_Height;

        bmpWidth = bmp_1.Width;
        bmp_1_Height = bmp_1.Height;
        bmp_2_Height = bmp_2.Height;

        Color c;
        bool notFound = false;
        int firstMatchedRow = 0;

        byte[,] bmp2_first2rows = new byte[3 * bmpWidth, 2];

        for (int b = 0; b < 2; b++)
        {
            for (int a = 0; a < bmpWidth; a++)
            {
                c = bmp_2.GetPixel(a, b);
                bmp2_first2rows[a * 3, b] = c.R;
                bmp2_first2rows[a * 3 + 1, b] = c.G;
                bmp2_first2rows[a * 3 + 2, b] = c.B;
            }
        }

        for (int y = 0; y < bmp_1_Height - 1; y++)
        {
            for (int j = 0; j < 2; j++)
            {
                for (int x = 0; x < bmpWidth; x++)
                {
                    c = bmp_1.GetPixel(x, y + j);
                    if ((bmp2_first2rows[x * 3, j] == c.R) &&
                        (bmp2_first2rows[x * 3 + 1, j] == c.G) &&
                        (bmp2_first2rows[x * 3 + 2, j] == c.B))
                    {
                    }
                    else
                    {
                        notFound = true;
                        break;
                    }
                }
                if (notFound)
                {
                    break;
                }
            }
            if (!notFound)
            {
                firstMatchedRow = y;
                break;
            }
            else
            {
                notFound = false;
            }
        }

        if (firstMatchedRow > 0)
        {
            Bitmap bmp = new Bitmap(bmpWidth, firstMatchedRow + bmp_2_Height);
            Graphics g = Graphics.FromImage(bmp);
            Rectangle RectDst = new Rectangle(0, 0, bmpWidth, firstMatchedRow);
            Rectangle RectSrc;
            g.DrawImage(bmp_1, RectDst, RectDst, GraphicsUnit.Pixel);
            RectDst = new Rectangle(0, firstMatchedRow, bmpWidth, bmp_2_Height);
            RectSrc = new Rectangle(0, 0, bmpWidth, bmp_2_Height);
            g.DrawImage(bmp_2, RectDst, RectSrc, GraphicsUnit.Pixel);
            return bmp;
        }
        else
        {
            return null;
        }
    }
    else
    {
        return null;
    }
}

And Finally I should mention that , these Fantastic Tutorials have Helped me a lot in Image Processing :

Image Processing for Dummies with C# and GDI+

Image Processing using C#

Hope it Helps :)

Mehran
  • 1,409
  • 2
  • 17
  • 27