Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/image/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
c#组合两个图像,其中包含彼此的部分_C#_Image - Fatal编程技术网

c#组合两个图像,其中包含彼此的部分

c#组合两个图像,其中包含彼此的部分,c#,image,C#,Image,我有两张照片,其中包含了彼此的一部分: 现在我想将它们结合在一起,但不重复公共部分,以获得类似的内容: 最好的方法是什么 @编辑:这些图像只是我想对任何两个图像执行的操作的示例,它们包含彼此的一部分,并且该部分位于第一个图像的底部,第二个图像的顶部。以下是解决您问题的方法: 我定义了一个函数,它接受2个位图,并返回组合的位图 ,在此函数中,我将第二个位图的前2行存储在字节数组中,以便在下一步将它们与第一个位图进行比较,然后我开始在第一个位图中查找匹配的行,每当我在其中定义匹配的行时,我存储y

我有两张照片,其中包含了彼此的一部分:

现在我想将它们结合在一起,但不重复公共部分,以获得类似的内容:

最好的方法是什么


@编辑:这些图像只是我想对任何两个图像执行的操作的示例,它们包含彼此的一部分,并且该部分位于第一个图像的底部,第二个图像的顶部。

以下是解决您问题的方法:

我定义了一个函数,它接受2个
位图
,并返回组合的
位图
,在此函数中,我将第二个位图的前2行存储在
字节数组中,以便在下一步将它们与第一个位图进行比较,然后我开始在第一个位图中查找匹配的行,每当我在其中定义匹配的行时,我存储
y
位置 ,现在我根据已找到的
y
组合这些位图

以下是演示项目:

以下是解决方案文件:

以下是的结果:

  • 例1

  • 例2

下面是函数:

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;
    }
}
专用位图组合图像(位图bmp_1、位图bmp_2)
{
如果(bmp_1.Width==bmp_2.Width)
{
int bmp_1_高度、bmp宽度、bmp_2_高度;
bmpWidth=bmp_1.Width;
bmp_1_高度=bmp_1.0高度;
bmp_2_Height=bmp_2.Height;
颜色c;
bool notFound=false;
int firstMatchedRow=0;
字节[,]bmp2_first2rows=新字节[3*bmpWidth,2];
对于(int b=0;b<2;b++)
{
对于(int a=0;a0)
{
位图bmp=新位图(bmpWidth,firstMatchedRow+bmp_2_高度);
Graphics g=Graphics.FromImage(bmp);
矩形RectDst=新矩形(0,0,bmpWidth,firstMatchedRow);
矩形RectSrc;
g、 DrawImage(bmp_1,RectDst,RectDst,GraphicsUnit.Pixel);
RectDst=新矩形(0,firstMatchedRow,bmpWidth,bmp_2_高度);
RectSrc=新矩形(0,0,bmpWidth,bmp_2_高度);
g、 DrawImage(bmp_2、RectDst、RectSrc、GraphicsUnit.Pixel);
返回bmp;
}
其他的
{
返回null;
}
}
其他的
{
返回null;
}
}
最后我要提到的是,这些精彩的教程在图像处理方面对我帮助很大:


希望有帮助:)

我们需要关于这些图片的更多信息,它们是否总是包含3种像这样的纯色?如果你告诉我更多关于它们的信息,我可以帮你看看我回答的这个问题:好的,我刚刚更新了我的问题,它们应该如何扩展和合并?在您的示例中,融合图像中的红色比原始图像中的红色多,这正常吗?抱歉,Imageshack自动调整了第一幅图像的大小。每个图像的宽度应该相同。它们不应该延伸,太好了!而且工作得很快;)非常感谢你,现在试着去理解它