C++影象處理 -- 影象黑白調整應用
轉自:http://blog.csdn.net/maozefa/article/details/6595831
閱讀提示:
《C++影象處理》系列以程式碼清晰,可讀性為主,全部使用C++程式碼。
《Delphi影象處理》系列以效率為側重點,一般程式碼為PASCAL,核心程式碼採用BASM。
儘可能保持二者內容一致,可相互對照。
本文程式碼必須包括《C++影象處理 -- 資料型別及公用函式》文章中的BmpData.h標頭檔案。
Photoshop CS的影象黑白調整功能,是通過對紅、黃、綠、青、藍和洋紅等6種顏色的比例調節來完成的。能更精細地將彩色圖片轉換為高質量的黑白照片。
Photoshop CS影象黑白調整功能的計算公式為:
gray = (max - min) * ratio_max + (mid - min) * ratio_max_mid + min
公式中:gray為畫素灰度值,max、mid和min分別為影象畫素R、G、B分量顏色的最大值、中間值和最小值,ratio_max為max所代表的分量顏色(單色)比率,ratio_max_mid則為max與mid兩種分量顏色所形成的複色比率。
用上面公式計算的灰度值,與我們通常所用的灰度計算方法有很大不同,通常所用的灰度公式為,是直接將顏色各分量乘以相應的比率相加而成,如:gray = 0.3R + 0.59G + 0.11B,而上面公式則是在最小值代表的顏色分量基礎上,用最大值與最小值之差表示單色部分(紅、綠、藍),用中間值與最小值之差表示複色部分(黃、青、洋紅),將單色和複色部分分別乘以與之對應的比率後相加,再加上最小值而得到灰度值。對於每個單獨的畫素來說,計算灰度值只需要用到上述6種顏色比率中的2種即可。在計算過程中可根據畫素RGB相互關係選擇對應的單色和複色比率,如畫素RGB的大小關係為R>G>B,單色比率選最大值R紅色,複色比率則為最大值R與中間值G所形成的複色黃色。
用程式程式碼實現上面的灰度計算公式並不複雜,難點還是前面所說的根據畫素RGB相互關係選擇對應的單色和複色比率。在前天我寫的《C++影象處理 -- 影象顏色混合(上)》文章中,已經實現了這項功能,同時,Photoshop影象黑白調整功能中附加的著色功能,也在文章中實現。本文的在上面文章程式碼基礎上,編寫一個相對簡單的影象黑白調整介面,來實現影象動態黑白調整。
下面是用BCB2007寫的一個介面程式程式碼:
程式標頭檔案部分:
- //---------------------------------------------------------------------------
- #ifndef bwMainH
- #define bwMainH
- //---------------------------------------------------------------------------
- #include <Classes.hpp>
- #include <Controls.hpp>
- #include <StdCtrls.hpp>
- #include <Forms.hpp>
- #include <ComCtrls.hpp>
- #include <Dialogs.hpp>
- #include <ExtCtrls.hpp>
- #define USE_GDIPLUS
- #include "BmpData.h"
- //---------------------------------------------------------------------------
- enum TLockType {ltEdit, ltTrack};
- typedef Set<TLockType, ltEdit, ltTrack> TLockTypes;
- class TForm1 : public TForm
- {
- __published: // IDE-managed Components
- TPaintBox *PaintBox1;
- TLabel *Label1;
- TLabel *Label2;
- TLabel *Label3;
- TLabel *Label4;
- TLabel *Label5;
- TLabel *Label6;
- TLabel *Label7;
- TLabel *Label8;
- TLabel *Label9;
- TLabel *Label10;
- TLabel *Label11;
- TLabel *Label12;
- TLabel *Label13;
- TLabel *Label18;
- TComboBox *ComboBox1;
- TEdit *Edit1;
- TTrackBar *TrackBar1;
- TEdit *Edit2;
- TTrackBar *TrackBar2;
- TEdit *Edit3;
- TTrackBar *TrackBar3;
- TEdit *Edit4;
- TTrackBar *TrackBar4;
- TEdit *Edit5;
- TTrackBar *TrackBar5;
- TEdit *Edit6;
- TTrackBar *TrackBar6;
- TCheckBox *CheckBox1;
- TGroupBox *GroupBox1;
- TLabel *Label14;
- TLabel *Label15;
- TLabel *Label16;
- TLabel *Label17;
- TPaintBox *PaintBox2;
- TEdit *Edit7;
- TTrackBar *TrackBar7;
- TEdit *Edit8;
- TTrackBar *TrackBar8;
- TColorDialog *ColorDialog1;
- void __fastcall FormCreate(TObject *Sender);
- void __fastcall FormDestroy(TObject *Sender);
- void __fastcall ComboBox1Change(TObject *Sender);
- void __fastcall TrackBar1Change(TObject *Sender);
- void __fastcall Edit1Change(TObject *Sender);
- void __fastcall Edit1KeyPress(TObject *Sender, char &Key);
- void __fastcall Edit1Exit(TObject *Sender);
- void __fastcall CheckBox1Click(TObject *Sender);
- void __fastcall TrackBar7Change(TObject *Sender);
- void __fastcall Edit7Change(TObject *Sender);
- void __fastcall Edit7KeyPress(TObject *Sender, char &Key);
- void __fastcall PaintBox2Click(TObject *Sender);
- void __fastcall PaintBox1Paint(TObject *Sender);
- void __fastcall PaintBox2Paint(TObject *Sender);
- void __fastcall TrackBar8Change(TObject *Sender);
- private: // User declarations
- Bitmap *Source; // 源影象
- Bitmap *Dest; // 調整後的影象
- BitmapData srcData;
- BitmapData dstData;
- float bwColors[6]; // 灰度選項陣列
- int Bright; // 亮度
- TTrackBar *TrackBars[6]; // 灰度選項條元件陣列
- TEdit *Edits[6]; // 灰度選項編輯框陣列
- TLockTypes Lock;
- Gdiplus::Rect rect;
- ARGBQuad MixColor; // 混合顏色
- int __fastcall GetHue(void);
- int __fastcall GetSat(void);
- void __fastcall SetHue(int hue);
- void __fastcall SetSat(int sat);
- void __fastcall MixColorToHSV(void);
- void __fastcall HSVToMixColor(void);
- void __fastcall Execute(void);
- void __fastcall MixColorChange(void);
- public: // User declarations
- __fastcall TForm1(TComponent* Owner);
- __property int Hue = {read=GetHue, write=SetHue}; // 色相
- __property int Sat = {read=GetSat, write=SetSat}; // 飽和度
- };
- //---------------------------------------------------------------------------
- const CustomIndex = 11; // 自定義選項索引
- const DefaultTint = 0xe1d3b3; // 預設混合顏色
- const int DefOptions[][6] = // 預定義灰度選項
- {
- {40, 60, 40, 60, 20, 80},
- {128, 128, 100, 100, 128, 100},
- {100, 100, 100, 100, 100, 100},
- {0, 0, 0, 0, 0, 0},
- {-40, 235, 144, -68, -3, -107},
- {120, 110, -10, -50, 0, 120},
- {50, 120, 90, 50, 0, 0},
- {0, 0, 0, 110, 110, 110},
- {120, 120, -10, -50, -50, 120},
- {-50, -50, -50, 150, 150, 150},
- {120, 110, 40, -30, 0, 70}
- };
- extern PACKAGE TForm1 *Form1;
- //---------------------------------------------------------------------------
- #endif
程式碼檔案部分:
- //---------------------------------------------------------------------------
- #include <vcl.h>
- #pragma hdrstop
- #include "bwMain.h"
- //---------------------------------------------------------------------------
- #pragma package(smart_init)
- #pragma resource "*.dfm"
- TForm1 *Form1;
- //---------------------------------------------------------------------------
- __fastcall TForm1::TForm1(TComponent* Owner)
- : TForm(Owner)
- {
- }
- //---------------------------------------------------------------------------
- ULONG gdiplusToken;
- typedef FLOAT BWParams, *PBWParams;
- // 黑白調整預設引數:紅,黃,綠,洋紅,藍,青
- CONST INT _BWDefault[] = {410, 614, 410, 819, 205, 614};
- enum
- {
- BWIndexBlue = 0x40000,
- BWIndexGreen = 0x20000,
- BWIndexRed = 0x00000
- };
- enum
- {
- IndexBlue = 0x00000,
- IndexGreen = 0x10000,
- IndexRed = 0x20000
- };
- typedef union // 顏色分量交換結構
- {
- INT tmp; // 交換時用的臨時變數
- struct
- {
- SHORT value; // 顏色分量值
- SHORT index; // 顏色分量索引
- };
- }RGBIndex;
- //---------------------------------------------------------------------------
- // 交換畫素分量
- FORCEINLINE
- VOID SwapRgb(RGBIndex &a, RGBIndex &b)
- {
- a.tmp ^= b.tmp;
- b.tmp ^= a.tmp;
- a.tmp ^= b.tmp;
- }
- //---------------------------------------------------------------------------
- // 獲取黑白灰度
- FORCEINLINE
- INT GetBWGray(CONST PARGBQuad pixel, CONST PINT bwParams)
- {
- RGBIndex max, mid, min;
- min.tmp = pixel->Blue | BWIndexBlue;
- mid.tmp = pixel->Green | BWIndexGreen;
- max.tmp = pixel->Red | BWIndexRed;
- if (max.value < mid.value)
- SwapRgb(max, mid);
- if (max.value < min.value)
- SwapRgb(max, min);
- if (min.value > mid.value)
- SwapRgb(min, mid);
- return (((max.value - mid.value) * bwParams[max.index] +
- (mid.value - min.value) * bwParams[max.index + mid.index - 1] +
- 512) >> 10) + min.value;
- }
- //---------------------------------------------------------------------------
- VOID ColorMix(PARGBQuad pd, CONST PARGBQuad ps, INT gray)
- {
- // 灰度計算常數:藍,綠、紅
- CONST INT ys[3] = {113, 604, 307};
- RGBIndex max, mid, min;
- min.tmp = ps->Blue | IndexBlue;
- mid.tmp = ps->Green | IndexGreen;
- max.tmp = ps->Red | IndexRed;
- if (max.value < mid.value)
- SwapRgb(max, mid);
- if (max.value < min.value)
- SwapRgb(max, min);
- if (min.value > mid.value)
- SwapRgb(min, mid);
- INT max_min = max.value - min.value;
- // 飽和度為0,返回灰度
- if (max_min == 0)
- {
- pd->Blue = pd->Green = pd->Red = gray;
- return;
- }
- INT mid_min = mid.value - min.value;
- INT newMax, newMid, newMin;
- gray <<= 10;
- newMax = (gray + (max_min - mid_min) * ys[mid.index] + max_min * ys[min.index] + 512) >> 10;
- newMin = newMax - max_min;
- if (newMax > 255)
- {
- INT hueCoef = (mid_min << 10) / max_min;
- INT v0 = (ys[mid.index] * hueCoef) >> 10;
- INT v1 = ys[min.index] + ys[mid.index] - v0;
- newMin = (gray - (ys[max.index] + v0) * 255 + (v1 >> 1)) / v1;
- newMid = newMin + (((255 ^ newMin) * hueCoef + 512) >> 10);
- newMax = 255;
- }
- else if (newMin < 0)
- {
- INT hueCoef = (mid_min << 10) / max_min;
- INT tmp = ys[max.index] + ((ys[mid.index] * hueCoef + 512) >> 10);
- newMax = (gray + (tmp >> 1)) / tmp;
- newMid = (newMax * hueCoef + 512) >> 10;
- newMin = 1;
- }
- else
- newMid = newMin + mid_min;
- ((LPBYTE)pd)[max.index] = newMax;
- ((LPBYTE)pd)[mid.index] = newMid;
- ((LPBYTE)pd)[min.index] = newMin;
- }
- //---------------------------------------------------------------------------
- // 影象黑白調整。
- // 調整引數bwParams為元素數等於6的陣列指標,分別為紅,黃,綠,青,藍,洋紅
- VOID ImageBWCopy(BitmapData *dest, CONST BitmapData *source, CONST PBWParams bwParams = NULL)
- {
- // 拷貝畫素灰度引數,並交換青色和洋紅色
- INT params[6], *pparams;
- if (bwParams)
- {
- for (INT i = 0; i < 6; i ++)
- params[i] = (INT)(bwParams[i] * 1024 + 0.5);
- params[3] ^= params[5];
- params[5] ^= params[3];
- params[3] ^= params[5];
- pparams = params;
- }
- else
- pparams = (INT*)_BWDefault;
- PARGBQuad pd, ps;
- UINT width, height;
- INT dstOffset, srcOffset;
- GetDataCopyParams(dest, source, width, height, pd, ps, dstOffset, srcOffset);
- for (UINT y = 0; y < height; y ++, pd += dstOffset, ps += srcOffset)
- {
- for (UINT x = 0; x < width; x ++, pd ++, ps ++)
- {
- INT gray = GetBWGray(ps, pparams);
- pd->Blue = pd->Green = pd->Red =
- (gray & ~0xff) == 0? gray : gray > 255? 255 : 0;
- }
- }
- }
- //---------------------------------------------------------------------------
- // 灰度影象染色。
- VOID ImageTint(BitmapData *grayData, ARGB color)
- {
- ARGBQuad colorTable[256];
- PARGBQuad p = colorTable;
- for (INT i = 0; i < 256; i ++, p ++)
- {
- ColorMix(p, (PARGBQuad)&color, i);
- p->Alpha = 255;
- }
- p = (PARGBQuad)grayData->Scan0;
- INT dataOffset = (grayData->Stride >> 2) - (INT)grayData->Width;
- for (UINT y = 0; y < grayData->Height; y ++, p += dataOffset)
- {
- for (UINT x = 0; x < grayData->Width; x ++, p ++)
- {
- p->Color = colorTable[p->Blue].Color;
- }
- }
- }
- //---------------------------------------------------------------------------
- void __fastcall TForm1::FormCreate(TObject *Sender)
- {
- Gdiplus::GdiplusStartupInput gdiplusStartupInput;
- GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);
- TrackBars[0] = TrackBar1;
- TrackBars[1] = TrackBar2;
- TrackBars[2] = TrackBar3;
- TrackBars[3] = TrackBar4;
- TrackBars[4] = TrackBar5;
- TrackBars[5] = TrackBar6;
- Edits[0] = Edit1;
- Edits[1] = Edit2;
- Edits[2] = Edit3;
- Edits[3] = Edit4;
- Edits[4] = Edit5;
- Edits[5] = Edit6;
- // 從檔案裝入影象到tmp
- Bitmap *tmp = new Bitmap(L"source1.jpg");
- rect.Width = tmp->GetWidth();
- rect.Height = tmp->GetHeight();
- // 分別建立新的源和目標影象資料到srcData和dstData
- GetBitmapData(rect.Width, rect.Height, &srcData);
- GetBitmapData(rect.Width, rect.Height, &dstData);
- // 將tmp影象資料分別鎖定拷貝到srcData和dstData
- tmp->LockBits(&rect,
- ImageLockModeRead | ImageLockModeWrite | ImageLockModeUserInputBuf,
- PixelFormat32bppARGB, &srcData);
- tmp->UnlockBits(&srcData);
- tmp->LockBits(&rect,
- ImageLockModeRead | ImageLockModeWrite | ImageLockModeUserInputBuf,
- PixelFormat32bppARGB, &dstData);
- tmp->UnlockBits(&dstData);
- delete tmp;
- // 分別用影象資料srcData和dstData建立點陣圖Source和Dest
- // 注:影象資料結構用於資料處理,點陣圖用於顯示,這樣即可繫結資料結構和點陣圖,
- // 又能避免每次處理影象資料時的鎖定和解鎖操作
- Source = new Bitmap(srcData.Width, srcData.Height, srcData.Stride,
- PixelFormat32bppARGB, (BYTE*)srcData.Scan0);
- Dest = new Bitmap(dstData.Width, dstData.Height, dstData.Stride,
- PixelFormat32bppARGB, (BYTE*)dstData.Scan0);
- ComboBox1Change(NULL);
- }
- //---------------------------------------------------------------------------
- void __fastcall TForm1::FormDestroy(TObject *Sender)
- {
- delete Dest;
- delete Source;
- FreeBitmapData(&dstData);
- FreeBitmapData(&srcData);
- GdiplusShutdown(gdiplusToken);
- }
- //---------------------------------------------------------------------------
- // 執行影象黑白調整
- void __fastcall TForm1::Execute(void)
- {
- for (int i = 0; i < 6; i ++) // 獲取灰度選項條資料
- bwColors[i] = TrackBars[i]->Position / 100.0;
- ImageBWCopy(&dstData, &srcData, bwColors); // 源圖黑白調整到目標圖
- if (CheckBox1->Checked && Sat) // 如果色調選項被選,著色
- ImageTint(&dstData, MixColor.Color);
- PaintBox1Paint(NULL); // 顯示影象
- }
- //---------------------------------------------------------------------------
- // 預設黑白調整選項改變
- void __fastcall TForm1::ComboBox1Change(TObject *Sender)
- {
- if (ComboBox1->ItemIndex == CustomIndex)
- return;
- MixColor.Color = DefaultTint; // 設定預設混合顏色
- MixColorToHSV(); // 計算並設定預設色相、飽和度控制元件
- Lock = TLockTypes() << ltEdit << ltTrack;
- try
- {
- for (int i = 0; i < 6; i ++) // 裝入預設的選項資料到相應的控制元件
- {
- TrackBars[i]->Position = DefOptions[ComboBox1->ItemIndex][i];
- Edits[i]->Text = DefOptions[ComboBox1->ItemIndex][i];
- }
- if (CheckBox1->Checked)
- CheckBox1->Checked = false; // 取消色調選項
- else
- Execute();
- }
- __finally
- {
- Lock.Clear();
- }
- }
- //---------------------------------------------------------------------------
- // 黑白調整資料選項條改變
- void __fastcall TForm1::TrackBar1Change(TObject *Sender)
- {
- if (Lock.Contains(ltTrack)) return;
- Lock = TLockTypes() << ltEdit;
- try
- {
- TTrackBar *bar = (TTrackBar*)Sender;
- Edits[bar->Tag]->Text = bar->Position;
- ComboBox1->ItemIndex = CustomIndex; // 預設下拉框設定為自定義
- Execute();
- }
- __finally
- {
- Lock.Clear();
- }
- }
- //---------------------------------------------------------------------------
- // 黑白調整資料編輯框改變
- void __fastcall TForm1::Edit1Change(TObject *Sender)
- {
- if (Lock.Contains(ltEdit)) return;
- TEdit *edit = (TEdit*)Sender;
- if (edit->Text != "" && edit->Text != "-")
- TrackBars[edit->Tag]->Position = StrToInt(edit->Text);
- }
- //---------------------------------------------------------------------------
- void __fastcall TForm1::Edit1KeyPress(TObject *Sender, char &Key)
- {
- if (Key >= ' ' && Key != '-' && (Key < '0' || Key > '9'))
- Key = 0;
- }
- //---------------------------------------------------------------------------
- void __fastcall TForm1::Edit1Exit(TObject *Sender)
- {
- TEdit *edit = (TEdit*)Sender;
- if (edit->Text == "")
- edit->Text = TrackBars[edit->Tag]->Position;
- }
- //---------------------------------------------------------------------------
- // 混合顏色改變,畫混合顏色,顯示其RGB值
- void __fastcall TForm1::MixColorChange(void)
- {
- PaintBox2Paint(NULL);
- Label18->Caption = "R: " + IntToStr(MixColor.Red) +
- ", G: " + MixColor.Green +
- ", B: " + MixColor.Blue;
- Execute();
- }
- //---------------------------------------------------------------------------
- inline void RgbSwap(int &a, int &b)
- {
- a ^= b;
- b ^= a;
- a ^= b;
- }
- // 按混合顏色計算並改變HSV
- void __fastcall TForm1::MixColorToHSV(void)
- {
- int max, mid, min;
- max = MixColor.Red;
- mid = MixColor.Green;
- min = MixColor.Blue;
- if (max < mid) RgbSwap(max, mid);
- if (max < min) RgbSwap(max, min);
- if (min > mid) RgbSwap(min, mid);
- int max_min = max - min;
- if (max_min == 0)
- {
- Hue = 0;
- Sat = 0;
- }
- else
- {
- int H;
- if (max == MixColor.Red)
- H = ((MixColor.Green - MixColor.Blue) * 60 + 30) / max_min;
- else if (max == MixColor.Green)
- H = ((MixColor.Blue - MixColor.Red) * 60 + 30) / max_min + 120;
- else
- H = ((MixColor.Red - MixColor.Green) * 60 + 30) / max_min + 240;
- Hue = H < 0? H + 360 : H;
- Sat = (max_min * 100) / max;
- }
- Bright = max;
- }
- //---------------------------------------------------------------------------
- inline ARGB RgbToColor(int r, int g, int b)
- {
- return (r << 16) | (g << 8) | b;
- }
- // 按HSV計算並改變混合顏色
- void __fastcall TForm1::HSVToMixColor(void)
- {
- if (Sat == 0)
- {
- MixColor.Blue = MixColor.Green = MixColor.Red = Bright;
- }
- else
- {
- int index = Hue / 60;
- int f = Hue % 60;
- if ((index & 1) == 0) f = 60 - f;
- int a = Bright;
- int b = (Bright * (6000 - Sat * f)) / 6000;
- int c = (Bright * (100 - Sat)) / 100;
- switch (index)
- {
- case 0:
- MixColor.Color = RgbToColor(a, b, c);
- break;
- case 1:
- MixColor.Color = RgbToColor(b, a, c);
- break;
- case 2:
- MixColor.Color = RgbToColor(c, a, b);
- break;
- case 3:
- MixColor.Color = RgbToColor(c, b, a);
- break;
- case 4:
- MixColor.Color = RgbToColor(b, c, a);
- break;
- case 5:
- MixColor.Color = RgbToColor(a, c, b);
- }
- }
- MixColorChange();
- }
- //---------------------------------------------------------------------------
- int __fastcall TForm1::GetHue(void)
- {
- return TrackBar7->Position;
- }
- //---------------------------------------------------------------------------
- int __fastcall TForm1::GetSat(void)
- {
- return TrackBar8->Position;
- }
- //---------------------------------------------------------------------------
- void __fastcall TForm1::SetHue(int hue)
- {
- if (Hue == hue) return;
- Lock = TLockTypes() << ltEdit << ltTrack;
- try
- {
- TrackBar7->Position = hue;
- Edit7->Text = hue;
- }
- __finally
- {
- Lock.Clear();
- }
- }
- //---------------------------------------------------------------------------
- void __fastcall TForm1::SetSat(int sat)
- {
- if (Sat == sat) return;
- Lock = TLockTypes() << ltEdit << ltTrack;
- try
- {
- TrackBar8->Position = sat;
- Edit8->Text = sat;
- }
- __finally
- {
- Lock.Clear();
- }
- }
- //---------------------------------------------------------------------------
- // 色調選盒改變
- void __fastcall TForm1::CheckBox1Click(TObject *Sender)
- {
- Label14->Enabled = CheckBox1->Checked;
- Label15->Enabled = CheckBox1->Checked;
- Label16->Enabled = CheckBox1->Checked;
- Label17->Enabled = CheckBox1->Checked;
- Label18->Visible = CheckBox1->Checked;
- Edit7->Enabled = CheckBox1->Checked;
- Edit8->Enabled = CheckBox1->Checked;
- TrackBar7->SliderVisible = CheckBox1->Checked;
- TrackBar8->SliderVisible = CheckBox1->Checked;
- if (CheckBox1->Checked)
- ComboBox1->ItemIndex = CustomIndex;
- MixColorChange();
- }
- //---------------------------------------------------------------------------
- // 色相選項條改變
- void __fastcall TForm1::TrackBar7Change(TObject *Sender)
- {
- if (!Lock.Contains(ltTrack))
- Edit7->Text = TrackBar7->Position;
- }
- //---------------------------------------------------------------------------
- // 飽和度選項條改變
- void __fastcall TForm1::TrackBar8Change(TObject *Sender)
- {
- if (!Lock.Contains(ltTrack))
- Edit8->Text = TrackBar8->Position;
- }
- //---------------------------------------------------------------------------
- // 色相或者飽和度編輯框改變
- void __fastcall TForm1::Edit7Change(TObject *Sender)
- {
- TEdit *edit = (TEdit*)Sender;
- if (Lock.Contains(ltEdit) || edit->Text == "")
- return;
- Lock = TLockTypes() << ltTrack;
- try
- {
- int val = StrToInt(edit->Text);
- TTrackBar *bar = edit->Tag == 0? TrackBar7 : TrackBar8;
- if (bar->Position != val)
- bar->Position = val;
- HSVToMixColor();
- }
- __finally
- {
- Lock.Clear();
- }
- }
- //---------------------------------------------------------------------------
- void __fastcall TForm1::Edit7KeyPress(TObject *Sender, char &Key)
- {
- if (Key >= ' ' && (Key < '0' || Key > '9'))
- Key = 0;
- }
- //---------------------------------------------------------------------------
- // 呼叫顏色對話方塊選擇混合顏色
- void __fastcall TForm1::PaintBox2Click(TObject *Sender)
- {
- if (CheckBox1->Checked && ColorDialog1->Execute(Handle))
- {
- MixColor.Color = (ARGB)ColorDialog1->Color;
- MixColor.Blue = MixColor.Red;
- MixColor.Red = (BYTE)ColorDialog1->Color;
- MixColorToHSV();
- MixColorChange();
- }
- }
- //---------------------------------------------------------------------------
- // 畫黑白調整影象和源影象
- void __fastcall TForm1::PaintBox1Paint(TObject *Sender)
- {
- Gdiplus::Graphics *g = new Gdiplus::Graphics(PaintBox1->Canvas->Handle);
- try
- {
- g->DrawImage(Dest, rect);
- if (Sender != NULL)
- {
- g->TranslateTransform(0, rect.Height);
- g->DrawImage(Source, rect);
- }
- }
- __finally
- {
- delete g;
- }
- }
- //---------------------------------------------------------------------------
- // 畫混合顏色
- void __fastcall TForm1::PaintBox2Paint(TObject *Sender)
- {
- if (CheckBox1->Checked)
- PaintBox2->Canvas->Brush->Color =
- (MixColor.Blue << 16) | (MixColor.Green << 8) | MixColor.Red;
- else
- PaintBox2->Canvas->Brush->Color = Color;
- PaintBox2->Canvas->Pen->Color = Color;
- PaintBox2->Canvas->Rectangle(PaintBox2->ClientRect);
- }
- //---------------------------------------------------------------------------
介面程式中,實現影象黑白調整功能主要靠Execute函式完成。
下面是幾張程式執行介面圖:
1、預設黑白調整引數執行介面,其中右上邊的下拉編輯框顯示的是一些預設黑白效果選項:
2、選擇紅外線效果黑白調整引數執行介面:
3、使用預設引數進行黑白調整後,再用所選顏色進行著色的介面:
4、在上面介面基礎上,色調不變,加大黃色調引數,使圖中人物衣服顏色明亮一些,同時減少藍色調引數,使人物的圍脖顏色變暗一些:
因水平有限,錯誤在所難免,歡迎指正和指導。郵箱地址:maozefa@hotmail.com
這裡可訪問《C++影象處理 -- 文章索引》。
相關文章
- iOS 影象處理 - 影象拼接iOS
- 影象處理之影象增強
- UIImage 影象處理UI
- Bayer影象處理
- [Python影象處理] 六.影象縮放、影象旋轉、影象翻轉與影象平移Python
- [Python影象處理] 八.影象腐蝕與影象膨脹Python
- Python-OpenCV 處理影象(七):影象灰度化處理PythonOpenCV
- Python-OpenCV 處理影象(八):影象二值化處理PythonOpenCV
- 前端影象處理指南前端
- c#影象處理C#
- MATLAB數字影象處理(二)影象增強Matlab
- 數字影象處理DIP
- 柵格影象的處理
- [Python影象處理] 五.影象融合、加法運算及影象型別轉換Python型別
- 實戰 | 用Python做影象處理(一)Python
- Python-OpenCV 處理影象(三):影象畫素點操作PythonOpenCV
- [Python影象處理] 七.影象閾值化處理及演算法對比Python演算法
- 影象中的畫素處理
- python PIL 影象處理操作Python
- 第十四章 處理影象
- GPU 加速下的影象處理GPU
- [Python影象處理] 一.影象處理基礎知識及OpenCV入門函式PythonOpenCV函式
- Python-OpenCV 處理影象(二):濾鏡和影象運算PythonOpenCV
- [Python影象處理] 三.獲取影象屬性、興趣ROI區域及通道處理Python
- 數字影象處理-第一節
- 影象處理庫GPUImage簡單使用GPUUI
- 數字影象處理目錄列表
- 6 款 Javascript 的影象處理庫JavaScript
- Kinect影象 骨骼點夾角處理
- 使用 canvas 對影象進行處理Canvas
- 神奇的影象處理演算法演算法
- 數字影象處理之二維碼影象提取演算法(一)演算法
- Python-OpenCV 處理影象(四):影象直方圖和反向投影PythonOpenCV直方圖
- C++影象縮放C++
- 影象處理的濾鏡演算法演算法
- Linux 上的科學影象處理Linux
- matlab影象處理常用命令Matlab
- Python影象處理庫Pillow入門Python