直方图变换——累计

#include <opencv2/core/core.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>
int main()
{
   	// 图像获取及验证
  	cv::Mat srcImage = cv::imread("..\\images\\flower3.jpg");
  	if( !srcImage.data ) 
    return 1;
    // 转化为灰度图像
    cv::Mat srcGray;
    cvtColor(srcImage, srcGray, CV_BGR2GRAY);
    // 计算图像的直方图
    const int channels[1]={0};
    const int histSize[1]={256};
    float hranges[2]={0,255};
    const float* ranges[1]={hranges};
    cv::MatND hist;
    calcHist(&srcGray, 1, channels, cv::Mat(),
    	      hist, 1, histSize, ranges);
	float table[256];
	int nPix = srcGray.cols * srcGray.rows;
	// 建立映射表
	for (int i = 0; i < 256; i++) {	
		float temp[256];
		// 像素变换
		temp[i] = hist.at<float>(i) / nPix * 255;
		if (i != 0) {
			// 像素累计
			table[i] = table[i -1] + temp[i];
		} else {
			table[i] = temp[i];
		}
	}
	// 通过映射进行表查找
	cv::Mat lookUpTable(cv::Size(1, 256), CV_8U);
	for (int i =0; i <256; i++) {
		lookUpTable.at<uchar>(i) = 
		    static_cast<uchar>(table[i]);
	}
	cv::Mat histTransResult;
	cv::LUT(srcGray, lookUpTable, histTransResult);
       // 显示图像
	cv::imshow("srcGray",srcGray);
	cv::imshow("histTransResult",histTransResult);
	cv::waitKey(0);
	return 0;

}

转载:http://blog.csdn.net/zhuwei1988


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值