深入理解直方图

#include <opencv2/opencv.hpp> 
#include<vector>
using namespace std;
using namespace cv;

//绘制直方图
void showHistCallback(Mat img)
{
	vector<Mat> bgr;
	split(img, bgr);
	int numbins = 256;

	float range[] = { 0, 256 };
	const float* histRange = { range };

	Mat b_hist, g_hist, r_hist;
	calcHist(&bgr[0], 1, 0, Mat(), b_hist, 1, &numbins, &histRange);
	calcHist(&bgr[1], 1, 0, Mat(), g_hist, 1, &numbins, &histRange);
	calcHist(&bgr[2], 1, 0, Mat(), r_hist, 1, &numbins, &histRange);

	int width = 512;
	int height = 256;

	Mat histImage(height, width, CV_8UC3, Scalar(20, 20, 20));

	normalize(b_hist, b_hist, 0, height, NORM_MINMAX);
	normalize(g_hist, g_hist, 0, height, NORM_MINMAX);
	normalize(r_hist, r_hist, 0, height, NORM_MINMAX);

	int binStep = cvRound((float)width / (float)numbins);
	for (int i = 1; i < numbins; i++)
	{
		line(histImage, Point(binStep*(i - 1), height - cvRound(b_hist.at<float>(i - 1))),
			Point(binStep * (i), height - cvRound(b_hist.at<float>(i))), Scalar(255, 0, 0));
		line(histImage, Point(binStep*(i - 1), height - cvRound(g_hist.at<float>(i - 1))),
			Point(binStep * (i), height - cvRound(g_hist.at<float>(i))), Scalar(0, 255, 0));
		line(histImage, Point(binStep*(i - 1), height - cvRound(r_hist.at<float>(i - 1))),
			Point(binStep * (i), height - cvRound(r_hist.at<float>(i))), Scalar(0, 0, 255));
	}
	imshow("histImage", histImage);
}
//图像色彩均衡化
void equalizeCallback(Mat img)
{
	Mat equalize;
	Mat ycrcb;
	cvtColor(img, ycrcb, COLOR_BGR2YCrCb);

	vector<Mat> channels;
	split(ycrcb, channels);

	//只均衡Y通道
	equalizeHist(channels[0], channels[0]);
	
	merge(channels, ycrcb);

	cvtColor(ycrcb, equalize, COLOR_YCrCb2BGR);

	imshow("equlized", equalize);
}
//LOMO效果
void lomoCallBack(Mat img)
{
	Mat lomo;
	const double expeonential_e = exp(1.0);

	Mat lut(1, 256, CV_8UC1);
	for (int i = 0; i < 256; i++)
	{
		float x = (float)i / 256.0;
		lut.at<uchar>(i) = cvRound(256 * (1 / (1 + pow(expeonential_e, -((x - 0.5) / 0.1)))));
	}

	//只对红色通道应用值变换
	vector<Mat> bgr;
	split(img, bgr);
	//LUT(bgr[0], lut, bgr[0]);
	//LUT(bgr[1], lut, bgr[1]);
	LUT(bgr[2], lut, bgr[2]);
	merge(bgr, lomo);

	//创建昏暗的图像
	Mat halo(img.rows, img.cols, CV_32FC3, Scalar(0.3, 0.3, 0.3));

	//创建圆
	circle(halo, Point(img.cols / 2, img.rows / 2), img.cols / 3, Scalar(1, 1, 1), -1);
	blur(halo, halo, Size(img.cols / 3, img.cols / 3));

	//将结果转为浮点型
	Mat lomof;
	lomo.convertTo(lomof, CV_32FC3);

	//将结果和halo相乘
	multiply(lomof, halo, lomof);

	//转为8位图像
	lomof.convertTo(lomo, CV_8UC3);
	imshow("lomo", lomo);
}
int main()
{
	Mat src = imread("1.jpg",1);
	imshow("src", src);
	showHistCallback(src);
	equalizeCallback(src);
	lomoCallBack(src);
	waitKey(0);

	return 0;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值