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.csdn.net/leixiaohua1020/article/details/50534150