我正在为iOS开发一些图像处理应用程序,阈值处理确实是一个巨大的瓶颈。所以我试着用霓虹灯来优化它。下面是函数的C版本。有没有办法用霓虹灯重写这个(不幸的是我在这方面完全没有经验)?
static void thresh_8u( const Image& _src, Image& _dst, uchar thresh, uchar maxval, int type ) {
int i, j;
uchar tab[256];
Size roi = _src.size();
roi.width *= _src.channels();
memset(&tab[0], 0, thresh);
memset(&tab[thresh], maxval, 256-thresh);
for( i = 0; i < roi.height; i++ ) {
const uchar* src = (const uchar*)(_src.data + _src.step*i);
uchar* dst = (uchar*)(_dst.data + _dst.step*i);
j = 0;
for(; j <= roi.width; ++j) {
dst[j] = tab[src[j]];
}
}
}发布于 2012-07-21 06:54:22
如果你能确保你的行宽总是16字节的倍数,这实际上是相当简单的,因为编译器(clang)有表示霓虹灯向量寄存器的特殊类型,并且知道如何对它们应用普通的C运算符。下面是我的小测试函数:
#ifdef __ARM_NEON__
#include <arm_neon.h>
void computeThreshold(void *input, void *output, int count, uint8_t threshold, uint8_t highValue) {
uint8x16_t thresholdVector = vdupq_n_u8(threshold);
uint8x16_t highValueVector = vdupq_n_u8(highValue);
uint8x16_t *__restrict inputVector = (uint8x16_t *)input;
uint8x16_t *__restrict outputVector = (uint8x16_t *)output;
for ( ; count > 0; count -= 16, ++inputVector, ++outputVector) {
*outputVector = (*inputVector > thresholdVector) & highValueVector;
}
}
#endif它一次操作16个字节。uint8x16_t是一个包含16个8位无符号整数的向量寄存器。vdupq_n_u8返回一个向量uint8x16_t,其中填充了16个参数副本。
应用于两个uint8x16_t值的>运算符在成对的8位无符号整数之间执行16次比较。当左边的输入大于右边的输入时,它返回0xff (这与普通的C >不同,后者只返回0x01)。如果左输入小于或等于右输入,则返回0。(它编译成VCGT.U8指令。)
应用于两个uint8x16_t值的&运算符计算128对比特的布尔与。
在发布版本中,循环编译为:
0x6e668: vldmia r2, {d4, d5}
0x6e66c: subs r0, #16
0x6e66e: vcgt.u8 q10, q10, q8
0x6e672: adds r2, #16
0x6e674: cmp r0, #0
0x6e676: vand q10, q10, q9
0x6e67a: vstmia r1, {d4, d5}
0x6e67e: add.w r1, r1, #16
0x6e682: bgt 0x6e668https://stackoverflow.com/questions/11587577
复制相似问题