0. 背景
本文基于雷神的博客,基于Visual Studio 2019,实现YUV数据分离…话说没想到又见到了大学系主任,讲数字图像处理时候,用到的Lena图…
1. 下载源代码,编译YUV播放器
从雷神的开源github下载YUVplayer源码,https://github.com/leixiaohua1020/YUVplayer
启动Visual Studio,打开刚才下载解压后的yuvplayer.sln文件.
升级一下工具集,
接下来编译:
接下来在release目录下面会找到应用程序,把它拿到桌面,一会使用。
2. 新建一个新项目
把Lena图片,放到刚才的工程目录下面,备注,Lena图,从以下博客获取:
https://github.com/leixiaohua1020/simplest_mediadata_test
当然你也可以从以下地方获取:
https://www.ece.rice.edu/~wakin/images/ (备注:这个是佛罗里达的原图,并不试用此测试)
然后写代码,参考:
https://blog.youkuaiyun.com/leixiaohua1020/article/details/50534150
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 YUV420P 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.
*
*/
int simplest_yuv420_split(char* url, int w, int h, int num) {
FILE* fp = fopen(url, "rb+");
FILE* fp1 = fopen("output_420_y.y", "wb+");
FILE* fp2 = fopen("output_420_u.y", "wb+");
FILE* fp3 = fopen("output_420_v.y", "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
fwrite(pic, 1, w * h, fp1);
//U
fwrite(pic + w * h, 1, w * h / 4, fp2);
//V
fwrite(pic + w * h * 5 / 4, 1, w * h / 4, fp3);
}
free(pic);
fclose(fp);
fclose(fp1);
fclose(fp2);
fclose(fp3);
return 0;
}
int main()
{
simplest_yuv420_split("lena_256x256_yuv420p.yuv", 256, 256, 1);
return 0;
}
改成x64,然后编译;
>C:\Users\Administrator\source\repos\YUV_Split\split.cpp(28,16):
>error C4996: 'fopen': This function or variable may be unsafe.
>Consider using fopen_s instead. To disable deprecation,
>use _CRT_SECURE_NO_WARNINGS. See online help for details
这个报错解决方法如下:
项目 ->属性,编辑预处理器: 添加 _CRT_SECURE_NO_WARNINGS
1>C:\Users\Administrator\source\repos\YUV_Split\split.cpp(58,66):
error C2664: “int simplest_yuv420_split(char *,int,int,int)”:
无法将参数 1 从“const char [25]”转换为“char *”
int simplest_yuv420_split(const char* url, int w, int h, int num)
参数修改加上const即可解决,最终代码如下:
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 YUV420P 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.
*
*/
int simplest_yuv420_split(const char* url, int w, int h, int num) {
FILE* fp = fopen(url, "rb+");
FILE* fp1 = fopen("output_420_y.y", "wb+");
FILE* fp2 = fopen("output_420_u.y", "wb+");
FILE* fp3 = fopen("output_420_v.y", "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
fwrite(pic, 1, w * h, fp1);
//U
fwrite(pic + w * h, 1, w * h / 4, fp2);
//V
fwrite(pic + w * h * 5 / 4, 1, w * h / 4, fp3);
}
//这里写的方法可参考我之前的博客,420就是这样存的,
//https://blog.youkuaiyun.com/Codeliang666/article/details/106288043
free(pic);
fclose(fp);
fclose(fp1);
fclose(fp2);
fclose(fp3);
return 0;
}
int main()
{
simplest_yuv420_split("lena_256x256_yuv420p.yuv", 256, 256, 1);
return 0;
}
3. 效果
以Y分量来看这些图片:
然后你会发现u分量是下面这样的,一脸懵逼:
原因是分辨率不是256256了,需要手动修改成128128
以下是v 分量的.
OVER! 本文到此结束,感谢阅读!