using OpenCvSharp;
using PaintDotNet.Camera;
using PaintDotNet.CustomControl;
using System;
using System.Collections.Generic;
using System.Windows.Forms;
namespace PaintDotNet.Instrument
{
///
/// 工具-直方图
///
internal class HistogramDialog : FloatingToolForm
{
#region 初始化变量
private AppWorkspace AppWorkspace;
///
/// 一个矩阵数组,用来接收直方图,记得全部初始化
///
private Mat[] oldHists = new Mat[] { new Mat(), new Mat(), new Mat() };
///
/// 灰度图
///
private bool isGray = false;
///
/// BGR线条颜色
///
private Scalar[] color = new Scalar[] { new Scalar(255, 0, 0, 255), new Scalar(0, 255, 0, 255), new Scalar(0, 0, 255, 255) };
///
/// 计算gamma的查找表
///
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();
}
///
/// 更改按钮焦点
///
/// 按钮区分
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();
}
}
}
///
/// 重置Mat
///
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();
}
///
/// 绘制直方图
///
///
///
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 ProbPixel = new List();
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(h) * hpt / maxVal);
int lastY1 = lastY2;
lastY2 = histSize - intensity - 1 - (hist.At(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(h) * hpt / maxVal);
int lastY1 = lastY2;
lastY2 = histSize - intensity - 1 - (hist.At(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);
}
///
/// 将拟合点绘制到空白图上
///
/// 拟合点
/// 拟合矩阵的数量
/// 空白图
///
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(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(i, 0) + (double)(Math.Pow(key_point[k].X, i) * key_point[k].Y);
}
}
//求解矩阵A
Cv2.Solve(X, Y, A, DecompTypes.LU);
return true;
}
///
/// 通过颜色通道去除像素点的千分比计算有效阈值
///
///
///
///
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(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;
}
///
/// 更新直方图特征曲线
///
/// 是否需要更新三个控制点的位置
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 points = new List();
//根据亮度和对比度计算左右阈值
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> pointsIn = new List>() { 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 points_fitted = new List();
List dataFitted = new List();
dataFitted.Add(A.At(0, 0));
dataFitted.Add(A.At(1, 0));
dataFitted.Add(A.At(2, 0));
dataFitted.Add(A.At(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(0, 0) + A.At(1, 0) * x + A.At(2, 0) * Math.Pow(x, 2) + A.At(3, 0) * Math.Pow(x, 3);
points_fitted.Add(new OpenCvSharp.Point(x, y));
}
////绘制折线
this.pictureBox1.Image = OpenCvSharp.Extensions.BitmapConverter.ToBitmap(image);
}
///
/// 根据亮度、对比度、gamma对图片进行处理
/// https://blog.csdn.net/kuweicai/article/details/78953017
/// https://blog.csdn.net/ren365880/article/details/103875302
/// https://zhuanlan.zhihu.com/p/92945623
///
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(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");
}
///
/// 移除事件
///
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);
}
///
/// 添加事件
///
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);
}
///
/// 所有事件的统一入口
///
///
///
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());
}
}
///
/// 最佳
///
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);
}
///
/// 最大最小
///
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);
}
///
/// 原始状态
///
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"));
}
///
/// 伽马0.45
///
public void Gamma45ButtonMethod()
{
this.trackBar3.Value = 45;
this.numericUpDown2.Value = decimal.Parse((trackBar3.Value / 100.0).ToString("f2"));
}
///
/// 重新处理图片
/// https://blog.csdn.net/qq_20095389/article/details/83658878
/// https://blog.csdn.net/lantishua/article/details/46377325
///
///
///
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);
}
///
/// 拖动左侧三角形
///
///
///
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();
}
///
/// 拖动中间三角形
///
///
///
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();
}
///
/// 拖动右侧三角形
///
///
///
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();
}
///
/// 亮度改变
///
///
///
private void trackBar1_Scroll(object sender, EventArgs e)
{
this.RemoveAllEvent();
this.label5.Text = (trackBar1.Value / 100.0).ToString("f2");
this.AddAllEvent();
}
///
/// 对比度改变
///
///
///
private void trackBar2_Scroll(object sender, EventArgs e)
{
this.RemoveAllEvent();
label6.Text = (trackBar2.Value / 100.0).ToString("f2");
this.AddAllEvent();
}
///
/// gamma改变
/// gamma值小于1时,会拉伸图像中灰度级较低的区域,同时会压缩灰度级较高的部分
/// gamma值大于1时,会拉伸图像中灰度级较高的区域,同时会压缩灰度级较低的部分
///
///
///
private void trackBar3_Scroll(object sender, EventArgs e)
{
this.RemoveAllEvent();
numericUpDown2.Value = decimal.Parse((trackBar3.Value / 100.0).ToString("f2"));
this.AddAllEvent();
}
}
}