【性能优化】优化笔记之一:图像RGB与YUV转换优化
本文主要介绍如何优化您自己的CODE,实现软件的加速。我们一个图象模式识别的项目,需要将RGB格式的彩色图像先转换成黑白图像。图像转换的公式如下:
#deinfe SIZE 256#define XSIZE 640#define YSIZE 480#define IMGSIZE XSIZE * YSIZEtypedef struct RGB{ unsigned char r; unsigned char g; unsigned char b;}RGB;struct RGB in[IMGSIZE]; unsigned char out[IMGSIZE * 3]; unsigned short Y_R[SIZE],Y_G[SIZE],Y_B[SIZE],U_R[SIZE],U_G[SIZE],U_B[SIZE],V_R[SIZE],V_G[SIZE],V_B[SIZE]; //查表数组void table_init(){ int i; for(i = 0; i < SIZE; i++){ Y_R[i] = (i * 1224) >> 12; //Y Y_G[i] = (i * 2404) >> 12; Y_B[i] = (i * 467) >> 12; U_R[i] = (i * 602) >> 12; //U U_G[i] = (i * 1183) >> 12; U_B[i] = (i * 1785) >> 12; V_R[i] = (i * 2519) >> 12; //V V_G[i] = (i * 2109) >> 12; V_B[i] = (i * 409) >> 12; }} inline void calc_lum(){ int i; for(i = 0; i < IMGSIZE; i += 2) { out[i] = Y_R[in[i].r] + Y_G[in[i].g] + Y_B[in[i].b]; //Y out[i + IMGSIZE] = U_B[in[i].b] - U_R[in[i].r] - U_G[in[i].g]; //U out[i + 2 * IMGSIZE] = V_R[in[i].r] - V_G[in[i].g] - V_B[in[i].b]; //V out[i + 1] = Y_R[in[i + 1].r] + Y_G[in[i + 1].g] + Y_B[in[i + 1].b]; //Y out[i + 1 + IMGSIZE] = U_B[in[i + 1].b] - U_R[in[i + 1].r] - U_G[in[i + 1].g]; //U out[i + 1 + 2 * IMGSIZE] = V_R[in[i + 1].r] - V_G[in[i + 1].g] - V_B[in[i + 1].b]; //V }}
这种算法应该是非常快的了.
===========================================================转载请注明出处:http://blog.csdn.net/songzitea/article/details/10474207