yuv的采样为4:4:4格式,分别对Y、U、V三个分量的情况进行统计,打印输出。
/***************************************************
* Imhist.cpp -- calculate the Histogram of YUV
* Image,
*
* Purpose:
* This file calculate the histogram of yuv
* image, the sampling rate is 4:4:4,
*
* History: 2019.8.15
**************************************************/
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
int main(int argc, char **argv)
{
FILE *pYUV = fopen("dump.yuv", "rb"); /* 以rb方式打开dump.yuv文件 */
if (!pYUV)
{
printf("open filed!\n");
return -1;
}
int width = 1440; // 图像宽
int height = 900; // 图像高
int PlaneSize = width*height; // 每个通道的大小
int lineByte = (8 * width / 8 + 3) / 4 * 4 * 3; // 每一行的字节数
unsigned char *pYUVBump = new unsigned char[lineByte * height]; // 临时分配的字节空间数
fread(pYUVBump, 1, lineByte*height, pYUV); // 从pYUV中读取1个linebyte*height大小的字节流
double stYUV[3][256] = { { 0.0 }, { 0.0 }, { 0.0 } }; // 定义每个通道各个像素的多少
int temp; // 临时像素值
int site; // Y、U、V临时位置
int i;
int j;
/* 统计各通道各灰度频数 */
for (i = 0; i < height; ++i)
{
for (j = 0; j < width*3; ++j)
{
temp = *(pYUVBump + i*lineByte + j);
site = (j + 1) % 3; //区分Y、U、V
if (site == 0)
stYUV[2][temp] = stYUV[2][temp] + 1;
else
stYUV[site - 1][temp] = stYUV[site - 1][temp] + 1;
}
}/*end for*/
/* 计算各通道各灰度概率 */
double fenmu = (double)(width*height); // 定义每个通道的像素总数
double dStYuv[3][256] = { { 0.0 }, { 0.0 }, { 0.0 } }; // 定义每个通道各个像素的频率
for (i = 0; i < 3; ++i)
{
for (j=0; j < 256; ++j)
{
dStYuv[i][j] = stYUV[i][j] / fenmu;
}
}/* end for*/
/* 统计情况打印输出 */
for (j = 0; j < 3; ++j)
{
switch (j)
{
case 0:
printf("Y通道统计情况:\n"); break;
case 1:
printf("U通道统计情况:\n"); break;
case 2:
printf("V通道统计情况:\n"); break;
default:
break;
}
for (int i = 0; i < 256; ++i)
{
printf("%f\t", dStYuv[j][i]);
}
printf("\n\n\n");
}
delete(pYUVBump); // 释放堆空间
fclose(pYUV); // 关闭文件
system("pause");
return(0);
}/* end main */