12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331 |
- using OpenCvSharp;
- using PaintDotNet.Camera;
- using PaintDotNet.CustomControl;
- using System;
- using System.Collections.Generic;
- using System.Windows.Forms;
- namespace PaintDotNet.Instrument
- {
- /// <summary>
- /// 工具-直方图
- /// </summary>
- internal class HistogramDialog : FloatingToolForm
- {
- #region 初始化变量
- private AppWorkspace AppWorkspace;
- /// <summary>
- /// 一个矩阵数组,用来接收直方图,记得全部初始化
- /// </summary>
- private Mat[] oldHists = new Mat[] { new Mat(), new Mat(), new Mat() };
- /// <summary>
- /// 灰度图
- /// </summary>
- private bool isGray = false;
- /// <summary>
- /// BGR线条颜色
- /// </summary>
- private Scalar[] color = new Scalar[] { new Scalar(255, 0, 0, 255), new Scalar(0, 255, 0, 255), new Scalar(0, 0, 255, 255) };
- /// <summary>
- /// 计算gamma的查找表
- /// </summary>
- private Mat lookUpTable = new Mat(1, 256, MatType.CV_8U);
- #endregion
- #region 控件相关
- private Button button2;
- private Button button3;
- private Button button4;
- private Button button5;
- private Label label1;
- private Label label2;
- private Label label3;
- private NumericUpDown numericUpDown1;
- private Label label4;
- private TriangleTrackBar trackBar1;
- private Label label5;
- private Label label6;
- private TriangleTrackBar trackBar2;
- private Label label7;
- private TriangleTrackBar trackBar3;
- private SelectButton skipButton;
- private SelectButton logButton;
- private UCTrackBar ucTrackBar1;
- private NumericUpDown numericUpDown2;
- private PictureBox pictureBox1;
- private void InitializeLanguageText()
- {
- this.button2.Text = PdnResources.GetString("Menu.optimal.text");
- this.button4.Text = PdnResources.GetString("Menu.Gammavalue.text") + "0.45";
- this.button5.Text = PdnResources.GetString("Menu.Originalstate.text");
- this.label1.Text = PdnResources.GetString("Menu.luminance.text") + ":";
- this.label2.Text = PdnResources.GetString("Menu.Contrast.text") + ":";
- this.label3.Text = PdnResources.GetString("Menu.Gammavalue.text") + ":";
- }
- private void InitializeComponent()
- {
- this.pictureBox1 = new System.Windows.Forms.PictureBox();
- this.button2 = new System.Windows.Forms.Button();
- this.button3 = new System.Windows.Forms.Button();
- this.button4 = new System.Windows.Forms.Button();
- this.button5 = new System.Windows.Forms.Button();
- this.label1 = new System.Windows.Forms.Label();
- this.label2 = new System.Windows.Forms.Label();
- this.label3 = new System.Windows.Forms.Label();
- this.numericUpDown1 = new System.Windows.Forms.NumericUpDown();
- this.label4 = new System.Windows.Forms.Label();
- this.trackBar1 = new PaintDotNet.CustomControl.TriangleTrackBar();
- this.label5 = new System.Windows.Forms.Label();
- this.label6 = new System.Windows.Forms.Label();
- this.trackBar2 = new PaintDotNet.CustomControl.TriangleTrackBar();
- this.label7 = new System.Windows.Forms.Label();
- this.trackBar3 = new PaintDotNet.CustomControl.TriangleTrackBar();
- this.skipButton = new PaintDotNet.CustomControl.SelectButton();
- this.logButton = new PaintDotNet.CustomControl.SelectButton();
- this.ucTrackBar1 = new PaintDotNet.CustomControl.UCTrackBar();
- this.numericUpDown2 = new System.Windows.Forms.NumericUpDown();
- ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit();
- ((System.ComponentModel.ISupportInitialize)(this.numericUpDown1)).BeginInit();
- ((System.ComponentModel.ISupportInitialize)(this.numericUpDown2)).BeginInit();
- this.SuspendLayout();
- //
- // pictureBox1
- //
- this.pictureBox1.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
- | System.Windows.Forms.AnchorStyles.Left)
- | System.Windows.Forms.AnchorStyles.Right)));
- this.pictureBox1.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
- this.pictureBox1.Location = new System.Drawing.Point(12, 12);
- this.pictureBox1.Name = "pictureBox1";
- this.pictureBox1.Size = new System.Drawing.Size(256, 124);
- this.pictureBox1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage;
- this.pictureBox1.TabIndex = 1;
- this.pictureBox1.TabStop = false;
- //
- // button2
- //
- this.button2.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
- this.button2.Location = new System.Drawing.Point(126, 274);
- this.button2.Name = "button2";
- this.button2.Size = new System.Drawing.Size(75, 23);
- this.button2.TabIndex = 4;
- this.button2.Text = "最佳";
- this.button2.UseVisualStyleBackColor = true;
- //
- // button3
- //
- this.button3.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
- this.button3.Location = new System.Drawing.Point(241, 303);
- this.button3.Name = "button3";
- this.button3.Size = new System.Drawing.Size(75, 23);
- this.button3.TabIndex = 5;
- this.button3.Text = "MIN/MAX";
- this.button3.UseVisualStyleBackColor = true;
- //
- // button4
- //
- this.button4.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
- this.button4.Location = new System.Drawing.Point(12, 303);
- this.button4.Name = "button4";
- this.button4.Size = new System.Drawing.Size(75, 23);
- this.button4.TabIndex = 6;
- this.button4.Text = "伽马值0.45";
- this.button4.UseVisualStyleBackColor = true;
- //
- // button5
- //
- this.button5.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
- this.button5.Location = new System.Drawing.Point(126, 303);
- this.button5.Name = "button5";
- this.button5.Size = new System.Drawing.Size(75, 23);
- this.button5.TabIndex = 7;
- this.button5.Text = "原始状态";
- this.button5.UseVisualStyleBackColor = true;
- //
- // label1
- //
- this.label1.AutoSize = true;
- this.label1.Location = new System.Drawing.Point(25, 166);
- this.label1.Margin = new System.Windows.Forms.Padding(2, 0, 2, 0);
- this.label1.Name = "label1";
- this.label1.Size = new System.Drawing.Size(41, 12);
- this.label1.TabIndex = 9;
- this.label1.Text = "亮度:";
- //
- // label2
- //
- this.label2.AutoSize = true;
- this.label2.Location = new System.Drawing.Point(13, 202);
- this.label2.Margin = new System.Windows.Forms.Padding(2, 0, 2, 0);
- this.label2.Name = "label2";
- this.label2.Size = new System.Drawing.Size(53, 12);
- this.label2.TabIndex = 10;
- this.label2.Text = "对比度:";
- //
- // label3
- //
- this.label3.AutoSize = true;
- this.label3.Location = new System.Drawing.Point(13, 238);
- this.label3.Margin = new System.Windows.Forms.Padding(2, 0, 2, 0);
- this.label3.Name = "label3";
- this.label3.Size = new System.Drawing.Size(53, 12);
- this.label3.TabIndex = 11;
- this.label3.Text = "伽马值:";
- //
- // numericUpDown1
- //
- this.numericUpDown1.Location = new System.Drawing.Point(242, 276);
- this.numericUpDown1.Margin = new System.Windows.Forms.Padding(2);
- this.numericUpDown1.Maximum = new decimal(new int[] {
- 499,
- 0,
- 0,
- 0});
- this.numericUpDown1.Name = "numericUpDown1";
- this.numericUpDown1.Size = new System.Drawing.Size(50, 21);
- this.numericUpDown1.TabIndex = 12;
- this.numericUpDown1.TextAlign = System.Windows.Forms.HorizontalAlignment.Right;
- this.numericUpDown1.Value = new decimal(new int[] {
- 200,
- 0,
- 0,
- 0});
- //
- // label4
- //
- this.label4.AutoSize = true;
- this.label4.Location = new System.Drawing.Point(296, 280);
- this.label4.Margin = new System.Windows.Forms.Padding(2, 0, 2, 0);
- this.label4.Name = "label4";
- this.label4.Size = new System.Drawing.Size(17, 12);
- this.label4.TabIndex = 13;
- this.label4.Text = "‰";
- //
- // trackBar1
- //
- this.trackBar1.Location = new System.Drawing.Point(67, 159);
- this.trackBar1.Maximum = 200;
- this.trackBar1.Minimum = -200;
- this.trackBar1.Name = "trackBar1";
- this.trackBar1.TrackBarName = "trackBar1";
- this.trackBar1.Size = new System.Drawing.Size(200, 30);
- this.trackBar1.TabIndex = 14;
- this.trackBar1.Value = -50;
- //
- // label5
- //
- this.label5.AutoSize = true;
- this.label5.Location = new System.Drawing.Point(272, 166);
- this.label5.Margin = new System.Windows.Forms.Padding(2, 0, 2, 0);
- this.label5.Name = "label5";
- this.label5.Size = new System.Drawing.Size(29, 12);
- this.label5.TabIndex = 15;
- this.label5.Text = "-0.5";
- //
- // label6
- //
- this.label6.AutoSize = true;
- this.label6.Location = new System.Drawing.Point(272, 202);
- this.label6.Margin = new System.Windows.Forms.Padding(2, 0, 2, 0);
- this.label6.Name = "label6";
- this.label6.Size = new System.Drawing.Size(29, 12);
- this.label6.TabIndex = 17;
- this.label6.Text = "1.00";
- //
- // trackBar2
- //
- this.trackBar2.Location = new System.Drawing.Point(67, 195);
- this.trackBar2.Maximum = 999;
- this.trackBar2.Minimum = 0;
- this.trackBar2.Name = "trackBar2";
- this.trackBar2.TrackBarName = "trackBar2";
- this.trackBar2.Size = new System.Drawing.Size(200, 30);
- this.trackBar2.TabIndex = 16;
- this.trackBar2.Value = 100;
- //
- // label7
- //
- this.label7.AutoSize = true;
- this.label7.Location = new System.Drawing.Point(272, 238);
- this.label7.Margin = new System.Windows.Forms.Padding(2, 0, 2, 0);
- this.label7.Name = "label7";
- this.label7.Size = new System.Drawing.Size(29, 12);
- this.label7.TabIndex = 19;
- this.label7.Text = "1.00";
- this.label7.Visible = false;
- //
- // trackBar3
- //
- this.trackBar3.Location = new System.Drawing.Point(67, 230);
- this.trackBar3.Maximum = 299;
- this.trackBar3.Minimum = 0;
- this.trackBar3.Name = "trackBar3";
- this.trackBar3.TrackBarName = "trackBar3";
- this.trackBar3.Size = new System.Drawing.Size(200, 30);
- this.trackBar3.TabIndex = 18;
- this.trackBar3.Value = 100;
- //
- // skipButton
- //
- this.skipButton.BackColor = System.Drawing.SystemColors.ControlDark;
- this.skipButton.BtnSelect = false;
- this.skipButton.BtnText = "skip";
- this.skipButton.Location = new System.Drawing.Point(275, 77);
- this.skipButton.Name = "skipButton";
- this.skipButton.Size = new System.Drawing.Size(41, 21);
- this.skipButton.TabIndex = 20;
- //
- // logButton
- //
- this.logButton.BackColor = System.Drawing.SystemColors.ControlDark;
- this.logButton.BtnSelect = false;
- this.logButton.BtnText = "log";
- this.logButton.Location = new System.Drawing.Point(275, 46);
- this.logButton.Name = "logButton";
- this.logButton.Size = new System.Drawing.Size(41, 21);
- this.logButton.TabIndex = 21;
- //
- // ucTrackBar1
- //
- this.ucTrackBar1.DcimalDigits = 0;
- this.ucTrackBar1.LineColor = System.Drawing.Color.FromArgb(((int)(((byte)(255)))), ((int)(((byte)(77)))), ((int)(((byte)(59)))));
- this.ucTrackBar1.LineWidth = 8F;
- this.ucTrackBar1.Location = new System.Drawing.Point(4, 139);
- this.ucTrackBar1.MaxValue = 255F;
- this.ucTrackBar1.MinValue = 0F;
- this.ucTrackBar1.Name = "ucTrackBar1";
- this.ucTrackBar1.Size = new System.Drawing.Size(274, 20);
- this.ucTrackBar1.TabIndex = 22;
- this.ucTrackBar1.Text = "ucTrackBar1";
- this.ucTrackBar1.Value1 = 0F;
- this.ucTrackBar1.Value2 = 127F;
- this.ucTrackBar1.Value3 = 255F;
- //
- // numericUpDown2
- //
- this.numericUpDown2.DecimalPlaces = 2;
- this.numericUpDown2.Increment = new decimal(new int[] {
- 1,
- 0,
- 0,
- 131072});
- this.numericUpDown2.Location = new System.Drawing.Point(266, 235);
- this.numericUpDown2.Margin = new System.Windows.Forms.Padding(2);
- this.numericUpDown2.Maximum = new decimal(new int[] {
- 299,
- 0,
- 0,
- 131072});
- this.numericUpDown2.Name = "numericUpDown2";
- this.numericUpDown2.Size = new System.Drawing.Size(50, 21);
- this.numericUpDown2.TabIndex = 23;
- this.numericUpDown2.TextAlign = System.Windows.Forms.HorizontalAlignment.Right;
- this.numericUpDown2.Value = new decimal(new int[] {
- 100,
- 0,
- 0,
- 131072});
- //
- // HistogramDialog
- //
- this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
- this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
- this.ClientSize = new System.Drawing.Size(325, 338);
- this.Controls.Add(this.numericUpDown2);
- this.Controls.Add(this.ucTrackBar1);
- this.Controls.Add(this.logButton);
- this.Controls.Add(this.skipButton);
- this.Controls.Add(this.label7);
- this.Controls.Add(this.trackBar3);
- this.Controls.Add(this.label6);
- this.Controls.Add(this.trackBar2);
- this.Controls.Add(this.label5);
- this.Controls.Add(this.trackBar1);
- this.Controls.Add(this.label4);
- this.Controls.Add(this.numericUpDown1);
- this.Controls.Add(this.label3);
- this.Controls.Add(this.label2);
- this.Controls.Add(this.label1);
- this.Controls.Add(this.button5);
- this.Controls.Add(this.button4);
- this.Controls.Add(this.button3);
- this.Controls.Add(this.button2);
- this.Controls.Add(this.pictureBox1);
- this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
- this.Margin = new System.Windows.Forms.Padding(6);
- this.Name = "HistogramDialog";
- this.Text = "直方图";
- this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.HistogramDialog_FormClosing);
- this.Controls.SetChildIndex(this.pictureBox1, 0);
- this.Controls.SetChildIndex(this.button2, 0);
- this.Controls.SetChildIndex(this.button3, 0);
- this.Controls.SetChildIndex(this.button4, 0);
- this.Controls.SetChildIndex(this.button5, 0);
- this.Controls.SetChildIndex(this.label1, 0);
- this.Controls.SetChildIndex(this.label2, 0);
- this.Controls.SetChildIndex(this.label3, 0);
- this.Controls.SetChildIndex(this.numericUpDown1, 0);
- this.Controls.SetChildIndex(this.label4, 0);
- this.Controls.SetChildIndex(this.trackBar1, 0);
- this.Controls.SetChildIndex(this.label5, 0);
- this.Controls.SetChildIndex(this.trackBar2, 0);
- this.Controls.SetChildIndex(this.label6, 0);
- this.Controls.SetChildIndex(this.trackBar3, 0);
- this.Controls.SetChildIndex(this.label7, 0);
- this.Controls.SetChildIndex(this.skipButton, 0);
- this.Controls.SetChildIndex(this.logButton, 0);
- this.Controls.SetChildIndex(this.ucTrackBar1, 0);
- this.Controls.SetChildIndex(this.numericUpDown2, 0);
- ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit();
- ((System.ComponentModel.ISupportInitialize)(this.numericUpDown1)).EndInit();
- ((System.ComponentModel.ISupportInitialize)(this.numericUpDown2)).EndInit();
- this.ResumeLayout(false);
- this.PerformLayout();
- }
- #endregion
- public HistogramDialog(AppWorkspace AppWorkspace)
- {
- this.AppWorkspace = AppWorkspace;
- InitializeComponent();
- InitializeLanguageText();
- this.Text = PdnResources.GetString("Menu.Tools.Histogram.Text");
- if (this.AppWorkspace.ActiveDocumentWorkspace != null)
- {
- updateHistImg(oldHists);
- updateHistogramRect(false);
- }
- if(this.AppWorkspace != null)
- this.AppWorkspace.ActiveDocumentWorkspaceChanged += new EventHandler(ButtonFocus);
-
- AddAllEvent();
- }
- /// <summary>
- /// 更改按钮焦点
- /// </summary>
- /// <param name="dis">按钮区分</param>
- public void ButtonFocus(object sender, EventArgs e)
- {
- if(AppWorkspace != null && AppWorkspace.ActiveDocumentWorkspace != null)
- {
- // 最佳
- if (AppWorkspace.ActiveDocumentWorkspace.buttonType == 0)
- {
- this.button2.Focus();
- }
- // 最大最小
- if (AppWorkspace.ActiveDocumentWorkspace.buttonType == 1)
- {
- this.button3.Focus();
- }
- // 伽马45
- if (AppWorkspace.ActiveDocumentWorkspace.buttonType == 2)
- {
- this.button4.Focus();
- }
- // 原始
- if (AppWorkspace.ActiveDocumentWorkspace.buttonType == 3)
- {
- this.button5.Focus();
- }
- }
- }
- /// <summary>
- /// 重置Mat
- /// </summary>
- public void ResetMat()
- {
- if (this.AppWorkspace.ActiveDocumentWorkspace == null || this.AppWorkspace.ActiveDocumentWorkspace.CompositionSurface == null)
- {
- this.Visible = false;
- isGray = true;
- return;
- }
- this.RemoveAllEvent();
- Mat[] mats = Cv2.Split(this.AppWorkspace.ActiveDocumentWorkspace.BoxMat);
- Mat[] mats0 = new Mat[] { mats[0] };
- Mat[] mats1 = new Mat[] { mats[1] };
- Mat[] mats2 = new Mat[] { mats[2] };
- int[] channels0 = new int[] { 0 };
- int[] channels1 = new int[] { 0 };
- int[] channels2 = new int[] { 0 };
- int[] histsize = new int[] { 256 };
- Rangef[] range = new Rangef[1];
- range[0] = new Rangef(0.0F, 256.0F);
- Mat mask = new Mat();
- Cv2.CalcHist(mats0, channels0, mask, oldHists[0], 1, histsize, range);
- Cv2.CalcHist(mats1, channels1, mask, oldHists[1], 1, histsize, range);
- Cv2.CalcHist(mats2, channels2, mask, oldHists[2], 1, histsize, range);
- if (this.AppWorkspace.ActiveDocumentWorkspace != null)
- {
- //获取gamma值
- double gamma = this.AppWorkspace.ActiveDocumentWorkspace.HistogramGamma;
- this.trackBar3.Value = Math.Min(299, (int)(gamma * 100));
- numericUpDown2.Value = decimal.Parse(gamma.ToString("f2"));
- //获取对比度
- double alpha = this.AppWorkspace.ActiveDocumentWorkspace.HistogramAlpha;
- //获取亮度
- double beta = this.AppWorkspace.ActiveDocumentWorkspace.HistogramBeta;
- //计算对比度
- label6.Text = alpha.ToString("f2");
- trackBar2.Value = Math.Min(trackBar2.Maximum, (int)(double.Parse(label6.Text) * 100));
- //计算亮度
- if (beta != double.Parse(label5.Text))
- {
- label5.Text = beta.ToString("f2");
- trackBar1.Value = (int)(double.Parse(label5.Text) * 100);
- }
- this.numericUpDown1.Value = new decimal(new int[] { (int)this.AppWorkspace.ActiveDocumentWorkspace.HistogramPercent, 0, 0, 0});
- skipButton.BtnSelect = this.AppWorkspace.ActiveDocumentWorkspace.HistogramSkipEnabled;
- logButton.BtnSelect = this.AppWorkspace.ActiveDocumentWorkspace.HistogramLogEnabled;
- }
- this.updateHistImg(null);
- this.updateHistogramRect(true);
- this.AddAllEvent();
- }
- /// <summary>
- /// 绘制直方图
- /// </summary>
- /// <param name="hist"></param>
- /// <returns></returns>
- private unsafe void updateHistImg(Mat[] hists)
- {
- if (this.AppWorkspace.ActiveDocumentWorkspace == null)
- {
- return;
- }
- //分割图像
- //Mat[] mats = Cv2.Split(this.AppWorkspace.ActiveDocumentWorkspace.OldMat);
- Mat[] mats = Cv2.Split(this.AppWorkspace.ActiveDocumentWorkspace.BoxMat);
- Mat[] mats0 = new Mat[] { mats[0] };
- Mat[] mats1 = new Mat[] { mats[1] };
- Mat[] mats2 = new Mat[] { mats[2] };
- //判断是否灰度
- isGray = ((mats[0].Sum())[0] == (mats[1].Sum())[0] && (mats[1].Sum())[0] == (mats[2].Sum())[0]);
-
- if (hists == null)
- {
- hists = new Mat[] { new Mat(), new Mat(), new Mat() };//一个矩阵数组,用来接收直方图,记得全部初始化
- int[] channels0 = new int[] { 0 };//一个通道,初始化为通道0,这些东西可以共用设置一个就行
- int[] channels1 = new int[] { 0 };
- int[] channels2 = new int[] { 0 };
- int[] histsize = new int[] { 256 };//一个通道,初始化为256箱子
- Rangef[] range = new Rangef[1];//一个通道,范围
- range[0] = new Rangef(0.0F, 256.0F);
- Mat mask = new Mat();//不做掩码
- Cv2.CalcHist(mats0, channels0, mask, hists[0], 1, histsize, range);//对被拆分的图片单独进行计算
- Cv2.CalcHist(mats1, channels1, mask, hists[1], 1, histsize, range);//对被拆分的图片单独进行计算
- Cv2.CalcHist(mats2, channels2, mask, hists[2], 1, histsize, range);//对被拆分的图片单独进行计算
- if (logButton.BtnSelect)
- {
- //取对数
- for (int j = 0; j < hists.Length; j++)
- {
- List<float> ProbPixel = new List<float>();
- for (int i = 0; i < hists[j].Rows; i++)
- {
- if (((float*)hists[j].Ptr(0))[i] == 0)
- {
- ((float*)hists[j].Ptr(0))[i] = ((float*)hists[j].Ptr(0))[i];
- ProbPixel.Add(0);
- }
- else
- {
- ((float*)hists[j].Ptr(0))[i] = (float)Math.Log10(((float*)hists[j].Ptr(0))[i]/* + 9*/);
- ProbPixel.Add(1);
- }
- }
- double max1jVal = 0;
- double min1jVal = 0;
- //找到直方图中的最大值和最小值
- Cv2.MinMaxLoc(hists[j], out min1jVal, out max1jVal);
- if (min1jVal >= max1jVal)
- {
- continue;
- }
- //归一化到0~255,并根据AxioVision添加偏置值
- for (int i = 0; i < hists[j].Rows; i++)
- {
- ((float*)hists[j].Ptr(0))[i] = (float)((((float*)hists[j].Ptr(0))[i] - 0) * 255.0 / (max1jVal - 0)) + (float)(min1jVal > 0 ? (ProbPixel[i] * min1jVal * 255.0 / max1jVal) : ProbPixel[i] * 5);
- }
- }
- }
- if (skipButton.BtnSelect)
- {
- //去掉黑色部分 和 白色部分
- for (int j = 0; j < hists.Length; j++)
- {
- ((float*)hists[j].Ptr(0))[0] = 0;
- ((float*)hists[j].Ptr(0))[255] = 0;
- }
- }
- }
- double max2Val = 0;
- double max1Val = 0;
- double max0Val = 0;
- double minVal = 0;
- //为了更好显示直方图细节,使用4倍尺寸绘制直方图
- int histSize = hists[0].Rows * 4;
- Mat histImg = new Mat(histSize, histSize, MatType.CV_8UC3, new Scalar(255, 255, 255));
- //找到直方图中的最大值和最小值
- Cv2.MinMaxLoc(hists[2], out minVal, out max2Val);
- Cv2.MinMaxLoc(hists[1], out minVal, out max1Val);
- Cv2.MinMaxLoc(hists[0], out minVal, out max0Val);
- double maxVal = Math.Max(max2Val, Math.Max(max0Val, max1Val));
- if (maxVal < 1)
- return;
- // 设置最大峰值为图像高度的90%
- double hpt = 0.9 * histSize;
- //灰度图的显示直方图较为简单,显示一个通道即可
- if (isGray)
- {
- Mat hist = hists[0];
- int lastY2 = histSize - 1;
- for (int h = 0; h < hists[0].Rows; h++)
- {
- int intensity = (int)(hist.At<float>(h) * hpt / maxVal);
- int lastY1 = lastY2;
- lastY2 = histSize - intensity - 1 - (hist.At<float>(h) > 0 ? 4 : 0);
- if (lastY2 < lastY1)
- {
- Cv2.Line(histImg, new OpenCvSharp.Point(h * 4, lastY1), new OpenCvSharp.Point(h * 4, Math.Min(lastY2, lastY1 - 2)), new Scalar(0, 0, 0, 255), 2, LineTypes.Link4);
- }
- else
- {
- Cv2.Line(histImg, new OpenCvSharp.Point(h * 4, lastY1), new OpenCvSharp.Point(h * 4, Math.Max(lastY2, lastY1 + 2)), new Scalar(0, 0, 0, 255), 2, LineTypes.Link4);
- }
- }
- }
- //彩度图显示BGR三个通道的直方图
- else
- {
- int lineWidth = 2;
- for (int j = hists.Length - 1; j >= 0; j--)
- {
- Mat hist = hists[j];
- int lastY2 = histSize - 1;
- for (int h = 0; h < hists[0].Rows; h++)
- {
- int intensity = (int)(hist.At<float>(h) * hpt / maxVal);
- int lastY1 = lastY2;
- lastY2 = histSize - intensity - 1 - (hist.At<float>(h) > 0 ? 4 : 0);
- if (h > 0)
- {
- //显示0.5位置的直方图,这样与AxioVision效果更加近似
- int lasty12 = (lastY1 + lastY2) / 2;
- if (lasty12 < lastY1)
- {
- Cv2.Line(histImg, new OpenCvSharp.Point(h * 4 - 2, lastY1), new OpenCvSharp.Point(h * 4 - 2, Math.Min(lasty12, lastY1 - 2)), color[j], lineWidth, LineTypes.Link4);
- }
- else
- {
- Cv2.Line(histImg, new OpenCvSharp.Point(h * 4 - 2, lastY1), new OpenCvSharp.Point(h * 4 - 2, Math.Max(lasty12, lastY1 + 2)), color[j], lineWidth, LineTypes.Link4/*AntiAlias*/);
- }
- if (lastY2 < lasty12)
- {
- Cv2.Line(histImg, new OpenCvSharp.Point(h * 4, lasty12), new OpenCvSharp.Point(h * 4, Math.Min(lastY2, lasty12 - 2)), color[j], lineWidth, LineTypes.Link4);
- }
- else
- {
- Cv2.Line(histImg, new OpenCvSharp.Point(h * 4, lasty12), new OpenCvSharp.Point(h * 4, Math.Max(lastY2, lasty12 + 2)), color[j], lineWidth, LineTypes.Link4/*AntiAlias*/);
- }
- }
- else
- {
- //灰度值为0的线的绘制
- if (lastY2 < lastY1)
- {
- Cv2.Line(histImg, new OpenCvSharp.Point(h * 4, lastY1), new OpenCvSharp.Point(h * 4, Math.Min(lastY2, lastY1 - 2)), color[j], lineWidth, LineTypes.Link4);
- }
- else
- {
- Cv2.Line(histImg, new OpenCvSharp.Point(h * 4, lastY1), new OpenCvSharp.Point(h * 4, Math.Max(lastY2, lastY1 + 2)), color[j], lineWidth, LineTypes.Link4/*AntiAlias*/);
- }
- }
- }
- }
- }
- this.pictureBox1.BackgroundImage = OpenCvSharp.Extensions.BitmapConverter.ToBitmap(histImg);
- }
- /// <summary>
- /// 将拟合点绘制到空白图上
- /// </summary>
- /// <param name="key_point">拟合点</param>
- /// <param name="n">拟合矩阵的数量</param>
- /// <param name="A">空白图</param>
- /// <returns></returns>
- private unsafe bool polynomial_curve_fit(OpenCvSharp.Point[] key_point, int n, Mat A)
- {
- int N = key_point.Length;
- //构造矩阵X
- Mat X = new Mat(n + 1, n + 1, MatType.CV_64FC1, new Scalar(0));
- for (int i = 0; i < n + 1; i++)
- {
- for (int j = 0; j < n + 1; j++)
- {
- ((double*)X.Ptr(i))[j] = 0;
- for (int k = 0; k < N; k++)
- {
- ((double*)X.Ptr(i))[j] = X.At<double>(i, j) + (double)Math.Pow(key_point[k].X, i + j);
- }
- }
- }
- //构造矩阵Y
- Mat Y = new Mat(n + 1, 1, MatType.CV_64FC1, new Scalar(0));
- for (int i = 0; i < n + 1; i++)
- {
- ((double*)Y.Ptr(i))[0] = 0;//111
- for (int k = 0; k < N; k++)
- {
- ((double*)Y.Ptr(i))[0] = Y.At<double>(i, 0) + (double)(Math.Pow(key_point[k].X, i) * key_point[k].Y);
- }
- }
- //求解矩阵A
- Cv2.Solve(X, Y, A, DecompTypes.LU);
- return true;
- }
- /// <summary>
- /// 通过颜色通道去除像素点的千分比计算有效阈值
- /// </summary>
- /// <param name="hists"></param>
- /// <param name="percentValue"></param>
- /// <returns></returns>
- private OpenCvSharp.Point getStartIndex(Mat mat, Mat[] hists, float percentValue)
- {
- //灰度化图片取第一通道
- Mat histGray = hists[0];
- //彩色图片取灰度化后的通道
- if (!isGray)
- {
- OpenCvSharp.Point startIndex = new OpenCvSharp.Point(0, histGray.Rows);
- //计算三次
- OpenCvSharp.Point startIndex0 = this.getStartRange(mat, hists[0], percentValue);
- OpenCvSharp.Point startIndex1 = this.getStartRange(mat, hists[1], percentValue);
- OpenCvSharp.Point startIndex2 = this.getStartRange(mat, hists[2], percentValue);
- startIndex.X = Math.Min(startIndex0.X, Math.Min(startIndex1.X, startIndex2.X));
- startIndex.Y = Math.Max(startIndex0.Y, Math.Max(startIndex1.Y, startIndex2.Y));
- startIndex.Y = Math.Min(255 - 1, startIndex.Y);
- startIndex.X = Math.Max(startIndex.X, 1);
- startIndex.Y = Math.Max(startIndex.X + 1, startIndex.Y);
- return startIndex;
- }
- return this.getStartRange(mat, histGray, percentValue);
- }
- private OpenCvSharp.Point getStartRange(Mat mat, Mat histGray, float percentValue)
- {
- if (this.AppWorkspace.ActiveDocumentWorkspace == null)
- {
- return new OpenCvSharp.Point(1, 254); ;
- }
- //计算灰度分布密度
- //float totalPoints = this.AppWorkspace.ActiveDocumentWorkspace.OldMat.Cols * this.AppWorkspace.ActiveDocumentWorkspace.OldMat.Rows;
- float totalPoints = mat.Cols * mat.Rows;
- OpenCvSharp.Point startIndex = new OpenCvSharp.Point(0, histGray.Rows);
- bool foundStartTag = false;
- bool foundEndTag = false;
- //根据累计直方图分布计算左右阈值
- float equalizeHists = 0;
- for (int i = 0; i < histGray.Rows; ++i)
- {
- equalizeHists += ((float)(histGray.At<float>(i) / (totalPoints * 1.0))) * 1000;
- if (!foundStartTag && equalizeHists > percentValue)
- {
- foundStartTag = true;
- startIndex.X = i;
- }
- else if (equalizeHists >= 1000 - percentValue)
- {
- foundEndTag = true;
- startIndex.Y = i;
- }
- if (foundStartTag && foundEndTag)
- break;
- }
- startIndex.Y = Math.Min(255 - 1, startIndex.Y);
- startIndex.X = Math.Max(startIndex.X, 1);
- startIndex.Y = Math.Max(startIndex.X + 1, startIndex.Y);
- return startIndex;
- }
- /// <summary>
- /// 更新直方图特征曲线
- /// </summary>
- /// <param name="updateThreePoints">是否需要更新三个控制点的位置</param>
- private void updateHistogramRect(bool updateThreePoints)
- {
- //为了更好显示直方图细节,使用4倍尺寸绘制直方图
- int histSize = 256 * 4;
- //创建用于绘制的深蓝色背景图像
- Mat image = new Mat(histSize, histSize, MatType.CV_8UC4, new Scalar(0, 0, 0, 0));
- //输入拟合点
- List<OpenCvSharp.Point> points = new List<OpenCvSharp.Point>();
- //根据亮度和对比度计算左右阈值
- int grayMin = (int)(0 - (256.0 / (double.Parse(label6.Text) == 0 ? 0.01 : double.Parse(label6.Text)) + double.Parse(label5.Text) * 510.0) / 2.0);
- int grayMax = (int)((256.0 / (double.Parse(label6.Text) == 0 ? 0.01 : double.Parse(label6.Text)) - double.Parse(label5.Text) * 510.0) / 2.0);
- if (grayMin >= grayMax)
- {
- return;
- }
- if (updateThreePoints)
- {
- ucTrackBar1.Value1 = grayMin;
- ucTrackBar1.Value3 = grayMax;
- }
- //gamma运算,gamma值根据AxioVision对比 取最小值0.01进行计算
- double gamma = Math.Max(0.01, /*double.Parse(label7.Text)*/(double)numericUpDown2.Value);
- //计算Value2
- ucTrackBar1.Value2 = (float)Math.Max(grayMin, Math.Min(grayMax, Math.Pow(0.5, 1.0 / gamma) * (grayMax - grayMin) + grayMin));
- ucTrackBar1.Refresh();
- int lastY2 = histSize + 0;// histSize - 1;
- for (int h = 0; h <= 256; h++)
- {
- if (h < grayMin)
- {
- points.Add(new OpenCvSharp.Point(h * 4, lastY2));
- continue;
- }
- else if (h > grayMax)
- {
- lastY2 = -1;// 0;
- points.Add(new OpenCvSharp.Point(h * 4, lastY2));
- continue;
- }
- int lastY1 = (int)Math.Round((1.0 - Math.Pow((h - grayMin) * 1.0 / (grayMax - grayMin), gamma)) * (histSize + 1/*histSize - 1*/)) - 1;
- lastY1 = Math.Min(lastY2, Math.Max(0, lastY1));
- if (lastY2 == lastY1)
- {
- if (h == 0 || h == 256)
- {
- points.Add(new OpenCvSharp.Point(h * 4, lastY1));
- }
- }
- else
- {
- points.Add(new OpenCvSharp.Point(h * 4, lastY1));
- lastY2 = lastY1;
- }
- }
- lastY2 = histSize + 0;
- //将第一个点的位置设置最底部,保证曲线平滑性
- List<List<OpenCvSharp.Point>> pointsIn = new List<List<OpenCvSharp.Point>>() { points };
- //绘制折线
- Cv2.Polylines(image, pointsIn, false, new Scalar(0, 0, 0, 255), 4, LineTypes.Link8, 0);
- Mat A = new Mat(3 + 1, 1, MatType.CV_64FC1, new Scalar(0));
- polynomial_curve_fit(points.ToArray(), 3, A);
- List<OpenCvSharp.Point> points_fitted = new List<OpenCvSharp.Point>();
- List<double> dataFitted = new List<double>();
- dataFitted.Add(A.At<double>(0, 0));
- dataFitted.Add(A.At<double>(1, 0));
- dataFitted.Add(A.At<double>(2, 0));
- dataFitted.Add(A.At<double>(3, 0));
- for (int x = 0; x < histSize + 1; x++)
- {
- if (x < grayMin * 4)
- {
- points_fitted.Add(new OpenCvSharp.Point(x, lastY2));
- continue;
- }
- else if (x > grayMax * 4)
- {
- lastY2 = -1;
- points_fitted.Add(new OpenCvSharp.Point(x, lastY2));
- continue;
- }
- double y = A.At<double>(0, 0) + A.At<double>(1, 0) * x + A.At<double>(2, 0) * Math.Pow(x, 2) + A.At<double>(3, 0) * Math.Pow(x, 3);
- points_fitted.Add(new OpenCvSharp.Point(x, y));
- }
- ////绘制折线
- this.pictureBox1.Image = OpenCvSharp.Extensions.BitmapConverter.ToBitmap(image);
- }
- /// <summary>
- /// 根据亮度、对比度、gamma对图片进行处理
- /// https://blog.csdn.net/kuweicai/article/details/78953017
- /// https://blog.csdn.net/ren365880/article/details/103875302
- /// https://zhuanlan.zhihu.com/p/92945623
- /// </summary>
- private unsafe void scrollMethod()
- {
- if (this.AppWorkspace.ActiveDocumentWorkspace == null)
- {
- return;
- }
- Mat[] mats = Cv2.Split(this.AppWorkspace.ActiveDocumentWorkspace.OldMat);
- //根据亮度和对比度计算左右阈值
- int grayMin = (int)(0 - (256.0 / (double.Parse(label6.Text) == 0 ? 0.01 : double.Parse(label6.Text)) + double.Parse(label5.Text) * 510.0) / 2.0);
- int grayMax = (int)((256.0 / (double.Parse(label6.Text) == 0 ? 0.01 : double.Parse(label6.Text)) - double.Parse(label5.Text) * 510.0) / 2.0);
- if (grayMin >= grayMax)
- {
- return;
- }
- Mat dst = new Mat();
- //如果是灰度化图片,则只需计算一个通道。对灰度值进行映射,BGR根据左右阈值做缩放操作
- if (isGray)
- {
- Cv2.Min(mats[0], new Mat(mats[0].Rows, mats[0].Cols, MatType.CV_8UC1, new Scalar(grayMax)), dst);
- Cv2.Max(dst, new Mat(mats[0].Rows, mats[0].Cols, MatType.CV_8UC1, new Scalar(grayMin)), dst);
- Cv2.Normalize(dst, dst, 0, 255, NormTypes.MinMax);
- }
- else
- {
- for (int j = 0; j < 3; j++)
- {
- Cv2.Min(mats[j], new Mat(mats[j].Rows, mats[j].Cols, MatType.CV_8UC1, new Scalar(grayMax)), mats[j]);
- Cv2.Max(mats[j], new Mat(mats[j].Rows, mats[j].Cols, MatType.CV_8UC1, new Scalar(grayMin)), mats[j]);
- }
- Cv2.Merge(mats, dst);
- Cv2.Normalize(dst, dst, 0, 255, NormTypes.MinMax);
- }
- /*Mat dst = this.AppWorkspace.ActiveDocumentWorkspace.OldMat;
- if (dst.Channels() > 3) dst = dst.CvtColor(ColorConversionCodes.BGRA2BGR);
- dst.ConvertTo(dst, dst.Type(), double.Parse(label6.Text), double.Parse(label5.Text));*/
- /*//灰度归一化
- dst.ConvertTo(dst, MatType.CV_64F, 1.0 / 255, 0);
- //gamma运算,gamma值根据AxioVision对比 取最小值0.01进行计算
- Cv2.Pow(dst, Math.Max(0.01, (double)numericUpDown2.Value), dst);
- dst.ConvertTo(dst, MatType.CV_8U, 255, 0);*/
-
- for (int i = 0; i < 256; ++i)
- lookUpTable.Set<byte>(0, i, (byte)(Math.Pow(i / 255.0, (double)numericUpDown2.Value) * 255.0));
- Cv2.LUT(dst, lookUpTable, dst);
- Document document = Document.FromImageMatForHistogram(dst, this.AppWorkspace.ActiveDocumentWorkspace.CompositionSurface);
- this.AppWorkspace.ActiveDocumentWorkspace.setDoc(document, false);
- this.AppWorkspace.ActiveDocumentWorkspace.Refresh();
- }
-
- private void HistogramDialog_FormClosing(object sender, FormClosingEventArgs e)
- {
- this.AppWorkspace.toolBar.RefreshBtnSelect(false, "Histogram");
- this.AppWorkspace.toolsPanel.RefreshBtnSelect(false, "Histogram");
- }
- /// <summary>
- /// 移除事件
- /// </summary>
- private void RemoveAllEvent()
- {
- /*this.button2.Click -= new System.EventHandler(this.button2_Click);
- this.button3.Click -= new System.EventHandler(this.button3_Click);
- this.button4.Click -= new System.EventHandler(this.button4_Click);
- this.button5.Click -= new System.EventHandler(this.button5_Click);
- this.numericUpDown1.ValueChanged -= new System.EventHandler(this.numericUpDown1_ValueChanged);
- this.label5.TextChanged -= new System.EventHandler(this.label5_Changed);
- this.label6.TextChanged -= new System.EventHandler(this.label6_Changed);
- this.label7.TextChanged -= new System.EventHandler(this.label7_Changed);
- this.skipButton.Click -= new System.EventHandler(this.skipButton_Click);
- this.logButton.Click -= new System.EventHandler(this.logButton_Click);
- this.ucTrackBar1.Value1Changed -= new System.EventHandler(this.ucTrackBar1_Value1Changed);
- this.ucTrackBar1.Value2Changed -= new System.EventHandler(this.ucTrackBar1_Value2Changed);
- this.ucTrackBar1.Value3Changed -= new System.EventHandler(this.ucTrackBar1_Value3Changed);
- this.numericUpDown2.ValueChanged -= new System.EventHandler(this.numericUpDown2_ValueChanged);*/
- this.button2.Click -= new System.EventHandler(this.EventIncident);
- this.button3.Click -= new System.EventHandler(this.EventIncident);
- this.button4.Click -= new System.EventHandler(this.EventIncident);
- this.button5.Click -= new System.EventHandler(this.EventIncident);
- this.numericUpDown1.ValueChanged -= new System.EventHandler(this.EventIncident);
- this.label5.TextChanged -= new System.EventHandler(this.EventIncident);
- this.label6.TextChanged -= new System.EventHandler(this.EventIncident);
- this.label7.TextChanged -= new System.EventHandler(this.EventIncident);
- this.skipButton.Click -= new System.EventHandler(this.EventIncident);
- this.logButton.Click -= new System.EventHandler(this.EventIncident);
- this.numericUpDown2.ValueChanged -= new System.EventHandler(this.EventIncident);
- this.ucTrackBar1.Value1Changed -= new System.EventHandler(this.ucTrackBar1_Value1Changed);
- this.ucTrackBar1.Value2Changed -= new System.EventHandler(this.ucTrackBar1_Value2Changed);
- this.ucTrackBar1.Value3Changed -= new System.EventHandler(this.ucTrackBar1_Value3Changed);
- this.ucTrackBar1.ValueMouseUp -= new System.EventHandler(this.EventIncident);
- this.trackBar1.TrackBarScroll -= new System.EventHandler(this.trackBar1_Scroll);
- this.trackBar2.TrackBarScroll -= new System.EventHandler(this.trackBar2_Scroll);
- this.trackBar3.TrackBarScroll -= new System.EventHandler(this.trackBar3_Scroll);
- this.trackBar1.TrackBarMouseUp -= new System.EventHandler(this.EventIncident);
- this.trackBar2.TrackBarMouseUp -= new System.EventHandler(this.EventIncident);
- this.trackBar3.TrackBarMouseUp -= new System.EventHandler(this.EventIncident);
- }
- /// <summary>
- /// 添加事件
- /// </summary>
- private void AddAllEvent()
- {
- /*this.button2.Click += new System.EventHandler(this.button2_Click);
- this.button3.Click += new System.EventHandler(this.button3_Click);
- this.button4.Click += new System.EventHandler(this.button4_Click);
- this.button5.Click += new System.EventHandler(this.button5_Click);
- this.numericUpDown1.ValueChanged += new System.EventHandler(this.numericUpDown1_ValueChanged);
- this.label5.TextChanged += new System.EventHandler(this.label5_Changed);
- this.label6.TextChanged += new System.EventHandler(this.label6_Changed);
- this.label7.TextChanged += new System.EventHandler(this.label7_Changed);
- this.skipButton.Click += new System.EventHandler(this.skipButton_Click);
- this.logButton.Click += new System.EventHandler(this.logButton_Click);
- this.ucTrackBar1.Value1Changed += new System.EventHandler(this.ucTrackBar1_Value1Changed);
- this.ucTrackBar1.Value2Changed += new System.EventHandler(this.ucTrackBar1_Value2Changed);
- this.ucTrackBar1.Value3Changed += new System.EventHandler(this.ucTrackBar1_Value3Changed);
- this.numericUpDown2.ValueChanged += new System.EventHandler(this.numericUpDown2_ValueChanged);*/
- this.button2.Click += new System.EventHandler(this.EventIncident);
- this.button3.Click += new System.EventHandler(this.EventIncident);
- this.button4.Click += new System.EventHandler(this.EventIncident);
- this.button5.Click += new System.EventHandler(this.EventIncident);
- this.numericUpDown1.ValueChanged += new System.EventHandler(this.EventIncident);
- this.label5.TextChanged += new System.EventHandler(this.EventIncident);
- this.label6.TextChanged += new System.EventHandler(this.EventIncident);
- this.label7.TextChanged += new System.EventHandler(this.EventIncident);
- this.skipButton.Click += new System.EventHandler(this.EventIncident);
- this.logButton.Click += new System.EventHandler(this.EventIncident);
- this.numericUpDown2.ValueChanged += new System.EventHandler(this.EventIncident);
- this.ucTrackBar1.Value1Changed += new System.EventHandler(this.ucTrackBar1_Value1Changed);
- this.ucTrackBar1.Value2Changed += new System.EventHandler(this.ucTrackBar1_Value2Changed);
- this.ucTrackBar1.Value3Changed += new System.EventHandler(this.ucTrackBar1_Value3Changed);
- this.ucTrackBar1.ValueMouseUp += new System.EventHandler(this.EventIncident);
- this.trackBar1.TrackBarScroll += new System.EventHandler(this.trackBar1_Scroll);
- this.trackBar2.TrackBarScroll += new System.EventHandler(this.trackBar2_Scroll);
- this.trackBar3.TrackBarScroll += new System.EventHandler(this.trackBar3_Scroll);
- this.trackBar1.TrackBarMouseUp += new System.EventHandler(this.EventIncident);
- this.trackBar2.TrackBarMouseUp += new System.EventHandler(this.EventIncident);
- this.trackBar3.TrackBarMouseUp += new System.EventHandler(this.EventIncident);
- }
- /// <summary>
- /// 所有事件的统一入口
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- public void EventIncident(object sender, EventArgs e)
- {
- if (this.AppWorkspace.ActiveDocumentWorkspace == null)
- {
- MessageBox.Show(PdnResources.GetString("Menu.Pleaseopenthepicture.Text"));
- return;
- }
- //移除事件
- this.RemoveAllEvent();
- string controlName = ((Control)sender).Name;
- switch(controlName)
- {
- case "button2":
- this.BestButtonMethod();
- this.EventIncidentBranch(null);
- this.SaveParameter();
- this.AppWorkspace.MaxMinBest(true, false, false, false);
- break;
- case "button3":
- this.MaxMinButtonMethod();
- this.EventIncidentBranch(null);
- this.SaveParameter();
- this.AppWorkspace.MaxMinBest(false, true, false, false);
- break;
- case "button4":
- this.Gamma45ButtonMethod();
- this.EventIncidentBranch(null);
- this.SaveParameter();
- this.AppWorkspace.MaxMinBest(false, false, false, true);
- break;
- case "button5":
- this.OriginButtonMethod();
- Document document = Document.FromImageMat(this.AppWorkspace.ActiveDocumentWorkspace.CompositionSurface.BackUpMat);
- this.AppWorkspace.ActiveDocumentWorkspace.setDoc(document, false);
- this.AppWorkspace.ActiveDocumentWorkspace.Refresh();
- this.EventIncidentBranch(null, false);
- this.SaveParameter();
- this.AppWorkspace.MaxMinBest(false, false, true, false);
- break;
- case "skipButton":
- this.skipButton.BtnSelect = !skipButton.BtnSelect;
- this.AppWorkspace.ActiveDocumentWorkspace.HistogramSkipEnabled = skipButton.BtnSelect;
- this.updateHistImg(null);
- break;
- case "logButton":
- this.logButton.BtnSelect = !logButton.BtnSelect;
- this.AppWorkspace.ActiveDocumentWorkspace.HistogramLogEnabled = logButton.BtnSelect;
- this.updateHistImg(null);
- break;
- case "ucTrackBar1":
- case "ucTrackBar2":
- case "ucTrackBar3":
- case "numericUpDown1":
- case "trackBar1":
- case "label5":
- case "label6":
- case "trackBar2":
- case "label7":
- case "trackBar3":
- case "numericUpDown2":
- case "triangleButtonSubtract":
- case "triangleButtonPlus":
- EventIncidentBranch(controlName);
- break;
- default:
- break;
- }
- //添加事件
- this.AddAllEvent();
- }
- public void SaveParameter()
- {
- if (this.AppWorkspace.ActiveDocumentWorkspace != null)
- {
- this.AppWorkspace.ActiveDocumentWorkspace.HistogramAlpha = double.Parse(label6.Text);
- this.AppWorkspace.ActiveDocumentWorkspace.HistogramBeta = double.Parse(label5.Text);
- this.AppWorkspace.ActiveDocumentWorkspace.HistogramPercent = (int)numericUpDown1.Value;
- this.AppWorkspace.ActiveDocumentWorkspace.HistogramGamma = double.Parse(numericUpDown2.Value.ToString());
- }
- }
- /// <summary>
- /// 最佳
- /// </summary>
- public void BestButtonMethod()
- {
- OpenCvSharp.Point startIndex = this.getStartIndex(this.AppWorkspace.ActiveDocumentWorkspace.BoxMat/*this.AppWorkspace.ActiveDocumentWorkspace.OldMat*/,
- oldHists, (float)numericUpDown1.Value);
- //计算对比度
- label6.Text = (256.0 / (startIndex.Y - startIndex.X)).ToString("f2");
- trackBar2.Value = Math.Min(trackBar2.Maximum, (int)(double.Parse(label6.Text) * 100));
- //计算亮度
- label5.Text = ((0 - startIndex.X - startIndex.Y) / 510.0).ToString("f2");
- trackBar1.Value = (int)(double.Parse(label5.Text) * 100);
- }
- /// <summary>
- /// 最大最小
- /// </summary>
- public void MaxMinButtonMethod()
- {
- OpenCvSharp.Point startIndex = this.getStartIndex(this.AppWorkspace.ActiveDocumentWorkspace.BoxMat/*this.AppWorkspace.ActiveDocumentWorkspace.OldMat*/,
- oldHists, 0);
- //计算对比度
- label6.Text = (256.0 / (startIndex.Y - startIndex.X)).ToString("f2");
- trackBar2.Value = Math.Min(trackBar2.Maximum, (int)(double.Parse(label6.Text) * 100));
- //计算亮度
- label5.Text = ((0 - startIndex.X - startIndex.Y) / 510.0).ToString("f2");
- trackBar1.Value = (int)(double.Parse(label5.Text) * 100);
- }
- /// <summary>
- /// 原始状态
- /// </summary>
- public void OriginButtonMethod()
- {
- this.trackBar1.Value = -50;
- this.trackBar2.Value = 100;
- this.trackBar3.Value = 100;
- label5.Text = (trackBar1.Value / 100.0).ToString("f2");
- label6.Text = (trackBar2.Value / 100.0).ToString("f2");
- numericUpDown2.Value = decimal.Parse((trackBar3.Value / 100.0).ToString("f2"));
- }
- /// <summary>
- /// 伽马0.45
- /// </summary>
- public void Gamma45ButtonMethod()
- {
- this.trackBar3.Value = 45;
- this.numericUpDown2.Value = decimal.Parse((trackBar3.Value / 100.0).ToString("f2"));
- }
- /// <summary>
- /// 重新处理图片
- /// https://blog.csdn.net/qq_20095389/article/details/83658878
- /// https://blog.csdn.net/lantishua/article/details/46377325
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- public void EventIncidentBranch(string sender, bool calc = true)
- {
- switch (sender)
- {
- case "trackBar1":
- this.label5.Text = (trackBar1.Value / 100.0).ToString("f2");
- break;
- case "trackBar2":
- this.label6.Text = (trackBar2.Value / 100.0).ToString("f2");
- break;
- case "trackBar3":
- this.numericUpDown2.Value = decimal.Parse((trackBar3.Value / 100.0).ToString("f2"));
- break;
- case "label6":
- if(this.AppWorkspace.ActiveDocumentWorkspace != null)
- this.AppWorkspace.ActiveDocumentWorkspace.HistogramAlpha = double.Parse(label6.Text);
- break;
- case "label5":
- if (this.AppWorkspace.ActiveDocumentWorkspace != null)
- this.AppWorkspace.ActiveDocumentWorkspace.HistogramBeta = double.Parse(label5.Text);
- break;
- case "numericUpDown1":
- if (this.AppWorkspace.ActiveDocumentWorkspace != null)
- this.AppWorkspace.ActiveDocumentWorkspace.HistogramPercent = (int)numericUpDown1.Value;
- break;
- case "numericUpDown2":
- if (this.AppWorkspace.ActiveDocumentWorkspace != null)
- this.AppWorkspace.ActiveDocumentWorkspace.HistogramGamma = double.Parse(numericUpDown2.Value.ToString());
- trackBar3.Value = (int)(numericUpDown2.Value * 100);
- break;
- default:
- this.label5.Text = (trackBar1.Value / 100.0).ToString("f2");
- this.label6.Text = (trackBar2.Value / 100.0).ToString("f2");
- this.numericUpDown2.Value = decimal.Parse((trackBar3.Value / 100.0).ToString("f2"));
- break;
- }
-
- //重新计算
- if(calc)
- this.scrollMethod();
- //更新直方图
- this.updateHistogramRect(true);
- }
- /// <summary>
- /// 拖动左侧三角形
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void ucTrackBar1_Value1Changed(object sender, EventArgs e)
- {
- this.RemoveAllEvent();
- OpenCvSharp.Point startIndex = new OpenCvSharp.Point(ucTrackBar1.Value1, ucTrackBar1.Value3);
- //计算对比度
- label6.Text = (256.0 / (startIndex.Y - startIndex.X)).ToString("f2");
- trackBar2.Value = Math.Min(trackBar2.Maximum, (int)(double.Parse(label6.Text) * 100));
- //计算亮度
- label5.Text = ((0 - startIndex.X - startIndex.Y) / 510.0).ToString("f2");
- trackBar1.Value = (int)(double.Parse(label5.Text) * 100);
- this.AddAllEvent();
- }
- /// <summary>
- /// 拖动中间三角形
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void ucTrackBar1_Value2Changed(object sender, EventArgs e)
- {
- this.RemoveAllEvent();
- //计算gamma值
- double gamma = 1.0 / Math.Log((ucTrackBar1.Value2 - ucTrackBar1.Value1) / (ucTrackBar1.Value3 - ucTrackBar1.Value1), 0.5);
- gamma = Math.Max(0.01, gamma);
- this.trackBar3.Value = Math.Min(299, (int)(gamma * 100));
- numericUpDown2.Value = decimal.Parse(Math.Min(2.99, gamma).ToString("f2"));
- this.AddAllEvent();
- }
- /// <summary>
- /// 拖动右侧三角形
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void ucTrackBar1_Value3Changed(object sender, EventArgs e)
- {
- this.RemoveAllEvent();
- OpenCvSharp.Point startIndex = new OpenCvSharp.Point(ucTrackBar1.Value1, ucTrackBar1.Value3);
- //计算对比度
- label6.Text = (256.0 / (startIndex.Y - startIndex.X)).ToString("f2");
- trackBar2.Value = Math.Min(trackBar2.Maximum, (int)(double.Parse(label6.Text) * 100));
- //计算亮度
- label5.Text = ((0 - startIndex.X - startIndex.Y) / 510.0).ToString("f2");
- trackBar1.Value = (int)(double.Parse(label5.Text) * 100);
- this.AddAllEvent();
- }
- /// <summary>
- /// 亮度改变
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void trackBar1_Scroll(object sender, EventArgs e)
- {
- this.RemoveAllEvent();
- this.label5.Text = (trackBar1.Value / 100.0).ToString("f2");
- this.AddAllEvent();
- }
- /// <summary>
- /// 对比度改变
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void trackBar2_Scroll(object sender, EventArgs e)
- {
- this.RemoveAllEvent();
- label6.Text = (trackBar2.Value / 100.0).ToString("f2");
- this.AddAllEvent();
- }
- /// <summary>
- /// gamma改变
- /// gamma值小于1时,会拉伸图像中灰度级较低的区域,同时会压缩灰度级较高的部分
- /// gamma值大于1时,会拉伸图像中灰度级较高的区域,同时会压缩灰度级较低的部分
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void trackBar3_Scroll(object sender, EventArgs e)
- {
- this.RemoveAllEvent();
- numericUpDown2.Value = decimal.Parse((trackBar3.Value / 100.0).ToString("f2"));
- this.AddAllEvent();
- }
- }
- }
|