文章目錄
- 一、實驗任務
- 二、實驗思路
- 三、代碼部分
- 1.rgb
- 2.yuv
- 四、結果分析
- 1.rgb的熵及概率分布圖
- 2.yuv的熵及概率分布圖
- 3.結論
一、實驗任務
分析rgb和yuv檔案的三個通道的概率分布,并計算各自的熵(編程實作),
注:
1. down.rgb和down.yuv兩個檔案的解析度均為256*256,
2. yuv為4:2:0采樣空間,
3. 存盤格式:rgb檔案按每個像素BGR分量依次存放;YUV格式按照全部像素的Y資料塊、U資料塊和V資料塊依次存放,
二、實驗思路
1. 分別讀取R、G、B(或Y、U、V)到陣列中,
- RGB檔案 按每個像素BGR分量依次存放,
- YUV檔案 按照全部像素的Y資料塊、U資料塊和V資料塊依次存放,本實驗為4:2:0采樣空間,
4:2:0是指水平和垂直 Y 各取樣兩個點,UV 各只取樣一個點,水平的取樣比例是 2:1,重直的取樣比例 2:1,也就是色度和亮度差 1/2 * 1/2 = 1/4,
2. 分別統計R、G、B(或Y、U、V)三通道的顏色強度級的頻數,
3. 別計算R、G、B(或Y、U、V)三通道的顏色強度級的概率,將概率寫入檔案,
4. 計算熵并輸出,

三、代碼部分
1.rgb
代碼如下:
#include<iostream>
#include<math.h>
using namespace std;
#define Res 256*256//解析度
int main()
{
unsigned char R[Res] = { 0 }, G[Res] = { 0 }, B[Res] = { 0 }; //定義R、G、B分量
double R1[256] = { 0 }, G1[256] = { 0 }, B1[256] = { 0 }; //定義R、G、B概率分量
double R2 = 0, G2 = 0, B2 = 0; //定義R、G、B的熵
FILE* Picture, * Red, * Green, * Blue;
fopen_s(&Picture, "/Users/cxr/Desktop/資料壓縮/chengxu/Project1/down.rgb", "rb");
fopen_s(&Red, "/Users/cxr/Desktop/資料壓縮/chengxu/Project1/Red.txt", "w");
fopen_s(&Green, "/Users/cxr/Desktop/資料壓縮/chengxu/Project1/Green.txt", "w");
fopen_s(&Blue, "/Users/cxr/Desktop/資料壓縮/chengxu/Project1/Blue.txt", "w");
if (Picture==0)
printf( "讀取圖片失敗!");
else
{
//分別讀取R、G、B到陣列中
unsigned char Array[Res * 3] = { 0 };
fread(Array, 1, Res * 3, Picture);
for (int i = 0, j = 0; i < Res * 3; i = i + 3, j++)
{
B[j] = *(Array + i);
G[j] = *(Array + i + 1);
R[j] = *(Array + i + 2);
}
//分別統計R、G、B三通道的256個顏色強度級的頻數
for (int i = 0; i < Res; i++)
{
R1[R[i]]++;
G1[G[i]]++;
B1[B[i]]++;
}
//分別計算R、G、B三通道的256個顏色強度級的概率
for (int i = 0; i < 256; i++)
{
R1[i] = R1[i] / (Res);
B1[i] = B1[i] / (Res);
G1[i] = G1[i] / (Res);
}
//將概率寫入檔案
for (int i = 0; i < 256; i++)
{
fprintf(Red, "%d\t%f\n", i, R1[i]);
fprintf(Green, "%d\t%f\n", i, G1[i]);
fprintf(Blue, "%d\t%f\n", i, B1[i]);
}
//計算并輸出熵
for (int i = 0; i < 256; i++)
{
if (R1[i] != 0) { R2 += -R1[i] * log(R1[i]) / log(2); }
if (G1[i] != 0) { G2 += -G1[i] * log(G1[i]) / log(2); }
if (B1[i] != 0) { B2 += -B1[i] * log(B1[i]) / log(2); }
}
printf("R的熵為%f\n", R2);
printf("G的熵為%f\n", G2);
printf("B的熵為%f\n", B2);
}
return 0;
}
運行結果如下:

2.yuv
代碼如下:
#include<iostream>
#include<math.h>
using namespace std;
#define Res 256*256//解析度
int main()
{
unsigned char Y[Res] = { 0 }, U[Res/4] = { 0 }, V[Res/4] = { 0 }; //定義Y、U、V分量
double Y1[256] = { 0 }, U1[256] = { 0 }, V1[256] = { 0 }; //定義Y、U、V概率分量
double Y2 = 0, U2 = 0, V2 = 0; //定義Y、U、V的熵
FILE* Picture, * PartY, * PartU, * PartV;
fopen_s(&Picture, "/Users/cxr/Desktop/資料壓縮/chengxu/Project2/down.yuv", "rb");
fopen_s(&PartY, "/Users/cxr/Desktop/資料壓縮/chengxu/Project2/PartY.txt", "w");
fopen_s(&PartU, "/Users/cxr/Desktop/資料壓縮/chengxu/Project2/PartU.txt", "w");
fopen_s(&PartV, "/Users/cxr/Desktop/資料壓縮/chengxu/Project2/PartV.txt", "w");
if (Picture == 0)
printf("讀取圖片失敗!");
else
{
//分別讀取Y、U、V到陣列中
unsigned char Array[98304];
fread(Array, 1, Res * 1.5, Picture);
for (int i = 0; i < Res ; i++)
{
Y[i] = *(Array + i);
}
for (int i = Res; i < Res * 1.25; i++)
{
U[i-65536] = *(Array + i);
}
for (int i = Res * 1.25; i < Res * 1.5; i++)
{
V[i - 81920] = *(Array + i);
}
//分別統計Y、U、V三通道的顏色強度級的頻數
for (int i = 0; i < Res; i++)
{
Y1[Y[i]]++;
}
for (int i = 0; i < (Res/4); i++)
{
U1[U[i]]++;
V1[V[i]]++;
}
//分別計算Y、U、V三通道的256個顏色強度級的概率
for (int i = 0; i < 256; i++)
{
Y1[i] = Y1[i] / (Res);
U1[i] = U1[i] / (Res/4);
V1[i] = V1[i] / (Res/4);
}
//將概率寫入檔案
for (int i = 0; i < 256; i++)
{
fprintf(PartY, "%d\t%f\n", i, Y1[i]);
fprintf(PartU, "%d\t%f\n", i, U1[i]);
fprintf(PartV, "%d\t%f\n", i, V1[i]);
}
//計算并輸出熵
for (int i = 0; i < 256; i++)
{
if (Y1[i] != 0) { Y2 += -Y1[i] * log(Y1[i]) / log(2); }
if (U1[i] != 0) { U2 += -U1[i] * log(U1[i]) / log(2); }
if (V1[i] != 0) { V2 += -V1[i] * log(V1[i]) / log(2); }
}
printf("Y的熵為%f\n", Y2);
printf("U的熵為%f\n", U2);
printf("V的熵為%f\n", V2);
}
return 0;
}
運行結果如下:

四、結果分析
將編程得到的txt檔案匯入Excel表格中,繪制影像,
1.rgb的熵及概率分布圖
| 分量 | R | G | B |
|---|---|---|---|
| 熵 | 7.22955 | 7.17846 | 6.85686 |

2.yuv的熵及概率分布圖
| 分量 | Y | U | V |
|---|---|---|---|
| 熵 | 6.331819 | 5.126402 | 4.113143 |

3.結論
觀察RGB與YUV的概率分布圖,可以看出YUV分量的值更不均勻,所以它的熵更小,該結論與編程計算的熵的結果相符,
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/267452.html
標籤:其他
下一篇:GPS載波同步MATLAB仿真
