1. 正文
extern "C"
{
#ifdef __cplusplus
#define __STDC_CONSTANT_MACROS
#endif
}
extern "C" {
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
}
/**
* Split Y, U, V planes in YUV444P file.
* @param url Location of Input YUV file.
* @param w Width of Input YUV file.
* @param h Height of Input YUV file.
* @param num Number of frames to process.
*
*/
/**
* Convert YUV420P file to gray picture
* @param url Location of Input YUV file.
* @param w Width of Input YUV file.
* @param h Height of Input YUV file.
* @param num Number of frames to process.
*/
int simplest_yuv420_border(const char* url, int w, int h, int border, int num) {
FILE* fp = fopen(url, "rb+");
FILE* fp1 = fopen("output_border.yuv", "wb+");
unsigned char* pic = (unsigned char*)malloc(w * h * 3 / 2);
for (int i = 0; i < num; i++) {
fread(pic, 1, w * h * 3 / 2, fp);
//Y
for (int j = 0; j < h; j++) {
for (int k = 0; k < w; k++) {
if (k<border || k>(w - border) || j<border || j>(h - border)) {
pic[j * w + k] = 0; //边界值
//pic[j*w+k]=0;
}
}
}
fwrite(pic, 1, w * h * 3 / 2, fp1);
}
free(pic);
fclose(fp);
fclose(fp1);
return 0;
}
int main()
{
simplest_yuv420_border("lena_256x256_yuv420p.yuv", 256, 256, 20, 1);
return 0;
}
原图:

效果如下:

2. 若边界值修改为255
extern "C"
{
#ifdef __cplusplus
#define __STDC_CONSTANT_MACROS
#endif
}
extern "C" {
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
}
/**
* Split Y, U, V planes in YUV444P file.
* @param url Location of Input YUV file.
* @param w Width of Input YUV file.
* @param h Height of Input YUV file.
* @param num Number of frames to process.
*
*/
/**
* Convert YUV420P file to gray picture
* @param url Location of Input YUV file.
* @param w Width of Input YUV file.
* @param h Height of Input YUV file.
* @param num Number of frames to process.
*/
int simplest_yuv420_border(const char* url, int w, int h, int border, int num) {
FILE* fp = fopen(url, "rb+");
FILE* fp1 = fopen("output_border.yuv", "wb+");
unsigned char* pic = (unsigned char*)malloc(w * h * 3 / 2);
for (int i = 0; i < num; i++) {
fread(pic, 1, w * h * 3 / 2, fp);
//Y
for (int j = 0; j < h; j++) {
for (int k = 0; k < w; k++) {
if (k<border || k>(w - border) || j<border || j>(h - border)) {
pic[j * w + k] = 255;
//pic[j*w+k]=0;
}
}
}
fwrite(pic, 1, w * h * 3 / 2, fp1);
}
free(pic);
fclose(fp);
fclose(fp1);
return 0;
}
int main()
{
simplest_yuv420_border("lena_256x256_yuv420p.yuv", 256, 256, 20, 1);
return 0;
}
效果如下:

参考如下:
https://blog.youkuaiyun.com/leixiaohua1020/article/details/50534150
本文介绍了一个使用C语言实现的简单程序,该程序读取YUV420P格式的视频帧,并对图像边界进行特定值填充处理。通过修改边界像素值为0或255,可以观察到不同的视觉效果。
3336

被折叠的 条评论
为什么被折叠?



