You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

134 lines
5.1 KiB

1 month ago
  1. using AForge.Controls;
  2. using OpenCvSharp;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.ComponentModel;
  6. using System.Data;
  7. using System.Drawing;
  8. using System.Linq;
  9. using System.Text;
  10. using System.Threading.Tasks;
  11. using System.Windows.Forms;
  12. namespace ShenTun.ImageCollection.AForgeCamera
  13. {
  14. public partial class FrmComparison : Form
  15. {
  16. private string selectedImagePath1;
  17. private string selectedImagePath2;
  18. public FrmComparison(string path1,string path2)
  19. {
  20. InitializeComponent();
  21. selectedImagePath1 = path1;
  22. selectedImagePath2 = path2;
  23. }
  24. private void Set()
  25. {
  26. //加载两张图片
  27. using (Mat image1 = Cv2.ImRead(selectedImagePath1))
  28. {
  29. using (Mat image2 = Cv2.ImRead(selectedImagePath2))
  30. {
  31. using (Mat result_image = image2.Clone())
  32. {
  33. // 调整图像尺寸使其匹配
  34. if (image1.Size() != image2.Size())
  35. {
  36. Cv2.Resize(image1, image1, image2.Size());
  37. }// 调整图像通道数使其匹配
  38. if (image1.Channels() != image2.Channels())
  39. {
  40. if (image1.Channels() == 1 && image2.Channels() == 3)
  41. {
  42. Cv2.CvtColor(image1, image1, ColorConversionCodes.GRAY2BGR);
  43. }
  44. else if (image1.Channels() == 3 && image2.Channels() == 1)
  45. {
  46. Cv2.CvtColor(image2, image2, ColorConversionCodes.GRAY2BGR);
  47. }
  48. }
  49. //创建一个空的Mat对象用于存储差异
  50. using (Mat difference = new Mat())
  51. {
  52. //计算两张图片的差异
  53. Cv2.Absdiff(image1, image2, difference);
  54. //模糊
  55. Cv2.Blur(difference, difference, new OpenCvSharp.Size(5, 5));
  56. //转换为灰度图
  57. Cv2.CvtColor(difference, difference, ColorConversionCodes.BGR2GRAY);
  58. //膨胀5次
  59. using (Mat kernel = Cv2.GetStructuringElement(MorphShapes.Rect, new OpenCvSharp.Size(5, 5)))
  60. {
  61. Cv2.Dilate(difference, difference, kernel, null, 5);
  62. //二值化
  63. Cv2.Threshold(difference, difference, 20, 200, ThresholdTypes.Binary);
  64. //找出轮廓
  65. OpenCvSharp.Point[][] contours = null;
  66. HierarchyIndex[] hierarchly;
  67. Cv2.FindContours(difference, out contours, out hierarchly, RetrievalModes.External, ContourApproximationModes.ApproxNone, new OpenCvSharp.Point(0, 0));
  68. Rect rect;
  69. for (int i = 0; i < contours.Length; i++)
  70. {
  71. rect = Cv2.BoundingRect(contours[i]);
  72. Cv2.Rectangle(result_image, rect, new Scalar(0, 255, 0), 2, LineTypes.Link8);
  73. }
  74. if (pictureBox.Image != null)
  75. pictureBox.Image.Dispose();
  76. pictureBox.Image = new Bitmap(result_image.ToMemoryStream());
  77. }
  78. }
  79. };
  80. };
  81. }
  82. }
  83. private void loadImage()
  84. {
  85. Image selectedImage = Image.FromFile(selectedImagePath1);
  86. if (pictureBox1.Image != null)
  87. pictureBox1.Image.Dispose();
  88. pictureBox1.Image = selectedImage;
  89. Image selectedImage1 = Image.FromFile(selectedImagePath2);
  90. if (pictureBox2.Image != null)
  91. pictureBox2.Image.Dispose();
  92. pictureBox2.Image = selectedImage1;
  93. }
  94. private void FrmComparison_Load(object sender, EventArgs e)
  95. {
  96. try {
  97. loadImage();
  98. }
  99. catch (Exception ex) {
  100. MessageBox.Show("打开图像比对失败:" + ex.Message);
  101. }
  102. }
  103. private void btnClose_Click(object sender, EventArgs e)
  104. {
  105. if (pictureBox1.Image != null)
  106. pictureBox1.Image.Dispose();
  107. if (pictureBox2.Image != null)
  108. pictureBox2.Image.Dispose();
  109. DialogResult = DialogResult.Cancel;
  110. }
  111. private void btnComp_Click(object sender, EventArgs e)
  112. {
  113. try
  114. {
  115. Set();
  116. }
  117. catch (Exception ex)
  118. {
  119. MessageBox.Show("生成差异图失败:" + ex.Message);
  120. }
  121. }
  122. }
  123. }