将YUV420P像素数据的周围加上边框

master
dezhihuang 2017-10-17 17:49:49 +08:00
parent 2cf608b44c
commit a920f59218
4 changed files with 138 additions and 0 deletions

View File

@ -102,6 +102,24 @@ yuv420_split.cpp 程序中的函数可以将YUV420P数据中的Y、U、V三个
## 将YUV420P像素数据的周围加上边框
> 说明本程序将距离图像边缘border范围内的像素的Y分量、U分量、Y分量的取值设置成了最大值255。
调用方法:
> ./yuv420p_border ./mediadata/lena_256x256_yuv420p.yuv 256 256 30
上述代码运行后将会把一张分辨率为256x256的名称为lena_256x256_yuv420p.yuv的YUV420P格式的像素数据文件处理成名称为output_420p_border.yuv的YUV420P格式的像素数据文件。输入的原图如下所示。
![](./images/yuv420p.png) ![](./images/yuv420p_border.png)
<br />
<br />
参考:[视音频数据处理入门RGB、YUV像素数据处理](http://blog.csdn.net/leixiaohua1020/article/details/50534150)
![](./images/leixiaohua_avDataProcess.png)

BIN
images/yuv420p.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 160 KiB

BIN
images/yuv420p_border.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 102 KiB

120
yuv420p_border.cpp Normal file
View File

@ -0,0 +1,120 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef int BOOL;
#define TRUE 1
#define FALSE 0
//分离YUV420P像素数据中的Y、U、V分量
BOOL yuv420p_border(const char *file, int width, int height, int border)
{
if (file == NULL) {
return FALSE;
}
FILE *fp = fopen(file, "rb+");
FILE *fp0 = fopen("./out/output_420p_border.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);
//给Y分量添加边框
for(int i=0; i<height; i++) {
for(int j=0; j<width; j++) {
//添加左右边框
if (j < border) {
data[i*width + j] = 255;
continue;
}
if (j >= width-border) {
data[i*width + j] = 255;
continue;
}
//添加上下边框
if (i < border) {
data[i*width + j] = 255;
continue;
}
if (i >= width-border) {
data[i*width + j] = 255;
continue;
}
}
}
int tmpBorder = border/2;
int tmpW = width/2;
int tmpH = height/2;
//给U分量添加边框
int uDataStart = width*height;
for(int i=0; i<tmpH; i++) {
for(int j=0; j<tmpW; j++) {
//添加左右边框
if (j < tmpBorder) {
data[uDataStart + i*tmpW + j] = 255;
continue;
}
if (j >= tmpW-tmpBorder) {
data[uDataStart + i*tmpW + j] = 255;
continue;
}
//添加上下边框
if (i < tmpBorder) {
data[uDataStart + i*tmpW + j] = 255;
continue;
}
if (i >= tmpW-tmpBorder) {
data[uDataStart + i*tmpW + j] = 255;
continue;
}
}
}
//给V分量添加边框
int vDataStart = width*height*5/4;
for(int i=0; i<tmpH; i++) {
for(int j=0; j<tmpW; j++) {
//添加左右边框
if (j < tmpBorder) {
data[vDataStart + i*tmpW + j] = 255;
continue;
}
if (j >= tmpW-tmpBorder) {
data[vDataStart + i*tmpW + j] = 255;
continue;
}
//添加上下边框
if (i < tmpBorder) {
data[vDataStart + i*tmpW + j] = 255;
continue;
}
if (i >= tmpW-tmpBorder) {
data[vDataStart + i*tmpW + j] = 255;
continue;
}
}
}
fwrite(data, 1, width*height*3/2, fp0);
free(data);
fclose(fp);
fclose(fp0);
return TRUE;
}
int main(int argc, char *argv[])
{
if (argc == 5) {
yuv420p_border(argv[1], atoi(argv[2]), atoi(argv[3]), atoi(argv[4]));
}
return 0;
}