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#
0 comments No comments
{count} votes

Accepted answer
  1. Daniel Zhang-MSFT 9,651 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 Answers by the question author, which helps users to know the answer solved the author's problem.