C ++ OpenCV解码缓慢 [英] C++ OpenCV imdecode slow

查看:549
本文介绍了C ++ OpenCV解码缓慢的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将图像的字节数组从C#发送到C ++库.我使用OpenCV(版本3.3.1)对图像进行解码.BMP图像的解码速度很快,而JPEG图像的速度很慢.

I send a byte array of an image from C# to a C++ Library. I decode the image with OpenCV (Version 3.3.1) BMP images are fast in decoding but JPEG images are slow.

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

How i can speed up the decoding time for JPEG images? (Multithreading, 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);
}

推荐答案

例如,您可以使用其他编解码器

You can use a other codec for example

  • https://github.com/libjpeg-turbo/libjpeg-turbo
  • http://www.fastcompression.com/news/2013-cuda-jpeg-codec.htm
  • https://software.intel.com/en-us/intel-ipp

有关此主题的有趣文章
http://www.briancbecker.com/blog/2010 /analysis-of-jpeg-decoding-speeds/

An interesting article on this Topic
http://www.briancbecker.com/blog/2010/analysis-of-jpeg-decoding-speeds/

使用libjpeg turbo构建OpenCV 3.0.0吗?
http://answers.opencv .org/question/68681/how-to-build-opencv-300-with-libjpeg-turbo/

Build OpenCV 3.0.0 with libjpeg turbo?
http://answers.opencv.org/question/68681/how-to-build-opencv-300-with-libjpeg-turbo/

这篇关于C ++ OpenCV解码缓慢的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆