C ++ Image Processing-linear brightness/contrast Adjustment

Source: Internet
Author: User

Reading Tips:

The C ++ image processing series focuses on code clarity and readability, all using C ++ code.

Delphi Image ProcessingThe series focuses on efficiency. The general code is Pascal, and the core code is BaSm.

Make sure that the two items are consistent and can be compared with each other.

The code in this article must include "C ++ Image Processing-data types and common functions"The header file of BMP data. h in this article.

 

The code in this article is based on "C ++ Image Processing-brightness/contrast adjustment", by changing non-brightness to linear brightness.

Image brightness adjustment can be divided into two methods: non-linear and linear.

The brightness of a non-linear image is to add or subtract a value to the R, G, and B of the image pixel respectively. The advantage is that the code is simple and the brightness adjustment speed is fast. The disadvantage is that the image information is greatly lost, the adjusted image looks dull and has no sense of attention.

The brightness of a linear image is generally used to convert the RGB color of the image pixel to the color space such as HSL (HSV). After the L (v) part is increased or decreased, it is converted to the RGB color space, the advantage is that you have a strong sense of image brightness adjustment. The disadvantage is that the Code is complicated and the adjustment speed is slow, and there is a great distortion when the image brightness increase or decrease is large.

In view of the advantages and disadvantages of the above two methods, I have improved the image brightness adjustment method by referring to the contrast and saturation adjustment principle of Photoshop (see my article, the effect is not bad, mainly including non-distortion adjustment of the range width, a good sense of attention, as much as possible to reduce the loss of image information, etc. At the same time, in the code processing, the grayscale table search method is adopted, first, a 256-element Linear brightness/contrast search table is created. Then, the adjusted data is obtained for image data in the search table based on the R, G, and B values, therefore, the processing speed is basically the same as the non-linear brightness/contrast in "C ++ Image Processing-brightness/contrast adjustment.

The principle is represented by a formula:

If the value range of the brightness increase or decrease is-1 -- + 1, when the value is greater than 0:

RGB = RGB + RGB * (1/(1-value)-1)

When value is <0:

RGB = RGB + RGB * value

The following is the code for adjusting the linear brightness of an image (using the C ++ Builder compiler and the GDI + Library), including the example code:

// ----------------------------------------------------------------------------- Forceinlineint checkvalue (INT value) {return (Value &~ 0xff) = 0? Values: value> 255? 255: 0;} // adjust linear brightness/contrast void linebrightandcontrast (bitmapdata * data, int bright, int contrast, byte threshold) {If (bright = 0 & contrast = 0) return; float bv = bright <=-255? -1.0f: bright/255.0f; If (bright> 0 & Bright <255) bv = 1.0f/(1.0f-BV)-1.0f; float CV = contrast <=-255? -1.0f: contrast/255.0f; If (contrast> 0 & contrast <255) CV = 1.0f/(1.0f-cv)-1.0f; byte values [256]; for (INT I = 0; I <256; I ++) {int v = contrast> 0? Checkvalue (I + (INT) (I * bv + 0.5f): I; If (contrast> = 255) V = V> = threshold? 255: 0; elsev = checkvalue (V + (INT) (V-threshold) * CV + 0.5f); Values [I] = contrast <= 0? Checkvalue (V + (INT) (V * bv + 0.5f): V;} pargbquad P = (pargbquad) Data-> scan0; int offset = data-> stride-data-> width * sizeof (argbquad); For (uint y = 0; y <data-> height; y ++, (byte *) P + = offset) {for (uint x = 0; x <data-> width; X ++, P ++) {P-> Blue = values [p-> Blue]; P-> Green = values [p-> Green]; p-> Red = values [p-> Red] ;}}// invalid void _ fastcall tform1: button2click (tobject * sender) {gdiplus :: bitmap * BMP = new gdiplus: Bitmap (L ".. \\.. \ media \ source1.jpg "); gdiplus: Graphics * g = new gdiplus: Graphics (canvas-> handle); G-> drawimage (BMP, 0, 0 ); bitmapdata data; lockbitmap (BMP, & data); linebrightandcontrast (& Data, 20, 0,121); unlockbitmap (BMP, & data); G-> drawimage (BMP, Data. width, 0); Delete g; Delete BMP ;}//---------------------------------------------------------------------------

The following figure shows how to use RGB nonlinear brightness adjustment (medium), HSL linear brightness adjustment (right), and the improved linear brightness adjustment method (left) to map the adjustment results of the same photo:

Source image:

 

Due to limited levels, errors are inevitable. Correction and guidance are welcome. Email Address:Maozefa@hotmail.com

Here, you can access "C ++ Image Processing-Article Index".

Related Article

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.