您好,登錄后才能下訂單哦!
這篇文章主要介紹了OpenCV在圖像對(duì)比度的示例分析,具有一定借鑒價(jià)值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。
圖像對(duì)比度指的是一幅圖像中明暗區(qū)域最亮的白和最暗的黑之間不同亮度層級(jí)的測(cè)量,即指一幅圖像灰度反差的大小。差異范圍越大代表對(duì)比越大,差異范圍越小代表對(duì)比越小。設(shè)置一個(gè)基準(zhǔn)值thresh,當(dāng)percent大于0時(shí),需要令圖像中的顏色對(duì)比更強(qiáng)烈,即數(shù)值距離thresh越遠(yuǎn),則變化越大;當(dāng)percent等于1時(shí),對(duì)比強(qiáng)到極致,只有255和0的區(qū)分;當(dāng)percent等于0時(shí),不變;當(dāng)percent小于0時(shí),對(duì)比下降,即令遠(yuǎn)離thresh的數(shù)值更近些;當(dāng)percent等于-1時(shí),沒有對(duì)比了,全是thresh值。
對(duì)比度調(diào)整算法的實(shí)現(xiàn)流程如下:
1.設(shè)置調(diào)整參數(shù)percent,取值為-100到100,類似PS中設(shè)置,歸一化后為-1到1。
2.針對(duì)圖像所有像素點(diǎn)單個(gè)處理。當(dāng)percent大于等于0時(shí),對(duì)比增強(qiáng),調(diào)整后的RGB三通道數(shù)值為:
3.若percent小于0時(shí),對(duì)比降低,此時(shí)調(diào)整后的圖像RGB三通道值為:
4.若percent等于1時(shí),大于thresh則等于255,小于則等于0。
至此,圖像實(shí)現(xiàn)了明度的調(diào)整,算法邏輯參考xingyanxiao。C++實(shí)現(xiàn)代碼如下。
功能函數(shù)代碼
// 對(duì)比度 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++測(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; } // 對(duì)比度 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可以實(shí)現(xiàn)圖像對(duì)比度的調(diào)整。
感謝你能夠認(rèn)真閱讀完這篇文章,希望小編分享的“OpenCV在圖像對(duì)比度的示例分析”這篇文章對(duì)大家有幫助,同時(shí)也希望大家多多支持億速云,關(guān)注億速云行業(yè)資訊頻道,更多相關(guān)知識(shí)等著你來學(xué)習(xí)!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。