[Video and Audio Data Processing] YUV420P 图像加边框

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值