[Video and Audio Data Processing] 分離RGB24像素數據中的R、G、B分量

0. Visual Studio 2019可編譯通過的代碼如下:

RGB24格式的每個像素的三個分量是連續存儲的。一幀寬高分別爲w、h的RGB24圖像一共佔用wh3 Byte的存儲空間。

RGB24格式規定首先存儲第一個像素的R、G、B,然後存儲第二個像素的R、G、B…以此類推,所以
後面的代碼就一個簡單的for循環遍歷,依次把數據取出來。

上述調用函數的代碼運行後,將會把一張分辨率爲500x500的名稱爲cie1931_500x500.rgb的RGB24格式的像素數據文件分離成爲三個文件:

output_r.y:R數據,分辨率爲500x500。
output_g.y:G數據,分辨率爲500x500。
output_b.y:B數據,分辨率爲500x500。

extern "C"
{
#ifdef __cplusplus
#define __STDC_CONSTANT_MACROS

#endif

}
extern "C" {

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
}



/**
 * Calculate PSNR between 2 YUV420P file
 * @param url1     Location of first Input YUV file.
 * @param url2     Location of another Input YUV file.
 * @param w        Width of Input YUV file.
 * @param h        Height of Input YUV file.
 * @param num      Number of frames to process.
 */
 /**
  * Split R, G, B planes in RGB24 file.
  * @param url  Location of Input RGB file.
  * @param w    Width of Input RGB file.
  * @param h    Height of Input RGB file.
  * @param num  Number of frames to process.
  *
  */
int simplest_rgb24_split(const char* url, int w, int h, int num) {
	FILE* fp = fopen(url, "rb+");
	FILE* fp1 = fopen("output_r.y", "wb+");
	FILE* fp2 = fopen("output_g.y", "wb+");
	FILE* fp3 = fopen("output_b.y", "wb+");

	unsigned char* pic = (unsigned char*)malloc(w * h * 3);

	for (int i = 0; i < num; i++) {

		fread(pic, 1, w * h * 3, fp);

		for (int j = 0; j < w * h * 3; j = j + 3) {
			//R
			fwrite(pic + j, 1, 1, fp1); //就RGB數據是依次存的,這樣就依次取嘛..
			//G
			fwrite(pic + j + 1, 1, 1, fp2);
			//B
			fwrite(pic + j + 2, 1, 1, fp3);
		}
	}

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

	return 0;
}




int main()
{
	simplest_rgb24_split("cie1931_500x500.rgb", 500, 500, 1);
    return 0;
}

1. 運行結果如下:

原圖:
在這裏插入圖片描述

在這裏插入圖片描述

R分量:

分辨率Y、U、V都設置爲500*500,像素格式選擇Y分量。
在這裏插入圖片描述

在這裏插入圖片描述

G分量:

在這裏插入圖片描述

B分量:

在這裏插入圖片描述

參考鏈接:

  1. https://blog.csdn.net/leixiaohua1020/article/details/50534150
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章