[TOC]

概述

文章参考:https://blog.csdn.net/Yemiekai/article/details/108009701

ARGB转gray

为了比较性能,现在用neon和纯C方法比较一下将彩色图片转成灰度的时间

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
//纯C函数
void method_argb2gray_c(AndroidBitmapInfo info, void *pixels) {
// rgb转灰度值公式
// Gray = (R*38 + G*75 + B*15) >> 7
cv::TickMeter tm1;
tm1.start();
uint32_t *pixel = NULL;
int a = 0, r = 0, g = 0, b = 0;
int rows=info.height;
int cols=info.width;

for (int y = 0; y < rows; ++y) {
for (int x = 0; x < cols; ++x) {
pixel = (uint32_t *) pixels + info.width * y + x;
a = (*pixel & 0xFF000000) >> 24;
r = (*pixel & 0x00FF0000) >> 16;
g = (*pixel & 0x0000FF00) >> 8;
b = (*pixel & 0x000000FF) >> 0;
int gray = (r * 38 + g * 75 + b * 15) >> 7;

*pixel = ((a << 24) | (gray << 16) | (gray << 8) | gray);
}
}
tm1.stop();
LOGI("method_argb2gray_c time: %lf", tm1.getTimeMilli());
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
//neon函数
void method_argb2gray_neon(AndroidBitmapInfo info, void *pixels) {
// Gray = (R*38 + G*75 + B*15) >> 7
TickMeter tm3;
tm3.start();
unsigned short *dst = (unsigned short *) pixels;
unsigned char *src = (unsigned char *) pixels;
uint8x8_t r = vdup_n_u8(38);
uint8x8_t g = vdup_n_u8(75);
uint8x8_t b = vdup_n_u8(15);
uint16x8_t alp = vdupq_n_u16(255 << 8);

uint16x8_t temp;
uint8x8_t gray;
uint8x8x4_t argb;
uint16x8_t hight;
uint16x8_t low;
uint16x8x2_t res;
int i, size = info.height * info.width / 8;

for (i = 0; i < size; ++i) {

//获取r、g、b值,计算灰度值
argb = vld4_u8(src);
temp = vmull_u8(argb.val[1], r);
temp = vmlal_u8(temp, argb.val[2], g);
temp = vmlal_u8(temp, argb.val[3], b);
gray = vshrn_n_u16 (temp, 7);
src += 8 * 4;

//赋值4通道argb
hight = vorrq_u16(alp, vmovl_u8(gray));
low = vorrq_u16(vshlq_n_u16(vmovl_u8(gray), 8), vmovl_u8(gray));
res = vzipq_u16(low, hight);
vst1q_u16(dst, res.val[0]);
dst += 8;
vst1q_u16(dst, res.val[1]);
dst += 8;

}
tm3.stop();
LOGI("method_argb2gray_neon time: %lf", tm3.getTimeMilli());
}