2017-12-27 472 views
0

我發送一個C#圖像的字節數組到C++庫。我用OpenCV(版本3.3.1)解碼圖像BMP圖像解碼速度快,但JPEG圖像速度慢。C++ OpenCV imdecode slow

我如何加快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); 
} 
+1

JPG格式使用壓縮算法來減少圖像大小,而通常BMP不會。 – Silencer

+0

我知道但有500%的差異?而在C#jpeg類似的問題獲勝? –

+0

您是否正在運行調試版本?你用什麼優化選項編譯? –

回答