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.Windows.Forms;
namespace PaintDotNet
{
class BinaryClass
{
private int menuId;
protected static string ParamKey_binaryStyle = "binaryStyle";//二值样式
//protected static string ParamKey_displaygrid = "displaygrid";//显示网格
//protected static string ParamKey_displaysections = "displaysections";//显示截点
///
/// 是否针对全图进行二值化操作
///
public bool processWholeMat = false;
///
/// 是否显示视场
///
public Boolean ShowDrawClassView = true;
///
/// 调色板
///
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 ParamObject analysisAction = new Data.Action.ActionAnalysis();
///
/// 二值参数配置值(当前图片)
///
private BinaryExtractionModel binaryExtractionModel;
///
/// 二值参数是否进行全部应用,用于更新图片二值化效果,默认为false
///
private Dictionary binaryModelFlag = new Dictionary();
///
/// 二值参数配置值(全部图片的)
///
private Dictionary binaryModelDict = new Dictionary();
///
/// 二值(备份全部图片)
///
private Dictionary binaryData1Dict = new Dictionary();
///
/// 相0的图片,处理多视场使用
///
public Mat PhaseModels0Mat;
///
/// 辅助线集成
///
public PaintDotNet.DedicatedAnalysis.GrainSizeStandard.IntegrationClass.GrainSizeGuideClass guideClass;
public ComboBox comboBox1;
//public object SelectedItem;
//public GrainSizeGuideClass GuideClass
//{
// set
// {
// this.guideClass = value;
// }
//}
public static Mat dstBinary;
///
/// 对二值图像的透明通道提取出单通道图像
///
///
///
public unsafe static Mat BGRA2GRAY(Mat source/*, out Mat dst*/)
{
if (source.Channels() != 4)
{
//Mat dst = source;
return source.CvtColor(ColorConversionCodes.BGR2GRAY);// dst;// false;、、should never go here !
}
//二值提取
if (dstBinary != null)
{
dstBinary.Dispose();
dstBinary = null;
}
dstBinary = new Mat(source.Rows, source.Cols, MatType.CV_8UC1, new Scalar(0));
//如果是rgba
source.ForEachAsVec4b(ForeachFunctionByteForWhole4B);
Mat dst = dstBinary.Clone();
dstBinary.Dispose();
dstBinary = null;
return dst;// true;
}
///
/// 提取二值化的透明通道并赋值
///
///
///
public unsafe static void ForeachFunctionByteForWhole4B(Vec4b* t, int* position)
{
dstBinary.Set(position[0], position[1], t->Item3);
}
///
/// 公开的事件,每次二值化之后需要的计算事件写在这里
///
public event EventHandler BinaryImplFinishAction;
private void OnBinaryImplFinishAction()
{
if (BinaryImplFinishAction != null)
{
BinaryImplFinishAction(this, new EventArgs());
}
}
public BinaryClass(int menuId)
{
this.menuId = menuId;
}
///
/// 判断是否进行二值,以及返回二值图片
///
///
///
public Mat getModelMat(string imagesKey)
{
if (binaryData1Dict.ContainsKey(imagesKey) && (!binaryModelDict.ContainsKey(imagesKey) && binaryExtractionModel.BinaryCheckFlag > 1
|| binaryModelDict.ContainsKey(imagesKey) && binaryModelDict[imagesKey].BinaryCheckFlag > 1))
return binaryData1Dict[imagesKey];
return null;
}
///
/// 判断是否进行二值
///
///
///
public bool getModelBinaryCheckFlag(string imagesKey)
{
if ((!binaryModelDict.ContainsKey(imagesKey) && binaryExtractionModel.BinaryCheckFlag > 1
|| binaryModelDict.ContainsKey(imagesKey) && binaryModelDict[imagesKey].BinaryCheckFlag > 1))
return true;
return false;
}
///
/// 判断是否需要计算,并根据判断结果进行相关处理
///
///
///
public void updateAnalysisData(string imagesKey, int analysisPicture)
{
if (!binaryModelDict.ContainsKey(imagesKey) && binaryExtractionModel.BinaryCheckFlag > 1
|| binaryModelDict.ContainsKey(imagesKey) && binaryModelDict[imagesKey].BinaryCheckFlag > 1)
////|| binaryModelFlag.ContainsKey(imagesKey) && binaryModelFlag[imagesKey]/* //################ && binaryModelFlag[imagesKey] = false;*/)
{
this.applyButtonImpl(imagesKey, analysisPicture, /*bitmap = */this.appWorkspace.DocumentWorkspaces[analysisPicture].CompositionSurface.CreatedAliasedMat());
//if (binaryModelFlag.ContainsKey(imagesKey) && binaryModelFlag[imagesKey])
// binaryModelFlag[imagesKey] = false;
}
else if (binaryData1Dict.ContainsKey(imagesKey))
{
if (binaryData1Dict[imagesKey] != null)
binaryData1Dict[imagesKey] = null;
}
}
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.OriginCheckedChangedAction += new EventHandler(this.bcOriginCheckedChanged);
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.OriginCheckedChangedAction += new EventHandler(this.bcOriginCheckedChanged);
bc.BinaryEditClickAction += new EventHandler(this.bcBinaryEditClickAction);
bc.PanelColorClickAction += new EventHandler(this.bcPanelColorClickAction);//初始化颜色点击事件
}
///
/// 外部需要改变二值化勾选状态时调用该方法
///
///
public void performClickBinaryCheck(bool toCheck)
{
if (bmc != null)
bmc.BinaryChecked = toCheck;
else
bc.BinaryChecked = toCheck;
}
///
/// 相颜色点击事件
///
///
///
private void bcPanelColorClickAction(object sender, EventArgs e)
{
this.colorsForm1.UserPrimaryColor = ColorBgra.FromColor(bmc != null ? bmc.BinaryBackColor : bc.BinaryBackColor);
this.colorsForm1.ShowDialog();
this.applyButtonImpl(/*bitmap*/);
}
///
/// 二值筛选
///
///
///
private void bcBinaryEditClickAction(object sender, EventArgs e)
{
if (this.documentWorkspace.PhaseModels[0].mat == null)
{
MessageBox.Show(PdnResources.GetString("Menu.Pleaseperonfirst.text"));
return;
}
PaintDotNet.DedicatedAnalysis.GrainSizeStandard.IntegrationClass.GrainBinaryBoundaryEditingDialog boundaryEditingDialog = new PaintDotNet.DedicatedAnalysis.GrainSizeStandard.IntegrationClass.GrainBinaryBoundaryEditingDialog(this.appWorkspace, this.listView1.FocusedItem != null ? this.listView1.FocusedItem.Index : this.listView1.SelectedItems[0].Index, bmc != null ? bmc.BinaryBackColor : bc.BinaryBackColor
, this.documentWorkspace.PhaseModels[0].mat.Clone(), ShowDrawClassView, this.appWorkspace.DocumentWorkspaces[this.listView1.FocusedItem != null ? this.listView1.FocusedItem.Index : this.listView1.SelectedItems[0].Index].GetRuler(MeasurementUnit.Micron));
////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 bcOriginCheckedChanged(object sender, EventArgs e)
{
if (bc == null && bmc == null)
return;
int BinaryCheckFlag = 0;
if (bmc != null ? bmc.OriginChecked : bc.OriginChecked)
BinaryCheckFlag += 1;
if (bmc != null ? bmc.BinaryChecked : bc.BinaryChecked)
BinaryCheckFlag += 2;
this.binaryExtractionModel.BinaryCheckFlag = BinaryCheckFlag;
}
///
/// 二值化勾选改变事件
///
///
///
private void bcBinaryCheckedChanged(object sender, EventArgs e)
{
if (bc == null && bmc == null)
return;
int BinaryCheckFlag = 0;
if (bmc != null ? bmc.OriginChecked : bc.OriginChecked)
BinaryCheckFlag += 1;
if (bmc != null ? bmc.BinaryChecked : bc.BinaryChecked)
BinaryCheckFlag += 2;
this.binaryExtractionModel.BinaryCheckFlag = BinaryCheckFlag;
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;
Mat imageMat = null;
if (listView1.FocusedItem != null || this.listView1.SelectedItems != null && this.listView1.SelectedItems.Count > 0)
imageMat = this.appWorkspace.DocumentWorkspaces[this.listView1.FocusedItem != null ? this.listView1.FocusedItem.Index : this.listView1.SelectedItems[0].Index].CompositionSurface.CreatedAliasedMat();
if (imageMat != null)
{
//先计算阈值
Mat gray = imageMat.CvtColor(ColorConversionCodes.BGR2GRAY);
double otsu = Cv2.Threshold(gray, gray, 0, 255, ThresholdTypes.Triangle/*.Otsu*/);
if (otsu <= 10 || otsu >= 245)
{
otsu = Cv2.Threshold(imageMat.CvtColor(ColorConversionCodes.BGR2GRAY), gray, 0, 255, ThresholdTypes.Otsu);//.Triangle
}
//如果当前是两个区间,则需要重新计算一次
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));
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);
this.initParamsToAction();
}
///
/// 获取相的工作结构
///
/// 指明获取第几个相的工作结构
///
public List getPhaseModels(int index)
{
return this.documentItems[index].phaseModels;
}
///
/// 应用到全部
///
/// 所有要改变参数的图片tag
/// 当前操作的图片tag
public void applyToAll(System.Collections.Specialized.StringCollection imagesKeys, string currentImagesKey)
{
foreach (var imagesKey in imagesKeys)
{
if (imagesKey.Equals(currentImagesKey))
continue;//#
if (!binaryModelDict.ContainsKey(imagesKey))
{
binaryModelFlag.Add(imagesKey, true);
binaryModelDict.Add(imagesKey, this.binaryExtractionModel.cloneModel());
if (!binaryData1Dict.ContainsKey(imagesKey))
{
binaryData1Dict.Add(imagesKey, null);
}
else
binaryData1Dict[imagesKey] = null;//###
}
else
{
binaryModelFlag[imagesKey] = true;
binaryModelDict[imagesKey] = this.binaryExtractionModel.cloneModel();
binaryData1Dict[imagesKey] = null;
}
}
}
///
/// 保存二值化图片
///
///
public void saveMat(string imagesKey)
{
if (binaryData1Dict.ContainsKey(imagesKey))
{
if (binaryData1Dict[imagesKey] != null)
binaryData1Dict[imagesKey] = null;
binaryData1Dict[imagesKey] = this.documentWorkspace.phaseModels[0].mat;
}
else
{
try
{
binaryData1Dict.Add(imagesKey, this.documentWorkspace.phaseModels[0].mat);
}
catch (Exception)
{
binaryData1Dict.Add(imagesKey, null);
}
}
}
///
/// ListView图片选择改变事件,不显示视场
///
/// 选择改变后的图片tag
/// 选择改变后的图片
public void listView1_SelectedIndexChangedHideDrawClassView(Mat imageMat, string imagesKey = null)
{
if (imagesKey != null)
{
if (!binaryModelDict.ContainsKey(imagesKey))
{
binaryModelFlag.Add(imagesKey, false);
this.binaryExtractionModel = this.binaryExtractionModel.cloneModel();
binaryModelDict.Add(imagesKey, this.binaryExtractionModel);
}
//else
{
this.binaryExtractionModel = binaryModelDict[imagesKey];
this.initParamsToAction();
this.InitParameterToControl();
}
}
Document document = Document.FromImageMat(imageMat/* //待测试.Clone()*/);
this.documentWorkspace.Document = document;
this.documentWorkspace.Visible = true;
GraphicsList itemGraphicsList = this.appWorkspace.DocumentWorkspaces[this.listView1.FocusedItem != null ? this.listView1.FocusedItem.Index : this.listView1.SelectedItems[0].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 != null ? this.listView1.FocusedItem.Index : this.listView1.SelectedItems[0].Index));
if ((imagesKey == null || !binaryModelFlag.ContainsKey(imagesKey) || !binaryModelFlag[imagesKey]/*用于更新图片二值化效果*/) && (bc != null || bmc != null) && this.documentWorkspace.PhaseModels[0].mat != null)
{
if (!(bmc != null ? bmc.BinaryChecked : bc.BinaryChecked) && this.documentWorkspace.PhaseModels[0].choise
/*&& (this.binaryExtractionModel.BinaryCheckFlag > 1)*/)
{
this.toApplyBinary = false;
if (bmc != null)
bmc.BinaryChecked = true;
else
bc.BinaryChecked = true;
}
}
else
{
if (binaryData1Dict.ContainsKey(imagesKey) && binaryData1Dict[imagesKey] != null)
{
documentWorkspace.PhaseModels[0].mat = binaryData1Dict[imagesKey];// this.PerformProcess(
//PaintDotNet.Camera.Tools.ToMat(documentWorkspace.GetFullSizeWithRegion()),
//PaintDotNet.Camera.Tools.ToMat(bitmap));
//documentWorkspace.PhaseModels[0].color = (bmc != null ? bmc.BinaryBackColor.ToArgb() : bc.BinaryBackColor.ToArgb());
documentWorkspace.Refresh();
}
else
this.applyButtonImpl();
}
if (imagesKey != null && binaryModelFlag.ContainsKey(imagesKey)/*用于更新图片二值化效果*/)
{
this.documentWorkspace.PhaseModels[0].choise = (this.binaryExtractionModel.BinaryCheckFlag == 2)/*(bmc != null ? bmc.BinaryChecked : bc.BinaryChecked)*/;
binaryModelFlag[imagesKey] = false;
if (bmc != null)
bmc.BinaryChecked = this.documentWorkspace.PhaseModels[0].choise || (this.binaryExtractionModel.BinaryCheckFlag > 1);
else
bc.BinaryChecked = this.documentWorkspace.PhaseModels[0].choise || (this.binaryExtractionModel.BinaryCheckFlag > 1);
}
else if ((bc != null || bmc != null) && !this.documentWorkspace.PhaseModels[0].choise
&& (this.binaryExtractionModel.BinaryCheckFlag <= 1))
if (bmc != null)
bmc.BinaryChecked = false;
else
bc.BinaryChecked = false;
if (imagesKey != null)
{
if (this.binaryExtractionModel.BinaryCheckFlag % 2 == 1)
{
if (bmc != null)
bmc.OriginChecked = true;
else
bc.OriginChecked = true;
}
else
{
if (bmc != null)
bmc.OriginChecked = false;
else
bc.OriginChecked = false;
}
}
//显示直方图
if (bc != null || bmc != null)
if (bmc != null)
bmc.CreateHistogram(imageMat, true, 339, 130, 0);
else
bc.CreateHistogram(imageMat, true, 339, 130, 0);
this.RefreshHistogramControl1Values();//##
}
///
/// ListView图片选择改变事件
///
/// 选择改变后的图片
public void listView1_SelectedIndexChanged(Mat imageMat, string imagesKey = null)
{
if (imagesKey != null)
{
if (!binaryModelDict.ContainsKey(imagesKey))
{
binaryModelFlag.Add(imagesKey, false);
this.binaryExtractionModel = this.binaryExtractionModel.cloneModel();
binaryModelDict.Add(imagesKey, this.binaryExtractionModel);
}
//else
{
this.binaryExtractionModel = binaryModelDict[imagesKey];
this.initParamsToAction();
this.InitParameterToControl();
}
}
Document document = Document.FromImageMat(imageMat/* //待测试.Clone()*/);
this.documentWorkspace.Document = document;
this.documentWorkspace.Visible = true;
this.documentWorkspace.GraphicsList = this.appWorkspace.DocumentWorkspaces[this.listView1.FocusedItem != null ? this.listView1.FocusedItem.Index : this.listView1.SelectedItems[0].Index].GraphicsList;
this.documentWorkspace.PhaseModels = new List();
this.documentWorkspace.PhaseModels.AddRange(this.getPhaseModels(this.listView1.FocusedItem != null ? this.listView1.FocusedItem.Index : this.listView1.SelectedItems[0].Index));
if (((imagesKey == null || !binaryModelFlag.ContainsKey(imagesKey) || !binaryModelFlag[imagesKey]/*用于更新图片二值化效果*/) && (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 (imagesKey != null && binaryModelFlag.ContainsKey(imagesKey)/*用于更新图片二值化效果*/)
{
this.documentWorkspace.PhaseModels[0].choise = (this.binaryExtractionModel.BinaryCheckFlag == 2);// (bmc != null ? bmc.BinaryChecked : bc.BinaryChecked);
binaryModelFlag[imagesKey] = false;
if (bmc != null)
bmc.BinaryChecked = this.documentWorkspace.PhaseModels[0].choise || (this.binaryExtractionModel.BinaryCheckFlag > 1);
else
bc.BinaryChecked = this.documentWorkspace.PhaseModels[0].choise || (this.binaryExtractionModel.BinaryCheckFlag > 1);
}
else if ((bc != null || bmc != null) && !this.documentWorkspace.PhaseModels[0].choise
&& (this.binaryExtractionModel.BinaryCheckFlag <= 1))
if (bmc != null)
bmc.BinaryChecked = false;
else
bc.BinaryChecked = false;
if (imagesKey != null)
{
if (this.binaryExtractionModel.BinaryCheckFlag % 2 == 1)
{
if (bmc != null)
bmc.OriginChecked = true;
else
bc.OriginChecked = true;
}
else
{
if (bmc != null)
bmc.OriginChecked = false;
else
bc.OriginChecked = false;
}
}
//显示直方图
if (bc != null || bmc != null)
if (bmc != null)
bmc.CreateHistogram(imageMat, true, 339, 130, 0);
else
bc.CreateHistogram(imageMat, true, 339, 130, 0);
this.RefreshHistogramControl1Values();
}
///
/// ListView图片选择改变事件
///
/// 选择改变后的图片
public void listView1_SelectedIndexChangedNoCheckedChanged(Mat imageMat, string imagesKey = null)
{
if (imagesKey != null)
{
if (!binaryModelDict.ContainsKey(imagesKey))
{
binaryModelFlag.Add(imagesKey, false);
this.binaryExtractionModel = this.binaryExtractionModel.cloneModel();
binaryModelDict.Add(imagesKey, this.binaryExtractionModel);
}
//else
{
this.binaryExtractionModel = binaryModelDict[imagesKey];
this.initParamsToAction();
this.InitParameterToControl();
}
}
Document document = Document.FromImageMat(imageMat/* //待测试.Clone()*/);
this.documentWorkspace.Document = document;
this.documentWorkspace.Visible = true;
this.documentWorkspace.GraphicsList = this.appWorkspace.DocumentWorkspaces[this.listView1.FocusedItem != null ? this.listView1.FocusedItem.Index : this.listView1.SelectedItems[0].Index].GraphicsList;
this.documentWorkspace.PhaseModels = new List();
this.documentWorkspace.PhaseModels.AddRange(this.getPhaseModels(this.listView1.FocusedItem != null ? this.listView1.FocusedItem.Index : this.listView1.SelectedItems[0].Index));
if ((imagesKey == null || !binaryModelFlag.ContainsKey(imagesKey) || !binaryModelFlag[imagesKey]/*用于更新图片二值化效果*/) && (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();
}
if (imagesKey != null && binaryModelFlag.ContainsKey(imagesKey)/*用于更新图片二值化效果*/)
{
this.documentWorkspace.PhaseModels[0].choise = (this.binaryExtractionModel.BinaryCheckFlag == 2);// (bmc != null ? bmc.BinaryChecked : bc.BinaryChecked);
binaryModelFlag[imagesKey] = false;
if (bmc != null)
bmc.BinaryChecked = this.documentWorkspace.PhaseModels[0].choise || (this.binaryExtractionModel.BinaryCheckFlag > 1);
else
bc.BinaryChecked = this.documentWorkspace.PhaseModels[0].choise || (this.binaryExtractionModel.BinaryCheckFlag > 1);
}
//else
////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(imageMat, true, 339, 130, 0);
else
bc.CreateHistogram(imageMat, true, 339, 130, 0);
this.RefreshHistogramControl1Values();
}
///
/// 构造相的工作结构
///
/// 相的命名,如果只有二值则只传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;
//如果是脚本执行,将参数带入(AppWorkspace.ScriptRunning && AppWorkspace.ScriptCurrentParam != null && AppWorkspace.ScriptCurrentParam.MenuId == action.MenuId);
if (this.appWorkspace.ScriptRunning && this.appWorkspace.ScriptCurrentParam != null)
{
binaryExtractionModel = new BinaryExtractionModel();
foreach (Args args in analysisAction.Lists)
{
Args param1 = this.appWorkspace.ScriptCurrentParam.Lists.Find(m => m.Key.Equals(args.Key));
if (param1 != null && param1.value != null)
args.Value = param1.value;
if (args.Key == "colorInterval")
{
this.binaryExtractionModel.ColorInterval = (Boolean)args.Value ? 2 : 1;
}
else if (args.Key == "scope1")
{
List twoL = (List)args.Value;
this.binaryExtractionModel.ColorOneStart = (int)twoL[0];
this.binaryExtractionModel.ColorOneEnd = (int)twoL[1];
}
else if (args.Key == "scope2")
{
List twoL = (List)args.Value;
this.binaryExtractionModel.ColorTwoStart = (int)twoL[0];
this.binaryExtractionModel.ColorTwoEnd = (int)twoL[1];
}
else if (args.Key == "scope3")
{
List twoL = (List)args.Value;
this.binaryExtractionModel.ColorThreeStart = (int)twoL[0];
this.binaryExtractionModel.ColorThreeEnd = (int)twoL[1];
}
else if (args.Key == "scope4")
{
this.binaryExtractionModel.DebrisAreaStart = (int)((List)args.Value)[0];
this.binaryExtractionModel.DebrisAreaEnd = (int)((List)args.Value)[1];
}
else if (args.Key == "binaryStyle")
{
this.binaryExtractionModel.BinaryStyle = (int)args.Value;
}
else if (args.Key == "phaseColor")
{
this.binaryExtractionModel.PhaseColor = (int)args.Value;
}
else if (args.Key == "binarization")
{
this.binaryExtractionModel.BinaryCheckFlag = Convert.ToBoolean(args.Value) == true ? 2 : 0;
}
else if (args.Key == "theoriginalcolor")
{
bool thisValue = Convert.ToBoolean(args.Value);
if (thisValue)
{
this.binaryExtractionModel.BinaryCheckFlag = this.binaryExtractionModel.BinaryCheckFlag == 2 ? 3 : 1;
}
}
}
//string xmlFilePath = Application.StartupPath + "\\Config\\" + Startup.instance.SettingPrefix + "\\BinaryConfig\\Default_" + this.menuId + ".xml";
//bool createNewFile = !System.IO.File.Exists(xmlFilePath);
//if (createNewFile)
// xmlFilePath = Application.StartupPath + "\\Config\\" + Startup.instance.SettingPrefix + "\\BinaryExtraction\\Default.xml";
//this.binaryExtractionModel = XmlSerializeHelper.DESerializer(FileOperationHelper.ReadStringFromFile(xmlFilePath, FileMode.Open));
//if (createNewFile)
// this.binaryExtractionModel.BinaryCheckFlag = 0;
this.initParamsToAction();
}
else
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++)
{
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(graphicsList, phaseModels));
}
//
//调色板
//
this.colorsForm1 = new ColorsForm();
this.colorsForm1.StartPosition = FormStartPosition.CenterScreen;
this.colorsForm1.setSaveBtn_Click(new System.EventHandler(this.colorsForm1Changed));
this.InitParameterToControl();//###
if (this.binaryExtractionModel != null)
this.applyButtonImpl();
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;
//如果是脚本执行,将参数带入(AppWorkspace.ScriptRunning && AppWorkspace.ScriptCurrentParam != null && AppWorkspace.ScriptCurrentParam.MenuId == action.MenuId);
if (this.appWorkspace.ScriptRunning && this.appWorkspace.ScriptCurrentParam != null)
{
binaryExtractionModel = new BinaryExtractionModel();
foreach (Args args in analysisAction.Lists)
{
Args param1 = this.appWorkspace.ScriptCurrentParam.Lists.Find(m => m.Key.Equals(args.Key));
if (param1 != null && param1.value != null)
args.Value = param1.value;
if (args.Key == "colorInterval")
{
this.binaryExtractionModel.ColorInterval = (Boolean)args.Value ? 2 : 1;
}
else if (args.Key == "scope1")
{
List twoL = (List)args.Value;
this.binaryExtractionModel.ColorOneStart = (int)twoL[0];
this.binaryExtractionModel.ColorOneEnd = (int)twoL[1];
}
else if (args.Key == "scope2")
{
List twoL = (List)args.Value;
this.binaryExtractionModel.ColorTwoStart = (int)twoL[0];
this.binaryExtractionModel.ColorTwoEnd = (int)twoL[1];
}
else if (args.Key == "scope3")
{
List twoL = (List)args.Value;
this.binaryExtractionModel.ColorThreeStart = (int)twoL[0];
this.binaryExtractionModel.ColorThreeEnd = (int)twoL[1];
}
else if (args.Key == "scope4")
{
this.binaryExtractionModel.DebrisAreaStart = (int)((List)args.Value)[0];
this.binaryExtractionModel.DebrisAreaEnd = (int)((List)args.Value)[1];
}
else if (args.Key == "binaryStyle")
{
this.binaryExtractionModel.BinaryStyle = (int)args.Value;
}
else if (args.Key == "phaseColor")
{
this.binaryExtractionModel.PhaseColor = (int)args.Value;
}
else if (args.Key == "binarization")
{
this.binaryExtractionModel.BinaryCheckFlag = Convert.ToBoolean(args.Value) == true ? 2 : 0;
}
else if (args.Key == "theoriginalcolor")
{
bool thisValue = Convert.ToBoolean(args.Value);
if (thisValue)
{
this.binaryExtractionModel.BinaryCheckFlag = this.binaryExtractionModel.BinaryCheckFlag == 2 ? 3 : 1;
}
}
}
//string xmlFilePath = Application.StartupPath + "\\Config\\" + Startup.instance.SettingPrefix + "\\BinaryConfig\\Default_" + this.menuId + ".xml";
//bool createNewFile = !System.IO.File.Exists(xmlFilePath);
//if (createNewFile)
// xmlFilePath = Application.StartupPath + "\\Config\\" + Startup.instance.SettingPrefix + "\\BinaryExtraction\\Default.xml";
//this.binaryExtractionModel = XmlSerializeHelper.DESerializer(FileOperationHelper.ReadStringFromFile(xmlFilePath, FileMode.Open));
//if (createNewFile)
// this.binaryExtractionModel.BinaryCheckFlag = 0;
this.initParamsToAction();
}
else
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++)
{
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(graphicsList, phaseModels));
}
//
//调色板
//
this.colorsForm1 = new ColorsForm();
this.colorsForm1.StartPosition = FormStartPosition.CenterScreen;
this.colorsForm1.setSaveBtn_Click(new System.EventHandler(this.colorsForm1Changed));
this.InitParameterToControl();//###
if (this.binaryExtractionModel != null)
this.applyButtonImpl();
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()
{
if (this.binaryExtractionModel != null)
{
if (bc != null || bmc != null)
{
if (this.binaryExtractionModel.BinaryCheckFlag > 1)
{
if (bmc != null)
bmc.BinaryChecked = true;
else
bc.BinaryChecked = true;
}
if (this.binaryExtractionModel.BinaryCheckFlag % 2 == 1)
{
if (bmc != null)
bmc.OriginChecked = true;
else
bc.OriginChecked = true;
}
//阈值相关
//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();
}
}
}
///
/// 更新显示直方图的数据
///
public void RefreshHistogramControl1Values()
{
////显示直方图
//if (bc != null || bmc != null)
// if (bmc != null)
// bmc.CreateHistogram(bitmap, true, 339, 130, 0);
// else
// bc.CreateHistogram(bitmap, true, 339, 130, 0);
if (bmc != null)
bmc.InitParameterToControl(this.binaryExtractionModel.ColorInterval);
else
bc.InitParameterToControl(this.binaryExtractionModel.ColorInterval);
}
Vec4b vecForPhaseColor4B;
public unsafe 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;
}
}
if (this.documentWorkspace.PhaseModels[0].mat == null || this.documentWorkspace.PhaseModels[0].mat.IsDisposed)
{
this.applyButtonImpl();
}
else
{
Mat source = this.documentWorkspace.PhaseModels[0].mat;
//##20143
if (source.Channels() != 4)
{
this.applyButtonImpl();// false;、、should never go here !
return;
}
//二值提取
if (dstBinary != null)
{
dstBinary.Dispose();
dstBinary = null;
}
dstBinary = new Mat(source.Rows, source.Cols, MatType.CV_8UC4, new Scalar(0, 0, 0, 0));
//赋值二值相的颜色
vecForPhaseColor4B = new Vec4b(color.B, color.G, color.R, color.A);
source.ForEachAsVec4b(ForeachFunctionByteForPhaseColor4B);
this.documentWorkspace.PhaseModels[0].mat/*Mat dst*/ = dstBinary.Clone();
this.documentWorkspace.Refresh();
dstBinary.Dispose();
dstBinary = null;
}
//return dst;// true;
}
///
/// 提取二值化的透明通道并赋值
///
///
///
public unsafe void ForeachFunctionByteForPhaseColor4B(Vec4b* t, int* position)
{
if (t->Item3 > 0)
dstBinary.Set(position[0], position[1], vecForPhaseColor4B);
}
///
/// 保证二值化颜色赋值到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(string imagesKey = null, int analysisPicture = -1, Mat imageMat = null)
{
if (bc == null && bmc == null)
return;
//long start = Cv2.GetTickCount();
if (imageMat != null ||
(listView1.FocusedItem != null || this.listView1.SelectedItems != null && this.listView1.SelectedItems.Count > 0) && (bmc != null ? bmc.BinaryChecked : bc.BinaryChecked))
{
bool processWholeMat11 = (imageMat != null);
if (imageMat == null)
imageMat = this.appWorkspace.DocumentWorkspaces[this.listView1.FocusedItem != null ? this.listView1.FocusedItem.Index : this.listView1.SelectedItems[0].Index].CompositionSurface.CreatedAliasedMat();
//判断是否存在视场,如果存在视场,则把视场提取出来,进行处理//可能会带视场//########################
DocumentView documentWorkspace = this.documentWorkspace;
if (analysisPicture >= 0)
documentWorkspace = this.appWorkspace.DocumentWorkspaces[analysisPicture];
if (!processWholeMat11
&& !processWholeMat && documentWorkspace.GraphicsList.IsExsitView())
{
if (analysisPicture == -1)
{
documentWorkspace.PhaseModels[0].mat = this.PerformProcess(
PaintDotNet.Camera.Tools.ToMat(documentWorkspace.GetFullSizeWithRegion()),
imageMat.Clone());
documentWorkspace.PhaseModels[0].color = (bmc != null ? bmc.BinaryBackColor.ToArgb() : bc.BinaryBackColor.ToArgb());
documentWorkspace.Refresh();
}
else
{
if (binaryData1Dict.ContainsKey(imagesKey))
{
if (binaryData1Dict[imagesKey] != null)
binaryData1Dict[imagesKey] = null;
binaryData1Dict[imagesKey] = this.PerformProcess(
PaintDotNet.Camera.Tools.ToMat(documentWorkspace.GetFullSizeWithRegion()),
imageMat.Clone());// this.documentWorkspace.phaseModels[0].mat;
}
else
{
try
{
binaryData1Dict.Add(imagesKey, this.PerformProcess(
PaintDotNet.Camera.Tools.ToMat(documentWorkspace.GetFullSizeWithRegion()),
imageMat.Clone())/*this.documentWorkspace.phaseModels[0].mat*/);
}
catch (Exception)
{
binaryData1Dict.Add(imagesKey, null);
}
}
}
}
else if (!processWholeMat11
&& processWholeMat && documentWorkspace.GraphicsList.IsExsitView())
{
if (PhaseModels0Mat != null)
{
PhaseModels0Mat.Dispose();
PhaseModels0Mat = null;
}
Mat OrgImg0 = this.PerformProcess(imageMat.Clone());
PhaseModels0Mat = OrgImg0.Clone();
Vec4b vec4 = new Vec4b(0, 0, 0, 0);
//视场图像
Mat exsitViewMatOrg = PaintDotNet.Camera.Tools.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);//不在视场图像范围内
}
}
if (analysisPicture == -1)
{
documentWorkspace.PhaseModels[0].mat = OrgImg0;
documentWorkspace.PhaseModels[0].color = (bmc != null ? bmc.BinaryBackColor.ToArgb() : bc.BinaryBackColor.ToArgb());
documentWorkspace.Refresh();
}
else
{
if (binaryData1Dict.ContainsKey(imagesKey))
{
if (binaryData1Dict[imagesKey] != null)
binaryData1Dict[imagesKey] = null;
binaryData1Dict[imagesKey] = OrgImg0;// this.documentWorkspace.phaseModels[0].mat;
}
else
{
try
{
binaryData1Dict.Add(imagesKey, OrgImg0/*this.documentWorkspace.phaseModels[0].mat*/);
}
catch (Exception)
{
binaryData1Dict.Add(imagesKey, null);
}
}
}
}
else
{
if (analysisPicture == -1)
{
documentWorkspace.PhaseModels[0].mat = this.PerformProcess(imageMat.Clone());
documentWorkspace.PhaseModels[0].color = (bmc != null ? bmc.BinaryBackColor.ToArgb() : bc.BinaryBackColor.ToArgb());
documentWorkspace.Refresh();
}
else
{
if (binaryData1Dict.ContainsKey(imagesKey))
{
if (binaryData1Dict[imagesKey] != null)
binaryData1Dict[imagesKey] = null;
binaryData1Dict[imagesKey] = this.PerformProcess(imageMat.Clone());// this.documentWorkspace.phaseModels[0].mat;
}
else
{
try
{
binaryData1Dict.Add(imagesKey, this.PerformProcess(imageMat.Clone())/*this.documentWorkspace.phaseModels[0].mat*/);
}
catch (Exception)
{
binaryData1Dict.Add(imagesKey, null);
}
}
}
}
}
////double time = (Cv2.GetTickCount() - start) / Cv2.GetTickFrequency();
////System.Console.WriteLine("执行时间:" + time);
////AddRectangleToRectangleFCaps();//0916###18745
//if (analysisPicture != -1)
OnBinaryImplFinishAction();
}
/////
///// (夹杂物专用)参数改变时,重新处理图像
/////
/// 不是当前显示图片的需要传值
/// 不是当前显示图片的需要传值
/// 不带视场处理的需要传值
public void applyButtonImplMethodInclusion(string imagesKey = null, int analysisPicture = -1, Mat imageMat = null, int index = 0, int color = 0, List valueList = null)
{
if (bc == null && bmc == null)
return;
if (imageMat != null ||
(listView1.FocusedItem != null || this.listView1.SelectedItems != null && this.listView1.SelectedItems.Count > 0))
{
bool processWholeMat11 = (imageMat != null);
if (imageMat == null)
imageMat = this.appWorkspace.DocumentWorkspaces[this.listView1.FocusedItem != null ? this.listView1.FocusedItem.Index : this.listView1.SelectedItems[0].Index].CompositionSurface.CreatedAliasedMat();
//判断是否存在视场,如果存在视场,则把视场提取出来,进行处理//可能会带视场//########################
DocumentView documentWorkspace = this.documentWorkspace;
if (analysisPicture >= 0)
documentWorkspace = this.appWorkspace.DocumentWorkspaces[analysisPicture];
if (!processWholeMat11
&& !processWholeMat && documentWorkspace.GraphicsList.IsExsitView())
{
//if (analysisPicture == -1)
//{
//}
//else
//{
// if (binaryData1Dict.ContainsKey(imagesKey))
// {
// if (binaryData1Dict[imagesKey] != null)
// binaryData1Dict[imagesKey] = null;
// binaryData1Dict[imagesKey] = this.PerformProcess(
// PaintDotNet.Camera.Tools.ToMat(documentWorkspace.GetFullSizeWithRegion()),
// PaintDotNet.Camera.Tools.ToMat(bitmap));// this.documentWorkspace.phaseModels[0].mat;
// }
// else
// {
// try
// {
// binaryData1Dict.Add(imagesKey, this.PerformProcess(
// PaintDotNet.Camera.Tools.ToMat(documentWorkspace.GetFullSizeWithRegion()),
// PaintDotNet.Camera.Tools.ToMat(bitmap))/*this.documentWorkspace.phaseModels[0].mat*/);
// }
// catch (Exception)
// {
// binaryData1Dict.Add(imagesKey, null);
// }
// }
//}
this.binaryExtractionModel.PhaseColor = color;
//###
foreach (Args args in action.Lists)
{
if (args.Key == "phaseColor")
{
args.Value = color;//this.binaryExtractionModel.PhaseColor
}
if (args.Key == "colorInterval")
{
args.Value = 1;
}
else if (args.Key == "scope1")
{
((List)args.Value)[0] = valueList[0];
((List)args.Value)[1] = valueList[1];
}
//else if (args.Key == "scope2")
//{
// ((List)args.Value)[0] = 0;
// ((List)args.Value)[1] = 139;
//}
//else if (args.Key == "scope3")
//{
// ((List)args.Value)[0] = 140;
// ((List)args.Value)[1] = 255;
//}
}
documentWorkspace.PhaseModels[index].mat = this.PerformProcess(
PaintDotNet.Camera.Tools.ToMat(documentWorkspace.GetFullSizeWithRegion()),
imageMat.Clone());
documentWorkspace.PhaseModels[index].color = color;
documentWorkspace.Refresh();
}
else if (!processWholeMat11
&& processWholeMat && documentWorkspace.GraphicsList.IsExsitView())
{
if (PhaseModels0Mat != null)
{
PhaseModels0Mat.Dispose();
PhaseModels0Mat = null;
}
Mat OrgImg0 = this.PerformProcess(imageMat.Clone());
PhaseModels0Mat = OrgImg0.Clone();
Vec4b vec4 = new Vec4b(0, 0, 0, 0);
//视场图像
Mat exsitViewMatOrg = PaintDotNet.Camera.Tools.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[index].mat = OrgImg0;
documentWorkspace.PhaseModels[index].color = color;
documentWorkspace.Refresh();
//if (analysisPicture == -1)
//{
//}
//else
//{
// if (binaryData1Dict.ContainsKey(imagesKey))
// {
// if (binaryData1Dict[imagesKey] != null)
// binaryData1Dict[imagesKey] = null;
// binaryData1Dict[imagesKey] = OrgImg0;// this.documentWorkspace.phaseModels[0].mat;
// }
// else
// {
// try
// {
// binaryData1Dict.Add(imagesKey, OrgImg0/*this.documentWorkspace.phaseModels[0].mat*/);
// }
// catch (Exception)
// {
// binaryData1Dict.Add(imagesKey, null);
// }
// }
//}
}
else
{
documentWorkspace.PhaseModels[0].mat = this.PerformProcess(imageMat.Clone());
documentWorkspace.PhaseModels[0].color = color;
documentWorkspace.Refresh();
//if (analysisPicture == -1)
//{
//}
//else
//{
// if (binaryData1Dict.ContainsKey(imagesKey))
// {
// if (binaryData1Dict[imagesKey] != null)
// binaryData1Dict[imagesKey] = null;
// binaryData1Dict[imagesKey] = this.PerformProcess(PaintDotNet.Camera.Tools.ToMat(bitmap));// this.documentWorkspace.phaseModels[0].mat;
// }
// else
// {
// try
// {
// binaryData1Dict.Add(imagesKey, this.PerformProcess(PaintDotNet.Camera.Tools.ToMat(bitmap))/*this.documentWorkspace.phaseModels[0].mat*/);
// }
// catch (Exception)
// {
// binaryData1Dict.Add(imagesKey, null);
// }
// }
//}
}
}
////double time = (Cv2.GetTickCount() - start) / Cv2.GetTickFrequency();
////System.Console.WriteLine("执行时间:" + time);
////AddRectangleToRectangleFCaps();//0916###18745
//if (analysisPicture != -1)
OnBinaryImplFinishAction();
}
///
/// 初始化二值化相关的变量
///
public void initParams()
{
string xmlFilePath = Application.StartupPath + "\\Config\\" + Startup.instance.SettingPrefix + "\\BinaryConfig\\Default_" + this.menuId + ".xml";
bool createNewFile = !System.IO.File.Exists(xmlFilePath);
if (createNewFile)
xmlFilePath = Application.StartupPath + "\\Config\\" + Startup.instance.SettingPrefix + "\\BinaryExtraction\\Default.xml";
this.binaryExtractionModel = XmlSerializeHelper.DESerializer(FileOperationHelper.ReadStringFromFile(xmlFilePath, FileMode.Open));
if (createNewFile)
this.binaryExtractionModel.BinaryCheckFlag = 0;
this.initParamsToAction();
}
///
/// 把参数的值设置到action
///
public void initParamsToAction()
{
foreach (Args args in action.Lists)
{
if (args.Key == "colorInterval")
args.Value = this.binaryExtractionModel.ColorInterval;
else if (args.Key == "binaryStyle")
args.Value = this.binaryExtractionModel.BinaryStyle;
else if (args.Key == "scope1")
{
((List)args.Value)[0] = (int)(this.binaryExtractionModel.ColorOneStart);
((List)args.Value)[1] = (int)(this.binaryExtractionModel.ColorOneEnd);
}
else if (args.Key == "scope2")
{
((List)args.Value)[0] = (int)(this.binaryExtractionModel.ColorTwoStart);
((List)args.Value)[1] = (int)(this.binaryExtractionModel.ColorTwoEnd);
}
else if (args.Key == "scope3")
{
((List)args.Value)[0] = (int)(this.binaryExtractionModel.ColorThreeStart);
((List)args.Value)[1] = (int)(this.binaryExtractionModel.ColorThreeEnd);
}
else if (args.Key == "phaseColor")//#22092
{
args.Value = this.binaryExtractionModel.PhaseColor;
}
}
}
///
/// 保存二值化相关的变量
///
public void saveParams()
{
string xmlFilePath = Application.StartupPath + "\\Config\\" + Startup.instance.SettingPrefix + "\\BinaryConfig\\Default_" + this.menuId + ".xml";
string userInfoXml = XmlSerializeHelper.XmlSerialize(this.binaryExtractionModel);
FileOperationHelper.WriteStringToFile(userInfoXml, xmlFilePath, System.IO.FileMode.Create);
}
#region [脚本录制参数]
public object setScriptRecording(string key) {
object value = null;
if (key == "colorInterval")
{
value = this.binaryExtractionModel.ColorInterval == 2 ? true : false;
}
else if (key == "scope1")
{
value = new List() { this.binaryExtractionModel.ColorOneStart, this.binaryExtractionModel.ColorOneEnd };
}
else if (key == "scope2")
{
value = new List() { this.binaryExtractionModel.ColorTwoStart, this.binaryExtractionModel.ColorTwoEnd };
}
else if (key == "scope3")
{
value = new List() { this.binaryExtractionModel.ColorThreeStart, this.binaryExtractionModel.ColorThreeEnd };
}
else if (key == "scope4")
{
value = new List() { this.binaryExtractionModel.DebrisAreaStart, this.binaryExtractionModel.DebrisAreaEnd };
}
else if (key == "binaryStyle")
{
value = this.binaryExtractionModel.BinaryStyle;
}
else if (key == "phaseColor")
{
value = this.binaryExtractionModel.PhaseColor;
}
else if (key == "binarization")
{
value = this.binaryExtractionModel.BinaryCheckFlag == 2 || this.binaryExtractionModel.BinaryCheckFlag == 3 ? true : false;
}
else if (key == "theoriginalcolor")
{
value = this.binaryExtractionModel.BinaryCheckFlag == 1 || this.binaryExtractionModel.BinaryCheckFlag == 3 ? true : false;
}
return value;
}
#endregion
#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 GraphicsList graphicsList;
public List phaseModels;
public DocumentItem(GraphicsList graphicsList, List phaseModels)
{
this.graphicsList = graphicsList;
this.phaseModels = phaseModels;
}
}
#endregion
}
}