123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Data;
- using System.Windows.Documents;
- using System.Windows.Input;
- using System.Windows.Media;
- using System.Windows.Media.Imaging;
- using System.Windows.Navigation;
- using System.Windows.Shapes;
- using System.Threading;
- using System.Windows.Threading;
- using System.IO;
- using System.Drawing;
- using Microsoft.Win32;
- namespace SegImgs
- {
- enum EnumAlgoType
- {
- CvCoreDll,
- CvCoreDll_2,
- }
- /// <summary>
- /// Interaction logic for MainWindow.xaml
- /// </summary>
- public partial class MainWindow : Window
- {
- private List<ImgInfoForSeg> _datas = new List<ImgInfoForSeg>();
- private string _origImgFolder;
- private string _dstImgFolder;
- private List<string> _imgs = new List<string>();
- private Queue<DirectoryInfo> _foldersToSearch = new Queue<DirectoryInfo>();
- private BitmapImage _imgorig;
- private BitmapImage _imgcrop;
- public delegate void deleMethod();
- public MainWindow()
- {
- InitializeComponent();
- _imgorig = null;
- _imgcrop = null;
- ImgOrig.Source = _imgorig;
- ImgCrop.Source = _imgcrop;
- }
- private void CropImgsInFolderClicked(object sender, RoutedEventArgs e)
- {
- try
- {
- System.Windows.Forms.FolderBrowserDialog origFolderD = new System.Windows.Forms.FolderBrowserDialog();
- origFolderD.Description = "请选择需要批量裁图的文件夹";
- if (origFolderD.ShowDialog() == System.Windows.Forms.DialogResult.OK)
- {
- if (string.IsNullOrEmpty(origFolderD.SelectedPath))
- {
- MessageBox.Show("待裁切的文件夹不能为空!");
- return;
- }
- _origImgFolder = origFolderD.SelectedPath;
- System.Windows.Forms.FolderBrowserDialog dstFolderD = new System.Windows.Forms.FolderBrowserDialog();
- dstFolderD.Description = "请选择保存裁切后图像的文件夹";
- if (dstFolderD.ShowDialog() == System.Windows.Forms.DialogResult.OK)
- {
- if (string.IsNullOrEmpty(dstFolderD.SelectedPath))
- {
- MessageBox.Show("保存裁切后图像的文件夹不能为空!");
- return;
- }
- _dstImgFolder = dstFolderD.SelectedPath;
- EnumAlgoType algo = EnumAlgoType.CvCoreDll;
- if (RadioBtnAlgoOpenCV.IsChecked ?? false)
- {
- algo = EnumAlgoType.CvCoreDll;
- }
- else if(RadioBtnAlgoOpenCVAnother.IsChecked ?? false)
- {
- algo = EnumAlgoType.CvCoreDll_2;
- }
- DoBatchCrop(algo);
- }
- }
- }
- catch (Exception excep)
- {
- MessageBox.Show("批量裁切文件夹中的图片时发生错误:" + excep.Message);
- }
- }
- private void CropOneImgClicked(object sender, RoutedEventArgs e)
- {
- try
- {
- OpenFileDialog openFileD = new OpenFileDialog();
- openFileD.Filter = "图片文件|*.png;*.bmp;*.jpg;*.jpeg;*.tiff";
- openFileD.Multiselect = false;
- openFileD.Title = "选择图像";
- if (openFileD.ShowDialog() ?? false)
- {
- Bitmap img = new Bitmap(openFileD.FileName, true);
- _imgorig = img.BitmapToBitmapImage();
- _imgcrop = null;
- ImgOrig.Source = _imgorig;
- ImgCrop.Source = _imgcrop;
- System.Drawing.Rectangle rect = System.Drawing.Rectangle.Empty;
- EnumAlgoType algo = EnumAlgoType.CvCoreDll;
- if (RadioBtnAlgoOpenCV.IsChecked ?? false)
- {
- algo = EnumAlgoType.CvCoreDll;
- }
- else if (RadioBtnAlgoOpenCVAnother.IsChecked ?? false)
- {
- algo = EnumAlgoType.CvCoreDll_2;
- }
- if (algo == EnumAlgoType.CvCoreDll)
- {
- if (!img.CropWithCVCore(out rect))
- {
- img.Dispose();
- MessageBox.Show("裁切失败");
- return;
- }
- }
- if (algo == EnumAlgoType.CvCoreDll_2)
- {
- if (!img.CropWithCvAlgorithm2(out rect))
- {
- img.Dispose();
- MessageBox.Show("裁切失败");
- return;
- }
- }
- Bitmap dstimg = new Bitmap(rect.Width, rect.Height, img.PixelFormat);
- using (var g = Graphics.FromImage(dstimg))
- {
- g.DrawImage(img, new System.Drawing.Rectangle(0, 0, rect.Width, rect.Height),
- new System.Drawing.Rectangle(rect.Left, rect.Top, rect.Width, rect.Height),
- GraphicsUnit.Pixel);
- g.Dispose();
- }
- _imgcrop = dstimg.BitmapToBitmapImage();
- ImgCrop.Source = _imgcrop;
- dstimg.Dispose();
- img.Dispose();
- }
- }
- catch (Exception excep)
- {
- MessageBox.Show("裁切一幅图时发生错误:" + excep.Message);
- }
- }
- private void DoBatchCrop(EnumAlgoType algoType)
- {
- ThreadPool.QueueUserWorkItem((e) =>
- {
- DirectoryInfo folder = new DirectoryInfo(_origImgFolder);
- foreach (FileInfo file in folder.GetFiles())
- {
- if (file.Extension == ".png" || file.Extension == ".jpg" || file.Extension == ".bmp" || file.Extension == ".tiff")
- {
- Bitmap img = new Bitmap(file.FullName, true);
- _imgorig = img.BitmapToBitmapImage();
- _imgcrop = null;
- Dispatcher.BeginInvoke(DispatcherPriority.Normal, new Action(() =>
- {
- ImgOrig.Source = _imgorig;
- ImgCrop.Source = _imgcrop;
- }));
- System.Drawing.Rectangle rect = System.Drawing.Rectangle.Empty;
- if (algoType == EnumAlgoType.CvCoreDll)
- {
- if (!img.CropWithCVCore(out rect))
- {
- img.Dispose();
- continue;
- }
- }
- if(algoType == EnumAlgoType.CvCoreDll_2)
- {
- if (!img.CropWithCvAlgorithm2(out rect))
- {
- img.Dispose();
- continue;
- }
- }
-
- Bitmap dstimg = new Bitmap(rect.Width, rect.Height, img.PixelFormat);
- using (var g = Graphics.FromImage(dstimg))
- {
- g.DrawImage(img, new System.Drawing.Rectangle(0, 0, rect.Width, rect.Height),
- new System.Drawing.Rectangle(rect.Left, rect.Top, rect.Width, rect.Height),
- GraphicsUnit.Pixel);
- g.Dispose();
- }
- dstimg.Save(_dstImgFolder + "\\" + System.IO.Path.GetFileNameWithoutExtension(file.Name) +".jpg",System.Drawing.Imaging.ImageFormat.Jpeg);
- _imgcrop = dstimg.BitmapToBitmapImage();
- Dispatcher.BeginInvoke(DispatcherPriority.Normal, new Action(() =>
- {
- ImgCrop.Source = _imgcrop;
- }));
- dstimg.Dispose();
- img.Dispose();
- }
- }
- MessageBox.Show("裁切完毕!");
- });
- }
- }
- }
|