2015-04-07 88 views
0

將兩個子圖像添加到一個新圖像中我有兩個不同尺寸的圖像,我想創建另一個包含它們的大圖像垂直。使用emgu cv

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; 
    } 

返回的圖像是一個黑色的圖像,不包含這兩個圖像,請幫助我哪裏的問題?

謝謝。

+0

什麼問題? – GPPK

+0

我解決了這個問題,正確的方法是把我的答案放在這裏或刪除我的問題帖子或什麼? – Abdo

+0

將其發佈爲答案 – GPPK

回答

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; 
} 
+0

此解決方案不適用於大型位圖(10600瓦x 9980小時) –

3

你的做法是正確的。您只需刪除投資回報率。只是在結尾處加上:

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;