Changing the contrast and brightness of an image!
来源:互联网 发布:许晴航母 知乎 编辑:程序博客网 时间:2024/05/16 00:43
Goal
In this tutorial you will learn how to:
- Access pixel values
- Initialize a matrix with zeros
- Learn what saturate_cast does and why it is useful
- Get some cool info about pixel transformations
Theory
Note
The explanation below belongs to the book Computer Vision: Algorithms and Applications by Richard Szeliski
Image Processing
- A general image processing operator is a function that takes one or more input images and produces an output image.
- Image transforms can be seen as:
- Point operators (pixel transforms)
- Neighborhood (area-based) operators
Pixel Transforms
- In this kind of image processing transform, each output pixel’s value depends on only the corresponding input pixel value (plus, potentially, some globally collected information or parameters).
- Examples of such operators include brightness and contrast adjustments as well as color correction and transformations.
Brightness and contrast adjustments
Two commonly used point processes are multiplication and addition with a constant:
The parameters and are often called the gain and bias parameters; sometimes these parameters are said to control contrastand brightness respectively.
You can think of as the source image pixels and as the output image pixels. Then, more conveniently we can write the expression as:
where and indicates that the pixel is located in the i-th row and j-th column.
Code
- The following code performs the operation :
Explanation
We begin by creating parameters to save and to be entered by the user:
We load an image using imread and save it in a Mat object:
Now, since we will make some transformations to this image, we need a new Mat object to store it. Also, we want this to have the following features:
- Initial pixel values equal to zero
- Same size and type as the original image
We observe that Mat::zeros returns a Matlab-style zero initializer based on image.size() and image.type()
Now, to perform the operation we will access to each pixel in image. Since we are operating with RGB images, we will have three values per pixel (R, G and B), so we will also access them separately. Here is the piece of code:
Notice the following:
- 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 andc is R, G or B (0, 1 or 2).
- Since the operation can give values out of range or not integers (if is float), we use saturate_cast to make sure the values are valid.
Finally, we create windows and show the images, the usual way.
Note
Instead of using the for loops to access each pixel, we could have simply used this command:
where convertTo would effectively perform new_image = a*image + beta. However, we wanted to show you how to access each pixel. In any case, both methods give the same result.
Result
Running our code and using and
We get this:
Help and Feedback
You did not find what you were looking for?- Ask a question on the Q&A forum.
- If you think something is missing or wrong in the documentation, please file a bug report.
- Changing the contrast and brightness of an image!
- Changing The Size Of An Image
- Brightness and contrast adjusment
- jQuery:mouseover and Increase the Size of an Image
- jQuery:mouseover and Increase the Size of an Image
- Android Brightness Changing
- Image Pre-compensation: Balancing Contrast and Ringing
- 7.Integrated Knowledge: changing acceleration to speed and speed to distance — an example of integra
- The changing marketplace of bioinformatics
- Changing the ID of Dynamixel
- CHANGING BEHAVIOR BASED ON THE VALUE OF AN ENUMERATED TYPE(ZT)
- Optimized contrast enhancement for real-time image and video dehazing
- Reducing the Number of Gray Levels in an Image
- Send an image from the iPhone using ASIHTTP and UIImagePicker
- Image-rotate-brightness-blur
- YUI Changing the Contents of the DataTable
- Changing the ID of the Dynamixel
- Use Retation Sensor to control the brightness of LED lights
- org.hibernate.MappingException: Unknown entity:
- eclipse Rcp 打开word,并能编辑
- 关于递归的小探索
- PHP-CGI 进程 CPU 100% 与 file_get_contents 函数的关系
- Hdu 1754 I Hate It
- Changing the contrast and brightness of an image!
- 网站的分布式架构
- Linux下软件的安装与卸载方法
- 超简单的javabean应用
- Java程序优化的一些最佳实践
- mysql中int、bigint、smallint 和 tinyint的区别与长度
- 【转】对setTimeout的误差堆积测试和简单分析
- 怎么对包含自定义对象的NSMutableArray排序
- Linux睡眠休眠状态