1.缩放
分辨率太高会导致算法耗时增加,同时一些变换耗时也会增加很多,因为分辨率越大,byte[]也就越大,所以很多时候我们都需要进行缩放操作,具体代码如下,我也是网上找到,出处已经不知道了,某些地方做过一些小优化,当然能找到原作更好,因为对于C++我懂的也很少。
void nv12_nearest_scale(uint8_t *__restrict src, uint8_t *__restrict dst,
int srcWidth, int srcHeight, int dstWidth, int
dstHeight) //restrict keyword is for compiler to optimize program
{
register int sw = srcWidth; //register keyword is for local var to accelorate
register int sh = srcHeight;
register int dw = dstWidth;
register int dh = dstHeight;
register int y, x;
unsigned long int srcy, srcx, src_index;
unsigned long int xrIntFloat_16 = (sw << 16) / dw + 1; //better than float division
unsigned long int yrIntFloat_16 = (sh << 16) / dh + 1;
uint8_t *dst_uv = dst + dh * dw; //memory start pointer of dest uv
uint8_t *src_uv = src + sh * sw; //memory start pointer of source uv
uint8_t *dst_uv_yScanline;
uint8_t *src_uv_yScanline;
uint8_t *dst_y_slice = dst; //memory start pointer of dest y
uint8_t *src_y_slice;
uint8_t *sp;
uint8_t *dp;
for (y = 0; y < (dh & ~7); ++y) //'dh & ~7' is to generate faster assembly code
{
srcy = (y * yrIntFloat_16) >> 16;
src_y_slice = src + srcy * sw;
if ((y & 1) == 0) {
dst_uv_yScanline = dst_uv + (y / 2) * dw;
src_uv_yScanline = src_uv + (srcy / 2) * sw;
}
for (x = 0; x < (dw & ~7); ++x) {
srcx = (x * xrIntFloat_16) >> 16;
dst_y_slice[x] = src_y_slice[srcx];
if ((y & 1) == 0) //y is even
{
if ((x & 1) == 0) //x is even
{
src_index = (srcx / 2) * 2;
sp = dst_uv_yScanline + x;
dp = src_uv_yScanline + src_index;
*sp = *dp;
++sp;
++dp;
*sp = *dp;
}
}
}
dst_y_slice += dw;
}
}
2.旋转
这个旋转算法是可以通过omp加速的,需要使用的话,加上头文件,添加“#pragma omp parallel for”就可以了,通过omp的多核运算,能很大程度提高速度,但同时对cpu资源的消耗也会很大。
#include <omp.h>
void YUV420spRotate90(unsigned char *dst, unsigned char *src, int imageWidth, int imageHeight) {
// Rotate the Y luma
int i = 0;
for (int x = 0; x < imageWidth; x++) {
for (int y = imageHeight - 1; y >= 0; y--) {
dst[i] = src[y * imageWidth + x];
i++;
}
}
// Rotate the U and V color components
i = imageWidth * imageHeight * 3 / 2 - 1;
for (int x = imageWidth - 1; x > 0; x = x - 2) {
for (int y = 0; y < imageHeight / 2; y++) {
dst[i] = src[(imageWidth * imageHeight) + (y * imageWidth) + x];
i--;
dst[i] = src[(imageWidth * imageHeight) + (y * imageWidth) + (x - 1)];
i--;
}
}
}
3.NV21转BGR
这个用的应该很多吧,现在图像处理算法大部分基于OpenCV,很多时候都得转换成BGR再进行处理,由于这个代码太多了,就不一一粘贴,后面会把.h文件放上来,直接下载看就可以了。
到这儿基本上图像处理就差不多结束了,从我们拿到预览数据,到缩放旋转,再到转化成BGR,一些列操作,每个步骤都会产生耗时,而且还会消耗CPU资源,如果再加个镜像操作就更大了,后面我会讲怎么直接一步到位,优化耗时。
文件地址:https://download.csdn.net/download/renlei0012/10988868,一个文件懒得放git了,没分的留下邮箱就行了