Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# OpenCV
Hi
 
How to plot an image in an inside another larger image using OpenCV?
 
Thank you
Posted 19-Sep-12 9:32am
Edited 19-Sep-12 10:25am
Nueman8.2K
v2

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

This is an example code to plot a small image on to a big image by using C# (for Format8bppIndexed only). I do not use OpenCV, however, you may write your own code in OpenCV with the hint inside this example.
 
 
// This is for Format8bppIndexed image. You may write your own if you are using BGR or other formats
        // based on this code example. Good luck!
        private static Bitmap PlotSmallImageOnBigImage(Bitmap bigImage, Bitmap smallImage)
        {
            // Error
            Rectangle r1 = new Rectangle(0, 0, bigImage.Width, bigImage.Height);
            Rectangle r2 = new Rectangle(0, 0, smallImage.Width, smallImage.Height);
            if (r1.Contains(r2) == false)
                throw new Exception("ERROR: small image must be inside the big image.");
 
            int offsetWidth = (bigImage.Width-smallImage.Width)/2;
            int offsetHeight = (bigImage.Height-smallImage.Height)/2;
            Rectangle rect = new Rectangle(offsetWidth, offsetHeight, smallImage.Width, smallImage.Height);
            BitmapData bigData = bigImage.LockBits(rect, ImageLockMode.ReadWrite, PixelFormat.Format8bppIndexed);
            BitmapData smallData=smallImage.LockBits(new Rectangle(0,0, smallImage.Width, smallImage.Height), ImageLockMode.ReadOnly,
                PixelFormat.Format8bppIndexed);
 
            unsafe
            {
                byte* ptrB = (byte*)(void*)bigData.Scan0;
                int offsetB = bigData.Stride - rect.Width * 1; // see "1", this is for Format8bppIndexed. If BGR it is 3 

                byte* ptrS = (byte*)(void*)smallData.Scan0;               
                int offsetS = smallData.Stride - smallData.Width * 1;
                for (int y = 0; y < rect.Height; y++)
                {
                    for (int x = 0; x < rect.Width; x++)
                    {
                        ptrB[0] = ptrS[0]; // in BGR you may add transparency options here.
                        //ptrB[0] = (byte)((ptrB[0] + ptrS[0])/2);
                        ptrB++;
                        ptrS++;
                    }
                    ptrB += offsetB; // if any
                    ptrS += offsetS;
                }
            }
            bigImage.UnlockBits(bigData);
            smallImage.UnlockBits(smallData);
            return bigImage;
        }
 

 
  Permalink  
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 428
1 Sergey Alexandrovich Kryukov 253
2 PhilLenoir 115
3 RajeeshMenoth 110
4 Peter Leow 100
0 Sergey Alexandrovich Kryukov 6,465
1 OriginalGriff 5,926
2 CPallini 2,473
3 Richard MacCutchan 1,687
4 Abhinav S 1,540


Advertise | Privacy | Mobile
Web04 | 2.8.140821.2 | Last Updated 19 Sep 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100