opencv学习:实现matlab中的stretchlim函数

matlab中的imadjust函数的第一种调用方式为:imadjust(I), I为待处理图像,在参数封装时调用了stretchlim函数。但opencv中没有相应的实现,故参照matlab的源代码实现了stretchlim函数。该函数用了上一篇文章中实现的hist函数,

void stretchlim(const IplImage* img, vector tol, vector& low_high)
{
	//接受参数
	double tol_low , tol_high;
	switch(tol.size())
	{
	case 0:	//默认值为 0.01, 0.99
		tol_low = 0.01;
		tol_high = 0.99;
		break;
	case 1:
		tol_low = tol[0];
		tol_high = 1-tol[0];
		break;
	case 2:		
		tol_low = tol[0];
		tol_high = tol[1];
		break;
	}

	int nbins = 255;
	if(img->depth == IPL_DEPTH_16U)
		nbins = 65535;

	//若容忍度低值小于高值则观察直方图获取灰度范围
	if(tol_low N = hist(img);

		//计算累积分布向量
		vector cdp = cumsumativeDistribution(N);

		//查找tol_low,tol_high边界灰度值
		int ilow = lower_bound(cdp.begin(), cdp.end(), tol_low)-cdp.begin();
		int ihigh = upper_bound(cdp.begin(), cdp.end(), tol_high)-cdp.begin();
		cout< cumsumativeDistribution(vector vec)
{
	vector distr;

	vector cum;
	long sum = 0;
	
	//计算累积向量和总和
	for(vector::iterator it = vec.begin(); it != vec.end(); it++)
	{
		sum += *it;
		cum.push_back(sum);
	}

	//计算累积分布率向量
	for(vector::iterator it = cum.begin(); it != cum.end(); it++)
	{
		distr.push_back(*it*1.0/sum);
	}
	return distr;
}



你可能感兴趣的:(图像处理与机器视觉,C++)