BinaryClass.cs 79 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772
  1. using OpenCvSharp;
  2. using PaintDotNet.Annotation;
  3. using PaintDotNet.Base;
  4. using PaintDotNet.Base.CommTool;
  5. using PaintDotNet.Base.Functionodel;
  6. using PaintDotNet.Base.SettingModel;
  7. using PaintDotNet.Data.Param;
  8. using System;
  9. using System.Collections.Generic;
  10. using System.Drawing;
  11. using System.IO;
  12. using System.Windows.Forms;
  13. namespace PaintDotNet
  14. {
  15. class BinaryClass
  16. {
  17. private int menuId;
  18. protected static string ParamKey_binaryStyle = "binaryStyle";//二值样式
  19. //protected static string ParamKey_displaygrid = "displaygrid";//显示网格
  20. //protected static string ParamKey_displaysections = "displaysections";//显示截点
  21. /// <summary>
  22. /// 是否针对全图进行二值化操作
  23. /// </summary>
  24. public bool processWholeMat = false;
  25. /// <summary>
  26. /// 是否显示视场
  27. /// </summary>
  28. public Boolean ShowDrawClassView = true;
  29. /// <summary>
  30. /// 调色板
  31. /// </summary>
  32. PaintDotNet.ColorsForm colorsForm1;
  33. /// <summary>
  34. /// 构造工作结构
  35. /// </summary>
  36. private Dictionary<int, DocumentItem> documentItems;
  37. /// <summary>
  38. /// 需要的数据在这里引用
  39. /// </summary>
  40. CustomControl.BinaryControlSmaller bmc;
  41. /// <summary>
  42. /// 需要的数据在这里引用
  43. /// </summary>
  44. CustomControl.BinaryControl bc;
  45. AppWorkspace appWorkspace;
  46. DocumentWorkspaceWindow documentWorkspace;
  47. ListView listView1;
  48. /// <summary>
  49. /// 是否要调用二值化的算法
  50. /// </summary>
  51. private bool toApplyBinary = true;
  52. /// <summary>
  53. /// 处理程序
  54. /// </summary>
  55. private ParamObject action = new Data.Action.Action901();
  56. /// <summary>
  57. /// 处理程序
  58. /// </summary>
  59. private ParamObject analysisAction = new Data.Action.ActionAnalysis();
  60. /// <summary>
  61. /// 二值参数配置值(当前图片)
  62. /// </summary>
  63. private BinaryExtractionModel binaryExtractionModel;
  64. /// <summary>
  65. /// 二值参数是否进行全部应用,用于更新图片二值化效果,默认为false
  66. /// </summary>
  67. private Dictionary<string, bool> binaryModelFlag = new Dictionary<string, bool>();
  68. /// <summary>
  69. /// 二值参数配置值(全部图片的)
  70. /// </summary>
  71. private Dictionary<string, BinaryExtractionModel> binaryModelDict = new Dictionary<string, BinaryExtractionModel>();
  72. /// <summary>
  73. /// 二值(备份全部图片)
  74. /// </summary>
  75. private Dictionary<string, Mat> binaryData1Dict = new Dictionary<string, Mat>();
  76. /// <summary>
  77. /// 相0的图片,处理多视场使用
  78. /// </summary>
  79. public Mat PhaseModels0Mat;
  80. /// <summary>
  81. /// 辅助线集成
  82. /// </summary>
  83. public PaintDotNet.DedicatedAnalysis.GrainSizeStandard.IntegrationClass.GrainSizeGuideClass guideClass;
  84. public ComboBox comboBox1;
  85. //public object SelectedItem;
  86. //public GrainSizeGuideClass GuideClass
  87. //{
  88. // set
  89. // {
  90. // this.guideClass = value;
  91. // }
  92. //}
  93. public static Mat dstBinary;
  94. /// <summary>
  95. /// 对二值图像的透明通道提取出单通道图像
  96. /// </summary>
  97. /// <param name="source"></param>
  98. /// <returns></returns>
  99. public unsafe static Mat BGRA2GRAY(Mat source/*, out Mat dst*/)
  100. {
  101. if (source.Channels() != 4)
  102. {
  103. //Mat dst = source;
  104. return source.CvtColor(ColorConversionCodes.BGR2GRAY);// dst;// false;、、should never go here !
  105. }
  106. //二值提取
  107. if (dstBinary != null)
  108. {
  109. dstBinary.Dispose();
  110. dstBinary = null;
  111. }
  112. dstBinary = new Mat(source.Rows, source.Cols, MatType.CV_8UC1, new Scalar(0));
  113. //如果是rgba
  114. source.ForEachAsVec4b(ForeachFunctionByteForWhole4B);
  115. Mat dst = dstBinary.Clone();
  116. dstBinary.Dispose();
  117. dstBinary = null;
  118. return dst;// true;
  119. }
  120. /// <summary>
  121. /// 提取二值化的透明通道并赋值
  122. /// </summary>
  123. /// <param name="t"></param>
  124. /// <param name="position"></param>
  125. public unsafe static void ForeachFunctionByteForWhole4B(Vec4b* t, int* position)
  126. {
  127. dstBinary.Set(position[0], position[1], t->Item3);
  128. }
  129. /// <summary>
  130. /// 公开的事件,每次二值化之后需要的计算事件写在这里
  131. /// </summary>
  132. public event EventHandler BinaryImplFinishAction;
  133. private void OnBinaryImplFinishAction()
  134. {
  135. if (BinaryImplFinishAction != null)
  136. {
  137. BinaryImplFinishAction(this, new EventArgs());
  138. }
  139. }
  140. public BinaryClass(int menuId)
  141. {
  142. this.menuId = menuId;
  143. }
  144. /// <summary>
  145. /// 判断是否进行二值,以及返回二值图片
  146. /// </summary>
  147. /// <param name="imagesKey"></param>
  148. /// <returns></returns>
  149. public Mat getModelMat(string imagesKey)
  150. {
  151. if (binaryData1Dict.ContainsKey(imagesKey) && (!binaryModelDict.ContainsKey(imagesKey) && binaryExtractionModel.BinaryCheckFlag > 1
  152. || binaryModelDict.ContainsKey(imagesKey) && binaryModelDict[imagesKey].BinaryCheckFlag > 1))
  153. return binaryData1Dict[imagesKey];
  154. return null;
  155. }
  156. /// <summary>
  157. /// 判断是否进行二值
  158. /// </summary>
  159. /// <param name="imagesKey"></param>
  160. /// <returns></returns>
  161. public bool getModelBinaryCheckFlag(string imagesKey)
  162. {
  163. if ((!binaryModelDict.ContainsKey(imagesKey) && binaryExtractionModel.BinaryCheckFlag > 1
  164. || binaryModelDict.ContainsKey(imagesKey) && binaryModelDict[imagesKey].BinaryCheckFlag > 1))
  165. return true;
  166. return false;
  167. }
  168. /// <summary>
  169. /// 判断是否需要计算,并根据判断结果进行相关处理
  170. /// </summary>
  171. /// <param name="imagesKey"></param>
  172. /// <param name="analysisPicture"></param>
  173. public void updateAnalysisData(string imagesKey, int analysisPicture)
  174. {
  175. if (!binaryModelDict.ContainsKey(imagesKey) && binaryExtractionModel.BinaryCheckFlag > 1
  176. || binaryModelDict.ContainsKey(imagesKey) && binaryModelDict[imagesKey].BinaryCheckFlag > 1)
  177. ////|| binaryModelFlag.ContainsKey(imagesKey) && binaryModelFlag[imagesKey]/* //################ && binaryModelFlag[imagesKey] = false;*/)
  178. {
  179. this.applyButtonImpl(imagesKey, analysisPicture, /*bitmap = */this.appWorkspace.DocumentWorkspaces[analysisPicture].CompositionSurface.CreatedAliasedMat());
  180. //if (binaryModelFlag.ContainsKey(imagesKey) && binaryModelFlag[imagesKey])
  181. // binaryModelFlag[imagesKey] = false;
  182. }
  183. else if (binaryData1Dict.ContainsKey(imagesKey))
  184. {
  185. if (binaryData1Dict[imagesKey] != null)
  186. binaryData1Dict[imagesKey] = null;
  187. }
  188. }
  189. public void InitBinaryControlEvent()
  190. {
  191. if (bmc != null)
  192. {
  193. bmc.InitBinaryControlEvent();
  194. bmc.BinaryGetParamAction += new EventHandler(this.bcBinaryGetParamAction);
  195. bmc.AutoThresClickAction += new EventHandler(this.bcAutoThresClickAction);
  196. bmc.InverseClickAction += new EventHandler(this.bcBinaryGetParamAction);
  197. bmc.InverseClickAction += new EventHandler(this.bcApplyButtonImplAction);
  198. bmc.RadioButton1ChangedAction += new EventHandler(this.bcBinaryGetParamAction);
  199. bmc.RadioButton2ChangedAction += new EventHandler(this.bcBinaryGetParamAction);
  200. bmc.ApplyButtonImplAction += new EventHandler(this.bcApplyButtonImplAction);
  201. bmc.BinaryCheckedChangedAction += new EventHandler(this.bcBinaryCheckedChanged);
  202. bmc.OriginCheckedChangedAction += new EventHandler(this.bcOriginCheckedChanged);
  203. bmc.BinaryEditClickAction += new EventHandler(this.bcBinaryEditClickAction);
  204. bmc.PanelColorClickAction += new EventHandler(this.bcPanelColorClickAction);//初始化颜色点击事件
  205. return;
  206. }
  207. if (bc == null)
  208. return;
  209. bc.InitBinaryControlEvent();
  210. bc.BinaryGetParamAction += new EventHandler(this.bcBinaryGetParamAction);
  211. bc.AutoThresClickAction += new EventHandler(this.bcAutoThresClickAction);
  212. bc.InverseClickAction += new EventHandler(this.bcBinaryGetParamAction);
  213. bc.InverseClickAction += new EventHandler(this.bcApplyButtonImplAction);
  214. bc.RadioButton1ChangedAction += new EventHandler(this.bcBinaryGetParamAction);
  215. bc.RadioButton2ChangedAction += new EventHandler(this.bcBinaryGetParamAction);
  216. bc.ApplyButtonImplAction += new EventHandler(this.bcApplyButtonImplAction);
  217. bc.BinaryCheckedChangedAction += new EventHandler(this.bcBinaryCheckedChanged);
  218. bc.OriginCheckedChangedAction += new EventHandler(this.bcOriginCheckedChanged);
  219. bc.BinaryEditClickAction += new EventHandler(this.bcBinaryEditClickAction);
  220. bc.PanelColorClickAction += new EventHandler(this.bcPanelColorClickAction);//初始化颜色点击事件
  221. }
  222. /// <summary>
  223. /// 外部需要改变二值化勾选状态时调用该方法
  224. /// </summary>
  225. /// <param name="toCheck"></param>
  226. public void performClickBinaryCheck(bool toCheck)
  227. {
  228. if (bmc != null)
  229. bmc.BinaryChecked = toCheck;
  230. else
  231. bc.BinaryChecked = toCheck;
  232. }
  233. /// <summary>
  234. /// 相颜色点击事件
  235. /// </summary>
  236. /// <param name="sender"></param>
  237. /// <param name="e"></param>
  238. private void bcPanelColorClickAction(object sender, EventArgs e)
  239. {
  240. this.colorsForm1.UserPrimaryColor = ColorBgra.FromColor(bmc != null ? bmc.BinaryBackColor : bc.BinaryBackColor);
  241. this.colorsForm1.ShowDialog();
  242. this.applyButtonImpl(/*bitmap*/);
  243. }
  244. /// <summary>
  245. /// 二值筛选
  246. /// </summary>
  247. /// <param name="sender"></param>
  248. /// <param name="e"></param>
  249. private void bcBinaryEditClickAction(object sender, EventArgs e)
  250. {
  251. if (this.documentWorkspace.PhaseModels[0].mat == null)
  252. {
  253. MessageBox.Show(PdnResources.GetString("Menu.Pleaseperonfirst.text"));
  254. return;
  255. }
  256. 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
  257. , 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));
  258. ////1013###19326 晶粒度的晶界编辑页面,需要把辅助线带过去(客户编辑只处理辅助线位置的晶界)
  259. if (this.guideClass != null && this.comboBox1 != null && this.comboBox1.SelectedItem != null)
  260. {
  261. boundaryEditingDialog.GuideClass = this.guideClass;
  262. boundaryEditingDialog.SelectedItem = this.comboBox1.SelectedItem;
  263. }
  264. if (boundaryEditingDialog.ShowDialog() == DialogResult.OK)
  265. {
  266. this.documentWorkspace.PhaseModels[0].mat = boundaryEditingDialog.PhaseMat.Clone();
  267. OnBinaryImplFinishAction();
  268. }
  269. }
  270. /// <summary>
  271. /// 显示原图勾选改变事件
  272. /// </summary>
  273. /// <param name="sender"></param>
  274. /// <param name="e"></param>
  275. private void bcOriginCheckedChanged(object sender, EventArgs e)
  276. {
  277. if (bc == null && bmc == null)
  278. return;
  279. int BinaryCheckFlag = 0;
  280. if (bmc != null ? bmc.OriginChecked : bc.OriginChecked)
  281. BinaryCheckFlag += 1;
  282. if (bmc != null ? bmc.BinaryChecked : bc.BinaryChecked)
  283. BinaryCheckFlag += 2;
  284. this.binaryExtractionModel.BinaryCheckFlag = BinaryCheckFlag;
  285. }
  286. /// <summary>
  287. /// 二值化勾选改变事件
  288. /// </summary>
  289. /// <param name="sender"></param>
  290. /// <param name="e"></param>
  291. private void bcBinaryCheckedChanged(object sender, EventArgs e)
  292. {
  293. if (bc == null && bmc == null)
  294. return;
  295. int BinaryCheckFlag = 0;
  296. if (bmc != null ? bmc.OriginChecked : bc.OriginChecked)
  297. BinaryCheckFlag += 1;
  298. if (bmc != null ? bmc.BinaryChecked : bc.BinaryChecked)
  299. BinaryCheckFlag += 2;
  300. this.binaryExtractionModel.BinaryCheckFlag = BinaryCheckFlag;
  301. if ((bmc != null ? bmc.BinaryChecked : bc.BinaryChecked) && !(bmc != null ? bmc.OriginChecked : bc.OriginChecked))
  302. {
  303. this.documentWorkspace.PhaseModels[0].choise = true;
  304. }
  305. else
  306. {
  307. this.documentWorkspace.PhaseModels[0].choise = false;
  308. }
  309. if (toApplyBinary)
  310. this.applyButtonImpl();
  311. else
  312. OnBinaryImplFinishAction();
  313. toApplyBinary = true;
  314. if (this.listView1.FocusedItem == null || !(bmc != null ? bmc.BinaryChecked : bc.BinaryChecked))
  315. this.documentWorkspace.Refresh();
  316. }
  317. /// <summary>
  318. /// 自动阈值
  319. /// </summary>
  320. /// <param name="sender"></param>
  321. /// <param name="e"></param>
  322. private void bcAutoThresClickAction(object sender, EventArgs e)
  323. {
  324. if (bc == null && bmc == null)
  325. return;
  326. Mat imageMat = null;
  327. if (listView1.FocusedItem != null || this.listView1.SelectedItems != null && this.listView1.SelectedItems.Count > 0)
  328. imageMat = this.appWorkspace.DocumentWorkspaces[this.listView1.FocusedItem != null ? this.listView1.FocusedItem.Index : this.listView1.SelectedItems[0].Index].CompositionSurface.CreatedAliasedMat();
  329. if (imageMat != null)
  330. {
  331. //先计算阈值
  332. Mat gray = imageMat.CvtColor(ColorConversionCodes.BGR2GRAY);
  333. double otsu = Cv2.Threshold(gray, gray, 0, 255, ThresholdTypes.Triangle/*.Otsu*/);
  334. if (otsu <= 10 || otsu >= 245)
  335. {
  336. otsu = Cv2.Threshold(imageMat.CvtColor(ColorConversionCodes.BGR2GRAY), gray, 0, 255, ThresholdTypes.Otsu);//.Triangle
  337. }
  338. //如果当前是两个区间,则需要重新计算一次
  339. if (this.ColorInterval == 2)
  340. {
  341. if (bmc != null)
  342. bmc.OnInverseClickAction();
  343. else
  344. bc.OnInverseClickAction();
  345. this.ColorInterval = (bmc != null ? bmc.getInverseStyle() : bc.getInverseStyle());
  346. }
  347. else
  348. {
  349. if (bmc != null)
  350. bmc.setInverseStyle(1);
  351. else
  352. bc.setInverseStyle(1);
  353. }
  354. //给控件赋值
  355. if (bmc != null)
  356. bmc.scope1Start = 0;
  357. else
  358. bc.scope1Start = 0;
  359. if (bmc != null)
  360. bmc.scope1End = otsu;
  361. else
  362. bc.scope1End = otsu;
  363. //处理直方图
  364. if (bmc != null)
  365. bmc.UpdateVerticalBarWithOneScope(0, Convert.ToInt32(bmc.scope1End));
  366. else
  367. bc.UpdateVerticalBarWithOneScope(0, Convert.ToInt32(bc.scope1End));
  368. GC.Collect();
  369. this.bcApplyButtonImplAction(sender, e);
  370. }
  371. else
  372. {
  373. MessageBox.Show(PdnResources.GetString("Menu.Pleaseselectapicturefirst.text"));
  374. }
  375. }
  376. /// <summary>
  377. /// 执行读取参数的事件
  378. /// </summary>
  379. /// <param name="sender"></param>
  380. /// <param name="e"></param>
  381. private void bcBinaryGetParamAction(object sender, EventArgs e)
  382. {
  383. if (bc == null && bmc == null)
  384. return;
  385. this.binaryExtractionModel.ColorInterval = (bmc != null ? bmc.getInverseStyle() : bc.getInverseStyle());//###
  386. this.binaryExtractionModel.BinaryStyle = (bmc != null ? bmc.BinaryStyle : bc.BinaryStyle);
  387. this.binaryExtractionModel.ColorOneStart = (int)(bmc != null ? bmc.scope1Start : bc.scope1Start);
  388. this.binaryExtractionModel.ColorOneEnd = (int)(bmc != null ? bmc.scope1End : bc.scope1End);
  389. this.binaryExtractionModel.ColorTwoStart = (int)(bmc != null ? bmc.scope2Start : bc.scope2Start);
  390. this.binaryExtractionModel.ColorTwoEnd = (int)(bmc != null ? bmc.scope2End : bc.scope2End);
  391. this.binaryExtractionModel.ColorThreeStart = (int)(bmc != null ? bmc.scope3Start : bc.scope3Start);
  392. this.binaryExtractionModel.ColorThreeEnd = (int)(bmc != null ? bmc.scope3End : bc.scope3End);
  393. this.initParamsToAction();
  394. }
  395. /// <summary>
  396. /// 获取相的工作结构
  397. /// </summary>
  398. /// <param name="index">指明获取第几个相的工作结构</param>
  399. /// <returns></returns>
  400. public List<PhaseModel> getPhaseModels(int index)
  401. {
  402. return this.documentItems[index].phaseModels;
  403. }
  404. /// <summary>
  405. /// 应用到全部
  406. /// </summary>
  407. /// <param name="imagesKeys">所有要改变参数的图片tag</param>
  408. /// <param name="currentImagesKey">当前操作的图片tag</param>
  409. public void applyToAll(System.Collections.Specialized.StringCollection imagesKeys, string currentImagesKey)
  410. {
  411. foreach (var imagesKey in imagesKeys)
  412. {
  413. if (imagesKey.Equals(currentImagesKey))
  414. continue;//#
  415. if (!binaryModelDict.ContainsKey(imagesKey))
  416. {
  417. binaryModelFlag.Add(imagesKey, true);
  418. binaryModelDict.Add(imagesKey, this.binaryExtractionModel.cloneModel());
  419. if (!binaryData1Dict.ContainsKey(imagesKey))
  420. {
  421. binaryData1Dict.Add(imagesKey, null);
  422. }
  423. else
  424. binaryData1Dict[imagesKey] = null;//###
  425. }
  426. else
  427. {
  428. binaryModelFlag[imagesKey] = true;
  429. binaryModelDict[imagesKey] = this.binaryExtractionModel.cloneModel();
  430. binaryData1Dict[imagesKey] = null;
  431. }
  432. }
  433. }
  434. /// <summary>
  435. /// 保存二值化图片
  436. /// </summary>
  437. /// <param name="imagesKey"></param>
  438. public void saveMat(string imagesKey)
  439. {
  440. if (binaryData1Dict.ContainsKey(imagesKey))
  441. {
  442. if (binaryData1Dict[imagesKey] != null)
  443. binaryData1Dict[imagesKey] = null;
  444. binaryData1Dict[imagesKey] = this.documentWorkspace.phaseModels[0].mat;
  445. }
  446. else
  447. {
  448. try
  449. {
  450. binaryData1Dict.Add(imagesKey, this.documentWorkspace.phaseModels[0].mat);
  451. }
  452. catch (Exception)
  453. {
  454. binaryData1Dict.Add(imagesKey, null);
  455. }
  456. }
  457. }
  458. /// <summary>
  459. /// ListView图片选择改变事件,不显示视场
  460. /// </summary>
  461. /// <param name="imagesKey">选择改变后的图片tag</param>
  462. /// <param name="imageMat">选择改变后的图片</param>
  463. public void listView1_SelectedIndexChangedHideDrawClassView(Mat imageMat, string imagesKey = null)
  464. {
  465. if (imagesKey != null)
  466. {
  467. if (!binaryModelDict.ContainsKey(imagesKey))
  468. {
  469. binaryModelFlag.Add(imagesKey, false);
  470. this.binaryExtractionModel = this.binaryExtractionModel.cloneModel();
  471. binaryModelDict.Add(imagesKey, this.binaryExtractionModel);
  472. }
  473. //else
  474. {
  475. this.binaryExtractionModel = binaryModelDict[imagesKey];
  476. this.initParamsToAction();
  477. this.InitParameterToControl();
  478. }
  479. }
  480. Document document = Document.FromImageMat(imageMat/* //待测试.Clone()*/);
  481. this.documentWorkspace.Document = document;
  482. this.documentWorkspace.Visible = true;
  483. GraphicsList itemGraphicsList = this.appWorkspace.DocumentWorkspaces[this.listView1.FocusedItem != null ? this.listView1.FocusedItem.Index : this.listView1.SelectedItems[0].Index].GraphicsList;
  484. GraphicsList graphicsList = new GraphicsList();
  485. for (int j = 0; j < itemGraphicsList.Count; j++)
  486. {
  487. if (itemGraphicsList[j].objectType != Annotation.Enum.DrawClass.View)
  488. graphicsList.Add(itemGraphicsList[j]);
  489. }
  490. this.documentWorkspace.GraphicsList = graphicsList;
  491. this.documentWorkspace.PhaseModels = new List<PhaseModel>();
  492. this.documentWorkspace.PhaseModels.AddRange(this.getPhaseModels(this.listView1.FocusedItem != null ? this.listView1.FocusedItem.Index : this.listView1.SelectedItems[0].Index));
  493. if ((imagesKey == null || !binaryModelFlag.ContainsKey(imagesKey) || !binaryModelFlag[imagesKey]/*用于更新图片二值化效果*/) && (bc != null || bmc != null) && this.documentWorkspace.PhaseModels[0].mat != null)
  494. {
  495. if (!(bmc != null ? bmc.BinaryChecked : bc.BinaryChecked) && this.documentWorkspace.PhaseModels[0].choise
  496. /*&& (this.binaryExtractionModel.BinaryCheckFlag > 1)*/)
  497. {
  498. this.toApplyBinary = false;
  499. if (bmc != null)
  500. bmc.BinaryChecked = true;
  501. else
  502. bc.BinaryChecked = true;
  503. }
  504. }
  505. else
  506. {
  507. if (binaryData1Dict.ContainsKey(imagesKey) && binaryData1Dict[imagesKey] != null)
  508. {
  509. documentWorkspace.PhaseModels[0].mat = binaryData1Dict[imagesKey];// this.PerformProcess(
  510. //PaintDotNet.Camera.Tools.ToMat(documentWorkspace.GetFullSizeWithRegion()),
  511. //PaintDotNet.Camera.Tools.ToMat(bitmap));
  512. //documentWorkspace.PhaseModels[0].color = (bmc != null ? bmc.BinaryBackColor.ToArgb() : bc.BinaryBackColor.ToArgb());
  513. documentWorkspace.Refresh();
  514. }
  515. else
  516. this.applyButtonImpl();
  517. }
  518. if (imagesKey != null && binaryModelFlag.ContainsKey(imagesKey)/*用于更新图片二值化效果*/)
  519. {
  520. this.documentWorkspace.PhaseModels[0].choise = (this.binaryExtractionModel.BinaryCheckFlag == 2)/*(bmc != null ? bmc.BinaryChecked : bc.BinaryChecked)*/;
  521. binaryModelFlag[imagesKey] = false;
  522. if (bmc != null)
  523. bmc.BinaryChecked = this.documentWorkspace.PhaseModels[0].choise || (this.binaryExtractionModel.BinaryCheckFlag > 1);
  524. else
  525. bc.BinaryChecked = this.documentWorkspace.PhaseModels[0].choise || (this.binaryExtractionModel.BinaryCheckFlag > 1);
  526. }
  527. else if ((bc != null || bmc != null) && !this.documentWorkspace.PhaseModels[0].choise
  528. && (this.binaryExtractionModel.BinaryCheckFlag <= 1))
  529. if (bmc != null)
  530. bmc.BinaryChecked = false;
  531. else
  532. bc.BinaryChecked = false;
  533. if (imagesKey != null)
  534. {
  535. if (this.binaryExtractionModel.BinaryCheckFlag % 2 == 1)
  536. {
  537. if (bmc != null)
  538. bmc.OriginChecked = true;
  539. else
  540. bc.OriginChecked = true;
  541. }
  542. else
  543. {
  544. if (bmc != null)
  545. bmc.OriginChecked = false;
  546. else
  547. bc.OriginChecked = false;
  548. }
  549. }
  550. //显示直方图
  551. if (bc != null || bmc != null)
  552. if (bmc != null)
  553. bmc.CreateHistogram(imageMat, true, 339, 130, 0);
  554. else
  555. bc.CreateHistogram(imageMat, true, 339, 130, 0);
  556. this.RefreshHistogramControl1Values();//##
  557. }
  558. /// <summary>
  559. /// ListView图片选择改变事件
  560. /// </summary>
  561. /// <param name="imageMat">选择改变后的图片</param>
  562. public void listView1_SelectedIndexChanged(Mat imageMat, string imagesKey = null)
  563. {
  564. if (imagesKey != null)
  565. {
  566. if (!binaryModelDict.ContainsKey(imagesKey))
  567. {
  568. binaryModelFlag.Add(imagesKey, false);
  569. this.binaryExtractionModel = this.binaryExtractionModel.cloneModel();
  570. binaryModelDict.Add(imagesKey, this.binaryExtractionModel);
  571. }
  572. //else
  573. {
  574. this.binaryExtractionModel = binaryModelDict[imagesKey];
  575. this.initParamsToAction();
  576. this.InitParameterToControl();
  577. }
  578. }
  579. Document document = Document.FromImageMat(imageMat/* //待测试.Clone()*/);
  580. this.documentWorkspace.Document = document;
  581. this.documentWorkspace.Visible = true;
  582. this.documentWorkspace.GraphicsList = this.appWorkspace.DocumentWorkspaces[this.listView1.FocusedItem != null ? this.listView1.FocusedItem.Index : this.listView1.SelectedItems[0].Index].GraphicsList;
  583. this.documentWorkspace.PhaseModels = new List<PhaseModel>();
  584. this.documentWorkspace.PhaseModels.AddRange(this.getPhaseModels(this.listView1.FocusedItem != null ? this.listView1.FocusedItem.Index : this.listView1.SelectedItems[0].Index));
  585. if (((imagesKey == null || !binaryModelFlag.ContainsKey(imagesKey) || !binaryModelFlag[imagesKey]/*用于更新图片二值化效果*/) && (bc != null || bmc != null)) && this.documentWorkspace.PhaseModels[0].mat != null)
  586. {
  587. if (!(bmc != null ? bmc.BinaryChecked : bc.BinaryChecked) && this.documentWorkspace.PhaseModels[0].choise)
  588. {
  589. this.toApplyBinary = false;
  590. if (bmc != null)
  591. bmc.BinaryChecked = true;
  592. else
  593. bc.BinaryChecked = true;
  594. }
  595. }
  596. else
  597. {
  598. this.applyButtonImpl();
  599. }
  600. if (imagesKey != null && binaryModelFlag.ContainsKey(imagesKey)/*用于更新图片二值化效果*/)
  601. {
  602. this.documentWorkspace.PhaseModels[0].choise = (this.binaryExtractionModel.BinaryCheckFlag == 2);// (bmc != null ? bmc.BinaryChecked : bc.BinaryChecked);
  603. binaryModelFlag[imagesKey] = false;
  604. if (bmc != null)
  605. bmc.BinaryChecked = this.documentWorkspace.PhaseModels[0].choise || (this.binaryExtractionModel.BinaryCheckFlag > 1);
  606. else
  607. bc.BinaryChecked = this.documentWorkspace.PhaseModels[0].choise || (this.binaryExtractionModel.BinaryCheckFlag > 1);
  608. }
  609. else if ((bc != null || bmc != null) && !this.documentWorkspace.PhaseModels[0].choise
  610. && (this.binaryExtractionModel.BinaryCheckFlag <= 1))
  611. if (bmc != null)
  612. bmc.BinaryChecked = false;
  613. else
  614. bc.BinaryChecked = false;
  615. if (imagesKey != null)
  616. {
  617. if (this.binaryExtractionModel.BinaryCheckFlag % 2 == 1)
  618. {
  619. if (bmc != null)
  620. bmc.OriginChecked = true;
  621. else
  622. bc.OriginChecked = true;
  623. }
  624. else
  625. {
  626. if (bmc != null)
  627. bmc.OriginChecked = false;
  628. else
  629. bc.OriginChecked = false;
  630. }
  631. }
  632. //显示直方图
  633. if (bc != null || bmc != null)
  634. if (bmc != null)
  635. bmc.CreateHistogram(imageMat, true, 339, 130, 0);
  636. else
  637. bc.CreateHistogram(imageMat, true, 339, 130, 0);
  638. this.RefreshHistogramControl1Values();
  639. }
  640. /// <summary>
  641. /// ListView图片选择改变事件
  642. /// </summary>
  643. /// <param name="imageMat">选择改变后的图片</param>
  644. public void listView1_SelectedIndexChangedNoCheckedChanged(Mat imageMat, string imagesKey = null)
  645. {
  646. if (imagesKey != null)
  647. {
  648. if (!binaryModelDict.ContainsKey(imagesKey))
  649. {
  650. binaryModelFlag.Add(imagesKey, false);
  651. this.binaryExtractionModel = this.binaryExtractionModel.cloneModel();
  652. binaryModelDict.Add(imagesKey, this.binaryExtractionModel);
  653. }
  654. //else
  655. {
  656. this.binaryExtractionModel = binaryModelDict[imagesKey];
  657. this.initParamsToAction();
  658. this.InitParameterToControl();
  659. }
  660. }
  661. Document document = Document.FromImageMat(imageMat/* //待测试.Clone()*/);
  662. this.documentWorkspace.Document = document;
  663. this.documentWorkspace.Visible = true;
  664. this.documentWorkspace.GraphicsList = this.appWorkspace.DocumentWorkspaces[this.listView1.FocusedItem != null ? this.listView1.FocusedItem.Index : this.listView1.SelectedItems[0].Index].GraphicsList;
  665. this.documentWorkspace.PhaseModels = new List<PhaseModel>();
  666. this.documentWorkspace.PhaseModels.AddRange(this.getPhaseModels(this.listView1.FocusedItem != null ? this.listView1.FocusedItem.Index : this.listView1.SelectedItems[0].Index));
  667. if ((imagesKey == null || !binaryModelFlag.ContainsKey(imagesKey) || !binaryModelFlag[imagesKey]/*用于更新图片二值化效果*/) && (bc != null || bmc != null) && this.documentWorkspace.PhaseModels[0].mat != null)
  668. {
  669. if (!(bmc != null ? bmc.BinaryChecked : bc.BinaryChecked) && this.documentWorkspace.PhaseModels[0].choise)//to sure and test
  670. {
  671. this.toApplyBinary = false;
  672. if (bmc != null)
  673. bmc.BinaryChecked = true;
  674. else
  675. bc.BinaryChecked = true;
  676. }
  677. }
  678. else
  679. {
  680. this.applyButtonImpl();
  681. }
  682. if (imagesKey != null && binaryModelFlag.ContainsKey(imagesKey)/*用于更新图片二值化效果*/)
  683. {
  684. this.documentWorkspace.PhaseModels[0].choise = (this.binaryExtractionModel.BinaryCheckFlag == 2);// (bmc != null ? bmc.BinaryChecked : bc.BinaryChecked);
  685. binaryModelFlag[imagesKey] = false;
  686. if (bmc != null)
  687. bmc.BinaryChecked = this.documentWorkspace.PhaseModels[0].choise || (this.binaryExtractionModel.BinaryCheckFlag > 1);
  688. else
  689. bc.BinaryChecked = this.documentWorkspace.PhaseModels[0].choise || (this.binaryExtractionModel.BinaryCheckFlag > 1);
  690. }
  691. //else
  692. ////to sure and test
  693. //if (bc != null && !this.documentWorkspace.PhaseModels[0].choise)
  694. // bc.BinaryChecked = false;
  695. //显示直方图
  696. if (bc != null || bmc != null)
  697. if (bmc != null)
  698. bmc.CreateHistogram(imageMat, true, 339, 130, 0);
  699. else
  700. bc.CreateHistogram(imageMat, true, 339, 130, 0);
  701. this.RefreshHistogramControl1Values();
  702. }
  703. /// <summary>
  704. /// 构造相的工作结构
  705. /// </summary>
  706. /// <param name="phaseNames">相的命名,如果只有二值则只传PdnResources.GetString("Menu.BinaryAction.BinaryExtraction.Text")即可,没有特殊情况命名第一个相为PdnResources.GetString("Menu.BinaryAction.BinaryExtraction.Text")</param>
  707. /// <param name="bmc"></param>
  708. /// <param name="appWorkspace"></param>
  709. /// <param name="documentWorkspace"></param>
  710. public void createDocumentItemsSmaller(string[] phaseNames, CustomControl.BinaryControlSmaller bmc, AppWorkspace appWorkspace
  711. , DocumentWorkspaceWindow documentWorkspace, ListView listView1)
  712. {
  713. this.bmc = bmc;
  714. this.appWorkspace = appWorkspace;
  715. this.documentWorkspace = documentWorkspace;
  716. this.listView1 = listView1;
  717. //如果是脚本执行,将参数带入(AppWorkspace.ScriptRunning && AppWorkspace.ScriptCurrentParam != null && AppWorkspace.ScriptCurrentParam.MenuId == action.MenuId);
  718. if (this.appWorkspace.ScriptRunning && this.appWorkspace.ScriptCurrentParam != null)
  719. {
  720. binaryExtractionModel = new BinaryExtractionModel();
  721. foreach (Args args in analysisAction.Lists)
  722. {
  723. Args param1 = this.appWorkspace.ScriptCurrentParam.Lists.Find(m => m.Key.Equals(args.Key));
  724. if (param1 != null && param1.value != null)
  725. args.Value = param1.value;
  726. if (args.Key == "colorInterval")
  727. {
  728. this.binaryExtractionModel.ColorInterval = (Boolean)args.Value ? 2 : 1;
  729. }
  730. else if (args.Key == "scope1")
  731. {
  732. List<double> twoL = (List<double>)args.Value;
  733. this.binaryExtractionModel.ColorOneStart = (int)twoL[0];
  734. this.binaryExtractionModel.ColorOneEnd = (int)twoL[1];
  735. }
  736. else if (args.Key == "scope2")
  737. {
  738. List<double> twoL = (List<double>)args.Value;
  739. this.binaryExtractionModel.ColorTwoStart = (int)twoL[0];
  740. this.binaryExtractionModel.ColorTwoEnd = (int)twoL[1];
  741. }
  742. else if (args.Key == "scope3")
  743. {
  744. List<double> twoL = (List<double>)args.Value;
  745. this.binaryExtractionModel.ColorThreeStart = (int)twoL[0];
  746. this.binaryExtractionModel.ColorThreeEnd = (int)twoL[1];
  747. }
  748. else if (args.Key == "scope4")
  749. {
  750. this.binaryExtractionModel.DebrisAreaStart = (int)((List<double>)args.Value)[0];
  751. this.binaryExtractionModel.DebrisAreaEnd = (int)((List<double>)args.Value)[1];
  752. }
  753. else if (args.Key == "binaryStyle")
  754. {
  755. this.binaryExtractionModel.BinaryStyle = (int)args.Value;
  756. }
  757. else if (args.Key == "phaseColor")
  758. {
  759. this.binaryExtractionModel.PhaseColor = (int)args.Value;
  760. }
  761. else if (args.Key == "binarization")
  762. {
  763. this.binaryExtractionModel.BinaryCheckFlag = Convert.ToBoolean(args.Value) == true ? 2 : 0;
  764. }
  765. else if (args.Key == "theoriginalcolor")
  766. {
  767. bool thisValue = Convert.ToBoolean(args.Value);
  768. if (thisValue)
  769. {
  770. this.binaryExtractionModel.BinaryCheckFlag = this.binaryExtractionModel.BinaryCheckFlag == 2 ? 3 : 1;
  771. }
  772. }
  773. }
  774. //string xmlFilePath = Application.StartupPath + "\\Config\\" + Startup.instance.SettingPrefix + "\\BinaryConfig\\Default_" + this.menuId + ".xml";
  775. //bool createNewFile = !System.IO.File.Exists(xmlFilePath);
  776. //if (createNewFile)
  777. // xmlFilePath = Application.StartupPath + "\\Config\\" + Startup.instance.SettingPrefix + "\\BinaryExtraction\\Default.xml";
  778. //this.binaryExtractionModel = XmlSerializeHelper.DESerializer<BinaryExtractionModel>(FileOperationHelper.ReadStringFromFile(xmlFilePath, FileMode.Open));
  779. //if (createNewFile)
  780. // this.binaryExtractionModel.BinaryCheckFlag = 0;
  781. this.initParamsToAction();
  782. }
  783. else
  784. this.initParams();
  785. for (int i = 0; i < phaseNames.Length; i++)
  786. {
  787. PhaseModel model = new PhaseModel();
  788. model.choise = true;
  789. model.mat = null;
  790. model.color = (i == 0 && bmc != null) ? bmc.BinaryBackColor.ToArgb() : Color.Green.ToArgb();//###
  791. model.position = documentWorkspace.PhaseModels.Count + 1;
  792. model.name = phaseNames[i];
  793. documentWorkspace.PhaseModels.Add(model);
  794. }
  795. this.documentItems = new Dictionary<int, DocumentItem>();
  796. // 构造工作结构
  797. for (int i = 0; i < appWorkspace.DocumentWorkspaces.Length; i++)
  798. {
  799. GraphicsList graphicsList = new GraphicsList();
  800. for (int j = 0; j < appWorkspace.DocumentWorkspaces[i].GraphicsList.Count; j++)
  801. {
  802. graphicsList.Add(appWorkspace.DocumentWorkspaces[i].GraphicsList[j]);
  803. }
  804. List<PhaseModel> phaseModels = new List<PhaseModel>();
  805. for (int j = 0; j < phaseNames.Length; j++)
  806. {
  807. //二值化相关
  808. if (j == 0)
  809. {
  810. PhaseModel analysisModel = appWorkspace.DocumentWorkspaces[i].AnalysisPhaseModel;
  811. if (analysisModel == null)
  812. {
  813. PhaseModel model = new PhaseModel();
  814. model.choise = true;
  815. model.mat = null;
  816. model.color = bmc != null ? bmc.BinaryBackColor.ToArgb() : Color.Green.ToArgb();
  817. model.position = phaseModels.Count + 1;
  818. model.name = phaseNames[j];
  819. phaseModels.Add(model);
  820. }
  821. else
  822. phaseModels.Add(analysisModel);
  823. }
  824. else
  825. {
  826. PhaseModel model = new PhaseModel();
  827. model.choise = true;
  828. model.mat = null;
  829. model.color = Color.Green/*panel2.BackColor*/.ToArgb();//###
  830. model.position = phaseModels.Count + 1;
  831. model.name = phaseNames[j];
  832. phaseModels.Add(model);
  833. }
  834. }
  835. this.documentItems.Add(i, new DocumentItem(graphicsList, phaseModels));
  836. }
  837. //
  838. //调色板
  839. //
  840. this.colorsForm1 = new ColorsForm();
  841. this.colorsForm1.StartPosition = FormStartPosition.CenterScreen;
  842. this.colorsForm1.setSaveBtn_Click(new System.EventHandler(this.colorsForm1Changed));
  843. this.InitParameterToControl();//###
  844. if (this.binaryExtractionModel != null)
  845. this.applyButtonImpl();
  846. this.InitBinaryControlEvent();
  847. }
  848. /// <summary>
  849. /// 构造相的工作结构
  850. /// </summary>
  851. /// <param name="phaseNames">相的命名,如果只有二值则只传(二值提取)即可,没有特殊情况命名第一个相为(二值提取)</param>
  852. /// <param name="bc"></param>
  853. /// <param name="appWorkspace"></param>
  854. /// <param name="documentWorkspace"></param>
  855. public void createDocumentItems(string[] phaseNames, CustomControl.BinaryControl bc, AppWorkspace appWorkspace
  856. , DocumentWorkspaceWindow documentWorkspace, ListView listView1)
  857. {
  858. this.bc = bc;
  859. this.appWorkspace = appWorkspace;
  860. this.documentWorkspace = documentWorkspace;
  861. this.listView1 = listView1;
  862. //如果是脚本执行,将参数带入(AppWorkspace.ScriptRunning && AppWorkspace.ScriptCurrentParam != null && AppWorkspace.ScriptCurrentParam.MenuId == action.MenuId);
  863. if (this.appWorkspace.ScriptRunning && this.appWorkspace.ScriptCurrentParam != null)
  864. {
  865. binaryExtractionModel = new BinaryExtractionModel();
  866. foreach (Args args in analysisAction.Lists)
  867. {
  868. Args param1 = this.appWorkspace.ScriptCurrentParam.Lists.Find(m => m.Key.Equals(args.Key));
  869. if (param1 != null && param1.value != null)
  870. args.Value = param1.value;
  871. if (args.Key == "colorInterval")
  872. {
  873. this.binaryExtractionModel.ColorInterval = (Boolean)args.Value ? 2 : 1;
  874. }
  875. else if (args.Key == "scope1")
  876. {
  877. List<double> twoL = (List<double>)args.Value;
  878. this.binaryExtractionModel.ColorOneStart = (int)twoL[0];
  879. this.binaryExtractionModel.ColorOneEnd = (int)twoL[1];
  880. }
  881. else if (args.Key == "scope2")
  882. {
  883. List<double> twoL = (List<double>)args.Value;
  884. this.binaryExtractionModel.ColorTwoStart = (int)twoL[0];
  885. this.binaryExtractionModel.ColorTwoEnd = (int)twoL[1];
  886. }
  887. else if (args.Key == "scope3")
  888. {
  889. List<double> twoL = (List<double>)args.Value;
  890. this.binaryExtractionModel.ColorThreeStart = (int)twoL[0];
  891. this.binaryExtractionModel.ColorThreeEnd = (int)twoL[1];
  892. }
  893. else if (args.Key == "scope4")
  894. {
  895. this.binaryExtractionModel.DebrisAreaStart = (int)((List<double>)args.Value)[0];
  896. this.binaryExtractionModel.DebrisAreaEnd = (int)((List<double>)args.Value)[1];
  897. }
  898. else if (args.Key == "binaryStyle")
  899. {
  900. this.binaryExtractionModel.BinaryStyle = (int)args.Value;
  901. }
  902. else if (args.Key == "phaseColor")
  903. {
  904. this.binaryExtractionModel.PhaseColor = (int)args.Value;
  905. }
  906. else if (args.Key == "binarization")
  907. {
  908. this.binaryExtractionModel.BinaryCheckFlag = Convert.ToBoolean(args.Value) == true ? 2 : 0;
  909. }
  910. else if (args.Key == "theoriginalcolor")
  911. {
  912. bool thisValue = Convert.ToBoolean(args.Value);
  913. if (thisValue)
  914. {
  915. this.binaryExtractionModel.BinaryCheckFlag = this.binaryExtractionModel.BinaryCheckFlag == 2 ? 3 : 1;
  916. }
  917. }
  918. }
  919. //string xmlFilePath = Application.StartupPath + "\\Config\\" + Startup.instance.SettingPrefix + "\\BinaryConfig\\Default_" + this.menuId + ".xml";
  920. //bool createNewFile = !System.IO.File.Exists(xmlFilePath);
  921. //if (createNewFile)
  922. // xmlFilePath = Application.StartupPath + "\\Config\\" + Startup.instance.SettingPrefix + "\\BinaryExtraction\\Default.xml";
  923. //this.binaryExtractionModel = XmlSerializeHelper.DESerializer<BinaryExtractionModel>(FileOperationHelper.ReadStringFromFile(xmlFilePath, FileMode.Open));
  924. //if (createNewFile)
  925. // this.binaryExtractionModel.BinaryCheckFlag = 0;
  926. this.initParamsToAction();
  927. }
  928. else
  929. this.initParams();
  930. for (int i = 0; i < phaseNames.Length; i++)
  931. {
  932. PhaseModel model = new PhaseModel();
  933. model.choise = true;
  934. model.mat = null;
  935. model.color = (i == 0 && bc != null) ? bc.BinaryBackColor.ToArgb() : Color.Green.ToArgb();//###
  936. model.position = documentWorkspace.PhaseModels.Count + 1;
  937. model.name = phaseNames[i];
  938. documentWorkspace.PhaseModels.Add(model);
  939. }
  940. this.documentItems = new Dictionary<int, DocumentItem>();
  941. // 构造工作结构
  942. for (int i = 0; i < appWorkspace.DocumentWorkspaces.Length; i++)
  943. {
  944. GraphicsList graphicsList = new GraphicsList();
  945. for (int j = 0; j < appWorkspace.DocumentWorkspaces[i].GraphicsList.Count; j++)
  946. {
  947. graphicsList.Add(appWorkspace.DocumentWorkspaces[i].GraphicsList[j]);
  948. }
  949. List<PhaseModel> phaseModels = new List<PhaseModel>();
  950. for (int j = 0; j < phaseNames.Length; j++)
  951. {
  952. //二值化相关
  953. if (j == 0)
  954. {
  955. PhaseModel analysisModel = appWorkspace.DocumentWorkspaces[i].AnalysisPhaseModel;
  956. if (analysisModel == null)
  957. {
  958. PhaseModel model = new PhaseModel();
  959. model.choise = true;
  960. model.mat = null;
  961. model.color = bc != null ? bc.BinaryBackColor.ToArgb() : Color.Green.ToArgb();
  962. model.position = phaseModels.Count + 1;
  963. model.name = phaseNames[j];
  964. phaseModels.Add(model);
  965. }
  966. else
  967. phaseModels.Add(analysisModel);
  968. }
  969. else
  970. {
  971. PhaseModel model = new PhaseModel();
  972. model.choise = true;
  973. model.mat = null;
  974. model.color = Color.Green/*panel2.BackColor*/.ToArgb();//###
  975. model.position = phaseModels.Count + 1;
  976. model.name = phaseNames[j];
  977. phaseModels.Add(model);
  978. }
  979. }
  980. this.documentItems.Add(i, new DocumentItem(graphicsList, phaseModels));
  981. }
  982. //
  983. //调色板
  984. //
  985. this.colorsForm1 = new ColorsForm();
  986. this.colorsForm1.StartPosition = FormStartPosition.CenterScreen;
  987. this.colorsForm1.setSaveBtn_Click(new System.EventHandler(this.colorsForm1Changed));
  988. this.InitParameterToControl();//###
  989. if (this.binaryExtractionModel != null)
  990. this.applyButtonImpl();
  991. this.InitBinaryControlEvent();
  992. }
  993. /// <summary>
  994. /// 无视场执行运算
  995. /// </summary>
  996. public OpenCvSharp.Mat PerformProcess(OpenCvSharp.Mat src) { return action.PerformProcess(src); }
  997. /// <summary>
  998. /// 多视场执行运算
  999. /// </summary>
  1000. /// <param name="src">视场mat</param>
  1001. /// <param name="mat">原图mat</param>
  1002. /// <returns></returns>
  1003. public OpenCvSharp.Mat PerformProcess(OpenCvSharp.Mat src, OpenCvSharp.Mat mat) { return action.PerformProcess(src, mat); }
  1004. public int ColorInterval
  1005. {
  1006. get
  1007. {
  1008. return this.binaryExtractionModel.ColorInterval;
  1009. }
  1010. set
  1011. {
  1012. this.binaryExtractionModel.ColorInterval = value;
  1013. }
  1014. }
  1015. private void colorsForm1Changed(object sender, EventArgs e)
  1016. {
  1017. Color color = this.colorsForm1.UserPrimaryColor.ToColor();
  1018. this.SetBinaryBackColor(color);//改变参数配置的相颜色
  1019. this.colorsForm1.Close();
  1020. }
  1021. /// <summary>
  1022. /// 把参数的值设置到控件上
  1023. /// </summary>
  1024. public void InitParameterToControl()
  1025. {
  1026. if (this.binaryExtractionModel != null)
  1027. {
  1028. if (bc != null || bmc != null)
  1029. {
  1030. if (this.binaryExtractionModel.BinaryCheckFlag > 1)
  1031. {
  1032. if (bmc != null)
  1033. bmc.BinaryChecked = true;
  1034. else
  1035. bc.BinaryChecked = true;
  1036. }
  1037. if (this.binaryExtractionModel.BinaryCheckFlag % 2 == 1)
  1038. {
  1039. if (bmc != null)
  1040. bmc.OriginChecked = true;
  1041. else
  1042. bc.OriginChecked = true;
  1043. }
  1044. //阈值相关
  1045. //1个颜色区间还是2个
  1046. if (bmc != null)
  1047. bmc.setInverseStyle((this.binaryExtractionModel.ColorInterval == 1) ? 1 : 2);
  1048. else
  1049. bc.setInverseStyle((this.binaryExtractionModel.ColorInterval == 1) ? 1 : 2);
  1050. //删除事件
  1051. if (bmc != null)
  1052. bmc.DeleteEventHandler();
  1053. else
  1054. bc.DeleteEventHandler();
  1055. if (bmc != null)
  1056. bmc.scope1End = this.binaryExtractionModel.ColorOneEnd;
  1057. else
  1058. bc.scope1End = this.binaryExtractionModel.ColorOneEnd;
  1059. if (bmc != null)
  1060. bmc.scope1Start = this.binaryExtractionModel.ColorOneStart;
  1061. else
  1062. bc.scope1Start = this.binaryExtractionModel.ColorOneStart;
  1063. if (bmc != null)
  1064. bmc.scope2End = this.binaryExtractionModel.ColorTwoEnd;
  1065. else
  1066. bc.scope2End = this.binaryExtractionModel.ColorTwoEnd;
  1067. if (bmc != null)
  1068. bmc.scope2Start = this.binaryExtractionModel.ColorTwoStart;
  1069. else
  1070. bc.scope2Start = this.binaryExtractionModel.ColorTwoStart;
  1071. if (bmc != null)
  1072. bmc.scope3End = this.binaryExtractionModel.ColorThreeEnd;
  1073. else
  1074. bc.scope3End = this.binaryExtractionModel.ColorThreeEnd;
  1075. if (bmc != null)
  1076. bmc.scope3Start = this.binaryExtractionModel.ColorThreeStart;
  1077. else
  1078. bc.scope3Start = this.binaryExtractionModel.ColorThreeStart;
  1079. if (bmc != null)
  1080. bmc.BinaryStyle = this.binaryExtractionModel.BinaryStyle;
  1081. else
  1082. bc.BinaryStyle = this.binaryExtractionModel.BinaryStyle;
  1083. if (bmc != null)
  1084. bmc.BinaryBackColor = Color.FromArgb(this.binaryExtractionModel.PhaseColor);
  1085. else
  1086. bc.BinaryBackColor = Color.FromArgb(this.binaryExtractionModel.PhaseColor);
  1087. //添加事件
  1088. if (bmc != null)
  1089. bmc.AddEventHandler();
  1090. else
  1091. bc.AddEventHandler();
  1092. if (bmc != null)
  1093. bmc.InitParameterToControl();
  1094. else
  1095. bc.InitParameterToControl();
  1096. }
  1097. }
  1098. }
  1099. /// <summary>
  1100. /// 更新显示直方图的数据
  1101. /// </summary>
  1102. public void RefreshHistogramControl1Values()
  1103. {
  1104. ////显示直方图
  1105. //if (bc != null || bmc != null)
  1106. // if (bmc != null)
  1107. // bmc.CreateHistogram(bitmap, true, 339, 130, 0);
  1108. // else
  1109. // bc.CreateHistogram(bitmap, true, 339, 130, 0);
  1110. if (bmc != null)
  1111. bmc.InitParameterToControl(this.binaryExtractionModel.ColorInterval);
  1112. else
  1113. bc.InitParameterToControl(this.binaryExtractionModel.ColorInterval);
  1114. }
  1115. Vec4b vecForPhaseColor4B;
  1116. public unsafe void SetBinaryBackColor(Color color)
  1117. {
  1118. if (bmc != null)
  1119. bmc.BinaryBackColor = color;//设置panel背景
  1120. else
  1121. bc.BinaryBackColor = color;//设置panel背景
  1122. this.binaryExtractionModel.PhaseColor = color.ToArgb();
  1123. //###
  1124. foreach (Args args in action.Lists)
  1125. {
  1126. if (args.Key == "phaseColor")
  1127. {
  1128. args.Value = color.ToArgb();//this.binaryExtractionModel.PhaseColor
  1129. break;
  1130. }
  1131. }
  1132. if (this.documentWorkspace.PhaseModels[0].mat == null || this.documentWorkspace.PhaseModels[0].mat.IsDisposed)
  1133. {
  1134. this.applyButtonImpl();
  1135. }
  1136. else
  1137. {
  1138. Mat source = this.documentWorkspace.PhaseModels[0].mat;
  1139. //##20143
  1140. if (source.Channels() != 4)
  1141. {
  1142. this.applyButtonImpl();// false;、、should never go here !
  1143. return;
  1144. }
  1145. //二值提取
  1146. if (dstBinary != null)
  1147. {
  1148. dstBinary.Dispose();
  1149. dstBinary = null;
  1150. }
  1151. dstBinary = new Mat(source.Rows, source.Cols, MatType.CV_8UC4, new Scalar(0, 0, 0, 0));
  1152. //赋值二值相的颜色
  1153. vecForPhaseColor4B = new Vec4b(color.B, color.G, color.R, color.A);
  1154. source.ForEachAsVec4b(ForeachFunctionByteForPhaseColor4B);
  1155. this.documentWorkspace.PhaseModels[0].mat/*Mat dst*/ = dstBinary.Clone();
  1156. this.documentWorkspace.Refresh();
  1157. dstBinary.Dispose();
  1158. dstBinary = null;
  1159. }
  1160. //return dst;// true;
  1161. }
  1162. /// <summary>
  1163. /// 提取二值化的透明通道并赋值
  1164. /// </summary>
  1165. /// <param name="t"></param>
  1166. /// <param name="position"></param>
  1167. public unsafe void ForeachFunctionByteForPhaseColor4B(Vec4b* t, int* position)
  1168. {
  1169. if (t->Item3 > 0)
  1170. dstBinary.Set(position[0], position[1], vecForPhaseColor4B);
  1171. }
  1172. /// <summary>
  1173. /// 保证二值化颜色赋值到action
  1174. /// </summary>
  1175. public void loadParams()
  1176. {
  1177. if (bc == null && bmc == null)
  1178. return;
  1179. foreach (Args args in action.Lists)
  1180. {
  1181. switch (args.Key)
  1182. {
  1183. case "scope1"://不反选时候的范围
  1184. if (bmc != null)
  1185. bmc.ScopeValue1ChangedAction += new EventHandler(((DecimalScope)args).numberScope_ValueChanged);
  1186. else
  1187. bc.ScopeValue1ChangedAction += new EventHandler(((DecimalScope)args).numberScope_ValueChanged);
  1188. break;
  1189. case "scope2"://反选时候的范围1
  1190. if (bmc != null)
  1191. bmc.ScopeValue2ChangedAction += new EventHandler(((DecimalScope)args).numberScope_ValueChanged);
  1192. else
  1193. bc.ScopeValue2ChangedAction += new EventHandler(((DecimalScope)args).numberScope_ValueChanged);
  1194. break;
  1195. case "scope3"://反选时候的范围2
  1196. if (bmc != null)
  1197. bmc.ScopeValue3ChangedAction += new EventHandler(((DecimalScope)args).numberScope_ValueChanged);
  1198. else
  1199. bc.ScopeValue3ChangedAction += new EventHandler(((DecimalScope)args).numberScope_ValueChanged);
  1200. break;
  1201. case "phaseColor":
  1202. args.Value = this.binaryExtractionModel.PhaseColor;
  1203. break;
  1204. default:
  1205. break;
  1206. }
  1207. }
  1208. }
  1209. /// <summary>
  1210. /// 执行二值方法的事件
  1211. /// </summary>
  1212. /// <param name="sender"></param>
  1213. /// <param name="e"></param>
  1214. private void bcApplyButtonImplAction(object sender, EventArgs e)
  1215. {
  1216. this.applyButtonImpl(/*bitmap*/);
  1217. }
  1218. ///// <summary>
  1219. ///// 参数改变时,重新处理图像
  1220. ///// </summary>
  1221. /// <param name="imagesKey">不是当前显示图片的需要传值</param>
  1222. /// <param name="analysisPicture">不是当前显示图片的需要传值</param>
  1223. /// <param name="imageMat">不带视场处理的需要传值</param>
  1224. public void applyButtonImpl(string imagesKey = null, int analysisPicture = -1, Mat imageMat = null)
  1225. {
  1226. if (bc == null && bmc == null)
  1227. return;
  1228. //long start = Cv2.GetTickCount();
  1229. if (imageMat != null ||
  1230. (listView1.FocusedItem != null || this.listView1.SelectedItems != null && this.listView1.SelectedItems.Count > 0) && (bmc != null ? bmc.BinaryChecked : bc.BinaryChecked))
  1231. {
  1232. bool processWholeMat11 = (imageMat != null);
  1233. if (imageMat == null)
  1234. imageMat = this.appWorkspace.DocumentWorkspaces[this.listView1.FocusedItem != null ? this.listView1.FocusedItem.Index : this.listView1.SelectedItems[0].Index].CompositionSurface.CreatedAliasedMat();
  1235. //判断是否存在视场,如果存在视场,则把视场提取出来,进行处理//可能会带视场//########################
  1236. DocumentView documentWorkspace = this.documentWorkspace;
  1237. if (analysisPicture >= 0)
  1238. documentWorkspace = this.appWorkspace.DocumentWorkspaces[analysisPicture];
  1239. if (!processWholeMat11
  1240. && !processWholeMat && documentWorkspace.GraphicsList.IsExsitView())
  1241. {
  1242. if (analysisPicture == -1)
  1243. {
  1244. documentWorkspace.PhaseModels[0].mat = this.PerformProcess(
  1245. PaintDotNet.Camera.Tools.ToMat(documentWorkspace.GetFullSizeWithRegion()),
  1246. imageMat.Clone());
  1247. documentWorkspace.PhaseModels[0].color = (bmc != null ? bmc.BinaryBackColor.ToArgb() : bc.BinaryBackColor.ToArgb());
  1248. documentWorkspace.Refresh();
  1249. }
  1250. else
  1251. {
  1252. if (binaryData1Dict.ContainsKey(imagesKey))
  1253. {
  1254. if (binaryData1Dict[imagesKey] != null)
  1255. binaryData1Dict[imagesKey] = null;
  1256. binaryData1Dict[imagesKey] = this.PerformProcess(
  1257. PaintDotNet.Camera.Tools.ToMat(documentWorkspace.GetFullSizeWithRegion()),
  1258. imageMat.Clone());// this.documentWorkspace.phaseModels[0].mat;
  1259. }
  1260. else
  1261. {
  1262. try
  1263. {
  1264. binaryData1Dict.Add(imagesKey, this.PerformProcess(
  1265. PaintDotNet.Camera.Tools.ToMat(documentWorkspace.GetFullSizeWithRegion()),
  1266. imageMat.Clone())/*this.documentWorkspace.phaseModels[0].mat*/);
  1267. }
  1268. catch (Exception)
  1269. {
  1270. binaryData1Dict.Add(imagesKey, null);
  1271. }
  1272. }
  1273. }
  1274. }
  1275. else if (!processWholeMat11
  1276. && processWholeMat && documentWorkspace.GraphicsList.IsExsitView())
  1277. {
  1278. if (PhaseModels0Mat != null)
  1279. {
  1280. PhaseModels0Mat.Dispose();
  1281. PhaseModels0Mat = null;
  1282. }
  1283. Mat OrgImg0 = this.PerformProcess(imageMat.Clone());
  1284. PhaseModels0Mat = OrgImg0.Clone();
  1285. Vec4b vec4 = new Vec4b(0, 0, 0, 0);
  1286. //视场图像
  1287. Mat exsitViewMatOrg = PaintDotNet.Camera.Tools.ToMat(documentWorkspace.GetFullSizeWithRegion());
  1288. for (int y = 0; y < exsitViewMatOrg/*temp*/.Rows; y++)
  1289. {
  1290. for (int x = 0; x < exsitViewMatOrg/*temp*/.Cols; x++)
  1291. {
  1292. if (exsitViewMatOrg.At<Vec4b>(y, x).Item3 == 0)
  1293. OrgImg0.Set(y, x, vec4);//不在视场图像范围内
  1294. }
  1295. }
  1296. if (analysisPicture == -1)
  1297. {
  1298. documentWorkspace.PhaseModels[0].mat = OrgImg0;
  1299. documentWorkspace.PhaseModels[0].color = (bmc != null ? bmc.BinaryBackColor.ToArgb() : bc.BinaryBackColor.ToArgb());
  1300. documentWorkspace.Refresh();
  1301. }
  1302. else
  1303. {
  1304. if (binaryData1Dict.ContainsKey(imagesKey))
  1305. {
  1306. if (binaryData1Dict[imagesKey] != null)
  1307. binaryData1Dict[imagesKey] = null;
  1308. binaryData1Dict[imagesKey] = OrgImg0;// this.documentWorkspace.phaseModels[0].mat;
  1309. }
  1310. else
  1311. {
  1312. try
  1313. {
  1314. binaryData1Dict.Add(imagesKey, OrgImg0/*this.documentWorkspace.phaseModels[0].mat*/);
  1315. }
  1316. catch (Exception)
  1317. {
  1318. binaryData1Dict.Add(imagesKey, null);
  1319. }
  1320. }
  1321. }
  1322. }
  1323. else
  1324. {
  1325. if (analysisPicture == -1)
  1326. {
  1327. documentWorkspace.PhaseModels[0].mat = this.PerformProcess(imageMat.Clone());
  1328. documentWorkspace.PhaseModels[0].color = (bmc != null ? bmc.BinaryBackColor.ToArgb() : bc.BinaryBackColor.ToArgb());
  1329. documentWorkspace.Refresh();
  1330. }
  1331. else
  1332. {
  1333. if (binaryData1Dict.ContainsKey(imagesKey))
  1334. {
  1335. if (binaryData1Dict[imagesKey] != null)
  1336. binaryData1Dict[imagesKey] = null;
  1337. binaryData1Dict[imagesKey] = this.PerformProcess(imageMat.Clone());// this.documentWorkspace.phaseModels[0].mat;
  1338. }
  1339. else
  1340. {
  1341. try
  1342. {
  1343. binaryData1Dict.Add(imagesKey, this.PerformProcess(imageMat.Clone())/*this.documentWorkspace.phaseModels[0].mat*/);
  1344. }
  1345. catch (Exception)
  1346. {
  1347. binaryData1Dict.Add(imagesKey, null);
  1348. }
  1349. }
  1350. }
  1351. }
  1352. }
  1353. ////double time = (Cv2.GetTickCount() - start) / Cv2.GetTickFrequency();
  1354. ////System.Console.WriteLine("执行时间:" + time);
  1355. ////AddRectangleToRectangleFCaps();//0916###18745
  1356. //if (analysisPicture != -1)
  1357. OnBinaryImplFinishAction();
  1358. }
  1359. ///// <summary>
  1360. ///// (夹杂物专用)参数改变时,重新处理图像
  1361. ///// </summary>
  1362. /// <param name="imagesKey">不是当前显示图片的需要传值</param>
  1363. /// <param name="analysisPicture">不是当前显示图片的需要传值</param>
  1364. /// <param name="bitmap">不带视场处理的需要传值</param>
  1365. public void applyButtonImplMethodInclusion(string imagesKey = null, int analysisPicture = -1, Mat imageMat = null, int index = 0, int color = 0, List<double> valueList = null)
  1366. {
  1367. if (bc == null && bmc == null)
  1368. return;
  1369. if (imageMat != null ||
  1370. (listView1.FocusedItem != null || this.listView1.SelectedItems != null && this.listView1.SelectedItems.Count > 0))
  1371. {
  1372. bool processWholeMat11 = (imageMat != null);
  1373. if (imageMat == null)
  1374. imageMat = this.appWorkspace.DocumentWorkspaces[this.listView1.FocusedItem != null ? this.listView1.FocusedItem.Index : this.listView1.SelectedItems[0].Index].CompositionSurface.CreatedAliasedMat();
  1375. //判断是否存在视场,如果存在视场,则把视场提取出来,进行处理//可能会带视场//########################
  1376. DocumentView documentWorkspace = this.documentWorkspace;
  1377. if (analysisPicture >= 0)
  1378. documentWorkspace = this.appWorkspace.DocumentWorkspaces[analysisPicture];
  1379. if (!processWholeMat11
  1380. && !processWholeMat && documentWorkspace.GraphicsList.IsExsitView())
  1381. {
  1382. //if (analysisPicture == -1)
  1383. //{
  1384. //}
  1385. //else
  1386. //{
  1387. // if (binaryData1Dict.ContainsKey(imagesKey))
  1388. // {
  1389. // if (binaryData1Dict[imagesKey] != null)
  1390. // binaryData1Dict[imagesKey] = null;
  1391. // binaryData1Dict[imagesKey] = this.PerformProcess(
  1392. // PaintDotNet.Camera.Tools.ToMat(documentWorkspace.GetFullSizeWithRegion()),
  1393. // PaintDotNet.Camera.Tools.ToMat(bitmap));// this.documentWorkspace.phaseModels[0].mat;
  1394. // }
  1395. // else
  1396. // {
  1397. // try
  1398. // {
  1399. // binaryData1Dict.Add(imagesKey, this.PerformProcess(
  1400. // PaintDotNet.Camera.Tools.ToMat(documentWorkspace.GetFullSizeWithRegion()),
  1401. // PaintDotNet.Camera.Tools.ToMat(bitmap))/*this.documentWorkspace.phaseModels[0].mat*/);
  1402. // }
  1403. // catch (Exception)
  1404. // {
  1405. // binaryData1Dict.Add(imagesKey, null);
  1406. // }
  1407. // }
  1408. //}
  1409. this.binaryExtractionModel.PhaseColor = color;
  1410. //###
  1411. foreach (Args args in action.Lists)
  1412. {
  1413. if (args.Key == "phaseColor")
  1414. {
  1415. args.Value = color;//this.binaryExtractionModel.PhaseColor
  1416. }
  1417. if (args.Key == "colorInterval")
  1418. {
  1419. args.Value = 1;
  1420. }
  1421. else if (args.Key == "scope1")
  1422. {
  1423. ((List<double>)args.Value)[0] = valueList[0];
  1424. ((List<double>)args.Value)[1] = valueList[1];
  1425. }
  1426. //else if (args.Key == "scope2")
  1427. //{
  1428. // ((List<double>)args.Value)[0] = 0;
  1429. // ((List<double>)args.Value)[1] = 139;
  1430. //}
  1431. //else if (args.Key == "scope3")
  1432. //{
  1433. // ((List<double>)args.Value)[0] = 140;
  1434. // ((List<double>)args.Value)[1] = 255;
  1435. //}
  1436. }
  1437. documentWorkspace.PhaseModels[index].mat = this.PerformProcess(
  1438. PaintDotNet.Camera.Tools.ToMat(documentWorkspace.GetFullSizeWithRegion()),
  1439. imageMat.Clone());
  1440. documentWorkspace.PhaseModels[index].color = color;
  1441. documentWorkspace.Refresh();
  1442. }
  1443. else if (!processWholeMat11
  1444. && processWholeMat && documentWorkspace.GraphicsList.IsExsitView())
  1445. {
  1446. if (PhaseModels0Mat != null)
  1447. {
  1448. PhaseModels0Mat.Dispose();
  1449. PhaseModels0Mat = null;
  1450. }
  1451. Mat OrgImg0 = this.PerformProcess(imageMat.Clone());
  1452. PhaseModels0Mat = OrgImg0.Clone();
  1453. Vec4b vec4 = new Vec4b(0, 0, 0, 0);
  1454. //视场图像
  1455. Mat exsitViewMatOrg = PaintDotNet.Camera.Tools.ToMat(documentWorkspace.GetFullSizeWithRegion());
  1456. for (int y = 0; y < exsitViewMatOrg/*temp*/.Rows; y++)
  1457. {
  1458. for (int x = 0; x < exsitViewMatOrg/*temp*/.Cols; x++)
  1459. {
  1460. if (exsitViewMatOrg.At<Vec4b>(y, x).Item3 == 0)
  1461. OrgImg0.Set(y, x, vec4);//不在视场图像范围内
  1462. }
  1463. }
  1464. documentWorkspace.PhaseModels[index].mat = OrgImg0;
  1465. documentWorkspace.PhaseModels[index].color = color;
  1466. documentWorkspace.Refresh();
  1467. //if (analysisPicture == -1)
  1468. //{
  1469. //}
  1470. //else
  1471. //{
  1472. // if (binaryData1Dict.ContainsKey(imagesKey))
  1473. // {
  1474. // if (binaryData1Dict[imagesKey] != null)
  1475. // binaryData1Dict[imagesKey] = null;
  1476. // binaryData1Dict[imagesKey] = OrgImg0;// this.documentWorkspace.phaseModels[0].mat;
  1477. // }
  1478. // else
  1479. // {
  1480. // try
  1481. // {
  1482. // binaryData1Dict.Add(imagesKey, OrgImg0/*this.documentWorkspace.phaseModels[0].mat*/);
  1483. // }
  1484. // catch (Exception)
  1485. // {
  1486. // binaryData1Dict.Add(imagesKey, null);
  1487. // }
  1488. // }
  1489. //}
  1490. }
  1491. else
  1492. {
  1493. documentWorkspace.PhaseModels[0].mat = this.PerformProcess(imageMat.Clone());
  1494. documentWorkspace.PhaseModels[0].color = color;
  1495. documentWorkspace.Refresh();
  1496. //if (analysisPicture == -1)
  1497. //{
  1498. //}
  1499. //else
  1500. //{
  1501. // if (binaryData1Dict.ContainsKey(imagesKey))
  1502. // {
  1503. // if (binaryData1Dict[imagesKey] != null)
  1504. // binaryData1Dict[imagesKey] = null;
  1505. // binaryData1Dict[imagesKey] = this.PerformProcess(PaintDotNet.Camera.Tools.ToMat(bitmap));// this.documentWorkspace.phaseModels[0].mat;
  1506. // }
  1507. // else
  1508. // {
  1509. // try
  1510. // {
  1511. // binaryData1Dict.Add(imagesKey, this.PerformProcess(PaintDotNet.Camera.Tools.ToMat(bitmap))/*this.documentWorkspace.phaseModels[0].mat*/);
  1512. // }
  1513. // catch (Exception)
  1514. // {
  1515. // binaryData1Dict.Add(imagesKey, null);
  1516. // }
  1517. // }
  1518. //}
  1519. }
  1520. }
  1521. ////double time = (Cv2.GetTickCount() - start) / Cv2.GetTickFrequency();
  1522. ////System.Console.WriteLine("执行时间:" + time);
  1523. ////AddRectangleToRectangleFCaps();//0916###18745
  1524. //if (analysisPicture != -1)
  1525. OnBinaryImplFinishAction();
  1526. }
  1527. /// <summary>
  1528. /// 初始化二值化相关的变量
  1529. /// </summary>
  1530. public void initParams()
  1531. {
  1532. string xmlFilePath = Application.StartupPath + "\\Config\\" + Startup.instance.SettingPrefix + "\\BinaryConfig\\Default_" + this.menuId + ".xml";
  1533. bool createNewFile = !System.IO.File.Exists(xmlFilePath);
  1534. if (createNewFile)
  1535. xmlFilePath = Application.StartupPath + "\\Config\\" + Startup.instance.SettingPrefix + "\\BinaryExtraction\\Default.xml";
  1536. this.binaryExtractionModel = XmlSerializeHelper.DESerializer<BinaryExtractionModel>(FileOperationHelper.ReadStringFromFile(xmlFilePath, FileMode.Open));
  1537. if (createNewFile)
  1538. this.binaryExtractionModel.BinaryCheckFlag = 0;
  1539. this.initParamsToAction();
  1540. }
  1541. /// <summary>
  1542. /// 把参数的值设置到action
  1543. /// </summary>
  1544. public void initParamsToAction()
  1545. {
  1546. foreach (Args args in action.Lists)
  1547. {
  1548. if (args.Key == "colorInterval")
  1549. args.Value = this.binaryExtractionModel.ColorInterval;
  1550. else if (args.Key == "binaryStyle")
  1551. args.Value = this.binaryExtractionModel.BinaryStyle;
  1552. else if (args.Key == "scope1")
  1553. {
  1554. ((List<double>)args.Value)[0] = (int)(this.binaryExtractionModel.ColorOneStart);
  1555. ((List<double>)args.Value)[1] = (int)(this.binaryExtractionModel.ColorOneEnd);
  1556. }
  1557. else if (args.Key == "scope2")
  1558. {
  1559. ((List<double>)args.Value)[0] = (int)(this.binaryExtractionModel.ColorTwoStart);
  1560. ((List<double>)args.Value)[1] = (int)(this.binaryExtractionModel.ColorTwoEnd);
  1561. }
  1562. else if (args.Key == "scope3")
  1563. {
  1564. ((List<double>)args.Value)[0] = (int)(this.binaryExtractionModel.ColorThreeStart);
  1565. ((List<double>)args.Value)[1] = (int)(this.binaryExtractionModel.ColorThreeEnd);
  1566. }
  1567. else if (args.Key == "phaseColor")//#22092
  1568. {
  1569. args.Value = this.binaryExtractionModel.PhaseColor;
  1570. }
  1571. }
  1572. }
  1573. /// <summary>
  1574. /// 保存二值化相关的变量
  1575. /// </summary>
  1576. public void saveParams()
  1577. {
  1578. string xmlFilePath = Application.StartupPath + "\\Config\\" + Startup.instance.SettingPrefix + "\\BinaryConfig\\Default_" + this.menuId + ".xml";
  1579. string userInfoXml = XmlSerializeHelper.XmlSerialize<BinaryExtractionModel>(this.binaryExtractionModel);
  1580. FileOperationHelper.WriteStringToFile(userInfoXml, xmlFilePath, System.IO.FileMode.Create);
  1581. }
  1582. #region [脚本录制参数]
  1583. public object setScriptRecording(string key) {
  1584. object value = null;
  1585. if (key == "colorInterval")
  1586. {
  1587. value = this.binaryExtractionModel.ColorInterval == 2 ? true : false;
  1588. }
  1589. else if (key == "scope1")
  1590. {
  1591. value = new List<double>() { this.binaryExtractionModel.ColorOneStart, this.binaryExtractionModel.ColorOneEnd };
  1592. }
  1593. else if (key == "scope2")
  1594. {
  1595. value = new List<double>() { this.binaryExtractionModel.ColorTwoStart, this.binaryExtractionModel.ColorTwoEnd };
  1596. }
  1597. else if (key == "scope3")
  1598. {
  1599. value = new List<double>() { this.binaryExtractionModel.ColorThreeStart, this.binaryExtractionModel.ColorThreeEnd };
  1600. }
  1601. else if (key == "scope4")
  1602. {
  1603. value = new List<double>() { this.binaryExtractionModel.DebrisAreaStart, this.binaryExtractionModel.DebrisAreaEnd };
  1604. }
  1605. else if (key == "binaryStyle")
  1606. {
  1607. value = this.binaryExtractionModel.BinaryStyle;
  1608. }
  1609. else if (key == "phaseColor")
  1610. {
  1611. value = this.binaryExtractionModel.PhaseColor;
  1612. }
  1613. else if (key == "binarization")
  1614. {
  1615. value = this.binaryExtractionModel.BinaryCheckFlag == 2 || this.binaryExtractionModel.BinaryCheckFlag == 3 ? true : false;
  1616. }
  1617. else if (key == "theoriginalcolor")
  1618. {
  1619. value = this.binaryExtractionModel.BinaryCheckFlag == 1 || this.binaryExtractionModel.BinaryCheckFlag == 3 ? true : false;
  1620. }
  1621. return value;
  1622. }
  1623. #endregion
  1624. #region 内部类
  1625. internal class LocalListViewItem
  1626. {
  1627. public DocumentWorkspace Value { get; }
  1628. public string Display { get; }
  1629. public LocalListViewItem(DocumentWorkspace Value, string Display)
  1630. {
  1631. this.Value = Value;
  1632. this.Display = Display;
  1633. }
  1634. }
  1635. internal class DocumentItem
  1636. {
  1637. public GraphicsList graphicsList;
  1638. public List<PhaseModel> phaseModels;
  1639. public DocumentItem(GraphicsList graphicsList, List<PhaseModel> phaseModels)
  1640. {
  1641. this.graphicsList = graphicsList;
  1642. this.phaseModels = phaseModels;
  1643. }
  1644. }
  1645. #endregion
  1646. }
  1647. }