[Video and Audio Data Processing] YUV420P像素数据的亮度减半

本文介绍了一种将YUV420P格式的视频文件转换为灰度图的方法,通过C语言实现,代码示例展示了如何读取YUV420P文件,对亮度通道进行处理,生成灰度图像,并保存到新的YUV文件中。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

0. 修改之后,可以直接编译的代码如下

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_halfy(const char* url, int w, int h, int num) {
    FILE* fp = fopen(url, "rb+");
    FILE* fp1 = fopen("output_half.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);
        //Half
        for (int j = 0; j < w * h; j++) { //前w*h的数据是存亮度的
            unsigned char temp = pic[j] / 2;
            //printf("%d,\n",temp);
            pic[j] = temp;
        }
        fwrite(pic, 1, w * h * 3 / 2, fp1);
    }

    free(pic);
    fclose(fp);
    fclose(fp1);

    return 0;
}




int main()
{
    simplest_yuv420_halfy("lena_256x256_yuv420p.yuv", 256, 256, 1);
    return 0;
}

1. 效果如下

原图:

在这里插入图片描述

效果如下:

在这里插入图片描述

参考链接:

https://blog.youkuaiyun.com/leixiaohua1020/article/details/50534150

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值