如何使用 C++ 在 OpenCV 中更改對比度?


在影像處理中,經常會使用更改亮度和對比度的方法進行編輯。下面,我將學習如何更改影像的對比度。對比度控制影像的清晰度。對比度越高,影像越清晰,對比度越低,影像越柔和。

更改對比度即是指增加畫素的權重。對比度越高,影像越清晰。要更改對比度,可以將畫素值乘以某個常數。例如,如果將影像中所有畫素的值乘以 2,則畫素值將加倍,且影像的清晰度將提高。

以下程式演示瞭如何在 OpenCV 中更改影像的對比度。

示例

#include<iostream>
#include<opencv2/highgui/highgui.hpp>
using namespace cv;
using namespace std;
int main() {
   Mat original;//Declaring a matrix to load the original image//
   Mat contrast;//Declaring a matrix to load the image after changing the brightness//
   namedWindow("Original");//Declaring window to show the original image//
   namedWindow("Contrast");//Declaring window for edited image//
   original = imread("mountain.jpg");//loading the image
   original.convertTo(contrast, -1, 2, 0);//changing contrast//
   imshow("Original", original);//showing original image//
   imshow("Contrast", contrast);//showing edited image//
   waitKey(0);//wait for keystroke//
   return(0);
}

輸出

更新於: 10-Mar-2021

1K+ 次瀏覽

振興你的事業

透過完成該課程獲得認證

開始學習
廣告