码迷,mamicode.com
首页 > 其他好文 > 详细

OpenCV Tutorials —— Changing the contrast and brightness of an image

时间:2014-11-16 21:24:27      阅读:263      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   io   ar   os   sp   for   div   

Brightness and contrast adjustments

  • Two commonly used point processes are multiplication and addition with a constant:

    bubuko.com,布布扣

 

  • The parameters bubuko.com,布布扣 and bubuko.com,布布扣 are often called the gain and bias parameters; sometimes these parameters are said to control contrast and brightness respectively.

 

  • You can think of bubuko.com,布布扣 as the source image pixels and bubuko.com,布布扣 as the output image pixels. Then, more conveniently we can write the expression as:

    bubuko.com,布布扣

    where bubuko.com,布布扣 and bubuko.com,布布扣 indicates that the pixel is located in the i-th row and j-th column.

 

亮度增强

bubuko.com,布布扣

 

#include "stdafx.h"

#include <cv.h>
#include <highgui.h>
#include <iostream>

using namespace cv;

double alpha; /**< Simple contrast control */
int beta;  /**< Simple brightness control */

int main( int argc, char** argv )
{
	/// Read image given by user
	Mat image = imread( "xue.jpg" );
	Mat new_image = Mat::zeros( image.size(), image.type() );

	/// Initialize values
	std::cout<<" Basic Linear Transforms "<<std::endl;
	std::cout<<"-------------------------"<<std::endl;
	std::cout<<"* Enter the alpha value [1.0-3.0]: ";std::cin>>alpha;
	std::cout<<"* Enter the beta value [0-100]: "; std::cin>>beta;

	/// Do the operation new_image(i,j) = alpha*image(i,j) + beta
	for( int y = 0; y < image.rows; y++ )
	{ for( int x = 0; x < image.cols; x++ )
	{ for( int c = 0; c < 3; c++ )
	{
		new_image.at<Vec3b>(y,x)[c] =
			saturate_cast<uchar>( alpha*( image.at<Vec3b>(y,x)[c] ) + beta );
	}
	}
	}

	/// Create Windows
	namedWindow("Original Image", 1);
	namedWindow("New Image", 1);

	/// Show stuff
	imshow("Original Image", image);
	imshow("New Image", new_image);

	/// Wait until user press some key
	waitKey();
	return 0;
}

 

new_image.at<Vec3b>(y,x)[c] =
saturate_cast<uchar>( alpha*( image.at<Vec3b>(y,x)[c] ) + beta ); 
 
注意 : y,x
注意: image.at<Vec3b>(y,x)[c]

To access each pixel in the images we are using this syntax: image.at<Vec3b>(y,x)[c] where y is the row, x is the column and c is R, G or B (0, 1 or 2).

 

最后,这样的方法OpenCV有现成的

image.convertTo(new_image, -1, alpha, beta);

OpenCV Tutorials —— Changing the contrast and brightness of an image

标签:style   blog   http   io   ar   os   sp   for   div   

原文地址:http://www.cnblogs.com/sprint1989/p/4101826.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!