using OpenCvSharp;
using PaintDotNet.Annotation;
using PaintDotNet.Base;
using PaintDotNet.Base.CommTool;
using PaintDotNet.Base.Functionodel;
using PaintDotNet.Base.SettingModel;
using PaintDotNet.Data.Param;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace PaintDotNet.DedicatedAnalysis.GrainSizeStandard.IntegrationClass
{
class BinaryClass
{
///
/// 是否针对全图进行二值化操作
///
public bool processWholeMat = false;
///
/// 调色板
///
PaintDotNet.ColorsForm colorsForm1;
///
/// 构造工作结构
///
private Dictionary documentItems;
///
/// 需要的数据在这里引用
///
CustomControl.BinaryControlSmaller bmc;
///
/// 需要的数据在这里引用
///
CustomControl.BinaryControl bc;
AppWorkspace appWorkspace;
DocumentWorkspaceWindow documentWorkspace;
ListView listView1;
///
/// 是否要调用二值化的算法
///
private bool toApplyBinary = true;
///
/// 处理程序
///
private ParamObject action = new Data.Action.Action901();
///
/// 初始系统参数配置值
///
private BinaryExtractionModel binaryExtractionModel;
///
/// 相0的图片,处理多视场使用
///
public Mat PhaseModels0Mat;
///
/// 辅助线集成
///
public GrainSizeGuideClass guideClass;
public ComboBox comboBox1;
//public object SelectedItem;
//public GrainSizeGuideClass GuideClass
//{
// set
// {
// this.guideClass = value;
// }
//}
///
/// 公开的事件,每次二值化之后需要的计算事件写在这里
///
public event EventHandler BinaryImplFinishAction;
private void OnBinaryImplFinishAction()
{
if (BinaryImplFinishAction != null)
{
BinaryImplFinishAction(this, new EventArgs());
}
}
public void InitBinaryControlEvent()
{
if (bmc != null)
{
bmc.InitBinaryControlEvent();
bmc.BinaryGetParamAction += new EventHandler(this.bcBinaryGetParamAction);
bmc.AutoThresClickAction += new EventHandler(this.bcAutoThresClickAction);
bmc.InverseClickAction += new EventHandler(this.bcBinaryGetParamAction);
bmc.InverseClickAction += new EventHandler(this.bcApplyButtonImplAction);
bmc.RadioButton1ChangedAction += new EventHandler(this.bcBinaryGetParamAction);
bmc.RadioButton2ChangedAction += new EventHandler(this.bcBinaryGetParamAction);
bmc.ApplyButtonImplAction += new EventHandler(this.bcApplyButtonImplAction);
bmc.BinaryCheckedChangedAction += new EventHandler(this.bcBinaryCheckedChanged);
bmc.BinaryEditClickAction += new EventHandler(this.bcBinaryEditClickAction);
bmc.PanelColorClickAction += new EventHandler(this.bcPanelColorClickAction);//初始化颜色点击事件
return;
}
if (bc == null)
return;
bc.InitBinaryControlEvent();
bc.BinaryGetParamAction += new EventHandler(this.bcBinaryGetParamAction);
bc.AutoThresClickAction += new EventHandler(this.bcAutoThresClickAction);
bc.InverseClickAction += new EventHandler(this.bcBinaryGetParamAction);
bc.InverseClickAction += new EventHandler(this.bcApplyButtonImplAction);
bc.RadioButton1ChangedAction += new EventHandler(this.bcBinaryGetParamAction);
bc.RadioButton2ChangedAction += new EventHandler(this.bcBinaryGetParamAction);
bc.ApplyButtonImplAction += new EventHandler(this.bcApplyButtonImplAction);
bc.BinaryCheckedChangedAction += new EventHandler(this.bcBinaryCheckedChanged);
bc.BinaryEditClickAction += new EventHandler(this.bcBinaryEditClickAction);
bc.PanelColorClickAction += new EventHandler(this.bcPanelColorClickAction);//初始化颜色点击事件
}
///
/// 相颜色点击事件
///
///
///
private void bcPanelColorClickAction(object sender, EventArgs e)
{
this.colorsForm1.UserPrimaryColor = ColorBgra.FromColor(bmc != null ? bmc.BinaryBackColor : bc.BinaryBackColor);
this.colorsForm1.ShowDialog();
}
///
/// 二值筛选
///
///
///
private void bcBinaryEditClickAction(object sender, EventArgs e)
{
if (this.documentWorkspace.PhaseModels[0].mat == null)
{
MessageBox.Show(PdnResources.GetString("Menu.Pleaseperonfirst.text"));
return;
}
GrainBinaryBoundaryEditingDialog boundaryEditingDialog = new GrainBinaryBoundaryEditingDialog(this.appWorkspace, this.listView1.FocusedItem.Index, bmc != null ? bmc.BinaryBackColor : bc.BinaryBackColor
, this.documentWorkspace.PhaseModels[0].mat.Clone());
////1013###19326 晶粒度的晶界编辑页面,需要把辅助线带过去(客户编辑只处理辅助线位置的晶界)
if (this.guideClass != null && this.comboBox1 != null && this.comboBox1.SelectedItem != null)
{
boundaryEditingDialog.GuideClass = this.guideClass;
boundaryEditingDialog.SelectedItem = this.comboBox1.SelectedItem;
}
if (boundaryEditingDialog.ShowDialog() == DialogResult.OK)
{
this.documentWorkspace.PhaseModels[0].mat = boundaryEditingDialog.PhaseMat.Clone();
OnBinaryImplFinishAction();
}
}
///
/// 二值化勾选改变事件
///
///
///
private void bcBinaryCheckedChanged(object sender, EventArgs e)
{
if (bc == null && bmc == null)
return;
if ((bmc != null ? bmc.BinaryChecked : bc.BinaryChecked) && !(bmc != null ? bmc.OriginChecked : bc.OriginChecked))
{
this.documentWorkspace.PhaseModels[0].choise = true;
}
else
{
this.documentWorkspace.PhaseModels[0].choise = false;
}
if (toApplyBinary)
this.applyButtonImpl();
else
OnBinaryImplFinishAction();
toApplyBinary = true;
if (this.listView1.FocusedItem == null || !(bmc != null ? bmc.BinaryChecked : bc.BinaryChecked))
this.documentWorkspace.Refresh();
}
///
/// 自动阈值
///
///
///
private void bcAutoThresClickAction(object sender, EventArgs e)
{
if (bc == null && bmc == null)
return;
Bitmap bitmap = null;
if (listView1.FocusedItem != null)
bitmap = this.appWorkspace.DocumentWorkspaces[this.listView1.FocusedItem.Index].CompositionSurface.CreateAliasedBitmap();
if (bitmap != null)
{
//先计算阈值
Mat mat = OpenCvSharp.Extensions.BitmapConverter.ToMat(bitmap);
Mat gray = mat.CvtColor(ColorConversionCodes.BGR2GRAY);
double otsu = Cv2.Threshold(gray, gray, 0, 255, ThresholdTypes.Triangle/*.Otsu*/);
//如果当前是两个区间,则需要重新计算一次
if (this.ColorInterval == 2)
{
if (bmc != null)
bmc.OnInverseClickAction();
else
bc.OnInverseClickAction();
this.ColorInterval = (bmc != null ? bmc.getInverseStyle() : bc.getInverseStyle());
}
else
{
if (bmc != null)
bmc.setInverseStyle(1);
else
bc.setInverseStyle(1);
}
//给控件赋值
if (bmc != null)
bmc.scope1Start = 0;
else
bc.scope1Start = 0;
if (bmc != null)
bmc.scope1End = otsu;
else
bc.scope1End = otsu;
//处理直方图
if (bmc != null)
bmc.UpdateVerticalBarWithOneScope(0, Convert.ToInt32(bmc.scope1End));
else
bc.UpdateVerticalBarWithOneScope(0, Convert.ToInt32(bc.scope1End));
mat.Dispose();
GC.Collect();
this.bcApplyButtonImplAction(sender, e);
}
else
{
MessageBox.Show(PdnResources.GetString("Menu.Pleaseselectapicturefirst.text"));
}
}
///
/// 执行读取参数的事件
///
///
///
private void bcBinaryGetParamAction(object sender, EventArgs e)
{
if (bc == null && bmc == null)
return;
this.binaryExtractionModel.ColorInterval = (bmc != null ? bmc.getInverseStyle() : bc.getInverseStyle());//###
this.binaryExtractionModel.BinaryStyle = (bmc != null ? bmc.BinaryStyle : bc.BinaryStyle);
this.binaryExtractionModel.ColorOneStart = (int)(bmc != null ? bmc.scope1Start : bc.scope1Start);
this.binaryExtractionModel.ColorOneEnd = (int)(bmc != null ? bmc.scope1End : bc.scope1End);
this.binaryExtractionModel.ColorTwoStart = (int)(bmc != null ? bmc.scope2Start : bc.scope2Start);
this.binaryExtractionModel.ColorTwoEnd = (int)(bmc != null ? bmc.scope2End : bc.scope2End);
this.binaryExtractionModel.ColorThreeStart = (int)(bmc != null ? bmc.scope3Start : bc.scope3Start);
this.binaryExtractionModel.ColorThreeEnd = (int)(bmc != null ? bmc.scope3End : bc.scope3End);
foreach (Args args in action.Lists)
{
if (args.Key == "colorInterval")
args.Value = this.binaryExtractionModel.ColorInterval;
else if (args.Key == "binaryStyle")
args.Value = (bmc != null ? bmc.BinaryStyle : bc.BinaryStyle);
else if (args.Key == "scope1")
{
((List)args.Value)[0] = (int)(bmc != null ? bmc.scope1Start : bc.scope1Start);
((List)args.Value)[1] = (int)(bmc != null ? bmc.scope1End : bc.scope1End);
}
else if (args.Key == "scope2")
{
((List)args.Value)[0] = (int)(bmc != null ? bmc.scope2Start : bc.scope2Start);
((List)args.Value)[1] = (int)(bmc != null ? bmc.scope2End : bc.scope2End);
}
else if (args.Key == "scope3")
{
((List)args.Value)[0] = (int)(bmc != null ? bmc.scope3Start : bc.scope3Start);
((List)args.Value)[1] = (int)(bmc != null ? bmc.scope3End : bc.scope3End);
}
}
}
///
/// 获取相的工作结构
///
/// 指明获取第几个相的工作结构
///
public List getPhaseModels(int index)
{
return this.documentItems[index].phaseModels;
}
/////
///// 判断是否存在视场
/////
/////
//public bool IsExsitView()
//{
// if (this.graphicsList != null && this.graphicsList.Count > 0
// && this.graphicsList.FindAll(a => a.objectType == DrawClass.View).Count > 0)
// {
// return true;
// }
// return false;
//}
///
/// ListView图片选择改变事件,不显示视场
///
/// 选择改变后的图片
public void listView1_SelectedIndexChangedHideDrawClassView(Bitmap bitmap)
{
Document document = Document.FromImage(bitmap);
this.documentWorkspace.Document = document;
this.documentWorkspace.Visible = true;
GraphicsList itemGraphicsList = this.appWorkspace.DocumentWorkspaces[this.listView1.FocusedItem.Index].GraphicsList;
GraphicsList graphicsList = new GraphicsList();
for (int j = 0; j < itemGraphicsList.Count; j++)
{
if (itemGraphicsList[j].objectType != Annotation.Enum.DrawClass.View)
graphicsList.Add(itemGraphicsList[j]);
}
this.documentWorkspace.GraphicsList = graphicsList;
this.documentWorkspace.PhaseModels = new List();
this.documentWorkspace.PhaseModels.AddRange(this.getPhaseModels(this.listView1.FocusedItem.Index));
if ((bc != null || bmc != null) && this.documentWorkspace.PhaseModels[0].mat != null)
{
if (!(bmc != null ? bmc.BinaryChecked : bc.BinaryChecked) && this.documentWorkspace.PhaseModels[0].choise)
{
this.toApplyBinary = false;
if (bmc != null)
bmc.BinaryChecked = true;
else
bc.BinaryChecked = true;
}
}
else
{
this.applyButtonImpl();
}
if ((bc != null || bmc != null) && !this.documentWorkspace.PhaseModels[0].choise)
if (bmc != null)
bmc.BinaryChecked = false;
else
bc.BinaryChecked = false;
//显示直方图
if (bc != null || bmc != null)
if (bmc != null)
bmc.CreateHistogram(bitmap, true, 339, 130, 0);
else
bc.CreateHistogram(bitmap, true, 339, 130, 0);
}
///
/// ListView图片选择改变事件
///
/// 选择改变后的图片
public void listView1_SelectedIndexChanged(Bitmap bitmap)
{
Document document = Document.FromImage(bitmap);
this.documentWorkspace.Document = document;
this.documentWorkspace.Visible = true;
this.documentWorkspace.GraphicsList = this.appWorkspace.DocumentWorkspaces[this.listView1.FocusedItem.Index].GraphicsList;
this.documentWorkspace.PhaseModels = new List();
this.documentWorkspace.PhaseModels.AddRange(this.getPhaseModels(this.listView1.FocusedItem.Index));
if ((bc != null || bmc != null) && this.documentWorkspace.PhaseModels[0].mat != null)
{
if (!(bmc != null ? bmc.BinaryChecked : bc.BinaryChecked) && this.documentWorkspace.PhaseModels[0].choise)
{
this.toApplyBinary = false;
if (bmc != null)
bmc.BinaryChecked = true;
else
bc.BinaryChecked = true;
}
}
else
{
this.applyButtonImpl();
}
if ((bc != null || bmc != null) && !this.documentWorkspace.PhaseModels[0].choise)
if (bmc != null)
bmc.BinaryChecked = false;
else
bc.BinaryChecked = false;
//显示直方图
if (bc != null || bmc != null)
if (bmc != null)
bmc.CreateHistogram(bitmap, true, 339, 130, 0);
else
bc.CreateHistogram(bitmap, true, 339, 130, 0);
}
///
/// ListView图片选择改变事件
///
/// 选择改变后的图片
public void listView1_SelectedIndexChangedNoCheckedChanged(Bitmap bitmap)
{
Document document = Document.FromImage(bitmap);
this.documentWorkspace.Document = document;
this.documentWorkspace.Visible = true;
this.documentWorkspace.GraphicsList = this.appWorkspace.DocumentWorkspaces[this.listView1.FocusedItem.Index].GraphicsList;
this.documentWorkspace.PhaseModels = new List();
this.documentWorkspace.PhaseModels.AddRange(this.getPhaseModels(this.listView1.FocusedItem.Index));
if ((bc != null || bmc != null) && this.documentWorkspace.PhaseModels[0].mat != null)
{
if (!(bmc != null ? bmc.BinaryChecked : bc.BinaryChecked) && this.documentWorkspace.PhaseModels[0].choise)//to sure and test
{
this.toApplyBinary = false;
if (bmc != null)
bmc.BinaryChecked = true;
else
bc.BinaryChecked = true;
}
}
else
{
this.applyButtonImpl();
}
////to sure and test
//if (bc != null && !this.documentWorkspace.PhaseModels[0].choise)
// bc.BinaryChecked = false;
//显示直方图
if (bc != null || bmc != null)
if (bmc != null)
bmc.CreateHistogram(bitmap, true, 339, 130, 0);
else
bc.CreateHistogram(bitmap, true, 339, 130, 0);
}
///
/// 构造相的工作结构
///
/// 相的命名,如果只有二值则只传PdnResources.GetString("Menu.BinaryAction.BinaryExtraction.Text")即可,没有特殊情况命名第一个相为PdnResources.GetString("Menu.BinaryAction.BinaryExtraction.Text")
///
///
///
public void createDocumentItemsSmaller(string[] phaseNames, CustomControl.BinaryControlSmaller bmc, AppWorkspace appWorkspace
, DocumentWorkspaceWindow documentWorkspace, ListView listView1)
{
this.bmc = bmc;
this.appWorkspace = appWorkspace;
this.documentWorkspace = documentWorkspace;
this.listView1 = listView1;
this.initParams();
for (int i = 0; i < phaseNames.Length; i++)
{
PhaseModel model = new PhaseModel();
model.choise = true;
model.mat = null;
model.color = (i == 0 && bmc != null) ? bmc.BinaryBackColor.ToArgb() : Color.Green.ToArgb();//###
model.position = documentWorkspace.PhaseModels.Count + 1;
model.name = phaseNames[i];
documentWorkspace.PhaseModels.Add(model);
}
this.documentItems = new Dictionary();
// 构造工作结构
for (int i = 0; i < appWorkspace.DocumentWorkspaces.Length; i++)
{
Bitmap bitmap = appWorkspace.DocumentWorkspaces[i].CompositionSurface.CreateAliasedBitmap();
GraphicsList graphicsList = new GraphicsList();
for (int j = 0; j < appWorkspace.DocumentWorkspaces[i].GraphicsList.Count; j++)
{
graphicsList.Add(appWorkspace.DocumentWorkspaces[i].GraphicsList[j]);
}
List phaseModels = new List();
for (int j = 0; j < phaseNames.Length; j++)
{
//二值化相关
if (j == 0)
{
PhaseModel analysisModel = appWorkspace.DocumentWorkspaces[i].AnalysisPhaseModel;
if (analysisModel == null)
{
PhaseModel model = new PhaseModel();
model.choise = true;
model.mat = null;
model.color = bmc != null ? bmc.BinaryBackColor.ToArgb() : Color.Green.ToArgb();
model.position = phaseModels.Count + 1;
model.name = phaseNames[j];
phaseModels.Add(model);
}
else
phaseModels.Add(analysisModel);
}
else
{
PhaseModel model = new PhaseModel();
model.choise = true;
model.mat = null;
model.color = Color.Green/*panel2.BackColor*/.ToArgb();//###
model.position = phaseModels.Count + 1;
model.name = phaseNames[j];
phaseModels.Add(model);
}
}
this.documentItems.Add(i, new DocumentItem(bitmap, graphicsList, phaseModels));
}
this.InitParameterToControl();//###
this.InitBinaryControlEvent();
}
///
/// 构造相的工作结构
///
/// 相的命名,如果只有二值则只传(二值提取)即可,没有特殊情况命名第一个相为(二值提取)
///
///
///
public void createDocumentItems(string[] phaseNames, CustomControl.BinaryControl bc, AppWorkspace appWorkspace
, DocumentWorkspaceWindow documentWorkspace, ListView listView1)
{
this.bc = bc;
this.appWorkspace = appWorkspace;
this.documentWorkspace = documentWorkspace;
this.listView1 = listView1;
this.initParams();
for (int i = 0; i < phaseNames.Length; i++)
{
PhaseModel model = new PhaseModel();
model.choise = true;
model.mat = null;
model.color = (i == 0 && bc != null) ? bc.BinaryBackColor.ToArgb() : Color.Green.ToArgb();//###
model.position = documentWorkspace.PhaseModels.Count + 1;
model.name = phaseNames[i];
documentWorkspace.PhaseModels.Add(model);
}
this.documentItems = new Dictionary();
// 构造工作结构
for (int i = 0; i < appWorkspace.DocumentWorkspaces.Length; i++)
{
Bitmap bitmap = appWorkspace.DocumentWorkspaces[i].CompositionSurface.CreateAliasedBitmap();
GraphicsList graphicsList = new GraphicsList();
for (int j = 0; j < appWorkspace.DocumentWorkspaces[i].GraphicsList.Count; j++)
{
graphicsList.Add(appWorkspace.DocumentWorkspaces[i].GraphicsList[j]);
}
List phaseModels = new List();
for (int j = 0; j < phaseNames.Length; j++)
{
//二值化相关
if (j == 0)
{
PhaseModel analysisModel = appWorkspace.DocumentWorkspaces[i].AnalysisPhaseModel;
if (analysisModel == null)
{
PhaseModel model = new PhaseModel();
model.choise = true;
model.mat = null;
model.color = bc != null ? bc.BinaryBackColor.ToArgb() : Color.Green.ToArgb();
model.position = phaseModels.Count + 1;
model.name = phaseNames[j];
phaseModels.Add(model);
}
else
phaseModels.Add(analysisModel);
}
else
{
PhaseModel model = new PhaseModel();
model.choise = true;
model.mat = null;
model.color = Color.Green/*panel2.BackColor*/.ToArgb();//###
model.position = phaseModels.Count + 1;
model.name = phaseNames[j];
phaseModels.Add(model);
}
}
this.documentItems.Add(i, new DocumentItem(bitmap, graphicsList, phaseModels));
}
this.InitParameterToControl();//###
this.InitBinaryControlEvent();
}
///
/// 无视场执行运算
///
public OpenCvSharp.Mat PerformProcess(OpenCvSharp.Mat src) { return action.PerformProcess(src); }
///
/// 多视场执行运算
///
/// 视场mat
/// 原图mat
///
public OpenCvSharp.Mat PerformProcess(OpenCvSharp.Mat src, OpenCvSharp.Mat mat) { return action.PerformProcess(src, mat); }
public int ColorInterval
{
get
{
return this.binaryExtractionModel.ColorInterval;
}
set
{
this.binaryExtractionModel.ColorInterval = value;
}
}
private void colorsForm1Changed(object sender, EventArgs e)
{
Color color = this.colorsForm1.UserPrimaryColor.ToColor();
this.SetBinaryBackColor(color);//改变参数配置的相颜色
this.colorsForm1.Close();
}
///
/// 把参数的值设置到控件上
///
public void InitParameterToControl()
{
//
//调色板
//
this.colorsForm1 = new ColorsForm();
this.colorsForm1.StartPosition = FormStartPosition.CenterScreen;
this.colorsForm1.setSaveBtn_Click(new System.EventHandler(this.colorsForm1Changed));
if (this.binaryExtractionModel != null)
{
if (bc != null || bmc != null)
{
//阈值相关
//1个颜色区间还是2个
if (bmc != null)
bmc.setInverseStyle((this.binaryExtractionModel.ColorInterval == 1) ? 1 : 2);
else
bc.setInverseStyle((this.binaryExtractionModel.ColorInterval == 1) ? 1 : 2);
//删除事件
if (bmc != null)
bmc.DeleteEventHandler();
else
bc.DeleteEventHandler();
if (bmc != null)
bmc.scope1End = this.binaryExtractionModel.ColorOneEnd;
else
bc.scope1End = this.binaryExtractionModel.ColorOneEnd;
if (bmc != null)
bmc.scope1Start = this.binaryExtractionModel.ColorOneStart;
else
bc.scope1Start = this.binaryExtractionModel.ColorOneStart;
if (bmc != null)
bmc.scope2End = this.binaryExtractionModel.ColorTwoEnd;
else
bc.scope2End = this.binaryExtractionModel.ColorTwoEnd;
if (bmc != null)
bmc.scope2Start = this.binaryExtractionModel.ColorTwoStart;
else
bc.scope2Start = this.binaryExtractionModel.ColorTwoStart;
if (bmc != null)
bmc.scope3End = this.binaryExtractionModel.ColorThreeEnd;
else
bc.scope3End = this.binaryExtractionModel.ColorThreeEnd;
if (bmc != null)
bmc.scope3Start = this.binaryExtractionModel.ColorThreeStart;
else
bc.scope3Start = this.binaryExtractionModel.ColorThreeStart;
if (bmc != null)
bmc.BinaryStyle = this.binaryExtractionModel.BinaryStyle;
else
bc.BinaryStyle = this.binaryExtractionModel.BinaryStyle;
if (bmc != null)
bmc.BinaryBackColor = Color.FromArgb(this.binaryExtractionModel.PhaseColor);
else
bc.BinaryBackColor = Color.FromArgb(this.binaryExtractionModel.PhaseColor);
//添加事件
if (bmc != null)
bmc.AddEventHandler();
else
bc.AddEventHandler();
if (bmc != null)
bmc.InitParameterToControl();
else
bc.InitParameterToControl();
}
this.applyButtonImpl();
}
}
public void SetBinaryBackColor(Color color)
{
if (bmc != null)
bmc.BinaryBackColor = color;//设置panel背景
else
bc.BinaryBackColor = color;//设置panel背景
this.binaryExtractionModel.PhaseColor = color.ToArgb();
//###
foreach (Args args in action.Lists)
{
if (args.Key == "phaseColor")
{
args.Value = color.ToArgb();//this.binaryExtractionModel.PhaseColor
break;
}
}
this.applyButtonImpl();
}
///
/// 保证二值化颜色赋值到action
///
public void loadParams()
{
if (bc == null && bmc == null)
return;
foreach (Args args in action.Lists)
{
switch (args.Key)
{
case "scope1"://不反选时候的范围
if (bmc != null)
bmc.ScopeValue1ChangedAction += new EventHandler(((DecimalScope)args).numberScope_ValueChanged);
else
bc.ScopeValue1ChangedAction += new EventHandler(((DecimalScope)args).numberScope_ValueChanged);
break;
case "scope2"://反选时候的范围1
if (bmc != null)
bmc.ScopeValue2ChangedAction += new EventHandler(((DecimalScope)args).numberScope_ValueChanged);
else
bc.ScopeValue2ChangedAction += new EventHandler(((DecimalScope)args).numberScope_ValueChanged);
break;
case "scope3"://反选时候的范围2
if (bmc != null)
bmc.ScopeValue3ChangedAction += new EventHandler(((DecimalScope)args).numberScope_ValueChanged);
else
bc.ScopeValue3ChangedAction += new EventHandler(((DecimalScope)args).numberScope_ValueChanged);
break;
case "phaseColor":
args.Value = this.binaryExtractionModel.PhaseColor;
break;
default:
break;
}
}
}
///
/// 执行二值方法的事件
///
///
///
private void bcApplyButtonImplAction(object sender, EventArgs e)
{
this.applyButtonImpl(/*bitmap*/);
}
///
/// 参数改变时,重新处理图像
///
///
///
public void applyButtonImpl(/*Bitmap bitmap*/)
{
if (bc == null && bmc == null)
return;
//long start = Cv2.GetTickCount();
if (listView1.FocusedItem != null && (bmc != null ? bmc.BinaryChecked : bc.BinaryChecked))
{
Bitmap bitmap = this.appWorkspace.DocumentWorkspaces[this.listView1.FocusedItem.Index].CompositionSurface.CreateAliasedBitmap();
//判断是否存在视场,如果存在视场,则把视场提取出来,进行处理
if (!processWholeMat && documentWorkspace.GraphicsList.IsExsitView())
{
documentWorkspace.PhaseModels[0].mat = this.PerformProcess(
OpenCvSharp.Extensions.BitmapConverter.ToMat(documentWorkspace.GetFullSizeWithRegion()),
OpenCvSharp.Extensions.BitmapConverter.ToMat(bitmap));
documentWorkspace.PhaseModels[0].color = (bmc != null ? bmc.BinaryBackColor.ToArgb() : bc.BinaryBackColor.ToArgb());
documentWorkspace.Refresh();
}
else if (processWholeMat && documentWorkspace.GraphicsList.IsExsitView())
{
if (PhaseModels0Mat != null)
{
PhaseModels0Mat.Dispose();
PhaseModels0Mat = null;
}
Mat OrgImg0 = this.PerformProcess(OpenCvSharp.Extensions.BitmapConverter.ToMat(bitmap));
PhaseModels0Mat = OrgImg0.Clone();
Vec4b vec4 = new Vec4b(0, 0, 0, 0);
//视场图像
Mat exsitViewMatOrg = OpenCvSharp.Extensions.BitmapConverter.ToMat(documentWorkspace.GetFullSizeWithRegion());
for (int y = 0; y < exsitViewMatOrg/*temp*/.Rows; y++)
{
for (int x = 0; x < exsitViewMatOrg/*temp*/.Cols; x++)
{
if (exsitViewMatOrg.At(y, x).Item3 == 0)
OrgImg0.Set(y, x, vec4);//不在视场图像范围内
}
}
documentWorkspace.PhaseModels[0].mat = OrgImg0;
documentWorkspace.PhaseModels[0].color = (bmc != null ? bmc.BinaryBackColor.ToArgb() : bc.BinaryBackColor.ToArgb());
documentWorkspace.Refresh();
}
else
{
documentWorkspace.PhaseModels[0].mat = this.PerformProcess(OpenCvSharp.Extensions.BitmapConverter.ToMat(bitmap));
documentWorkspace.PhaseModels[0].color = (bmc != null ? bmc.BinaryBackColor.ToArgb() : bc.BinaryBackColor.ToArgb());
documentWorkspace.Refresh();
}
}
//double time = (Cv2.GetTickCount() - start) / Cv2.GetTickFrequency();
//System.Console.WriteLine("执行时间:" + time);
//AddRectangleToRectangleFCaps();//0916###18745
OnBinaryImplFinishAction();
}
///
/// 初始化二值化相关的变量
///
public void initParams()
{
this.binaryExtractionModel = XmlSerializeHelper.DESerializer(FileOperationHelper.ReadStringFromFile(Application.StartupPath + "\\Config\\" + Startup.instance.SettingPrefix + "\\BinaryExtraction\\Default.xml", FileMode.Open));
}
#region 内部类
internal class LocalListViewItem
{
public DocumentWorkspace Value { get; }
public string Display { get; }
public LocalListViewItem(DocumentWorkspace Value, string Display)
{
this.Value = Value;
this.Display = Display;
}
}
internal class DocumentItem
{
public Bitmap bitmap;
public GraphicsList graphicsList;
public List phaseModels;
public DocumentItem(Bitmap bitmap, GraphicsList graphicsList, List phaseModels)
{
this.bitmap = bitmap;
this.graphicsList = graphicsList;
this.phaseModels = phaseModels;
}
}
#endregion
}
}