C++ OpenCV imdecode 慢

标签 c++ performance opencv jpeg bmp

我将图像的字节数组从 C# 发送到 C++ 库。我用OpenCV(版本3.3.1)解码图像,BMP图像解码速度快,但JPEG图像解码速度慢。

如何加快 JPEG 图像的解码时间? (多线程、GPU,...?)

解码性能

---------------------------------------------------------
| Resolution | Format | Size  | Duration  |             |
---------------------------------------------------------
| 800x600    | BMP    | 2MB   | 0.7 ms    |             |
---------------------------------------------------------
| 800x600    | JPEG   | 10KB  | 4 ms      | 500% slower |
---------------------------------------------------------

OpenCV C++ 方法

VMAPI char* __stdcall SendImage(unsigned char* pArray, int nSize)
{
    cv::Mat buf(1, nSize, CV_8UC1, (void*)pArray);
    auto start = std::chrono::high_resolution_clock::now();
    //cv::Mat input = cv::imdecode(buf, CV_LOAD_IMAGE_COLOR);
    cv::Mat input = cv::imdecode(buf, -1);
    auto finish = std::chrono::high_resolution_clock::now();
    std::chrono::duration<double> elapsed = finish - start;
    std::string result = "Test Version 1.0 - Elapsed time: " + std::to_string(elapsed.count() * 1000) + " s\n";

    return _strdup(result.c_str());
}

C# 请求

[DllImport("VideoModule.dll")]
public static extern string SendImage(IntPtr pArray, int nSize);

static void ProcessImage()
{
    var bitmap = new Bitmap(800, 600);
    using (var graphic = Graphics.FromImage(bitmap))
    {
        graphic.Clear(Color.White);
        graphic.DrawRectangle(new Pen(Color.DarkBlue), 20, 20, 60, 60);
        graphic.DrawRectangle(new Pen(Color.DarkGreen), 200, 200, 60, 60);
        graphic.DrawRectangle(new Pen(Color.Red), 500, 400, 60, 60);
    }

    var memoryStream = new MemoryStream();
    //Return an image in JPEG
    bitmap.Save(memoryStream, ImageFormat.Jpeg);
    //Return an image in BMP
    //bitmap.Save(memoryStream, ImageFormat.Bmp);
    var imageData = memoryStream.GetBuffer();

    var size = Marshal.SizeOf(imageData[0]) * imageData.Length;
    IntPtr pnt = Marshal.AllocHGlobal(size);

    try
    {
        // Copy the array to unmanaged memory.
        Marshal.Copy(imageData, 0, pnt, imageData.Length);
    }
    catch (Exception)
    {
    }

    result = SendImage(pnt, imageData.Length);
    Marshal.FreeHGlobal(pnt);
    Console.WriteLine(result);
}

最佳答案

关于C++ OpenCV imdecode 慢,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47994538/

相关文章:

c++ - 使用迭代器的 std::vector 模板构造函数是否允许转换?

c++ - 删除范围外的变量

c++ - 使用 boost 在文件中查找正则表达式

c - 高效地编写 For 循环

r - 在R中工作时无法在Windows OS中安装RopenCVLite

c++ - 处理二维线段的有效方法

Python、numpy、einsum 将一叠矩阵相乘

java - 如何优化activemq

python - 如何裁剪图像,如果坐标不存在,则延伸另一侧,从而保持裁剪的大小相同?

c++ - 从 cv::Mat 继承,一个好主意?