shimat / opencvsharp

OpenCV wrapper for .NET
Apache License 2.0
5.22k stars 1.13k forks source link

Why is Csharp imdecode faster than C++ #1553

Closed lsd1994 closed 1 year ago

lsd1994 commented 1 year ago

Summary of your issue

I run imdecode on both c++ and csharp, but csharp is faster than c++, is that right? (In my opinion, csharp just call c++ dll so it should slower than or the same speed as c++)

Environment

win 11, opencv 4.7, .net4.8

What did you do when you faced the problem?

Write here

Example code:

c++

    std::string img_path = "E:/1.jpg";
    cv::Mat cvimg = cv::imread(img_path);
    double start, end;
    double tot_cv = 0;
    int times = 100;
    std::vector<uchar> buf;
    cv::imencode(".jpg", cvimg, buf);
    cv::Mat t1 = cv::imdecode(buf, IMREAD_ANYCOLOR);
    for (int i = 0; i < times; i++) {
        start = static_cast<double>(cv::getTickCount());
        cv::Mat cvres = cv::imdecode(buf, IMREAD_ANYCOLOR);
        end = static_cast<double>(cv::getTickCount());
        tot_cv += (end - start) * 1000 / cv::getTickFrequency();
    }
    std::cout << "imdecode avg time : " << tot_cv / times << std::endl;

csharp

            Bitmap map = new Bitmap(@"E:/1.jpg");
            string base64 = SaveJpegImage_ToMemory(map, 75); //same as encode
            byte[] arrbase64 = System.Convert.FromBase64String(base64);
            Stopwatch sw = new Stopwatch();

            OpenCvSharp.Mat m1 = OpenCvSharp.Cv2.ImDecode(arrbase64, OpenCvSharp.ImreadModes.AnyColor);
            sw.Restart();
            for(int i=0;i<100;i++)
            {
                m1 = OpenCvSharp.Cv2.ImDecode(arrbase64, OpenCvSharp.ImreadModes.AnyColor);
            }
            sw.Stop();
            System.Console.WriteLine("opencv: {0}", sw.ElapsedMilliseconds/100);

Output:

On my computer, c++ runs 34ms while csharp runs 16ms. It means csharp imdecode doesn't call c++ imdecode actually? Can anyone explain this? Thanks.

What did you intend to be?

lsd1994 commented 1 year ago

Solve this problem by compiling jpeg-turbo library with nasm, thus ensure simd is used to accelerate encode/decode process.