OpenCV 对比度拉伸图像增强函数contrastStretching()

  • 操作系统:ubuntu22.04
  • OpenCV版本:OpenCV4.9
  • IDE:Visual Studio Code
  • 编程语言:C++11

算法描述

该函数用于对输入图像进行对比度拉伸(Contrast Stretching),是一种常见的强度变换方法。

它通过将输入图像的灰度值映射到一个新的范围内来增强图像的对比度。

给定一个输入的BGR或灰度图像,在[0, 255]区间上应用线性对比度拉伸,并返回结果图像。

函数原型

cpp 复制代码
void cv::intensity_transform::contrastStretching 	
(
 	const Mat  	input,
	Mat &  	output,
	const int  	r1,
	const int  	s1,
	const int  	r2,
	const int  	s2 
) 		

参数

  • input:输入BGR或灰度图像。
  • output:对比度拉伸后的结果图像。
  • r1:变换函数中第一个点(r1, s1)的x坐标。
  • s1:变换函数中第一个点(r1, s1)的y坐标。
  • r2:变换函数中第二个点(r2, s2)的x坐标。
  • s2:变换函数中第二个点(r2, s2)的y坐标。

代码示例

cpp 复制代码
#include <opencv2/intensity_transform.hpp>
#include <opencv2/opencv.hpp>

int main()
{
    // 读取图像(灰度图)
    cv::Mat img = cv::imread( "/media/dingxin/data/study/OpenCV/sources/images/Lenna.png", cv::IMREAD_GRAYSCALE );
    if ( img.empty() )
    {
        std::cerr << "Could not load image!" << std::endl;
        return -1;
    }

    cv::Mat result;

    // 设置参数:r1=70, s1=30, r2=180, s2=230
    cv::intensity_transform::contrastStretching( img, result, 70, 30, 180, 230 );

    // 显示结果
    cv::imshow( "Original", img );
    cv::imshow( "Contrast Stretched", result );
    cv::waitKey( 0 );

    return 0;
}

运行结果