C++取消引用 char-Pointer(图像数组)非常慢
我在快速访问无符号字符数组时遇到一些问题。
我想实际将 BGRABGRA....BGRABGRA.... 逐行编码图像数组复制到使用三层的 OpenCV 版本。下面的代码工作正常,但速度非常慢(640*480 图像大约需要 0.5 秒)。我指出取消引用运算符 * 会使速度变慢。您有计划如何解决这个问题吗? (提示:BYTE 是一个无符号字符)
// run thorugh all pixels and copy image data
for (int y = 0; y<imHeight; y++){
BYTE* pLine= vrIm->mp_buffer + y * vrIm->m_pitch;
for (int x = 0; x<imWidth; x++){
BYTE* b= pLine++; // fast pointer operation
BYTE* g= pLine++;
BYTE* r= pLine++;
BYTE* a= pLine++; // (alpha)
BYTE bc = *b; // this is really slow!
BYTE gc = *g; // this is really slow!
BYTE rc = *r; // this is really slow!
}
}
谢谢!
I have some trouble getting fast access to an unsigned character array.
I want to actually copy a BGRABGRA....BGRABGRA.... linewise coded image array to the OpenCV-version which uses three layers. The code below works fine but is really slow (around 0.5 seconds for a 640*480 image). I pointed out that the dereferencing operator * makes it slow. Do you have any plan how to fix this? (Hint: BYTE is an unsigned char)
// run thorugh all pixels and copy image data
for (int y = 0; y<imHeight; y++){
BYTE* pLine= vrIm->mp_buffer + y * vrIm->m_pitch;
for (int x = 0; x<imWidth; x++){
BYTE* b= pLine++; // fast pointer operation
BYTE* g= pLine++;
BYTE* r= pLine++;
BYTE* a= pLine++; // (alpha)
BYTE bc = *b; // this is really slow!
BYTE gc = *g; // this is really slow!
BYTE rc = *r; // this is really slow!
}
}
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不应该 - 除非您在 8086 上执行此操作,否则 640x480 不可能花费 0.5 秒。是否还有其他代码未显示?目标内存当前不会去任何地方
ps 看一下 cvCvtColor() 它使用优化的 SSE2/SIMD 指令来执行此操作
Shouldn't be - there is no way that is taking 0.5sec for a 640x480 unless you are doing this on a 8086. Is there some other code you aren't showing? The destination memory doesn't currently go anywhere
ps take a look at cvCvtColor() it uses optimized SSE2/SIMD instructions to do this
您正在读取的内存位于什么硬件上?也许该设备所使用的内存带宽有限,或者 RAM 速度较慢。如果内存由许多设备共享,则其访问也可能存在瓶颈。尝试使用 memcpy() 将整个屏幕(?)读取到本地内存,在本地 RAM 中对其执行操作,然后使用 memcpy() 将其写回。这会将您必须协商访问它的次数从 640*480 减少到 1 次。
What hardware is the memory you're reading located on? Perhaps that device has limited bandwidth to the memory it uses or just has slow RAM. If the memory is shared by many devices there may also be bottle necks on it's access. Try reading the entire screen(?) to local memory using memcpy(), performing your operations on it in local RAM, then writing it back using memcpy(). This will reduce the number of times you must negotiate access to it from 640*480 to 1.