最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

OpenCV 圖像對比度的實踐

 更新時間:2021年09月05日 10:15:49   作者:翟天保Steven  
本文主要介紹了OpenCV 圖像對比度的實踐,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文主要介紹了OpenCV 圖像對比度,具有一定的參考價值,感興趣的可以了解一下

實現原理

圖像對比度指的是一幅圖像中明暗區(qū)域最亮的白和最暗的黑之間不同亮度層級的測量,即指一幅圖像灰度反差的大小。差異范圍越大代表對比越大,差異范圍越小代表對比越小。設置一個基準值thresh,當percent大于0時,需要令圖像中的顏色對比更強烈,即數值距離thresh越遠,則變化越大;當percent等于1時,對比強到極致,只有255和0的區(qū)分;當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 圖像對比度內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

  • 使用pth文件添加Python環(huán)境變量方式

    使用pth文件添加Python環(huán)境變量方式

    這篇文章主要介紹了使用pth文件添加Python環(huán)境變量方式,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    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 模塊的基礎上開發(fā)而來,繼承了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

最新評論

隆德县| 施秉县| 奎屯市| 习水县| 康马县| 汕头市| 五大连池市| 富顺县| 仁化县| 麻栗坡县| 建瓯市| 乌恰县| 宁阳县| 华坪县| 正蓝旗| 登封市| 民丰县| 石泉县| 肇州县| 通城县| 阿荣旗| 乐清市| 云浮市| 太保市| 鄂尔多斯市| 楚雄市| 大宁县| 陈巴尔虎旗| 深泽县| 宣武区| 涞源县| 汽车| 张家口市| 凤山市| 赫章县| 东方市| 鄂托克前旗| 凤庆县| 那坡县| 来宾市| 洞头县|