InterImageIntent.cs 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760
  1. using OpenCvSharp;
  2. using PaintDotNet.Base;
  3. using System;
  4. using System.Collections.Generic;
  5. namespace PaintDotNet.Adjust
  6. {
  7. /// <summary>
  8. /// 图像间操作
  9. /// </summary>
  10. public class InterImageIntent
  11. {
  12. /// <summary>
  13. /// 图像间操作-最小值
  14. /// </summary>
  15. /// <param name="src"></param>
  16. /// <returns></returns>
  17. public static Mat ImageMin(Mat src, List<Args> lists)
  18. {
  19. Mat dst = new Mat();
  20. Mat mat1 = new Mat();
  21. Mat mat2 = new Mat();
  22. try
  23. {
  24. for (int i = 0; i < lists.Count; i++)
  25. {
  26. Args args = lists[i];
  27. switch (args.Key)
  28. {
  29. case "Src2":
  30. mat1 = (Mat)args.Value;
  31. break;
  32. default:
  33. break;
  34. }
  35. }
  36. int w = src.Width;
  37. int h = src.Height;
  38. int w1 = mat1.Width;
  39. int h1 = mat1.Height;
  40. mat2 = new Mat(h, w, src.Type(), Scalar.All(0));
  41. Rect rect = new Rect(0, 0, Math.Min(w, w1), Math.Min(h, h1));
  42. Mat tempMat = mat1.Clone(rect);
  43. //复制用户选中的区域到原图像
  44. Mat mask = tempMat.CvtColor(ColorConversionCodes.RGBA2GRAY);
  45. Mat pos = new Mat(mat2, rect);
  46. tempMat.CopyTo(pos, mask);
  47. //Cv2.CopyMakeBorder(src, src, 0, maxh - src.Height, 0, maxw - src.Width, BorderTypes.Isolated);
  48. //Cv2.CopyMakeBorder(mat1, mat1, 0, maxh - mat1.Height, 0, maxw - mat1.Width, BorderTypes.Isolated);
  49. Mat tempMat0 = src.Clone(rect);
  50. //复制用户选中的区域到原图像
  51. Mat mask0 = tempMat0.CvtColor(ColorConversionCodes.RGBA2GRAY);
  52. Mat mat0 = new Mat(h, w, src.Type(), Scalar.All(/*255*/0));
  53. Mat pos0 = new Mat(mat0, rect);
  54. tempMat0.CopyTo(pos0, mask0);
  55. Cv2.Min(mat0, mat2, dst);
  56. }
  57. catch (Exception e)
  58. {
  59. Console.WriteLine(e.ToString());
  60. src.CopyTo(dst);
  61. }
  62. finally
  63. {
  64. if (src != null) dst.CopyTo(src);
  65. if (dst != null) dst.Dispose();
  66. }
  67. return src;
  68. }
  69. /// <summary>
  70. /// 图像间操作-最大值
  71. /// </summary>
  72. /// <param name="src"></param>
  73. /// <returns></returns>
  74. public static Mat ImageMax(Mat src, List<Args> lists)
  75. {
  76. Mat dst = new Mat();
  77. Mat mat1 = new Mat();
  78. Mat mat2 = new Mat();
  79. try
  80. {
  81. for (int i = 0; i < lists.Count; i++)
  82. {
  83. Args args = lists[i];
  84. switch (args.Key)
  85. {
  86. case "Src2":
  87. mat1 = (Mat)args.Value;
  88. break;
  89. default:
  90. break;
  91. }
  92. }
  93. int w = src.Width;
  94. int h = src.Height;
  95. int w1 = mat1.Width;
  96. int h1 = mat1.Height;
  97. mat2 = new Mat(h, w, src.Type(), Scalar.All(0));
  98. Rect rect = new Rect(0, 0, Math.Min(w, w1), Math.Min(h, h1));
  99. Mat tempMat = mat1.Clone(rect);
  100. //复制用户选中的区域到原图像
  101. Mat mask = tempMat.CvtColor(ColorConversionCodes.RGBA2GRAY);
  102. Mat pos = new Mat(mat2, rect);
  103. tempMat.CopyTo(pos, mask);
  104. //Cv2.CopyMakeBorder(src, src, 0, maxh - src.Height, 0, maxw - src.Width, BorderTypes.Isolated);
  105. //Cv2.CopyMakeBorder(mat1, mat1, 0, maxh - mat1.Height, 0, maxw - mat1.Width, BorderTypes.Isolated);
  106. Mat tempMat0 = src.Clone(rect);
  107. //复制用户选中的区域到原图像
  108. Mat mask0 = tempMat0.CvtColor(ColorConversionCodes.RGBA2GRAY);
  109. Mat mat0 = new Mat(h, w, src.Type(), Scalar.All(/*255*/0));
  110. Mat pos0 = new Mat(mat0, rect);
  111. tempMat0.CopyTo(pos0, mask0);
  112. Cv2.Max(mat0, mat2, dst);
  113. }
  114. catch (Exception e)
  115. {
  116. Console.WriteLine(e.ToString());
  117. src.CopyTo(dst);
  118. }
  119. finally
  120. {
  121. if (src != null) dst.CopyTo(src);
  122. if (dst != null) dst.Dispose();
  123. }
  124. return src;
  125. }
  126. /// <summary>
  127. /// 图像间操作-指数
  128. /// </summary>
  129. /// <param name="src"></param>
  130. /// <returns></returns>
  131. public static Mat ImageIndex(Mat src)
  132. {
  133. Mat dst = new Mat();
  134. try
  135. {
  136. Mat resultImage = new Mat(src.Size(), src.Type());
  137. src.ConvertTo(resultImage, MatType.CV_64F, 1.0 / 255, 0);
  138. Cv2.Pow(resultImage, 5.5, resultImage);
  139. Cv2.ConvertScaleAbs(resultImage * 255, resultImage);
  140. Cv2.Normalize(resultImage, resultImage, 1, 255, NormTypes.MinMax);
  141. resultImage.CopyTo(dst);
  142. }
  143. catch (Exception e)
  144. {
  145. Console.WriteLine(e.ToString());
  146. src.CopyTo(dst);
  147. }
  148. finally
  149. {
  150. if (src != null) dst.CopyTo(src);
  151. if (dst != null) dst.Dispose();
  152. }
  153. return src;
  154. }
  155. /// <summary>
  156. /// 图像间操作-对数
  157. /// </summary>
  158. /// <param name="src"></param>
  159. /// <returns></returns>
  160. public static Mat ImageLogarithm(Mat src)
  161. {
  162. bool isFourChannels = false;
  163. if (src.Channels() == 4)
  164. {
  165. Cv2.CvtColor(src, src, OpenCvSharp.ColorConversionCodes.BGRA2BGR);
  166. isFourChannels = true;
  167. }
  168. Mat dst = new Mat();
  169. try
  170. {
  171. Mat resultImage = new Mat(src.Size(), src.Type());
  172. for (int y = 0; y < src.Height; y++)//行
  173. {
  174. for (int x = 0; x < src.Width; x++)//列
  175. {
  176. Vec3b pix = src.At<Vec3b>(y, x);
  177. pix[0] = (byte)(Math.Log(pix[0] + 1) * 9);
  178. pix[1] = (byte)(Math.Log(pix[1] + 1) * 9);
  179. pix[2] = (byte)(Math.Log(pix[2] + 1) * 9);
  180. //if (pix[0] > 255) pix[0] = 255;
  181. //if (pix[0] < 0) pix[0] = 0;
  182. //if (pix[1] > 255) pix[1] = 255;
  183. //if (pix[1] < 0) pix[1] = 0;
  184. //if (pix[2] > 255) pix[2] = 255;
  185. //if (pix[2] < 0) pix[2] = 0;
  186. resultImage.Set<Vec3b>(y, x, pix);
  187. }
  188. }
  189. Cv2.ConvertScaleAbs(resultImage, resultImage);
  190. Cv2.Normalize(resultImage, resultImage, 1, 255, NormTypes.MinMax);
  191. resultImage.CopyTo(dst);
  192. }
  193. catch (Exception e)
  194. {
  195. Console.WriteLine(e.ToString());
  196. src.CopyTo(dst);
  197. }
  198. finally
  199. {
  200. if (src != null) dst.CopyTo(src);
  201. if (dst != null) dst.Dispose();
  202. }
  203. if (isFourChannels)
  204. {
  205. Cv2.CvtColor(src, src, OpenCvSharp.ColorConversionCodes.BGR2BGRA);
  206. }
  207. return src;
  208. }
  209. /// <summary>
  210. /// 图像间操作-平方根
  211. /// </summary>
  212. /// <param name="src"></param>
  213. /// <returns></returns>
  214. public static Mat ImageSquareBoot(Mat src)
  215. {
  216. Mat dst = new Mat();
  217. bool isFourChannels = false;
  218. if (src.Channels() == 4)
  219. {
  220. Cv2.CvtColor(src, src, OpenCvSharp.ColorConversionCodes.BGRA2BGR);
  221. isFourChannels = true;
  222. }
  223. try
  224. {
  225. Mat resultImage = new Mat(src.Size(), src.Type());
  226. for (int y = 0; y < src.Height; y++)//行
  227. {
  228. for (int x = 0; x < src.Width; x++)//列
  229. {
  230. Vec3b pix = src.At<Vec3b>(y, x);
  231. pix[0] = (byte)(Math.Sqrt(pix[0]) * 15);
  232. pix[1] = (byte)(Math.Sqrt(pix[1]) * 15);
  233. pix[2] = (byte)(Math.Sqrt(pix[2]) * 15);
  234. //if (pix[0] > 255) pix[0] = 255;
  235. //if (pix[0] < 0) pix[0] = 0;
  236. //if (pix[1] > 255) pix[1] = 255;
  237. //if (pix[1] < 0) pix[1] = 0;
  238. //if (pix[2] > 255) pix[2] = 255;
  239. //if (pix[2] < 0) pix[2] = 0;
  240. resultImage.Set<Vec3b>(y, x, pix);
  241. }
  242. }
  243. Cv2.ConvertScaleAbs(resultImage, resultImage);
  244. Cv2.Normalize(resultImage, resultImage, 1, 255, NormTypes.MinMax);
  245. resultImage.CopyTo(dst);
  246. }
  247. catch (Exception e)
  248. {
  249. Console.WriteLine(e.ToString());
  250. src.CopyTo(dst);
  251. }
  252. finally
  253. {
  254. if (src != null) dst.CopyTo(src);
  255. if (dst != null) dst.Dispose();
  256. }
  257. if (isFourChannels)
  258. {
  259. Cv2.CvtColor(src, src, OpenCvSharp.ColorConversionCodes.BGR2BGRA);
  260. }
  261. return src;
  262. }
  263. /// <summary>
  264. /// 图像间操作-平方
  265. /// </summary>
  266. /// <param name="src"></param>
  267. /// <returns></returns>
  268. public static Mat ImageSquare(Mat src)
  269. {
  270. Mat dst = new Mat();
  271. try
  272. {
  273. Mat resultImage = new Mat(src.Size(), src.Type());
  274. src.ConvertTo(resultImage, MatType.CV_64F, 1.0 / 255, 0);
  275. Cv2.Pow(resultImage, 2, resultImage);
  276. Cv2.ConvertScaleAbs(resultImage * 255, resultImage);
  277. Cv2.Normalize(resultImage, resultImage, 1, 255, NormTypes.MinMax);
  278. resultImage.CopyTo(dst);
  279. }
  280. catch (Exception e)
  281. {
  282. Console.WriteLine(e.ToString());
  283. src.CopyTo(dst);
  284. }
  285. finally
  286. {
  287. if (src != null) dst.CopyTo(src);
  288. if (dst != null) dst.Dispose();
  289. }
  290. return src;
  291. }
  292. /// <summary>
  293. /// 图像间操作-平均
  294. /// </summary>
  295. /// <param name="src"></param>
  296. /// <returns></returns>
  297. public static Mat ImageAverage(Mat src, List<Args> lists)
  298. {
  299. Mat dst = new Mat();
  300. Mat mat1 = new Mat();
  301. Mat mat2;// = new Mat();
  302. try
  303. {
  304. for (int i = 0; i < lists.Count; i++)
  305. {
  306. Args args = lists[i];
  307. switch (args.Key)
  308. {
  309. case "Src2":
  310. mat1 = (Mat)args.Value;
  311. break;
  312. default:
  313. break;
  314. }
  315. }
  316. int w = src.Width;
  317. int h = src.Height;
  318. int w1 = mat1.Width;
  319. int h1 = mat1.Height;
  320. mat2 = new Mat(h, w, src.Type(), Scalar.All(0));
  321. Rect rect = new Rect(0, 0, Math.Min(w, w1), Math.Min(h, h1));
  322. Mat tempMat = mat1.Clone(rect);
  323. //复制用户选中的区域到原图像
  324. Mat mask = tempMat.CvtColor(ColorConversionCodes.RGBA2GRAY);
  325. Mat pos = new Mat(mat2, rect);
  326. tempMat.CopyTo(pos, mask);
  327. //Cv2.CopyMakeBorder(src, src, 0, maxh - src.Height, 0, maxw - src.Width, BorderTypes.Isolated);
  328. //Cv2.CopyMakeBorder(mat1, mat1, 0, maxh - mat1.Height, 0, maxw - mat1.Width, BorderTypes.Isolated);
  329. Mat tempMat0 = src.Clone(rect);
  330. //复制用户选中的区域到原图像
  331. Mat mask0 = tempMat0.CvtColor(ColorConversionCodes.RGBA2GRAY);
  332. Mat mat0 = new Mat(h, w, src.Type(), Scalar.All(/*255*/0));
  333. Mat pos0 = new Mat(mat0, rect);
  334. tempMat0.CopyTo(pos0, mask0);
  335. Cv2.AddWeighted(src/*mat0*/, 0.5, mat2, 0.5, 1, dst);// (mat0, mat2, dst);
  336. //Cv2.Add(mat0, mat2, dst);
  337. //dst = dst / 2;
  338. ////Cv2.Normalize(dst, dst, 1, 255, NormTypes.MinMax);
  339. }
  340. catch (Exception e)
  341. {
  342. Console.WriteLine(e.ToString());
  343. src.CopyTo(dst);
  344. }
  345. finally
  346. {
  347. if (src != null) dst.CopyTo(src);
  348. if (dst != null) dst.Dispose();
  349. }
  350. return src;
  351. }
  352. /// <summary>
  353. /// 图像间操作-除
  354. /// </summary>
  355. /// <param name="src"></param>
  356. /// <returns></returns>
  357. public static Mat ShadingDivide(Mat src, List<Args> lists)
  358. {
  359. Mat dst = new Mat();
  360. Mat mat1 = new Mat();
  361. Mat mat2;// = new Mat();
  362. double factor = 1;
  363. bool isFourChannels = false;
  364. if (src.Channels() == 4)
  365. {
  366. Cv2.CvtColor(src, src, OpenCvSharp.ColorConversionCodes.BGRA2BGR);
  367. isFourChannels = true;
  368. }
  369. try
  370. {
  371. for (int i = 0; i < lists.Count; i++)
  372. {
  373. Args args = lists[i];
  374. switch (args.Key)
  375. {
  376. case "Src2":
  377. var mat = (Mat)args.Value;
  378. if (mat.Channels() == 4)
  379. {
  380. Cv2.CvtColor(mat, mat, OpenCvSharp.ColorConversionCodes.BGRA2BGR);
  381. }
  382. mat1 = mat;
  383. break;
  384. case "Factor":
  385. if (double.TryParse(args.Value.ToString(), out factor))
  386. {
  387. if (factor == 0)
  388. {
  389. factor = 1;
  390. }
  391. else
  392. {
  393. factor = 1.0 / factor;
  394. }
  395. }
  396. break;
  397. default:
  398. break;
  399. }
  400. }
  401. int w = src.Width;
  402. int h = src.Height;
  403. int w1 = mat1.Width;
  404. int h1 = mat1.Height;
  405. mat2 = new Mat(h, w, src.Type(), Scalar.All(0));
  406. Rect rect = new Rect(0, 0, Math.Min(w, w1), Math.Min(h, h1));
  407. Mat tempMat = mat1.Clone(rect);
  408. //复制用户选中的区域到原图像
  409. Mat mask = tempMat.CvtColor(ColorConversionCodes.RGBA2GRAY);
  410. Mat pos = new Mat(mat2, rect);
  411. tempMat.CopyTo(pos, mask);
  412. //Cv2.CopyMakeBorder(src, src, 0, maxh - src.Height, 0, maxw - src.Width, BorderTypes.Isolated);
  413. //Cv2.CopyMakeBorder(mat1, mat1, 0, maxh - mat1.Height, 0, maxw - mat1.Width, BorderTypes.Isolated);
  414. Mat tempMat0 = src.Clone(rect);
  415. //复制用户选中的区域到原图像
  416. Mat mask0 = tempMat0.CvtColor(ColorConversionCodes.RGBA2GRAY);
  417. Mat mat0 = new Mat(h, w, src.Type(), Scalar.All(/*255*/0));
  418. Mat pos0 = new Mat(mat0, rect);
  419. tempMat0.CopyTo(pos0, mask0);
  420. Cv2.Divide(src/*mat0*/, mat2, dst, factor);
  421. //Cv2.Normalize(dst, dst, 1, 255, NormTypes.MinMax);
  422. }
  423. catch (Exception e)
  424. {
  425. Console.WriteLine(e.ToString());
  426. src.CopyTo(dst);
  427. }
  428. finally
  429. {
  430. if (src != null) dst.CopyTo(src);
  431. if (dst != null) dst.Dispose();
  432. }
  433. if (isFourChannels)
  434. {
  435. Cv2.CvtColor(src, src, OpenCvSharp.ColorConversionCodes.BGR2BGRA);
  436. }
  437. return src;
  438. }
  439. /// <summary>
  440. /// 图像间操作-乘
  441. /// </summary>
  442. /// <param name="src"></param>
  443. /// <returns></returns>
  444. public static Mat ShadingMultiply(Mat src, List<Args> lists)
  445. {
  446. bool isFourChannels = false;
  447. if(src.Channels() == 4)
  448. {
  449. Cv2.CvtColor(src, src, OpenCvSharp.ColorConversionCodes.BGRA2BGR);
  450. isFourChannels = true;
  451. }
  452. Mat dst = new Mat();
  453. Mat mat1 = new Mat();
  454. Mat mat2;//待优化: = new Mat();
  455. double factor = 1;
  456. try
  457. {
  458. for (int i = 0; i < lists.Count; i++)
  459. {
  460. Args args = lists[i];
  461. switch (args.Key)
  462. {
  463. case "Src2":
  464. var mat = (Mat)args.Value;
  465. if (mat.Channels() == 4)
  466. {
  467. Cv2.CvtColor(mat, mat, OpenCvSharp.ColorConversionCodes.BGRA2BGR);
  468. }
  469. mat1 = mat;
  470. break;
  471. case "Factor":
  472. if (double.TryParse(args.Value.ToString(), out factor))
  473. {
  474. if (factor == 0)
  475. {
  476. factor = 1;
  477. }
  478. else
  479. {
  480. factor = 1.0 / factor;
  481. }
  482. }
  483. break;
  484. default:
  485. break;
  486. }
  487. }
  488. int w = src.Width;
  489. int h = src.Height;
  490. int w1 = mat1.Width;
  491. int h1 = mat1.Height;
  492. mat2 = new Mat(h, w, src.Type(), Scalar.All(0));
  493. Rect rect = new Rect(0, 0, Math.Min(w, w1), Math.Min(h, h1));
  494. Mat tempMat = mat1.Clone(rect);
  495. //复制用户选中的区域到原图像
  496. Mat mask = tempMat.CvtColor(ColorConversionCodes.RGBA2GRAY);
  497. Mat pos = new Mat(mat2, rect);
  498. tempMat.CopyTo(pos, mask);
  499. //Cv2.CopyMakeBorder(src, src, 0, maxh - src.Height, 0, maxw - src.Width, BorderTypes.Isolated);
  500. //Cv2.CopyMakeBorder(mat1, mat1, 0, maxh - mat1.Height, 0, maxw - mat1.Width, BorderTypes.Isolated);
  501. Mat tempMat0 = src.Clone(rect);
  502. //复制用户选中的区域到原图像
  503. Mat mask0 = tempMat0.CvtColor(ColorConversionCodes.RGBA2GRAY);
  504. Mat mat0 = new Mat(h, w, src.Type(), Scalar.All(/*255*/0));
  505. Mat pos0 = new Mat(mat0, rect);
  506. tempMat0.CopyTo(pos0, mask0);
  507. Cv2.Multiply(src/*mat0*/, mat2, dst, factor);
  508. //Cv2.Normalize(dst, dst, 1, 255, NormTypes.MinMax);
  509. }
  510. catch (Exception e)
  511. {
  512. Console.WriteLine(e.ToString());
  513. src.CopyTo(dst);
  514. }
  515. finally
  516. {
  517. if (src != null) dst.CopyTo(src);
  518. if (dst != null) dst.Dispose();
  519. }
  520. if (isFourChannels)
  521. {
  522. Cv2.CvtColor(src, src, OpenCvSharp.ColorConversionCodes.BGR2BGRA);
  523. }
  524. return src;
  525. }
  526. /// <summary>
  527. /// 图像间操作-减
  528. /// </summary>
  529. /// <param name="src"></param>
  530. /// <returns></returns>
  531. public static Mat ShadingSubtract(Mat src, List<Args> lists)
  532. {
  533. Mat dst = new Mat();
  534. Mat mat1 = new Mat();
  535. Mat mat2 = new Mat();
  536. bool isFourChannels = false;
  537. if(src.Channels() == 4)
  538. {
  539. Cv2.CvtColor(src, src, OpenCvSharp.ColorConversionCodes.BGRA2BGR);
  540. isFourChannels = true;
  541. }
  542. try
  543. {
  544. for (int i = 0; i < lists.Count; i++)
  545. {
  546. Args args = lists[i];
  547. switch (args.Key)
  548. {
  549. case "Src2":
  550. var mat = (Mat)args.Value;
  551. if (mat.Channels() == 4)
  552. {
  553. Cv2.CvtColor(mat, mat, OpenCvSharp.ColorConversionCodes.BGRA2BGR);
  554. }
  555. mat1 = mat;
  556. break;
  557. default:
  558. break;
  559. }
  560. }
  561. int w = src.Width;
  562. int h = src.Height;
  563. int w1 = mat1.Width;
  564. int h1 = mat1.Height;
  565. mat2 = new Mat(h, w, src.Type(), Scalar.All(0));
  566. Rect rect = new Rect(0, 0, Math.Min(w, w1), Math.Min(h, h1));
  567. Mat tempMat = mat1.Clone(rect);
  568. //复制用户选中的区域到原图像
  569. Mat mask = tempMat.CvtColor(ColorConversionCodes.RGBA2GRAY);
  570. Mat pos = new Mat(mat2, rect);
  571. tempMat.CopyTo(pos, mask);
  572. //Cv2.CopyMakeBorder(src, src, 0, maxh - src.Height, 0, maxw - src.Width, BorderTypes.Isolated);
  573. //Cv2.CopyMakeBorder(mat1, mat1, 0, maxh - mat1.Height, 0, maxw - mat1.Width, BorderTypes.Isolated);
  574. Mat tempMat0 = src.Clone(rect);
  575. //复制用户选中的区域到原图像
  576. Mat mask0 = tempMat0.CvtColor(ColorConversionCodes.RGBA2GRAY);
  577. Mat mat0 = new Mat(h, w, src.Type(), Scalar.All(/*255*/0));
  578. Mat pos0 = new Mat(mat0, rect);
  579. tempMat0.CopyTo(pos0, mask0);
  580. Cv2.Subtract(src/*待确认效果:mat0*/, mat2, dst);
  581. //Cv2.Normalize(dst, dst, 1, 255, NormTypes.MinMax);
  582. }
  583. catch (Exception e)
  584. {
  585. Console.WriteLine(e.ToString());
  586. src.CopyTo(dst);
  587. }
  588. finally
  589. {
  590. if (src != null) dst.CopyTo(src);
  591. if (dst != null) dst.Dispose();
  592. }
  593. if (isFourChannels) {
  594. Cv2.CvtColor(src, src, OpenCvSharp.ColorConversionCodes.BGR2BGRA);
  595. }
  596. return src;
  597. }
  598. /// <summary>
  599. /// 图像间操作-加
  600. /// </summary>
  601. /// <param name="src"></param>
  602. /// <returns></returns>
  603. public static Mat ShadingAdd(Mat src, List<Args> lists)
  604. {
  605. Mat dst = new Mat();
  606. Mat mat1 = new Mat();
  607. Mat mat2 = new Mat(); ;
  608. try
  609. {
  610. for (int i = 0; i < lists.Count; i++)
  611. {
  612. Args args = lists[i];
  613. switch (args.Key)
  614. {
  615. case "Src2":
  616. mat1 = (Mat)args.Value;
  617. break;
  618. default:
  619. break;
  620. }
  621. }
  622. int w = src.Width;
  623. int h = src.Height;
  624. int w1 = mat1.Width;
  625. int h1 = mat1.Height;
  626. int maxw = w;// w > w1 ? w : w1;
  627. int maxh = h;// h > h1 ? h : h1;
  628. //Scalar s = new Scalar(0, 0, 255);//创建一个颜色对象
  629. //mat2 = new Mat(w, h, MatType.CV_16SC1, s);
  630. mat2 = new Mat(maxh, maxw, src.Type(), Scalar.All(/*255*/0));
  631. Rect rect = new Rect(0, 0, Math.Min(w, w1), Math.Min(h, h1));
  632. Mat tempMat = mat1.Clone(rect);
  633. //复制用户选中的区域到原图像
  634. Mat mask = tempMat.CvtColor(ColorConversionCodes.RGBA2GRAY);
  635. Mat pos = new Mat(mat2, rect);
  636. tempMat.CopyTo(pos, mask);
  637. //Cv2.CopyMakeBorder(src, src, 0, maxh - src.Height, 0, maxw - src.Width, BorderTypes.Isolated);
  638. //Cv2.CopyMakeBorder(mat1, mat1, 0, maxh - mat1.Height, 0, maxw - mat1.Width, BorderTypes.Isolated);
  639. Mat tempMat0 = src.Clone(rect);
  640. //复制用户选中的区域到原图像
  641. Mat mask0 = tempMat0.CvtColor(ColorConversionCodes.RGBA2GRAY);
  642. Mat mat0 = new Mat(maxh, maxw, src.Type(), Scalar.All(/*255*/0));
  643. Mat pos0 = new Mat(mat0, rect);
  644. tempMat0.CopyTo(pos0, mask0);
  645. Cv2.Add(/*mat0*/src, mat2, dst);
  646. //Cv2.Normalize(dst, dst, 1, 255, NormTypes.MinMax);
  647. }
  648. catch(Exception e)
  649. {
  650. Console.WriteLine(e.ToString());
  651. src.CopyTo(dst);
  652. }
  653. finally
  654. {
  655. if (src != null) dst.CopyTo(src);
  656. if (dst != null) dst.Dispose();
  657. if (mat2 != null)
  658. {
  659. mat2.Dispose();
  660. }
  661. }
  662. return src;
  663. }
  664. /// <summary>
  665. /// 图像任意角度旋转,进行插值(仿射变换)
  666. /// </summary>
  667. /// <param name="src">源</param>
  668. /// <param name="dst">目标</param>
  669. /// <param name="angle">角度</param>
  670. public static void ImageRotate(Mat src, Mat dst, float angle, InterpolationFlags flags)
  671. {
  672. Mat dst1 = new Mat();
  673. Point2f center = new Point2f(src.Cols / 2, src.Rows / 2);
  674. Mat rot = Cv2.GetRotationMatrix2D(center, -angle, 1);
  675. Size2f s2f = new Size2f(src.Size().Width, src.Size().Height);
  676. Rect box = new RotatedRect(new Point2f(0, 0), s2f, -angle).BoundingRect();
  677. double xx = rot.At<double>(0, 2) + box.Width / 2 - src.Cols / 2;
  678. double zz = rot.At<double>(1, 2) + box.Height / 2 - src.Rows / 2;
  679. rot.Set(0, 2, xx);
  680. rot.Set(1, 2, zz);
  681. //对图片进行仿射变换
  682. Cv2.WarpAffine(src, dst1, rot, box.Size, flags);
  683. dst1.CopyTo(dst);
  684. }
  685. }
  686. }