校园春色亚洲色图_亚洲视频分类_中文字幕精品一区二区精品_麻豆一区区三区四区产品精品蜜桃

主頁 > 知識庫 > OpenCV 圖像對比度的實踐

OpenCV 圖像對比度的實踐

熱門標簽:哈爾濱外呼系統代理商 徐州天音防封電銷卡 佛山防封外呼系統收費 湛江電銷防封卡 鄭州智能外呼系統運營商 電話機器人適用業務 南昌辦理400電話怎么安裝 不錯的400電話辦理 獲客智能電銷機器人

本文主要介紹了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.ptruchar>(i);
		uchar *s = src.ptruchar>(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_castint>(thresh + (r - thresh) / (1 - alpha));
				newg = static_castint>(thresh + (g - thresh) / (1 - alpha));
				newb = static_castint>(thresh + (b - thresh) / (1 - alpha));
			}
			else {
				newr = static_castint>(thresh + (r - thresh) * (1 + alpha));
				newg = static_castint>(thresh + (g - thresh) * (1 + alpha));
				newb = static_castint>(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_castuchar>(newr);
			t[3 * j + 1] = static_castuchar>(newg);
			t[3 * j] = static_castuchar>(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.ptruchar>(i);
		uchar *s = src.ptruchar>(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_castint>(thresh + (r - thresh) / (1 - alpha));
				newg = static_castint>(thresh + (g - thresh) / (1 - alpha));
				newb = static_castint>(thresh + (b - thresh) / (1 - alpha));
			}
			else {
				newr = static_castint>(thresh + (r - thresh) * (1 + alpha));
				newg = static_castint>(thresh + (g - thresh) * (1 + alpha));
				newb = static_castint>(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_castuchar>(newr);
			t[3 * j + 1] = static_castuchar>(newg);
			t[3 * j] = static_castuchar>(newb);
		}
	}
	return temp;
}

測試效果

圖1 原圖

 

圖2 參數為50的效果圖

圖3 參數為-50的效果圖

通過調整percent可以實現圖像對比度的調整。

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

您可能感興趣的文章:
  • opencv調整圖像亮度對比度的示例代碼

標簽:蕪湖 紹興 呂梁 蘭州 吉安 安康 廣西 懷化

巨人網絡通訊聲明:本文標題《OpenCV 圖像對比度的實踐》,本文關鍵詞  OpenCV,圖像,對比度,的,實踐,;如發現本文內容存在版權問題,煩請提供相關信息告之我們,我們將及時溝通與處理。本站內容系統采集于網絡,涉及言論、版權與本站無關。
  • 相關文章
  • 下面列出與本文章《OpenCV 圖像對比度的實踐》相關的同類信息!
  • 本頁收集關于OpenCV 圖像對比度的實踐的相關信息資訊供網民參考!
  • 推薦文章
    主站蜘蛛池模板: 武乡县| 乌拉特后旗| 奈曼旗| 连云港市| 涞水县| 大丰市| 东城区| 分宜县| 玉树县| 蛟河市| 丰镇市| 体育| 张家口市| 恭城| 磴口县| 雷山县| 海安县| 定边县| 株洲县| 雷州市| 尉犁县| 沂水县| 静海县| 阳山县| 新龙县| 辽中县| 抚州市| 潜江市| 宣威市| 丰城市| 盐山县| 抚远县| 湖南省| 岐山县| 都江堰市| 东源县| 祥云县| 区。| 桦南县| 莲花县| 邓州市|