OpenCV学习笔记(8):增强图像对比度

#include "stdafx.h"
#include
using namespace cv;
int ImgStrong(Mat&img, Mat&result)
{
	//***************
	//p[]各个灰度级出现的概率
	//p1[]各个灰度级之前的概率和
	//各个灰度级出现的次数
	//*****************
	assert((img.cols == result.cols)&&(img.rows == result.rows));
	double p[256], p1[256], num[256];
	int nheight = img.rows;
	int nwidth = img.cols;
	int total = nheight*nwidth;
	memset(p, 0, sizeof(p));
	memset(p1, 0, sizeof(p1));
	memset(num, 0, sizeof(num));
	//各个灰度级出现的次数
	for (int i = 0; i < nheight; i++)
	{
		uchar *data = img.ptr(i);
		for (int j = 0; j < nwidth; j++)
		{
			num[data[j]]++;
		}
	}

	//各个灰度级出现的概率
	for (int i = 0; i < 256; i++)
	{
		p[i] = num[i] / total;
	}
	//各个灰度级之前的概率和
	for (int i = 0; i < 256; i++)
	{
		for (int j = 0; j <=i; j++)
		{
			p1[i] += p[j];
		}
	}

	//直方图变换
	for (int i = 0; i < nheight; i++)
	{
		uchar *data = img.ptr(i);
		uchar *data0 = result.ptr(i);
		for (int j = 0; j < nwidth; j++)
		{
			data0[j] = p1[data[j]] * 255 + 0.5;
		}
	}
	return 0;
}
int _tmain(int argc, _TCHAR* argv[])
{
	Mat src = imread("00.jpg", CV_LOAD_IMAGE_GRAYSCALE),src2;
	Mat dst(src.size(), src.depth()),dst2;
	ImgStrong(src, dst);
	Canny(src, src2, 50, 150, 3);
	Canny(dst, dst2, 50, 150, 3);
	imshow("src", src);
	imshow("src2", src2);
	imshow("dst", dst);
	imshow("dst2", dst2);
	waitKey(0);
	return 0;
}

你可能感兴趣的:(OpenCV学习笔记)