Convert int/byte[] or Bitmap to the Mat type of openCv

Ahmed Wassim BEN SALEM (ENISo) 241 Reputation points
2021-04-21T12:08:57.223+00:00

Hello Guys , is there any way to convert array or bitmap or whatever to The type Mat of openCv ..
I have a image object and i want to make usable for openCv , so first i need to convert it to Mat

Developer technologies | C#
Developer technologies | C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
0 comments No comments
{count} votes

Answer accepted by question author
  1. Daniel Zhang-MSFT 9,661 Reputation points
    2021-04-22T06:15:31.4+00:00

    Hi AhmedWassimBENSALEMENISo-3626,
    To use OpenCvSharp, you need to add both OpenCvSharp4 and OpenCvSharp4.runtime.* packages to your project.
    90185-opencv.png
    Right-click your project namre->Manager NuGet Packages->Browse-> Search and Install them.
    Then you can achieve it via BitmapConverter.ToMat() method.
    Here is my test code you can refer to.

    Image img = Image.FromFile(@"C:\Users\Pictures\home.PNG");  
    Bitmap bmpScreenshot = new Bitmap(img);  
    var imageMat = OpenCvSharp.Extensions.BitmapConverter.ToMat(bmpScreenshot);  
    

    Best Regards,
    Daniel Zhang


    If the response is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentationto enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.