日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

OpenCV之亮度、对比度详解

發(fā)布時(shí)間:2024/1/1 编程问答 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 OpenCV之亮度、对比度详解 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

一、亮度和對(duì)比度調(diào)整的理論依據(jù)


首先我們給出算子的概念。一般的圖像處理算子都是一個(gè)函數(shù),它接受一個(gè)或多個(gè)輸入圖像,并產(chǎn)生輸出圖像。下式給出了算子的一般形式:


? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ??或者


今天我們所講解的圖像亮度和對(duì)比度的調(diào)整操作,其實(shí)屬于圖像處理變換中比較簡單的一種——點(diǎn)操作(pointoperators)。點(diǎn)操作有一個(gè)特點(diǎn),僅僅根據(jù)輸入像素值(有時(shí)可加上某些全局信息或參數(shù)),來計(jì)算相應(yīng)的輸出像素值。這類算子包括亮度(brightness)和對(duì)比度(contrast)調(diào)整,以及顏色校正(colorcorrection)和變換(transformations)。

?

最兩種常用的點(diǎn)操作(或者說點(diǎn)算子),很顯然,是乘上一個(gè)常數(shù)(對(duì)應(yīng)對(duì)比度的調(diào)節(jié))以及加上一個(gè)常數(shù)(對(duì)應(yīng)亮度值的調(diào)節(jié))。用公式表示出來就是這樣:


? ? ? ? ? ??

?

看到這個(gè)式子,我們關(guān)于圖像亮度和對(duì)比度調(diào)整的策略就呼之欲出了。


其中:

  • 參數(shù)f(x)表示源圖像像素。
  • 參數(shù)g(x) 表示輸出圖像像素。
  • 參數(shù)a(需要滿足a>0)被稱為增益(gain),常常被用來控制圖像的對(duì)比度。
  • 參數(shù)b通常被稱為偏置(bias),常常被用來控制圖像的亮度。

而更近一步,我們這樣改寫這個(gè)式子:


其中,i 和 j 表示像素位于第i行 和 第j列 。

那么,這個(gè)式子就可以用來作為我們?cè)贠penCV中控制圖像的亮度和對(duì)比度的理論公式,其實(shí)現(xiàn)方法可以通過addWeighted函數(shù)或for逐個(gè)訪問圖像像素實(shí)現(xiàn)。


例程:

// Bright_and_Contrast.cpp : 定義控制臺(tái)應(yīng)用程序的入口點(diǎn)。 //#include "stdafx.h" #include <opencv2/core/core.hpp> #include <opencv2/imgproc/imgproc.hpp> #include <opencv2/highgui/highgui.hpp> #include <iostream>using namespace std; using namespace cv;Mat srcImage; int bright_init=1; int contrast_init=1;void bright_track(int,void*) {Mat dstImage;//調(diào)用函數(shù)實(shí)現(xiàn)addWeighted(srcImage,1,srcImage,0,bright_init,dstImage);/************************************************************************//* 訪問圖像中像素實(shí)現(xiàn)三個(gè)for循環(huán),執(zhí)行運(yùn)算 new_image(i,j) =a*image(i,j) + b Mat dstImage(srcImage.rows,srcImage.cols,srcImage.type());for(int y = 0; y < srcImage.rows; y++ ) { for(int x = 0; x < srcImage.cols; x++ ) { for(int c = 0; c < 3; c++ ) { //使用saturate_cast防止像素值超值或者為浮點(diǎn)數(shù)dstImage.at<Vec3b>(y,x)[c]= saturate_cast<uchar>( (contrast_init*0.01)*(srcImage.at<Vec3b>(y,x)[c] ) + bright_init ); } } } *//************************************************************************/ imshow("Image",dstImage); } void contrast_track(int,void*) {Mat dstImage;addWeighted(srcImage,contrast_init*0.02,srcImage,0,0,dstImage);/************************************************************************//* 訪問圖像中像素實(shí)現(xiàn)三個(gè)for循環(huán),執(zhí)行運(yùn)算 new_image(i,j) =a*image(i,j) + b Mat dstImage(srcImage.rows,srcImage.cols,srcImage.type());for(int y = 0; y < srcImage.rows; y++ ) { for(int x = 0; x < srcImage.cols; x++ ) { for(int c = 0; c < 3; c++ ) { //使用saturate_cast防止像素值溢出或者為浮點(diǎn)數(shù)dstImage.at<Vec3b>(y,x)[c]= saturate_cast<uchar>( (contrast_init*0.01)*(srcImage.at<Vec3b>(y,x)[c] ) + bright_init ); } } } *//************************************************************************/ imshow("Image",dstImage); }int _tmain(int argc, _TCHAR* argv[]) {srcImage=imread("1.jpg",1);if (srcImage.empty()){cout<<"讀入文件錯(cuò)誤"<<endl;return -1;}namedWindow("Image",CV_WINDOW_NORMAL);imshow("Image",srcImage);createTrackbar("亮度","Image",&bright_init,100,bright_track);createTrackbar("對(duì)比度","Image",&contrast_init,100,contrast_track);waitKey(0);return 0; }

總結(jié)

以上是生活随笔為你收集整理的OpenCV之亮度、对比度详解的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。