本博文摘錄《OpenCV圖像處理編程實例》2.4章節,更詳細的內容請參考本書。
圖書購買地址:
當當:http://product.dangdang.com/23956649.html
京東:http://item.jd.com/11929148.html
2.4.6 圖像批量讀取——規則
在進行圖片序列處理時,我們常常需要讀取文件夾下的每一個圖片,然后再進行分析處理,因此需要對文件名連續及無規則情況分開討論。對於文件名連續的情況,文件讀取就簡單得多,可以利用sprintf函數實現在窗口中連續讀取同一文件夾下的圖片序列,而對於無規則的情況則可以采用基於VC下WIN32_ FIND_DATA文件的讀取方式。文件名連續情況下的讀取如代碼2-32所示。
1 // 功能:代碼 2-32 文件名連續情況下 2 // 作者:朱偉 zhu1988wei@163.com 3 // 來源:《OpenCV圖像處理編程實例》 4 // 博客:http://blog.csdn.net/zhuwei1988 5 // 更新:2016-8-1 6 // 說明:版權所有,引用或摘錄請聯系作者,並按照上面格式注明出處,謝謝。// 7 #include <iostream> 8 #include <stdio.h> 9 #include <stdlib.h> 10 #include <opencv2/highgui/highgui.hpp> 11 #include <opencv2/imgproc/imgproc.hpp> 12 using namespace cv; 13 using namespace std; 14 int main() 15 { 16 // 定義相關參數 17 const int num = 4; 18 char fileName[50]; 19 char windowName[50]; 20 cv::Mat srcImage; 21 for (int i = 1; i <= num; i++) 22 { 23 // sprintf讀入指定路徑下圖片序列 24 sprintf_s(fileName, "..\\images\\test\\1 (%d).jpg", i); 25 sprintf_s(windowName, "NO%d", i); 26 // 按照圖像文件名讀取 27 srcImage = cv::imread(fileName); 28 if (!srcImage.data) 29 { 30 std::cout << "No data!" << std::endl; 31 return -1; 32 } 33 cv::namedWindow(windowName); 34 cv::imshow(windowName, srcImage); 35 std::cout << "NO: " << i << std::endl; 36 //cv::waitKey(0); 37 /* 該處可以添加處理步驟 */ 38 } 39 cv::waitKey(0); 40 return 0; 41 }
第16行代碼利用sprintf將對應的圖像文件路徑轉換為char*,在這種文件名連續的時候可以選中文件夾中的所有圖像文件,然后用鼠標右鍵選定並重命名,鍵入1后,文件夾的所有文件自動命名為1 (k).jpg,其中k取值為1,2……然后就可以根據本方法進行批量讀取了。
2.4.7 圖像批量讀取——無規則
文件名無規則的情況讀取如代碼2-33所示。
1 // 功能:代碼 2-33 文件名無規則情況讀取 2 // 作者:朱偉 zhu1988wei@163.com 3 // 來源:《OpenCV圖像處理編程實例》 4 // 博客:http://blog.csdn.net/zhuwei1988 5 // 更新:2016-8-1 6 // 說明:版權所有,引用或摘錄請聯系作者,並按照上面格式注明出處,謝謝。// 7 #include <opencv2/core/core.hpp> 8 #include <opencv2/highgui/highgui.hpp> 9 #include <opencv2/imgproc/imgproc.hpp> 10 #include <iostream> 11 #include <stdio.h> 12 #include <windows.h> 13 using namespace std; 14 // LPCWSTR轉string 15 std::string WChar2Ansi(LPCWSTR pwszSrc) 16 { 17 int nLen = WideCharToMultiByte(CP_ACP, 0, pwszSrc, -1, NULL, 0, NULL, NULL); 18 19 if (nLen <= 0) return std::string(""); 20 21 char* pszDst = new char[nLen]; 22 if (NULL == pszDst) return std::string(""); 23 24 WideCharToMultiByte(CP_ACP, 0, pwszSrc, -1, pszDst, nLen, NULL, NULL); 25 pszDst[nLen - 1] = 0; 26 27 std::string strTemp(pszDst); 28 delete[] pszDst; 29 30 return strTemp; 31 } 32 33 // 利用winWIN32_FIND_DATA讀取文件下的文件名 34 void readImgNamefromFile(char* fileName, vector <string> &imgNames) 35 { 36 // vector清零 參數設置 37 imgNames.clear(); 38 WIN32_FIND_DATA file; 39 int i = 0; 40 char tempFilePath[MAX_PATH + 1]; 41 char tempFileName[50]; 42 // 轉換輸入文件名 43 sprintf_s(tempFilePath, "%s/*", fileName); 44 // 多字節轉換 45 WCHAR wstr[MAX_PATH] = { 0 }; 46 MultiByteToWideChar(CP_ACP, 0, tempFilePath, -1, wstr, sizeof(wstr)); 47 // 查找該文件待操作文件的相關屬性讀取到WIN32_FIND_DATA 48 HANDLE handle = FindFirstFile(wstr, &file); 49 if (handle != INVALID_HANDLE_VALUE) 50 { 51 FindNextFile(handle, &file); 52 FindNextFile(handle, &file); 53 // 循環遍歷得到文件夾的所有文件名 54 do 55 { 56 sprintf(tempFileName, "%s", fileName); 57 imgNames.push_back(WChar2Ansi(file.cFileName)); 58 imgNames[i].insert(0, tempFileName); 59 i++; 60 } while (FindNextFile(handle, &file)); 61 } 62 FindClose(handle); 63 } 64 int main() 65 { 66 // 設置讀入圖像序列文件夾的路徑 67 char* fileName = "..\\images\\test\\"; 68 std::vector <string> imgNames; 69 // 獲取對應文件夾下所有文件名 70 readImgNamefromFile(fileName, imgNames); 71 // 遍歷對應文件夾下所有文件名 72 for (int i = 0; i < imgNames.size(); i++) 73 { 74 cv::Mat img = cv::imread(imgNames[i]); 75 if (!img.data) 76 return -1; 77 /* 可添加圖像處理算法code*/ 78 cv::imshow("im", img); 79 cv::waitKey(0); 80 } 81 return 0; 82 }
利用winWIN32_FIND_DATA讀取文件夾下文件的思路:首先轉換文件夾名,利用FindFirstFile獲取當前文件夾名的句柄;然后遍歷當前文件夾名下的所有文件,將得到的所有文件名稱轉換后賦值於圖像文件向量;最后遍歷完當前文件下的所有文件,生成相應圖像文件索引名稱,用於文件夾中所有圖像文件的讀取,在讀取單個圖像文件后可進行相關的圖像處理操作。