MainWindow.xaml.cs 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Windows;
  7. using System.Windows.Controls;
  8. using System.Windows.Data;
  9. using System.Windows.Documents;
  10. using System.Windows.Input;
  11. using System.Windows.Media;
  12. using System.Windows.Media.Imaging;
  13. using System.Windows.Navigation;
  14. using System.Windows.Shapes;
  15. using System.Threading;
  16. using System.Windows.Threading;
  17. using System.IO;
  18. using System.Drawing;
  19. using Microsoft.Win32;
  20. namespace SegImgs
  21. {
  22. enum EnumAlgoType
  23. {
  24. CvCoreDll,
  25. CvCoreDll_2,
  26. }
  27. /// <summary>
  28. /// Interaction logic for MainWindow.xaml
  29. /// </summary>
  30. public partial class MainWindow : Window
  31. {
  32. private List<ImgInfoForSeg> _datas = new List<ImgInfoForSeg>();
  33. private string _origImgFolder;
  34. private string _dstImgFolder;
  35. private List<string> _imgs = new List<string>();
  36. private Queue<DirectoryInfo> _foldersToSearch = new Queue<DirectoryInfo>();
  37. private BitmapImage _imgorig;
  38. private BitmapImage _imgcrop;
  39. public delegate void deleMethod();
  40. public MainWindow()
  41. {
  42. InitializeComponent();
  43. _imgorig = null;
  44. _imgcrop = null;
  45. ImgOrig.Source = _imgorig;
  46. ImgCrop.Source = _imgcrop;
  47. }
  48. private void CropImgsInFolderClicked(object sender, RoutedEventArgs e)
  49. {
  50. try
  51. {
  52. System.Windows.Forms.FolderBrowserDialog origFolderD = new System.Windows.Forms.FolderBrowserDialog();
  53. origFolderD.Description = "请选择需要批量裁图的文件夹";
  54. if (origFolderD.ShowDialog() == System.Windows.Forms.DialogResult.OK)
  55. {
  56. if (string.IsNullOrEmpty(origFolderD.SelectedPath))
  57. {
  58. MessageBox.Show("待裁切的文件夹不能为空!");
  59. return;
  60. }
  61. _origImgFolder = origFolderD.SelectedPath;
  62. System.Windows.Forms.FolderBrowserDialog dstFolderD = new System.Windows.Forms.FolderBrowserDialog();
  63. dstFolderD.Description = "请选择保存裁切后图像的文件夹";
  64. if (dstFolderD.ShowDialog() == System.Windows.Forms.DialogResult.OK)
  65. {
  66. if (string.IsNullOrEmpty(dstFolderD.SelectedPath))
  67. {
  68. MessageBox.Show("保存裁切后图像的文件夹不能为空!");
  69. return;
  70. }
  71. _dstImgFolder = dstFolderD.SelectedPath;
  72. EnumAlgoType algo = EnumAlgoType.CvCoreDll;
  73. if (RadioBtnAlgoOpenCV.IsChecked ?? false)
  74. {
  75. algo = EnumAlgoType.CvCoreDll;
  76. }
  77. else if(RadioBtnAlgoOpenCVAnother.IsChecked ?? false)
  78. {
  79. algo = EnumAlgoType.CvCoreDll_2;
  80. }
  81. DoBatchCrop(algo);
  82. }
  83. }
  84. }
  85. catch (Exception excep)
  86. {
  87. MessageBox.Show("批量裁切文件夹中的图片时发生错误:" + excep.Message);
  88. }
  89. }
  90. private void CropOneImgClicked(object sender, RoutedEventArgs e)
  91. {
  92. try
  93. {
  94. OpenFileDialog openFileD = new OpenFileDialog();
  95. openFileD.Filter = "图片文件|*.png;*.bmp;*.jpg;*.jpeg;*.tiff";
  96. openFileD.Multiselect = false;
  97. openFileD.Title = "选择图像";
  98. if (openFileD.ShowDialog() ?? false)
  99. {
  100. Bitmap img = new Bitmap(openFileD.FileName, true);
  101. _imgorig = img.BitmapToBitmapImage();
  102. _imgcrop = null;
  103. ImgOrig.Source = _imgorig;
  104. ImgCrop.Source = _imgcrop;
  105. System.Drawing.Rectangle rect = System.Drawing.Rectangle.Empty;
  106. EnumAlgoType algo = EnumAlgoType.CvCoreDll;
  107. if (RadioBtnAlgoOpenCV.IsChecked ?? false)
  108. {
  109. algo = EnumAlgoType.CvCoreDll;
  110. }
  111. else if (RadioBtnAlgoOpenCVAnother.IsChecked ?? false)
  112. {
  113. algo = EnumAlgoType.CvCoreDll_2;
  114. }
  115. if (algo == EnumAlgoType.CvCoreDll)
  116. {
  117. if (!img.CropWithCVCore(out rect))
  118. {
  119. img.Dispose();
  120. MessageBox.Show("裁切失败");
  121. return;
  122. }
  123. }
  124. if (algo == EnumAlgoType.CvCoreDll_2)
  125. {
  126. if (!img.CropWithCvAlgorithm2(out rect))
  127. {
  128. img.Dispose();
  129. MessageBox.Show("裁切失败");
  130. return;
  131. }
  132. }
  133. Bitmap dstimg = new Bitmap(rect.Width, rect.Height, img.PixelFormat);
  134. using (var g = Graphics.FromImage(dstimg))
  135. {
  136. g.DrawImage(img, new System.Drawing.Rectangle(0, 0, rect.Width, rect.Height),
  137. new System.Drawing.Rectangle(rect.Left, rect.Top, rect.Width, rect.Height),
  138. GraphicsUnit.Pixel);
  139. g.Dispose();
  140. }
  141. _imgcrop = dstimg.BitmapToBitmapImage();
  142. ImgCrop.Source = _imgcrop;
  143. dstimg.Dispose();
  144. img.Dispose();
  145. }
  146. }
  147. catch (Exception excep)
  148. {
  149. MessageBox.Show("裁切一幅图时发生错误:" + excep.Message);
  150. }
  151. }
  152. private void DoBatchCrop(EnumAlgoType algoType)
  153. {
  154. ThreadPool.QueueUserWorkItem((e) =>
  155. {
  156. DirectoryInfo folder = new DirectoryInfo(_origImgFolder);
  157. foreach (FileInfo file in folder.GetFiles())
  158. {
  159. if (file.Extension == ".png" || file.Extension == ".jpg" || file.Extension == ".bmp" || file.Extension == ".tiff")
  160. {
  161. Bitmap img = new Bitmap(file.FullName, true);
  162. _imgorig = img.BitmapToBitmapImage();
  163. _imgcrop = null;
  164. Dispatcher.BeginInvoke(DispatcherPriority.Normal, new Action(() =>
  165. {
  166. ImgOrig.Source = _imgorig;
  167. ImgCrop.Source = _imgcrop;
  168. }));
  169. System.Drawing.Rectangle rect = System.Drawing.Rectangle.Empty;
  170. if (algoType == EnumAlgoType.CvCoreDll)
  171. {
  172. if (!img.CropWithCVCore(out rect))
  173. {
  174. img.Dispose();
  175. continue;
  176. }
  177. }
  178. if(algoType == EnumAlgoType.CvCoreDll_2)
  179. {
  180. if (!img.CropWithCvAlgorithm2(out rect))
  181. {
  182. img.Dispose();
  183. continue;
  184. }
  185. }
  186. Bitmap dstimg = new Bitmap(rect.Width, rect.Height, img.PixelFormat);
  187. using (var g = Graphics.FromImage(dstimg))
  188. {
  189. g.DrawImage(img, new System.Drawing.Rectangle(0, 0, rect.Width, rect.Height),
  190. new System.Drawing.Rectangle(rect.Left, rect.Top, rect.Width, rect.Height),
  191. GraphicsUnit.Pixel);
  192. g.Dispose();
  193. }
  194. dstimg.Save(_dstImgFolder + "\\" + System.IO.Path.GetFileNameWithoutExtension(file.Name) +".jpg",System.Drawing.Imaging.ImageFormat.Jpeg);
  195. _imgcrop = dstimg.BitmapToBitmapImage();
  196. Dispatcher.BeginInvoke(DispatcherPriority.Normal, new Action(() =>
  197. {
  198. ImgCrop.Source = _imgcrop;
  199. }));
  200. dstimg.Dispose();
  201. img.Dispose();
  202. }
  203. }
  204. MessageBox.Show("裁切完毕!");
  205. });
  206. }
  207. }
  208. }