c++ opencv实现图像对比度增强

#include<iostream>
#include<opencv2/opencv.hpp>
#include<math.h>

using namespace cv;

int main()
{
	Mat example = imread("D:\\VC项目\\img1.jpg");
	if (!example.data)
	{
		printf("无法打开图像!!!");
		return -1;
	}
	imshow("example", example);
	Mat outexample;
	// "********方式一(源码对图像进行掩膜操作)*********************"
	
	example.copyTo(outexample);//把原图片拷贝给outexample
	int example_c = example.channels();//获取图片的通道数  
	int height = example.rows;//得到图片的高度
	int width = example.cols;//得到图片的宽度  
	int cols = example.cols * example_c;//利用通道数*图片的宽度
	for (int row = 1; row < height - 1; row++)
	{
		const uchar* previous = example.ptr<uchar>(row - 1); //获取前一行的指针
		const uchar* current = example.ptr<uchar>(row);//获取当前行的指针
		const uchar* next = example.ptr<uchar>(row +1);// 获取下一行的指针
		uchar* output = outexample.ptr<uchar>(row);
		for (int col = example_c; col < example_c * (example.cols - 1); col++)
		{
			*output = saturate_cast<uchar>(5 * current[col] - previous[col] - next[col] - current[col - example_c] - current[col + example_c]);
			output++;
		}
	}	
	printf("输入图片的信息:\n");
	std::cout << "通道数:"<<example_c<<"\n";
	std::cout << "图片的大小:" << width << "*" << height;

	// "********方式二(直接利用cv种的filter2D对图像进行掩膜操作)*********************"
	Mat kernel = (Mat_<char>(3, 3) << 0, -1, 0, -1, 10, -1, 0, -1, 0);
	filter2D(example, outexample, example.depth(), kernel);
	imshow("outexample", outexample);
	waitKey(0);

	return 0;
}
  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值