数字视频处理(二)——直方图均衡化


  解决方案资源管理器界面如下:
在这里插入图片描述

main.cpp

#include <iostream>
#include <cstdio>
#include <fstream>
#include "YHistogram.h"

using namespace std;

#define width 500
#define height 500


int main()
{
	//读入文件
	ifstream inYUV("seed.yuv", ios::binary);
	ofstream outYUV("HisSeed.yuv", ios::binary);
	if (!inYUV) { cout << "error to open file1!" << endl; }
	if (!outYUV) { cout << "error to open file2" << endl; }

	int TotalSize = width * height * 3;
	int YSize = width * height;
	unsigned char* in_YUV = new unsigned char[TotalSize];
	unsigned char* in_Y = new unsigned char[YSize];
	unsigned char* out_Y = new unsigned char[YSize];
	unsigned char* out_YUV = new unsigned char[TotalSize];

	inYUV.read((char*)in_YUV, TotalSize);
	
	//for (int i = 0; i < TotalSize; i++)
	//{
	//	int y = in_YUV[i];
	//}
	//提取Y分量
	EXTRACT(in_YUV, in_Y, width, height);
	
	//直方图均衡化
	Histogram(in_Y, out_Y, width, height);
	
	//UV分量的计算
	Y2YUV(out_Y, out_YUV, width, height);
	
	//写入YUV文件
	outYUV.write((char*)out_YUV, TotalSize);
	
	inYUV.close();
	outYUV.close();

	return 0;
}


YHistogram.h

#pragma once
void EXTRACT(unsigned char* in_YUV, unsigned char* in_Y, int width, int height);
void Histogram(unsigned char* in_Y, unsigned char* out_Y, int width, int height);
void Y2YUV(unsigned char* out_Y, unsigned char* out_YUV, int width, int height);

YHistogram.cpp

#include <iostream>
#include <cstdio>
#include "YHistogram.h"

using namespace std;

void EXTRACT(unsigned char* in_YUV, unsigned char* in_Y, int width, int height)
{
	int TotalSize = width * height * 3;
	int YSize = width * height;

	for (int i = 0; i < YSize; i++)
	{
		*(in_Y + i) = *(in_YUV + i);
	}
}

void Histogram(unsigned char* in_Y, unsigned char* out_Y, int width, int height)
{
	int TotalSize = width * height * 3;
	int YSize = width * height;
	double* count_p = new double[256];
	int* CCOUNT = new int[256];
	int* COUNT = new int[256];

	//初始化为0
	for (int i = 0; i < 256; i++)
	{
		COUNT[i] = 0;
	}

	//进行直方图均衡化
	
	//先计算总数
	for (int i = 0; i < YSize; i++)
	{
		int y = in_Y[i];
		COUNT[y]++;
	}

	//计算频数
	for (int i = 0; i < 256; i++)
	{
		count_p[i] = (double)COUNT[i] / YSize;
	}

	CCOUNT[0] = int(255 * count_p[0] + 0.5);
	
	//计算累加和
	for (int i = 1; i < 256; i++)
	{
		count_p[i] = count_p[i] + count_p[i - 1];
		CCOUNT[i] = int(255 * count_p[i] + 0.5);
	}

	//进行直方图均衡化
	for (int i = 0; i < YSize; i++)
	{
		out_Y[i] = CCOUNT[in_Y[i]];
	}
}

void Y2YUV(unsigned char* out_Y, unsigned char* out_YUV, int width, int height)
{
	int TotalSize = width * height * 3;
	int YSize = width * height;

	for (int i = 0; i < TotalSize; i++)
	{
		if (i < YSize)
		{
			out_YUV[i] = out_Y[i];
		}
		else
			out_YUV[i] = 128;
	}
}

实验结果

处理前处理后
在这里插入图片描述在这里插入图片描述
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值