OPENCV Official Document Learning record (3)

Source: Internet
Author: User

To change the brightness and contrast of an image:

Formula:

Code:

1#include <opencv2\opencv.hpp>2#include <iostream>3#include <string>4 5 #pragmaComment (linker, "/subsystem:\" windows\ "/entry:\" Maincrtstartup\ "")6 7 using namespacestd;8 using namespaceCV;9 Ten voidShow (std::stringname,mat img) One { A Namedwindow (name, cv_window_autosize); - imshow (Name, IMG); - } the  - intMain () - { -Mat img = Imread ("Lena.png"); +Mat new_img =Mat::zeros (Img.size (), Img.type ()); -  +     DoubleAlpha =2.2; A     intBeta = -; at      for(inty =0; Y < img.rows; ++y) -     { -          for(intx =0; x < Img.cols; ++x) -         { -              for(intc =0; C <3; ++c) -             { inNew_img.at<vec3b> (y, x) [c] = static_cast<uchar> (alpha* (img.at<vec3b> (y, x) [C]) +beta); -             } to         } +     } -Show ("Re", IMG); theShow ("Bright", new_img); *Imwrite ("./after/bright.png", new_img); $ Waitkey ();Panax Notoginseng     return 0; -}

Results:

(I don't know why my results are not the same as the examples, and I might have calculated errors)

Where we are using

New_img.at<vec3b> (y, x) [C]

Get the value of the channel, use Saturate_cast to do data overflow protection;

Alternatively, you can convert directly using a simpler function:

1#include <opencv2\opencv.hpp>2#include <iostream>3#include <string>4 5 #pragmaComment (linker, "/subsystem:\" windows\ "/entry:\" Maincrtstartup\ "")6 7 using namespacestd;8 using namespaceCV;9 Ten voidShow (std::stringname,mat img) One { A Namedwindow (name, cv_window_autosize); - imshow (Name, IMG); - } the  - intMain () - { -Mat img = Imread ("Lena.png"); +Mat new_img =Mat::zeros (Img.size (), Img.type ()); -     DoubleAlpha =2.2; +     intBeta = -; AImg.convertto (New_img,-1, alpha, beta); atShow (" After", new_img); - Waitkey (); -     return 0; -}

At this point the result is similar to the example:

OPENCV Official Document Learning record (3)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.