欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

OpenCV 圖像對比度的實踐

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

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

實現(xiàn)原理

圖像對比度指的是一幅圖像中明暗區(qū)域最亮的白和最暗的黑之間不同亮度層級的測量,即指一幅圖像灰度反差的大小。差異范圍越大代表對比越大,差異范圍越小代表對比越小。設(shè)置一個基準值thresh,當percent大于0時,需要令圖像中的顏色對比更強烈,即數(shù)值距離thresh越遠,則變化越大;當percent等于1時,對比強到極致,只有255和0的區(qū)分;當percent等于0時,不變;當percent小于0時,對比下降,即令遠離thresh的數(shù)值更近些;當percent等于-1時,沒有對比了,全是thresh值。

對比度調(diào)整算法的實現(xiàn)流程如下:

1.設(shè)置調(diào)整參數(shù)percent,取值為-100到100,類似PS中設(shè)置,歸一化后為-1到1。

2.針對圖像所有像素點單個處理。當percent大于等于0時,對比增強,調(diào)整后的RGB三通道數(shù)值為:

3.若percent小于0時,對比降低,此時調(diào)整后的圖像RGB三通道值為:

4.若percent等于1時,大于thresh則等于255,小于則等于0。

至此,圖像實現(xiàn)了明度的調(diào)整,算法邏輯參考xingyanxiao。C++實現(xiàn)代碼如下。

功能函數(shù)代碼

// 對比度
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 參數(shù)為50的效果圖

圖3 參數(shù)為-50的效果圖

通過調(diào)整percent可以實現(xiàn)圖像對比度的調(diào)整。

到此這篇關(guān)于OpenCV 圖像對比度的實踐的文章就介紹到這了,更多相關(guān)OpenCV 圖像對比度內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • pandas分組聚合詳解

    pandas分組聚合詳解

    這篇文章主要介紹了pandas分組聚合詳解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2020-04-04
  • 關(guān)于pytorch訓練分類器

    關(guān)于pytorch訓練分類器

    這篇文章主要介紹了關(guān)于pytorch訓練分類器問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2023-09-09
  • 利用Python將時間或時間間隔轉(zhuǎn)為ISO 8601格式方法示例

    利用Python將時間或時間間隔轉(zhuǎn)為ISO 8601格式方法示例

    國際標準化組織的國際標準ISO8601是日期和時間的表示方法,全稱為《數(shù)據(jù)存儲和交換形式·信息交換·日期和時間的表示方法》,下面這篇文章主要給大家介紹了關(guān)于利用Python將時間或時間間隔轉(zhuǎn)為ISO 8601格式的相關(guān)資料,需要的朋友可以參考下。
    2017-09-09
  • python批量修改xml屬性的實現(xiàn)方式

    python批量修改xml屬性的實現(xiàn)方式

    這篇文章主要介紹了python批量修改xml屬性的實現(xiàn)方式,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-03-03
  • python 字符串轉(zhuǎn)列表 list 出現(xiàn)\ufeff的解決方法

    python 字符串轉(zhuǎn)列表 list 出現(xiàn)\ufeff的解決方法

    下面小編就為大家?guī)硪黄猵ython 字符串轉(zhuǎn)列表 list 出現(xiàn)\ufeff的解決方法。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-06-06
  • python字符串常用方法

    python字符串常用方法

    這篇文章主要介紹了python字符串常用方法,find、count、replace、split、startswith、endswith等多種方法,需要的朋友可以參考一下文章得具體內(nèi)容,希望對你有所幫助
    2021-10-10
  • Python+OpenCV+pyQt5錄制雙目攝像頭視頻的實例

    Python+OpenCV+pyQt5錄制雙目攝像頭視頻的實例

    今天小編就為大家分享一篇Python+OpenCV+pyQt5錄制雙目攝像頭視頻的實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-06-06
  • python中協(xié)程實現(xiàn)TCP連接的實例分析

    python中協(xié)程實現(xiàn)TCP連接的實例分析

    在本篇文章中我們給大家分享了python中協(xié)程實現(xiàn)TCP連接的代碼示例內(nèi)容,有需要的朋友們可以跟著學習下。
    2018-10-10
  • 使用Python進行同期群分析(Cohort?Analysis)

    使用Python進行同期群分析(Cohort?Analysis)

    同期群(Cohort)的字面意思(有共同特點或舉止類同的)一群人,比如不同性別,不同年齡。這篇文章主要介紹了用Python語言來進行同期群分析,感興趣的同學可以閱讀參考一下本文
    2023-03-03
  • python中幾種自動微分庫解析

    python中幾種自動微分庫解析

    這篇文章主要介紹了python中幾種自動微分庫解析,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2019-08-08

最新評論