用CCD批量拍攝了幾百副映像,命名方式為 1.bmp,2.bmp,3.bmp。。。 ,按順序來的,因為有些映像因為模糊或者不符合處理
條件,我手動將其剔除,於是有了剛才提到的問題,現在的大量圖片檔案名稱不連續了,用Matlab處理時不方便。於是寫了個C#小程式用來自動排文映像。
我是菜鳥,呵呵,如果各位高手有更好的解決方案和演算法,多多指教啊。代碼如下
1 using System;
2 using System.Collections.Generic;
3 using System.Text;
4 using System.IO;
5 using System.Windows.Forms;
6 // 自動重新命名
7 namespace renamebmp
8 {
9 class Program
10 {
11 static void Main(string[] args)
12 {
13 int first = 1; //起始圖片編號
14 int last = 301; //結束圖片編號
15 int temp;
16 string path1; //尋找到的第一個空位置
17 string path2; //尋找到的空位置後的最小編號圖片檔案路徑
18 int count = 0; //圖片數量
19 string temppath = "";
20 for (int k = first; k <= last; k++)
21 {
22 temppath = "F:\test6\" + k.ToString() + ".bmp";
23 if (File.Exists(temppath))
24 count++;
25 }
26 for (int i = first; i <= count; i++)
27 {
28 temp = i + 1;
29 path1 = "F:\test6\" + i.ToString() + ".bmp";
30 if (!File.Exists(path1))
31 {
32 path2 = "F:\test6\" + temp.ToString() + ".bmp";
33 while (!File.Exists(path2))
34 {
35 temp++;
36 if (temp == last)
37 {
38 path2 = "F:\test6\" + last.ToString() + ".bmp";
39 break;
40 }
41 path2 = "F:\test6\" + temp.ToString() + ".bmp";
42 }
43 File.Move(path2, path1); // 使用move方法重新命名
44 }
45 }
46 MessageBox.Show("排序完畢,排序後圖片數量為 "+count.ToString());
47 }
48 }
49 }