将YUV420P像素数据的亮度减半

master
dezhihuang 2017-10-18 10:13:48 +08:00
parent 1adeabc57d
commit a571e3a1e4
2 changed files with 58 additions and 0 deletions

View File

@ -124,5 +124,21 @@ yuv420_split.cpp 程序中的函数可以将YUV420P数据中的Y、U、V三个
## 将YUV420P像素数据的亮度减半
>说明如果打算将图像的亮度减半只要将图像的每个像素的Y值取出来分别进行除以2的工作就可以了。图像的每个Y值占用1 Byte取值范围是0至255对应C语言中的unsigned char数据类型。
调用方法:
> ./yuv420p_half_y ./mediadata/lena_256x256_yuv420p.yuv 256 256
上述代码运行后将会把一张分辨率为256x256的名称为lena_256x256_yuv420p.yuv的YUV420P格式的像素数据文件处理成名称为output_420p_half_y.yuv的YUV420P格式的像素数据文件。
<br />
<br />
参考:[视音频数据处理入门RGB、YUV像素数据处理](http://blog.csdn.net/leixiaohua1020/article/details/50534150)
![](./images/leixiaohua_avDataProcess.png)

42
yuv420p_half_y.cpp Normal file
View File

@ -0,0 +1,42 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef int BOOL;
#define TRUE 1
#define FALSE 0
//分离YUV420P像素数据中的亮度分量Y的数值减半降低图像的亮度
BOOL yuv420p_half_y(const char *file, int width, int height)
{
if (file == NULL) {
return FALSE;
}
FILE *fp = fopen(file, "rb+");
FILE *fp0 = fopen("./out/output_420p_half_y.yuv", "wb+");
unsigned char *data = (unsigned char *)malloc(width*height*3/2);
memset(data, 0, width*height*3/2);
fread(data, 1, width*height*3/2, fp);
for(int i=0; i<width*height; i++) {
data[i] = data[i] / 2; //Y分量减半
}
fwrite(data, 1, width*height*3/2, fp0);
free(data);
fclose(fp);
fclose(fp0);
return TRUE;
}
int main(int argc, char *argv[])
{
if (argc == 4) {
yuv420p_half_y(argv[1], atoi(argv[2]), atoi(argv[3]));
}
return 0;
}