EdgeDetectionIntent.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  1. using OpenCvSharp;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. namespace PaintDotNet.Adjust
  8. {
  9. public class EdgeDetectionIntent
  10. {
  11. /// <summary>
  12. /// Log
  13. /// </summary>
  14. /// <param name="src"></param>
  15. /// <param name="dst"></param>
  16. public static Mat ImageLog(Mat src)
  17. {
  18. Mat gray = null;
  19. Mat gray1 = null;
  20. Mat dstImage = null;
  21. try
  22. {
  23. gray = new Mat();
  24. //将原图像转换为灰度图像
  25. Cv2.CvtColor(src, gray, ColorConversionCodes.BGR2GRAY);
  26. gray1 = new Mat();
  27. //先做高斯模糊
  28. Cv2.GaussianBlur(gray, gray1, new OpenCvSharp.Size(3, 3), 0);
  29. dstImage = new Mat();
  30. //拉普拉斯变换
  31. Cv2.Laplacian(gray1, dstImage, MatType.CV_16S, 3);
  32. Cv2.ConvertScaleAbs(dstImage, dstImage);
  33. dstImage.CopyTo(src);
  34. return src;
  35. }
  36. catch (Exception ex)
  37. {
  38. throw ex;
  39. }
  40. finally
  41. {
  42. if (gray != null) gray.Dispose();
  43. if (gray1 != null) gray1.Dispose();
  44. if (dstImage != null) dstImage.Dispose();
  45. GC.Collect();
  46. }
  47. }
  48. /// <summary>
  49. /// Canny
  50. /// </summary>
  51. /// <param name="src"></param>
  52. /// <returns></returns>
  53. public static Mat ImageCanny(Mat src, List<Base.Args> lists)
  54. {
  55. Mat src_gray = null;
  56. try
  57. {
  58. //过滤尺寸(最大/最小),需要填写数值
  59. int size1 = 20;
  60. int size2 = 100;
  61. //西格玛值
  62. double SigmaValue = 1.5;
  63. foreach (Base.Args args in lists)
  64. {
  65. switch (args.key)
  66. {
  67. case "KernelSize1":
  68. size1 = int.Parse(args.Value.ToString());
  69. break;
  70. case "KernelSize2":
  71. size2 = int.Parse(args.Value.ToString());
  72. break;
  73. case "Sigma":
  74. SigmaValue = double.Parse(args.Value.ToString());
  75. break;
  76. }
  77. }
  78. src_gray = new Mat();
  79. //该算法对噪声敏感,必须降噪
  80. Cv2.GaussianBlur(src, src_gray, new OpenCvSharp.Size(3, 3), SigmaValue);
  81. //将原图像转换为灰度图像
  82. Cv2.CvtColor(src_gray, src_gray, ColorConversionCodes.BGR2GRAY);
  83. Cv2.Canny(src_gray, src_gray, size1, size2);
  84. src_gray.CopyTo(src);
  85. return src;
  86. }
  87. catch (Exception ex)
  88. {
  89. throw ex;
  90. }
  91. finally {
  92. if(src_gray!=null) src_gray.Dispose();
  93. GC.Collect();
  94. }
  95. }
  96. /// <summary>
  97. /// Sobel
  98. /// </summary>
  99. /// <param name="src"></param>
  100. /// <param name="dst"></param>
  101. public static Mat ImageSobel(Mat src)
  102. {
  103. Mat gray = null, xgrad = null, ygrad = null, output = null;
  104. try
  105. {
  106. //转为灰度
  107. gray = new Mat();
  108. Cv2.CvtColor(src, gray, ColorConversionCodes.BGR2GRAY);
  109. MatType m = src.Type();
  110. //求 X 和 Y 方向的梯度 Sobel and scharr
  111. xgrad = new Mat();
  112. ygrad = new Mat();
  113. Cv2.Sobel(gray, xgrad, MatType.CV_16S, 1, 0, 3);
  114. Cv2.Sobel(gray, ygrad, MatType.CV_16S, 0, 1, 3);
  115. Cv2.ConvertScaleAbs(xgrad, xgrad);//缩放、计算绝对值并将结果转换为8位。不做转换的化显示不了,显示图相只能是8U类型
  116. Cv2.ConvertScaleAbs(ygrad, ygrad);
  117. //加强边缘检测
  118. //Cv2.Scharr(gray, xgrad, -1, 1, 0, 3);
  119. //Cv2.Scharr(gray, ygrad, -1, 0, 1, 3);
  120. output = new Mat(xgrad.Size(), xgrad.Type());
  121. //图像混合相加(基于权重 0.5)不精确
  122. //Cv2.AddWeighted(xgrad, 0.5, ygrad, 0.5, 0, output);
  123. //基于 算法 G=|Gx|+|Gy|
  124. int width = xgrad.Cols;
  125. int hight = xgrad.Rows;
  126. //基于 G= (Gx*Gx +Gy*Gy)的开方根
  127. for (int x = 0; x < hight; x++)
  128. {
  129. for (int y = 0; y < width; y++)
  130. {
  131. int xg = xgrad.At<byte>(x, y);
  132. int yg = ygrad.At<byte>(x, y);
  133. double v1 = Math.Pow(xg, 2);
  134. double v2 = Math.Pow(yg, 2);
  135. int val = (int)Math.Sqrt(v1 + v2);
  136. if (val > 255) //确保像素值在 0 -- 255 之间
  137. {
  138. val = 255;
  139. }
  140. if (val < 0)
  141. {
  142. val = 0;
  143. }
  144. byte xy = (byte)val;
  145. output.Set<byte>(x, y, xy);
  146. }
  147. }
  148. output.CopyTo(src);
  149. return src;
  150. }
  151. catch (Exception ex)
  152. {
  153. throw ex;
  154. }
  155. finally
  156. {
  157. if (gray != null) gray.Dispose();
  158. if (xgrad != null) gray.Dispose();
  159. if (ygrad != null) gray.Dispose();
  160. if (output != null) gray.Dispose();
  161. GC.Collect();
  162. }
  163. }
  164. /// <summary>
  165. /// Kirsch
  166. /// </summary>
  167. /// <param name="src"></param>
  168. /// <param name="dst"></param>
  169. public static Mat ImageKirsch(Mat src)
  170. {
  171. Mat src_gray = null;
  172. Mat dst = null;
  173. try
  174. {
  175. dst = new Mat();
  176. src_gray = new Mat();
  177. Cv2.CvtColor(src, src_gray, ColorConversionCodes.BGR2GRAY);
  178. List<Mat> dstImages = new List<Mat>();
  179. for (int i = 0; i < 8; i++)
  180. {
  181. dstImages.Add(new Mat());
  182. }
  183. InputArray kernel1 = InputArray.Create<int>(new int[3, 3] { { 5, 5, 5 }, { -3, 0, -3 }, { -3, -3, -3 } });
  184. InputArray kernel2 = InputArray.Create<int>(new int[3, 3] { { -3, 5, 5 }, { -3, 0, 5 }, { -3, -3, -3 } });
  185. InputArray kernel3 = InputArray.Create<int>(new int[3, 3] { { -3, -3, 5 }, { -3, 0, 5 }, { -3, -3, 5 } });
  186. InputArray kernel4 = InputArray.Create<int>(new int[3, 3] { { -3, -3, -3 }, { -3, 0, 5 }, { -3, 5, 5 } });
  187. InputArray kernel5 = InputArray.Create<int>(new int[3, 3] { { -3, -3, -3 }, { -3, 0, -3 }, { 5, 5, 5 } });
  188. InputArray kernel6 = InputArray.Create<int>(new int[3, 3] { { -3, -3, -3 }, { 5, 0, -3 }, { 5, 5, -3 } });
  189. InputArray kernel7 = InputArray.Create<int>(new int[3, 3] { { 5, -3, -3 }, { 5, 0, -3 }, { 5, -3, -3 } });
  190. InputArray kernel8 = InputArray.Create<int>(new int[3, 3] { { 5, 5, -3 }, { 5, 0, -3 }, { -3, -3, -3 } });
  191. Cv2.Filter2D(src_gray, dstImages[0], MatType.CV_8U, kernel1);
  192. Cv2.Filter2D(src_gray, dstImages[1], MatType.CV_8U, kernel2);
  193. Cv2.Filter2D(src_gray, dstImages[2], MatType.CV_8U, kernel3);
  194. Cv2.Filter2D(src_gray, dstImages[3], MatType.CV_8U, kernel4);
  195. Cv2.Filter2D(src_gray, dstImages[4], MatType.CV_8U, kernel5);
  196. Cv2.Filter2D(src_gray, dstImages[5], MatType.CV_8U, kernel6);
  197. Cv2.Filter2D(src_gray, dstImages[6], MatType.CV_8U, kernel7);
  198. Cv2.Filter2D(src_gray, dstImages[7], MatType.CV_8U, kernel8);
  199. for (int i = 0; i < dstImages.Count(); i++)
  200. {
  201. dstImages[i] = Cv2.Abs(dstImages[i]);
  202. }
  203. dst = dstImages[0];
  204. for (int i = 1; i < dstImages.Count(); i++)
  205. {
  206. Cv2.Max(dst, dstImages[i], dst);
  207. }
  208. dst.CopyTo(src);
  209. return src;
  210. }
  211. catch(Exception e)
  212. {
  213. throw e;
  214. }
  215. finally
  216. {
  217. if (src_gray != null) src_gray.Dispose();
  218. if (dst != null) dst.Dispose();
  219. GC.Collect();
  220. }
  221. }
  222. /// <summary>
  223. /// Prewitt
  224. /// </summary>
  225. /// <param name="src"></param>
  226. /// <param name="dst"></param>
  227. public static Mat ImagePrewitt(Mat src)
  228. {
  229. Mat grad_x = null, grad_y = null, gray = null, grad = null,
  230. abs_grad_x =null, abs_grad_y=null;
  231. try
  232. {
  233. grad_x = new Mat();
  234. grad_y = new Mat();
  235. abs_grad_x = new Mat();
  236. abs_grad_y = new Mat();
  237. gray = new Mat();
  238. //将原图像转换为灰度图像
  239. Cv2.CvtColor(src, gray, ColorConversionCodes.BGR2GRAY);
  240. //定义卷积核
  241. InputArray kernelx = InputArray.Create<double>(new double[3, 3] { { 1, 0, -1 }, { 1, 0, -1 }, { 1, 0, -1 } });
  242. InputArray kernely = InputArray.Create<double>(new double[3, 3] { { -1, -1, -1 }, { 0, 0, 0 }, { 1, 1, 1 } });
  243. //卷积运算
  244. Cv2.Filter2D(gray, grad_x, MatType.CV_16S, kernelx);
  245. Cv2.Filter2D(gray, grad_y, MatType.CV_16S, kernely);
  246. //转为8位
  247. Cv2.ConvertScaleAbs(grad_x, abs_grad_x);
  248. Cv2.ConvertScaleAbs(grad_y, abs_grad_y);
  249. grad = new Mat(abs_grad_x.Size(), abs_grad_x.Type());
  250. //图像混合相加(基于权重 0.5)不精确
  251. //Cv2.AddWeighted(abs_grad_x, 0.5, abs_grad_y, 0.5, 0, grad);
  252. //基于 算法 G=|Gx|+|Gy|
  253. int width = abs_grad_x.Cols;
  254. int height = abs_grad_x.Rows;
  255. //基于 G= (Gx*Gx +Gy*Gy)的开方根
  256. for (int x = 0; x < height; x++)
  257. {
  258. for (int y = 0; y < width; y++)
  259. {
  260. int xg = abs_grad_x.At<byte>(x, y);
  261. int yg = abs_grad_y.At<byte>(x, y);
  262. //int val = (int)Math.Sqrt(xg * xg + yg * yg);
  263. grad.Set<int>(x, y, Math.Max(xg, yg));
  264. }
  265. }
  266. grad.CopyTo(src);
  267. return src;
  268. }
  269. catch(Exception e)
  270. {
  271. throw e;
  272. }
  273. finally
  274. {
  275. if (grad_x != null) grad_x.Dispose();
  276. if (grad_y != null) grad_y.Dispose();
  277. if (gray != null) gray.Dispose();
  278. if (grad != null) grad.Dispose();
  279. if (abs_grad_x != null) abs_grad_x.Dispose();
  280. if (abs_grad_y != null) abs_grad_y.Dispose();
  281. GC.Collect();
  282. }
  283. }
  284. /// <summary>
  285. /// Roberts
  286. /// </summary>
  287. /// <param name="src">源</param>
  288. /// <param name="dst">目标</param>
  289. public static Mat ImageRoberts(Mat src)
  290. {
  291. Mat output = null;
  292. Mat src_gray = null;
  293. Mat dstImageX = null;
  294. Mat dstImageY = null;
  295. try
  296. {
  297. src_gray = new Mat();
  298. Cv2.CvtColor(src, src_gray, ColorConversionCodes.BGR2GRAY);
  299. dstImageX = src_gray.Clone();
  300. dstImageY = src_gray.Clone();
  301. //Robert算子 X向量
  302. InputArray kernelRX = InputArray.Create<int>(new int[2, 2] { { -1, 0 }, { 0, 1 } });
  303. Cv2.Filter2D(src_gray, dstImageX, MatType.CV_16S, kernelRX, new OpenCvSharp.Point(-1, -1), 0);
  304. ////Robert算子 Y向量
  305. InputArray kernelRY = InputArray.Create<int>(new int[2, 2] { { 0, -1 }, { 1, 0 } });
  306. Cv2.Filter2D(src_gray, dstImageY, MatType.CV_16S, kernelRY, new OpenCvSharp.Point(-1, -1), 0);
  307. Cv2.ConvertScaleAbs(dstImageX, dstImageX);
  308. Cv2.ConvertScaleAbs(dstImageY, dstImageY);
  309. output = new Mat(dstImageX.Size(), dstImageY.Type());
  310. //图像混合相加(基于权重 0.5)不精确
  311. //Cv2.AddWeighted(dstImageX, 0.5, dstImageY, 0.5, 0, output);
  312. //基于 算法 G=|Gx|+|Gy|
  313. int width = dstImageX.Cols;
  314. int height = dstImageY.Rows;
  315. //基于 G= (Gx*Gx +Gy*Gy)的开方根
  316. for (int x = 0; x < height; x++)
  317. {
  318. for (int y = 0; y < width; y++)
  319. {
  320. int xg = dstImageX.At<byte>(x, y);
  321. int yg = dstImageY.At<byte>(x, y);
  322. double v1 = Math.Pow(xg, 2);
  323. double v2 = Math.Pow(yg, 2);
  324. int val = (int)Math.Sqrt(v1 + v2);
  325. if (val > 255) //确保像素值在 0 -- 255 之间
  326. {
  327. val = 255;
  328. }
  329. if (val < 0)
  330. {
  331. val = 0;
  332. }
  333. byte xy = (byte)val;
  334. output.Set<byte>(x, y, xy);
  335. }
  336. }
  337. output.CopyTo(src);
  338. return src;
  339. }
  340. catch (Exception ex)
  341. {
  342. throw ex;
  343. }
  344. finally
  345. {
  346. if (src_gray != null) src_gray.Dispose();
  347. if (dstImageX != null) dstImageX.Dispose();
  348. if (dstImageY != null) dstImageY.Dispose();
  349. if (output != null) output.Dispose();
  350. GC.Collect();
  351. }
  352. }
  353. /// <summary>
  354. /// Laplacian
  355. /// </summary>
  356. /// <param name="src"></param>
  357. /// <param name="dst"></param>
  358. public static Mat ImageLaplace(Mat src)
  359. {
  360. Mat gray = null, dstImage = null;
  361. try
  362. {
  363. gray = new Mat();
  364. dstImage = new Mat();
  365. Cv2.CvtColor(src, gray, ColorConversionCodes.BGR2GRAY);
  366. Cv2.Laplacian(gray, dstImage, MatType.CV_16S, 1);
  367. Cv2.ConvertScaleAbs(dstImage, dstImage);
  368. dstImage.CopyTo(src);
  369. return src;
  370. }
  371. catch (Exception ex)
  372. {
  373. throw ex;
  374. }
  375. finally
  376. {
  377. if (gray != null) gray.Dispose();
  378. if (dstImage != null) dstImage.Dispose();
  379. GC.Collect();
  380. }
  381. }
  382. }
  383. }