Merge.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using OpenCvSharp;
  7. namespace PaintDotNet.ImageCollect.CameraEDOF
  8. {
  9. class Merge
  10. {
  11. private Mat ImMer;
  12. private Mat[] coll;
  13. private Mat[] Coll_Ori;
  14. private int dstWidth = 0;
  15. //计算景深扩展高度值
  16. private Base.SettingModel.ThreeDataDemoModel threeDataDemo;
  17. private List<List<double>> threedDataList;
  18. private List<double> dataValueList;
  19. /// <summary>
  20. /// 返回景深扩展的结果,解决了之前方法内存释放不及时的问题
  21. /// </summary>
  22. /// <param name="Coll_Ori">输入Mat集合</param>
  23. /// <param name="errorMsg">null 表示成功, ""表示失败但没有返回提示信息, 其他表示失败并且有返回提示信息</param>
  24. /// <returns></returns>
  25. public static Mat GetMergeMat(Mat [] Coll_Ori, ref String errorMsg)
  26. {
  27. Merge merge = new Merge();
  28. Mat dst;
  29. try
  30. {
  31. dst = merge.ImMerge(Coll_Ori, ref errorMsg);
  32. }
  33. catch (Exception)
  34. {
  35. dst = Coll_Ori[0].Clone();
  36. }
  37. return dst;
  38. }
  39. /// <summary>
  40. /// 返回景深扩展的结果
  41. /// </summary>
  42. /// <param name="Coll_Ori">输入Mat集合</param>
  43. /// <returns></returns>
  44. private unsafe Mat ImMerge(Mat[] mats, ref String errorMsg)
  45. {
  46. this.ImMer = new Mat(mats[0].Size(), mats[0].Type());
  47. bool isSameSize = true;
  48. for (int index = 1; index < mats.Length; index++)
  49. {
  50. if (mats[index].Width != ImMer.Width || mats[index].Height != ImMer.Height)
  51. {
  52. isSameSize = false;
  53. break;
  54. }
  55. }
  56. if (!isSameSize)//图像宽高不一致
  57. {
  58. errorMsg = "";
  59. System.Windows.Forms.MessageBox.Show(PdnResources.GetString("Menu.noyizhi.Text"));
  60. return ImMer;
  61. }
  62. Mat[] Coll_deal = new Mat[mats.Length];
  63. for (int index = 0; index < mats.Length; index++)
  64. {
  65. //灰度sobel平滑
  66. Coll_deal[index] = this.Deal(mats[index]);
  67. }
  68. this.coll = Coll_deal;
  69. this.Coll_Ori = mats;
  70. dstWidth = coll[0].Width;
  71. coll[0].ForEachAsDouble(DefineProfileForEachAsVec4b);
  72. if (Coll_deal != null)
  73. {
  74. foreach (Mat item in Coll_deal)
  75. {
  76. item.Dispose();
  77. }
  78. //Coll_deal[0].Dispose();
  79. }
  80. GC.Collect();
  81. errorMsg = null;
  82. return ImMer;
  83. }
  84. private List<decimal> bitsHeight = new List<decimal>();
  85. /// <summary>
  86. /// 返回景深扩展的结果,存储到配置文件中
  87. /// </summary>
  88. /// <param name="Coll_Ori">输入Mat集合</param>
  89. /// <returns></returns>
  90. public static bool GetMergeMatHeight(Mat[] Coll_Ori, ref String errorMsg, List<decimal> bitsHeight)
  91. {
  92. Merge merge = new Merge();
  93. merge.bitsHeight.AddRange(bitsHeight);
  94. bool dst;
  95. try
  96. {
  97. dst = merge.ImMergeHeight(Coll_Ori, ref errorMsg);
  98. }
  99. catch (Exception)
  100. {
  101. dst = false;
  102. }
  103. return dst;
  104. }
  105. private unsafe bool ImMergeHeight(Mat[] Coll_Ori, ref String errorMsg)
  106. {
  107. this.ImMer = new Mat(Coll_Ori[0].Size(), Coll_Ori[0].Type());
  108. bool isSameSize = true;
  109. for (int index = 1; index < Coll_Ori.Length; index++)
  110. {
  111. if (Coll_Ori[index].Width != Coll_Ori[0].Size().Width || Coll_Ori[index].Height != Coll_Ori[0].Size().Height)
  112. {
  113. isSameSize = false;
  114. break;
  115. }
  116. }
  117. if (!isSameSize)//图像宽高不一致
  118. {
  119. errorMsg = "";
  120. System.Windows.Forms.MessageBox.Show(PdnResources.GetString("Menu.noyizhi.Text"));
  121. return false;
  122. }
  123. Mat[] Coll_deal = new Mat[Coll_Ori.Length];
  124. for (int index = 0; index < Coll_Ori.Length; index++)
  125. {
  126. //灰度sobel平滑
  127. Coll_deal[index] = this.Deal(Coll_Ori[index]);
  128. }
  129. this.coll = Coll_deal;
  130. this.Coll_Ori = Coll_Ori;
  131. dstWidth = coll[0].Width;
  132. int matHeight = coll[0].Height;
  133. dataValueList = new List<double>();
  134. threedDataList = new List<List<double>>();
  135. threeDataDemo = new Base.SettingModel.ThreeDataDemoModel();// Base.CommTool.XmlSerializeHelper.DESerializer<Base.SettingModel.ThreeDataDemoModel>(Base.CommTool.FileOperationHelper.ReadStringFromFile(System.Windows.Forms.Application.StartupPath + "\\Config\\" + "Default"/*SettingPrefix*/ + "\\ThreeDataDemo.xml", System.IO.FileMode.Open));
  136. threeDataDemo.size = new Base.SettingModel.ThreeDataDemoModel.Size();//.Height = coll[0].Height;
  137. threeDataDemo.size.Height = coll[0].Height;
  138. threeDataDemo.size.Width = dstWidth;
  139. threeDataDemo.Z_value = new List<string>();
  140. for (int threeD = 0; threeD < dstWidth; threeD++)
  141. {
  142. List<double> threedData = new List<double>();
  143. for (int indexY = 0; indexY < coll[0].Height; indexY++)
  144. threedData.Add(0.0);
  145. threedDataList.Add(threedData);
  146. }
  147. coll[0].ForEachAsDouble(DefineProfileForEachAsVec4bForHeight);
  148. if (this.ImMer != null)
  149. {
  150. Mat matGray = this.ImMer.CvtColor(ColorConversionCodes.BGR2GRAY);
  151. for (int index_x = 0; index_x < threedDataList.Count; index_x++)
  152. {
  153. List<double> threedData = threedDataList[index_x];
  154. for (int index_y = 0; index_y < matHeight; index_y++)
  155. {//高度与颜色相乘
  156. byte vec = matGray.At<byte>(index_y, index_x);
  157. threedData[index_y] = 1.6 * threedData[index_y];// * vec / 255.0;
  158. }
  159. threeDataDemo.Z_value.Add(string.Join(",", threedData.ToArray()));
  160. }
  161. }
  162. //if (dataValueList.Count > 0)
  163. //{
  164. //}
  165. threeDataDemo.Z_level = "16.000000,0.000000";
  166. if (threeDataDemo.Dyeing == null || threeDataDemo.Dyeing.Count == 0)
  167. threeDataDemo.Dyeing = new List<string>();
  168. //threeDataDemo.Dyeing.Add("100.00,0.00,100.00");
  169. threeDataDemo.Dyeing.Add("255.00,0.00,0.00");
  170. threeDataDemo.Dyeing.Add("0.00,255.00,0.00");
  171. string filePath = System.Windows.Forms.Application.StartupPath/* + "\\Config\\" + "Default"*//*SettingPrefix*/ + "\\ThreeDataDemo_temp.xml";
  172. string toolbarXml = Base.CommTool.XmlSerializeHelper.XmlSerialize<Base.SettingModel.ThreeDataDemoModel>(threeDataDemo);
  173. if (Base.CommTool.FileOperationHelper.WriteStringToFile(toolbarXml, filePath, System.IO.FileMode.Create))
  174. errorMsg = "配置文件保存成功";
  175. //System.Windows.Forms.MessageBox.Show("配置文件保存成功");
  176. else
  177. errorMsg = "配置文件保存成功";
  178. //System.Windows.Forms.MessageBox.Show("配置文件保存失败");
  179. if (Coll_deal != null)
  180. {
  181. foreach (Mat item in Coll_deal)
  182. {
  183. item.Dispose();
  184. }
  185. //Coll_deal[0].Dispose();
  186. }
  187. GC.Collect();
  188. string filePath_ImMer = System.Windows.Forms.Application.StartupPath/* + "\\Config\\" + "Default"*//*SettingPrefix*/ + "\\ThreeDataDemo_temp.bmp";
  189. Cv2.ImWrite(filePath_ImMer, this.ImMer);
  190. errorMsg = null;
  191. return true;
  192. }
  193. /// <summary>
  194. /// 返回景深扩展的结果,硬件调用实现
  195. /// </summary>
  196. /// <param name="Coll_Ori">输入Mat集合</param>
  197. /// <returns></returns>
  198. public static Mat GetMergeMatForCamera(Mat[] Coll_Ori)
  199. {
  200. Merge merge = new Merge();
  201. Mat dst;
  202. try
  203. {
  204. dst = merge.ImMergeForCamera(Coll_Ori);
  205. }
  206. catch (Exception)
  207. {
  208. dst = Coll_Ori[0].Clone();
  209. }
  210. return dst;
  211. }
  212. /// <summary>
  213. /// 返回景深扩展的结果
  214. /// </summary>
  215. /// <param name="Coll_Ori">输入Mat集合</param>
  216. /// <returns></returns>
  217. private unsafe Mat ImMergeForCamera(Mat[] Coll_Ori)
  218. {
  219. this.ImMer = new Mat(Coll_Ori[0].Size(), Coll_Ori[0].Type());
  220. bool isSameSize = true;
  221. for (int index = 1; index < Coll_Ori.Length; index++)
  222. {
  223. if (Coll_Ori[index].Width != ImMer.Width || Coll_Ori[index].Height != ImMer.Height)
  224. {
  225. isSameSize = false;
  226. break;
  227. }
  228. }
  229. if (!isSameSize)//图像宽高不一致
  230. {
  231. //System.Windows.MessageBox.Show("导入图片大小不一致,无法融合");
  232. return ImMer;
  233. }
  234. Mat[] Coll_deal = new Mat[Coll_Ori.Length];
  235. for (int index = 0; index < Coll_Ori.Length; index++)
  236. {
  237. //灰度sobel平滑
  238. Coll_deal[index] = this.Deal(Coll_Ori[index]);
  239. }
  240. this.coll = Coll_deal;
  241. this.Coll_Ori = Coll_Ori;
  242. dstWidth = coll[0].Width;
  243. coll[0].ForEachAsDouble(DefineProfileForEachAsVec3b);
  244. //Mat col0 = coll[0];
  245. //double* pixels2 = (double*)col0.Data;
  246. //for (int i = 0; i < col0.Rows; i++)
  247. //{
  248. // for (int j = 0; j < col0.Cols; j++)
  249. // {
  250. // int index_y = i;// position[0];
  251. // int index = j;// position[1];
  252. // double G1 = pixels2[i * dstWidth + j];
  253. // int numIdx = 0;
  254. // for (int num = 1; num < coll.Length; num++)
  255. // {
  256. // double* pixels3 = (double*)coll[num].Data;
  257. // double G2 = pixels3[i * dstWidth + j];
  258. // if (G1 <= G2)
  259. // {
  260. // G1 = (double)G2;
  261. // numIdx = num;
  262. // }
  263. // }
  264. // Vec3b* pixels4 = (Vec3b*)Coll_Ori[numIdx].Data;
  265. // ImMer.Set<Vec3b>(index_y, index, pixels4[index_y * dstWidth + index]);
  266. // }
  267. //}
  268. if (Coll_deal != null)
  269. {
  270. foreach (Mat item in Coll_deal)
  271. {
  272. item.Dispose();
  273. }
  274. //Coll_deal[0].Dispose();
  275. }
  276. GC.Collect();
  277. return ImMer;
  278. }
  279. /// <summary>
  280. /// 灰度sobel平滑
  281. /// </summary>
  282. /// <param name="ImIn"></param>
  283. /// <returns></returns>
  284. private Mat Deal(Mat ImIn)
  285. {
  286. Mat mat_gray = new Mat();
  287. Cv2.CvtColor(ImIn, mat_gray, ColorConversionCodes.BGR2GRAY);
  288. Mat X = new Mat(); Mat Y = new Mat();
  289. //sobel边缘
  290. //-----------这里开始转变为16位运算-------------
  291. Cv2.Sobel(mat_gray, X, MatType.CV_16S, 1, 0, 3, 1, 0, BorderTypes.Replicate);
  292. Cv2.Sobel(mat_gray, Y, MatType.CV_16S, 0, 1, 3, 1, 0, BorderTypes.Replicate);
  293. //-------double型数据的对应像素深度是64F---------
  294. var output = new Mat(X.Size(), MatType.CV_64F);
  295. Mat ZZ = (X.Pow(2) + Y.Pow(2));
  296. ZZ.ConvertTo(output, MatType.CV_64F);
  297. Cv2.Sqrt(output, output);
  298. Cv2.GaussianBlur(output, output, new OpenCvSharp.Size(31, 31), 11);
  299. if (mat_gray != null) mat_gray.Dispose();
  300. if (X != null) X.Dispose();
  301. if (Y != null) Y.Dispose();
  302. if (ZZ != null) ZZ.Dispose();
  303. return output;
  304. }
  305. /// <summary>
  306. /// 获取ID标识并进行赋值
  307. /// </summary>
  308. /// <param name="value"></param>
  309. /// <param name="position"></param>
  310. private unsafe void DefineProfileForEachAsVec4b(double* value, int* position)
  311. {
  312. int index_y = position[0];
  313. int index = position[1];
  314. double G1 = *value;
  315. int numIdx = 0;
  316. for (int num = 1; num < coll.Length; num++)
  317. {
  318. double* pixels3 = (double*)coll[num].Data;
  319. double G2 = pixels3[position[0] * dstWidth + position[1]];
  320. if (G1 <= G2)
  321. {
  322. G1 = (double)G2;
  323. numIdx = num;
  324. }
  325. }
  326. Vec4b* pixels4 = (Vec4b*)Coll_Ori[numIdx].Data;
  327. ImMer.Set<Vec4b>(index_y, index, pixels4[index_y * dstWidth + index]);
  328. }
  329. /// <summary>
  330. /// 获取ID标识并进行赋值高度
  331. /// </summary>
  332. /// <param name="value"></param>
  333. /// <param name="position"></param>
  334. private unsafe void DefineProfileForEachAsVec4bForHeight(double* value, int* position)
  335. {
  336. int index_y = position[0];
  337. int index = position[1];
  338. double G1 = *value;
  339. //double G111 = *value;
  340. ////if (threeDataDemo.Dyeing == null || threeDataDemo.Dyeing.Count == 0)
  341. //// threeDataDemo.Dyeing = new List<string>();
  342. ////threeDataDemo.Dyeing.Add("100.00,0.00,100.00");
  343. //string filePath = Application.StartupPath + "\\Config\\" + "Default"/*SettingPrefix*/ + "\\ThreeDataDemo_5.xml";
  344. //string toolbarXml = XmlSerializeHelper.XmlSerialize<ThreeDataDemoModel>(threeDataDemo);
  345. //if (FileOperationHelper.WriteStringToFile(toolbarXml, filePath, FileMode.Create))
  346. // MessageBox.Show("配置文件保存成功");
  347. //else
  348. // MessageBox.Show("配置文件保存失败");
  349. ////foreach (var item in threeDataDemo.Size)
  350. ////{
  351. //// string item11 = item + "";
  352. ////}
  353. int numIdx = 0;
  354. for (int num = 1; num < coll.Length; num++)
  355. {
  356. double* pixels3 = (double*)coll[num].Data;
  357. double G2 = pixels3[position[0] * dstWidth + position[1]];
  358. if (G1 < G2)
  359. {
  360. G1 = (double)G2;
  361. numIdx = num;
  362. }
  363. //else if (G1 - G2 < 5)
  364. //{
  365. // G1 = (double)G2;
  366. // numIdx = num;
  367. //}
  368. ////if (G111 > G2)
  369. //// G111 = (double)G2;
  370. }
  371. if (this.bitsHeight.Count > numIdx)
  372. threedDataList[index][index_y] = (double)this.bitsHeight[numIdx];// + G1;// / 1000.0;
  373. //{
  374. // threedDataList[index][index_y] = 3.6 * (double)(0/*38*/ + this.bitsHeight[numIdx]);// + G1 * 0.5;// + G1;// / 1000.0;
  375. // if (!dataValueList.Contains((double)this.bitsHeight[numIdx]))
  376. // {
  377. // dataValueList.Add((double)this.bitsHeight[numIdx]);
  378. // }
  379. //}
  380. else
  381. threedDataList[index][index_y] = G1;
  382. Vec4b* pixels4 = (Vec4b*)Coll_Ori[numIdx].Data;
  383. ImMer.Set<Vec4b>(index_y, index, pixels4[index_y * dstWidth + index]);
  384. }
  385. /// <summary>
  386. /// 获取ID标识并进行赋值
  387. /// </summary>
  388. /// <param name="value"></param>
  389. /// <param name="position"></param>
  390. private unsafe void DefineProfileForEachAsVec3b(double* value, int* position)
  391. {
  392. int index_y = position[0];
  393. int index = position[1];
  394. double G1 = *value;
  395. int numIdx = 0;
  396. for (int num = 1; num < coll.Length; num++)
  397. {
  398. double* pixels3 = (double*)coll[num].Data;
  399. double G2 = pixels3[position[0] * dstWidth + position[1]];
  400. if (G1 <= G2)
  401. {
  402. G1 = (double)G2;
  403. numIdx = num;
  404. }
  405. }
  406. Vec3b* pixels4 = (Vec3b*)Coll_Ori[numIdx].Data;
  407. ImMer.Set<Vec3b>(index_y, index, pixels4[index_y * dstWidth + index]);
  408. }
  409. }
  410. }