使用emgu cv将两个子图像添加到一个新图像中

时间:2015-04-07 09:41:46

标签: c# opencv emgucv

我有两个不同尺寸的图像,我想创建另一个大图像,包括它们。

private Image<Gray, Byte> newImage(Image<Gray, Byte> image1, Image<Gray, Byte> image2)
    {
        int ImageWidth = 0;
        int ImageHeight = 0;

   //get max width
        if (image1.Width > image2.Width)
            ImageWidth = image1.Width;
        else
            ImageWidth = image2.Width;

  //calculate new height
        ImageHeight = image1.Height + image2.Height;

 //declare new image (large image).
        Image<Gray, Byte> imageResult = new Image<Gray, Byte>(ImageWidth, ImageHeight);


        imageResult.ROI = new Rectangle(0, 0, image1.Width, image1.Height);
        image1.CopyTo(imageResult);
        imageResult.ROI = new Rectangle(0, image1.Height, image2.Width, image2.Height);
        image2.CopyTo(imageResult);



        return imageResult;
    }

返回的图片是黑色图片,不包含两张图片,请帮我解决问题所在?

感谢。

2 个答案:

答案 0 :(得分:4)

你的方法是正确的。您只需删除ROI即可。最后添加:

imageResult.ROI = Rectangle.Empty;

最终结果应如下所示:

imageResult.ROI = new Rectangle(0, 0, image1.Width, image1.Height);
image1.CopyTo(imageResult);
imageResult.ROI = new Rectangle(0, image1.Height, image2.Width, image2.Height);
image2.CopyTo(imageResult);
imageResult.ROI = Rectangle.Empty;

答案 1 :(得分:2)

以下解决方案:

private Image<Gray, Byte> newImage(Image<Gray, Byte> image1, Image<Gray, Byte> image2)
{
    int ImageWidth = 0;
    int ImageHeight = 0;

//get max width
    if (image1.Width > image2.Width)
        ImageWidth = image1.Width;
    else
        ImageWidth = image2.Width;

//calculate new height
    ImageHeight = image1.Height + image2.Height;

//declare new image (large image).
    Image<Gray, Byte> imageResult;

    Bitmap bitmap = new Bitmap(Math.Max(image1.Width , image2.Width), image1.Height + image2.Height);
        using (Graphics g = Graphics.FromImage(bitmap))
        {
            g.DrawImage(image1.Bitmap, 0, 0);
            g.DrawImage(image2.Bitmap, 0, image1.Height);

        }

        imageResult = new Image<Gray, byte>(bitmap);



    return imageResult;
}