How do I get a .NET Bitmap and an OpenCV image to point to the same chunk of memory? I know that I can copy from one to the other, but I would prefer that they just point to the same pixels.
Bonus points for a solution using Emgu.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Following is just a theory and might not work, I’m not very experienced with opencv/emgucv
Both
System.Drawing.BitmapandEmgu.CV.Imagehave constructors takingscan0as an argument. You can allocate memory for image and pass this pointer to that constructors.memory can be allocated by
var ptr = Marshal.AllocHGlobal(stride*height)(dont forget to free ofc) or by allocating a managed array of sufficent size and aquiring its address. Address can be aquired following way:This also “pins” array, so it cannot be moved by garbage collector and address won’t change.
We are going to use these constructors:
widthandheightarguments are self-explanatory.formatfor bitmap isPixelFormat.Format24bppRgb.strideis amount of bytes for single line of image, it also must be aligned (be a multiple of 4). You can get it this way:And
scan0is a pointer to our memory block.So I suggest that after creating
System.Drawing.BitmapandEmgu.CV.Imageusing these constructors will use the same memory block. If not, it means that opencv or Bitmap or both copy provided memory block and possible solution (if it exists) is definetly not worth efforts.