Image processing 二进制图像上的快速像素计数-ARM neon intrinsics-iOS开发
有人能告诉我一个快速的函数来计算二值图像中的白色像素数吗。我需要它用于iOS应用程序开发。我直接在定义为Image processing 二进制图像上的快速像素计数-ARM neon intrinsics-iOS开发,image-processing,ios5,arm,simd,neon,Image Processing,Ios5,Arm,Simd,Neon,有人能告诉我一个快速的函数来计算二值图像中的白色像素数吗。我需要它用于iOS应用程序开发。我直接在定义为 bool *imageData = (bool *) malloc(noOfPixels * sizeof(bool)); 我正在实现这个功能 int whiteCount = 0; for (int q=i; q<i+windowHeight; q++) { for
bool *imageData = (bool *) malloc(noOfPixels * sizeof(bool));
我正在实现这个功能
int whiteCount = 0;
for (int q=i; q<i+windowHeight; q++)
{
for (int w=j; w<j+windowWidth; w++)
{
if (imageData[q*W + w] == 1)
whiteCount++;
}
}
int-whiteCount=0;
对于(int q=i;q
第6.55.3.6节
矢量化算法将为您进行比较,并将它们放在一个结构中,但您仍然需要遍历该结构的每个元素,并确定它是否为零
该循环当前运行的速度有多快?您需要它运行的速度有多快?还请记住,NEON将在与浮点单元相同的寄存器中工作,因此在此处使用NEON可能会强制进行FPU上下文切换。首先,您可以通过分解乘法和去掉分支来稍微加快原始代码的速度:
int whiteCount = 0;
for (int q = i; q < i + windowHeight; q++)
{
const bool * const row = &imageData[q * W];
for (int w = j; w < j + windowWidth; w++)
{
whiteCount += row[w];
}
}
(这假设imageData[]
是真正的二进制,imageWidth我来看看这个,但是sizeof(bool)
是什么?还有,imageData[]
中可能的值是什么?它只是0还是1,或者有其他非零值吗?对不起,这里有几个输入错误(现已修复)-我应该提到,这是未经测试的代码,因此可能需要进一步的工作-我只是想介绍一下一般的想法。很抱歉再次打扰您..但我在uint8x16_t v=vld1q_u8(&row[j])行收到一个错误,表示-无法初始化const unit8_t*的变量(也称为unsigned char*)使用const bool*类型的值-知道问题可能是什么吗?我使用bool*作为图像数据,因为我假设它会更快,因为它每分钟只需要1位内存value@Shreyas:我在上面问过你sizeof(bool)是什么,你没有回答-通常是sizeof(char)或sizeof(int)-现在我假设它是sizeof(char)(即1字节),但如果不正确,则需要修改代码或使用合适的1字节类型(例如uint8_t)。否-这将使实现更容易溢出-我在上面添加了一个更新版本,用于处理white==255的情况。
#include <arm_neon.h>
// ...
int i, w;
int whiteCount = 0;
uint32x4_t v_count = { 0 };
for (q = i; q < i + windowHeight; q++)
{
const bool * const row = &imageData[q * W];
uint16x8_t vrow_count = { 0 };
for (w = j; w <= j + windowWidth - 16; w += 16) // SIMD loop
{
uint8x16_t v = vld1q_u8(&row[j]); // load 16 x 8 bit pixels
vrow_count = vpadalq_u8(vrow_count, v); // accumulate 16 bit row counts
}
for ( ; w < j + windowWidth; ++w) // scalar clean up loop
{
whiteCount += row[j];
}
v_count = vpadalq_u16(v_count, vrow_count); // update 32 bit image counts
} // from 16 bit row counts
// add 4 x 32 bit partial counts from SIMD loop to scalar total
whiteCount += vgetq_lane_s32(v_count, 0);
whiteCount += vgetq_lane_s32(v_count, 1);
whiteCount += vgetq_lane_s32(v_count, 2);
whiteCount += vgetq_lane_s32(v_count, 3);
// total is now in whiteCount
#include <arm_neon.h>
// ...
int i, w;
int whiteCount = 0;
const uint8x16_t v_mask = { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 };
uint32x4_t v_count = { 0 };
for (q = i; q < i + windowHeight; q++)
{
const uint8_t * const row = &imageData[q * W];
uint16x8_t vrow_count = { 0 };
for (w = j; w <= j + windowWidth - 16; w += 16) // SIMD loop
{
uint8x16_t v = vld1q_u8(&row[j]); // load 16 x 8 bit pixels
v = vandq_u8(v, v_mask); // mask out all but LS bit
vrow_count = vpadalq_u8(vrow_count, v); // accumulate 16 bit row counts
}
for ( ; w < j + windowWidth; ++w) // scalar clean up loop
{
whiteCount += (row[j] == 255);
}
v_count = vpadalq_u16(v_count, vrow_count); // update 32 bit image counts
} // from 16 bit row counts
// add 4 x 32 bit partial counts from SIMD loop to scalar total
whiteCount += vgetq_lane_s32(v_count, 0);
whiteCount += vgetq_lane_s32(v_count, 1);
whiteCount += vgetq_lane_s32(v_count, 2);
whiteCount += vgetq_lane_s32(v_count, 3);
// total is now in whiteCount