[Video and Audio Data Processing] 生成灰階測試圖

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_graybar(int width, int height, int ymin, int ymax, int barnum, 
const char* url_out) {
	int barwidth;  //每個灰階的寬度
	float lum_inc;
	unsigned char lum_temp;
	int uv_width, uv_height;
	FILE* fp = NULL;
	unsigned char* data_y = NULL;
	unsigned char* data_u = NULL;
	unsigned char* data_v = NULL;
	int t = 0, i = 0, j = 0;

	barwidth = width / barnum; //獲取每個灰階的寬度
	lum_inc = ((float)(ymax - ymin)) / ((float)(barnum - 1)); 
	//根據灰階獲取平均亮度28.333,若以後面的main函數來調用的話
	uv_width = width / 2; //uv分量的寬度
	uv_height = height / 2; // UV分量的高

	data_y = (unsigned char*)malloc(width * height); //給y分量/亮度分量,申請內存空間
	data_u = (unsigned char*)malloc(uv_width * uv_height); //給u分量,色度分量申請內存空間
	data_v = (unsigned char*)malloc(uv_width * uv_height); //給V分量,色度分量申請內存空間

	if ((fp = fopen(url_out, "wb+")) == NULL) {
		printf("Error: Cannot create file!");
		return -1;
	}

	//Output Info
	printf("Y, U, V value from picture's left to right:\n");
	for (t = 0; t < (width / barwidth); t++) {
		lum_temp = ymin + (char)(t * lum_inc);
		printf("%3d, 128, 128\n", lum_temp); //打印出平均的亮度
	}
	//Gen Data
	for (j = 0; j < height; j++) {
		for (i = 0; i < width; i++) {
			t = i / barwidth; //t是int型,恰好可以如下從0,遞增的分配亮度
			lum_temp = ymin + (char)(t * lum_inc);
			data_y[j * width + i] = lum_temp;
		}
	}
	for (j = 0; j < uv_height; j++) {
		for (i = 0; i < uv_width; i++) {
			data_u[j * uv_width + i] = 128; 
			//由於目的是生成灰階圖,所以把U、V分量的色度設置爲0(128即轉換前的0)
		}
	}
	for (j = 0; j < uv_height; j++) {
		for (i = 0; i < uv_width; i++) {
			data_v[j * uv_width + i] = 128;
			//由於目的是生成灰階圖,所以把U、V分量的色度設置爲0(128即轉換前的0)
		}
	}
	fwrite(data_y, width * height, 1, fp);
	fwrite(data_u, uv_width * uv_height, 1, fp);
	fwrite(data_v, uv_width * uv_height, 1, fp);
	fclose(fp);
	free(data_y);
	free(data_u);
	free(data_v);
	return 0;
}




int main()
{
	simplest_yuv420_graybar(640, 360, 0, 255, 10, "graybar_640x360.yuv");
    return 0;
}

1. 效果如下:

在這裏插入圖片描述

參考鏈接:

https://blog.csdn.net/leixiaohua1020/article/details/50534150

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