How to get a .NET bitmap and OpenCV image to point to the same piece of memory?

How do I get a .NET bitmap and an OpenCV image to point to the same piece of memory? I know that I can copy from one to another, but I would prefer that they just point to the same pixels.

Bonus points for solving using Emgu.

+1
source share
3 answers

The following is just a theory and may not work, I'm not very experienced with opencv / emgucv


Both System.Drawing.Bitmap and Emgu.CV.Image have constructors that take scan0 as an argument. You can allocate memory for the image and pass this pointer to these constructors. memory can be allocated var ptr = Marshal.AllocHGlobal(stride*height) (do not forget about free) or by allocating a managed array of sufficient size and access to its address. The address can be purchased as follows:

 var array = new byte[stride*height]; var gch = GCHandle.Alloc(array, GCHandleType.Pinned); var ptr = gch.AddrOfPinnedObject(); 

It also "issues" the array, so it cannot be moved by the garbage collector, and the address will not change.

We will use these constructors:

 Bitmap(int width, int height, int stride, PixelFormat format, IntPtr scan0); Image<Bgr, Byte>(int width, int height, int stride, IntPtr scan0); 

width and height arguments are self explanatory. format for the PixelFormat.Format24bppRgb PixelFormat.Format24bppRgb . stride - the number of bytes for one line of the image, it should also be aligned (be a multiple of 4). This can be done like this:

 var stride = (width * 3); var align = stride % 4; if(align != 0) stride += 4 - align; 

And scan0 is a pointer to our memory block.

Therefore, I suggest that after creating System.Drawing.Bitmap and Emgu.CV.Image using these constructors, the same memory block will be used. If not, this means that opencv or Bitmap, or both the blocks provided by the copy, and the possible solution (if one exists) are definitely not worth the effort.

+5
source

From source Emgu.CV :

 // Summary: // The Get property provide a more efficient way to convert Image<Gray, Byte>, // Image<Bgr, Byte> and Image<Bgra, Byte> into Bitmap such that the image data // is shared with Bitmap. If you change the pixel value on the Bitmap, you change // the pixel values on the Image object as well! For other types of image this // property has the same effect as ToBitmap() Take extra caution not to use // the Bitmap after the Image object is disposed The Set property convert the // bitmap to this Image type. public Bitmap Bitmap { get; set; } 

Basically, for certain types of images, you can simply use the .Bitmap property of the .Bitmap image, which will then point directly to the CV image data.

Example:

 string filename; // ... point filename to your file of choice ... Image<Bgr, byte> cvImg = new Image<Bgr, byte>(filename); Bitmap netBmp = cvImg.Bitmap; 
+2
source

Not sure if this helps, but you can get a pointer from an Emgu image using .Ptr

eg. IntPtr ip = img1.Ptr;

0
source

Source: https://habr.com/ru/post/970366/


All Articles