C ++ image processing and image processing

Source: Internet
Author: User

C ++ image processing and image processing

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.


In Photoshop, Image Color Order adjustment is widely used. The process of Image Color Order adjustment described in this article is basically the same as that of Photoshop.

Photoshop's color order adjustment is divided into input color order adjustment and Output Color Order adjustment. The input color order adjustment has three adjustment points, that is, the black field, white field, and gray field adjustment.

The basic algorithm for inputting color order adjustment is not complex. First, the deviation Diff between the white field and the black field is calculated, and then the deviation between each pixel value and the black field rgbDiff is calculated, if rgbDiff <= 0, the value of each part of the pixel is equal to 0. Otherwise, the reciprocal power of the gray field is calculated based on the ratio of rgbDiff to Diff. Expressed by formula:

Diff = Highlight-Shadow

RgbDiff = RGB-Shadow

ClRGB = Power (rgbDiff/Diff, 1/Midtones)

Among them, Shadow is the low-end data of the input color level (black field), Highlight is the high-end data of the input color level (white field), and Midtones is the middle data of the input color level (gray field ), diff is the deviation between the two (must be greater than 1), RGB is the pixel component value before adjustment, clRGB is the pixel component value after adjustment of the input color level.

The adjustment of the Output Color Order is simpler. First, calculate the ratio of the deviation between the output color field and the black field to 255, and then multiply the coefficient by the pixel component value after the adjustment of the input color order, add the output black field value. Expressed by formula:

OutClRGB = clRGB * (outHighlight-outShadow)/255 + outShadow

OutShadow indicates the output black field, outHighlight indicates the output white field, and outClRGB indicates the pixel component value after all levels are adjusted.

As mentioned above, the deviation of the input color level black and white fields must be greater than 1, and the input color level does not have this limit. The deviation of the output black and white fields can be negative, when the output black and white fields are completely reversed, the adjusted Color Order of the output image is the negative part of the original image.

The color order adjustment involves four channels, namely the R, G, and B component channels and the overall color channels. If each channel is adjusted separately, it will be troublesome and time-consuming. This article uses the color order table replacement method, you can adjust the color levels of all four channels at a time.

The following code adjusts the color order of an image:

// Color level item structure typedef struct {UINT Shadow; FLOAT Midtones; UINT Highlight; UINT OutShadow; UINT levels;} ColorLevelItem, * PColorLevelItem; typedef struct {colorlevelblue item; ColorLevelItem Green; colorLevelItem Red; ColorLevelItem RGB;} ColorLevelData, * PColorLevelData; VOID InitColorLevelData (PColorLevelData clData) {PColorLevelItem = & clData-> Blue; for (INT I = 0; I <4; I ++, item ++) {item-> Shadow = ite M-> OutShadow = 0; item-> Highlight = item-> OutHighlight = 255; item-> Midtones = 1.0 ;}} BOOL GetColorLevelTable (PColorLevelItem item, LPBYTE clTable) {INT diff = (INT) (item-> Highlight-item-> Shadow); INT outDiff = (INT) (item-> OutHighlight-item-> OutShadow); if (! (Item-> Highlight <= 255 & diff <255 & diff> = 2) | (item-> OutShadow <= 255 & item-> OutHighlight <= 255 & outDiff <255) | (! (Item-> Midtones> 9.99 & item-> Midtones> 0.1) & item-> Midtones! = 1.0) return FALSE; DOUBLE coef = 255.0/diff; DOUBLE outCoef = outDiff/255.0; DOUBLE exponent = 1.0/item-> Midtones; for (INT I = 0; I <256; I ++) {INT v; // calculate the input color level black and white field if (clTable [I] <= (BYTE) item-> Shadow) v = 0; else {v = (INT) (clTable [I]-item-> Shadow) * coef + 0.5); if (v> 255) v = 255 ;} // calculate the input color level gray field v = (INT) (pow (v/255.0, exponent) * 255.0 + 0.5); // calculate the output color level clTable [I] = (BYTE) (v * outCoef + item-> O UtShadow + 0.5);} return TRUE;} BOOL CheckColorLevelData (PColorLevelData clData, BYTE clTables [] [256]) {BOOL result = FALSE; INT I, j; for (I = 0; I <3; I ++) {for (j = 0; j <256; j ++) clTables [I] [j] = (BYTE) j;} PColorLevelItem item = & clData-> Blue; for (I = 0; I <3; I ++, item ++) {if (GetColorLevelTable (item, clTables [I]) result = TRUE;} for (I = 0; I <3; I ++) {if (! GetColorLevelTable (item, clTables [I]) break; result = TRUE;} return result;} // adjust the color level of image data VOID ImageColorLevel (BitmapData * dest, BitmapData * source, PColorLevelData clData) {PARGBQuad pd, ps; UINT width, height; INT dstOffset, srcOffset; GetDataCopyParams (dest, source, width, height, pd, ps, dstOffset, srcOffset ); BYTE clTables [3] [2, 256]; if (CheckColorLevelData (clData, clTables) {for (UINT y = 0; y 

The following is an example of calling a simple image color order adjustment function:

void __fastcall TForm1::Button1Click(TObject *Sender){BitmapData dest, source;Bitmap *sBmp = new Bitmap(L"..\\..\\media\\source1.jpg");LockBitmap(sBmp, &source);Bitmap *dBmp = new Bitmap(source.Width, source.Height, PixelFormat32bppARGB);LockBitmap(dBmp, &dest);ColorLevelData clData;InitColorLevelData(&clData);clData.RGB.Shadow = 10;clData.RGB.Midtones = 1.2;clData.RGB.Highlight = 240;clData.RGB.OutShadow = 50;clData.RGB.OutHighlight = 200;/*clData.RGB.OutShadow = 255;clData.RGB.OutHighlight = 0;*/ImageColorLevel(&dest, &source, &clData);UnlockBitmap(dBmp, &dest);UnlockBitmap(sBmp, &source);Gdiplus::Graphics g(Canvas->Handle);g.DrawImage(sBmp, 0, 0);g.DrawImage(dBmp, source.Width, 0);delete dBmp;delete sBmp;}

The following is an articleDelphi Image Processing-Image Color Order Adjustment"In the example running interface, the first is the color order adjustment of the green channel, and the second is the negative image when the Color Order of the RGB output is completely reversed. For detailed examples of the Image Color Order adjustment interface, seeDelphi Image Processing-Image Color Order Adjustment.


The code in this article is edited and compiled using BCB XE7.


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".


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.