C++opencv改变图片对比度和亮度convertTo() |
您所在的位置:网站首页 › 调节图片亮度 › C++opencv改变图片对比度和亮度convertTo() |
改变图片的对比度和亮度可直接使用opencv库中的像素转换函数convertTo()。 Mat imgProc(Mat myImg,float contrast, int brightness) { Mat imgSrc = myImg; Mat imgDst = Mat::zeros (imgSrc.size(),imgSrc.type ());//生成零像素矩阵 imgSrc.convertTo (imgDst,-1,contrast,brightness); return imgDst; }函数原型: void convertTo( OutputArray m, int rtype, double alpha=1, double beta=0 ) const;@brief Converts an array to another data type with optional scaling. The method converts source pixel values to the target data type. saturate_cast\ is applied at the end to avoid possible overflows: \f[m(x,y) = saturate \_ cast( \alpha (*this)(x,y) + \beta )\f] @param m output matrix; if it does not have a proper size or type before the operation, it is reallocated. m为输出的矩阵 @param rtype desired output matrix type or, rather, the depth since the number of channels are the same as the input has; if rtype is negative, the output matrix will have the same type as the input.为负数时,输入输出为相同类型 @param alpha optional scale factor.增益参数,控制图片对比度 @param beta optional delta added to the scaled values.β是偏置参数,控制图片亮度 convertTo(imgDst, -1, con, bri);OpenCV增强图像使用的是点算子,即用常数对每个像素点执行乘法和加法的复合运算,公式如下: 式中,f (i, j)代表一个原图像像素点;a是增益参数,控制图像对比度;b是偏置参数,控制图片亮度;而g (i, j)则表示经处理后的对应像素点。 知道了原理之后可以将convertTo()改成自己的实现方法: Mat imgProc(Mat myImg,float contrast, int brightness) { Mat imgSrc = myImg; Mat imgDst = Mat::zeros (imgSrc.size(),imgSrc.type ());//生成零像素矩阵 //执行运算 imgDst(i,j) = contrast * imgSrc(i,j) + brightness; for(int i=0; i |
今日新闻 |
推荐新闻 |
CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3 |