OpenCV 图像对比度的实践

 更新时间:2021年09月05日 10:15:49   作者:翟天保Steven  
本文主要介绍了OpenCV 图像对比度的实践,文中通过示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文主要介绍了OpenCV 图像对比度,具有一定的参考价值,感兴趣的可以了解一下

实现原理

图像对比度指的是一幅图像中明暗区域最亮的白和最暗的黑之间不同亮度层级的测量,即指一幅图像灰度反差的大小。差异范围越大代表对比越大,差异范围越小代表对比越小。设置一个基准值thresh,当percent大于0时,需要令图像中的颜色对比更强烈,即数值距离thresh越远,则变化越大;当percent等于1时,对比强到极致,只有255和0的区分;当percent等于0时,不变;当percent小于0时,对比下降,即令远离thresh的数值更近些;当percent等于-1时,没有对比了,全是thresh值。

对比度调整算法的实现流程如下:

1.设置调整参数percent,取值为-100到100,类似PS中设置,归一化后为-1到1。

2.针对图像所有像素点单个处理。当percent大于等于0时,对比增强,调整后的RGB三通道数值为:

3.若percent小于0时,对比降低,此时调整后的图像RGB三通道值为:

4.若percent等于1时,大于thresh则等于255,小于则等于0。

至此,图像实现了明度的调整,算法逻辑参考xingyanxiao。C++实现代码如下。

功能函数代码

// 对比度
cv::Mat Contrast(cv::Mat src, int percent)
{
	float alpha = percent / 100.f;
	alpha = max(-1.f, min(1.f, alpha));
	cv::Mat temp = src.clone();
	int row = src.rows;
	int col = src.cols;
	int thresh = 127;
	for (int i = 0; i < row; ++i)
	{
		uchar *t = temp.ptr<uchar>(i);
		uchar *s = src.ptr<uchar>(i);
		for (int j = 0; j < col; ++j)
		{
			uchar b = s[3 * j];
			uchar g = s[3 * j + 1];
			uchar r = s[3 * j + 2];
			int newb, newg, newr;
			if (alpha == 1)
			{
				t[3 * j + 2] = r > thresh ? 255 : 0;
				t[3 * j + 1] = g > thresh ? 255 : 0;
				t[3 * j] = b > thresh ? 255 : 0;
				continue;
			}
			else if (alpha >= 0)
			{
				newr = static_cast<int>(thresh + (r - thresh) / (1 - alpha));
				newg = static_cast<int>(thresh + (g - thresh) / (1 - alpha));
				newb = static_cast<int>(thresh + (b - thresh) / (1 - alpha));
			}
			else {
				newr = static_cast<int>(thresh + (r - thresh) * (1 + alpha));
				newg = static_cast<int>(thresh + (g - thresh) * (1 + alpha));
				newb = static_cast<int>(thresh + (b - thresh) * (1 + alpha));
 
			}
			newr = max(0, min(255, newr));
			newg = max(0, min(255, newg));
			newb = max(0, min(255, newb));
			t[3 * j + 2] = static_cast<uchar>(newr);
			t[3 * j + 1] = static_cast<uchar>(newg);
			t[3 * j] = static_cast<uchar>(newb);
		}
	}
	return temp;
}

C++测试代码

#include <opencv2/opencv.hpp>
#include <iostream>
using namespace cv;
using namespace std;
 
cv::Mat Contrast(cv::Mat src, int percent);
 
int main()
{
	cv::Mat src = imread("5.jpg");
	cv::Mat result = Contrast(src, 50.f);
	imshow("original", src);
	imshow("result", result);
	waitKey(0);
	return 0;
}
 
// 对比度
cv::Mat Contrast(cv::Mat src, int percent)
{
	float alpha = percent / 100.f;
	alpha = max(-1.f, min(1.f, alpha));
	cv::Mat temp = src.clone();
	int row = src.rows;
	int col = src.cols;
	int thresh = 127;
	for (int i = 0; i < row; ++i)
	{
		uchar *t = temp.ptr<uchar>(i);
		uchar *s = src.ptr<uchar>(i);
		for (int j = 0; j < col; ++j)
		{
			uchar b = s[3 * j];
			uchar g = s[3 * j + 1];
			uchar r = s[3 * j + 2];
			int newb, newg, newr;
			if (alpha == 1)
			{
				t[3 * j + 2] = r > thresh ? 255 : 0;
				t[3 * j + 1] = g > thresh ? 255 : 0;
				t[3 * j] = b > thresh ? 255 : 0;
				continue;
			}
			else if (alpha >= 0)
			{
				newr = static_cast<int>(thresh + (r - thresh) / (1 - alpha));
				newg = static_cast<int>(thresh + (g - thresh) / (1 - alpha));
				newb = static_cast<int>(thresh + (b - thresh) / (1 - alpha));
			}
			else {
				newr = static_cast<int>(thresh + (r - thresh) * (1 + alpha));
				newg = static_cast<int>(thresh + (g - thresh) * (1 + alpha));
				newb = static_cast<int>(thresh + (b - thresh) * (1 + alpha));
 
			}
			newr = max(0, min(255, newr));
			newg = max(0, min(255, newg));
			newb = max(0, min(255, newb));
			t[3 * j + 2] = static_cast<uchar>(newr);
			t[3 * j + 1] = static_cast<uchar>(newg);
			t[3 * j] = static_cast<uchar>(newb);
		}
	}
	return temp;
}

测试效果

图1 原图

 

图2 参数为50的效果图

图3 参数为-50的效果图

通过调整percent可以实现图像对比度的调整。

到此这篇关于OpenCV 图像对比度的实践的文章就介绍到这了,更多相关OpenCV 图像对比度内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • 使用pth文件添加Python环境变量方式

    使用pth文件添加Python环境变量方式

    这篇文章主要介绍了使用pth文件添加Python环境变量方式,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-05-05
  • python将字典列表导出为Excel文件的方法

    python将字典列表导出为Excel文件的方法

    这篇文章主要介绍了python将字典列表导出为Excel文件的方法,本文给大家介绍的非常详细,具有一定的参考借鉴价值,需要的朋友可以参考下
    2019-09-09
  • 3个 Python 编程技巧

    3个 Python 编程技巧

    这篇文章主要介绍 Python 编程技巧,我们知道,字典的本质是哈希表,本身是无法排序的,但 Python 3.6 之后,字典是可以按照插入的顺序进行遍历的,这就是有序字典,其中的原理,可以阅读为什么 Python3.6 之后字典是有序的。本文也会介绍该内容,需要的朋友可以参考一下
    2021-10-10
  • Python爬虫之Requests库基本使用详解

    Python爬虫之Requests库基本使用详解

    这篇文章主要介绍了Python爬虫之Requests库基本使用详解,Requests 库是在 urllib 模块的基础上开发而来,继承了urllib.request的所有特性,与urllib.request 相比,Requests 在使用时更加简洁方便、快捷,所以 Requests 库在编写爬虫程序时使用较多,需要的朋友可以参考下
    2023-09-09
  • python神经网络Xception模型复现详解

    python神经网络Xception模型复现详解

    这篇文章主要为大家介绍了python神经网络Xception模型复现详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-05-05
  • python私有属性和方法实例分析

    python私有属性和方法实例分析

    这篇文章主要介绍了python私有属性和方法的用法,实例分析了python私有属性和方法的原理及具体使用技巧,具有一定参考借鉴价值,需要的朋友可以参考下
    2015-01-01
  • Python数字图像处理之霍夫线变换实现详解

    Python数字图像处理之霍夫线变换实现详解

    这篇文章主要介绍了Python数字图像处理之霍夫线变换实现详解,具有一定借鉴价值,需要的朋友可以参考下
    2018-01-01
  • Python 利用scrapy爬虫通过短短50行代码下载整站短视频

    Python 利用scrapy爬虫通过短短50行代码下载整站短视频

    近日,有朋友向我求助一件小事儿,他在一个短视频app上看到一个好玩儿的段子,想下载下来,可死活找不到下载的方法。经过我的一番研究才找到解决方法,下面小编给大家分享Python 利用scrapy爬虫通过短短50行代码下载整站短视频的方法,感兴趣的朋友一起看看吧
    2018-10-10
  • python单链路性能测试实践

    python单链路性能测试实践

    这篇文章主要为大家介绍了python单链路性能测试实践示例详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-07-07
  • Python数据结构与算法中的队列详解(2)

    Python数据结构与算法中的队列详解(2)

    这篇文章主要为大家详细介绍了Python中的队列,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下,希望能够给你带来帮助
    2022-03-03

最新评论