opencv对图像是软解码_C ++ OpenCV解码缓慢

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.

How i can speed up the decoding time for JPEG images? (Multithreading, GPU, ...?)

Performance of decode

---------------------------------------------------------

| Resolution | Format | Size | Duration | |

---------------------------------------------------------

| 800x600 | BMP | 2MB | 0.7 ms | |

---------------------------------------------------------

| 800x600 | JPEG | 10KB | 4 ms | 500% slower |

---------------------------------------------------------

OpenCV C++ Method

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 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# Request

[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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值