[Video and Audio Data Processing] YUV420P像素數據的亮度減半

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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章