The OpenCV image file reads the programming instance in bulk

  • 2020-06-01 10:46:14
  • OfStack

Batch reading of image files for OpenCV programming instances.

This blog is an excerpt from chapter 2.4 of OpenCV image processing programming example. Please refer to this book for more details.

In image sequence processing, we often need to read every image in the folder and then analyze it, so we need to discuss the sequential and irregular file names separately. For the case of continuous file names, file reading is much easier, you can use the sprintf function to continuously read the image sequence under the same folder in the window, and for the case of no rules can be based on C++ WIN32_ FIND_DATA file reading.

2.4.6 image batch reading -- rules

The file name read in sequence is shown in code 2-32.


 //  Function: code  2-32  File name continuous case 
 //  Author: zhu wei  zhu1988wei@163.com
 //  Source:" OpenCV Example of image processing programming 
 //  Blog: http://blog.csdn.net/zhuwei1988
 //  Update: 2016-8-1
 //  Note: all rights reserved. Please contact the author for quotation or excerpt. // 
 #include <iostream>
 #include <stdio.h>
 #include <stdlib.h>
 #include <opencv2/highgui/highgui.hpp>
 #include <opencv2/imgproc/imgproc.hpp>
 using namespace cv;
 using namespace std;
 int main()
 {
 //  Define correlation parameters 
 const int num = 4;
 char fileName[50];
 char windowName[50];
 cv::Mat srcImage;
 for (int i = 1; i <= num; i++)
 {
 // sprintf Reads the sequence of images under the specified path 
 sprintf_s(fileName, "..\\images\\test\\1 (%d).jpg", i);
 sprintf_s(windowName, "NO%d", i);
 //  Read by image filename 
 srcImage = cv::imread(fileName);
 if (!srcImage.data)
 {
 std::cout << "No data!" << std::endl;
 return -1;
 }
 cv::namedWindow(windowName);
 cv::imshow(windowName, srcImage);
 std::cout << "NO: " << i << std::endl;
 //cv::waitKey(0);
 /*  There you can add processing steps  */
 }
 cv::waitKey(0);
 return 0;
 }

The code in line 16 USES sprintf to convert the corresponding image file path to char*. When the file name is continuous, all image files in the folder can be selected and renamed with the right mouse button. After typing 1, all files in the folder are automatically named 1 (k). You can then read in bulk according to this method.

2.4.7 batch image reading -- no rules

The file name is read without rules as shown in code 2-33.


 //  Function: code  2-33  File name read without rules 
 //  Author: zhu wei  zhu1988wei@163.com
 //  Source:" OpenCV Example of image processing programming 
 //  Blog: http://blog.csdn.net/zhuwei1988
 //  Update: 2016-8-1
 //  Note: all rights reserved. Please contact the author for quotation or excerpt. // 
 #include <opencv2/core/core.hpp>
 #include <opencv2/highgui/highgui.hpp>
 #include <opencv2/imgproc/imgproc.hpp>
 #include <iostream>
 #include <stdio.h>
 #include <windows.h>
 using namespace std;
 // LPCWSTR turn string
 std::string WChar2Ansi(LPCWSTR pwszSrc)
 {
 int nLen = WideCharToMultiByte(CP_ACP, 0, pwszSrc, -1, NULL, 0, NULL, NULL);

 if (nLen <= 0) return std::string("");

 char* pszDst = new char[nLen];
 if (NULL == pszDst) return std::string("");

 WideCharToMultiByte(CP_ACP, 0, pwszSrc, -1, pszDst, nLen, NULL, NULL);
 pszDst[nLen - 1] = 0;

 std::string strTemp(pszDst);
 delete[] pszDst;

 return strTemp;
 }

 //  using winWIN32_FIND_DATA Reads the file name under the file 
 void readImgNamefromFile(char* fileName, vector <string> &imgNames)
 {
 // vector reset   Parameter Settings 
 imgNames.clear();
 WIN32_FIND_DATA file;
 int i = 0;
 char tempFilePath[MAX_PATH + 1];
 char tempFileName[50];
 //  Convert input file name 
 sprintf_s(tempFilePath, "%s/*", fileName);
 //  Multibyte conversion 
 WCHAR wstr[MAX_PATH] = { 0 };
 MultiByteToWideChar(CP_ACP, 0, tempFilePath, -1, wstr, sizeof(wstr));
 //  Find the relevant properties of the file to be operated on WIN32_FIND_DATA
 HANDLE handle = FindFirstFile(wstr, &file);
 if (handle != INVALID_HANDLE_VALUE)
 {
  FindNextFile(handle, &file);
  FindNextFile(handle, &file);
  //  Loop through to get all the file names of the folder  
  do
  {
   sprintf(tempFileName, "%s", fileName);
   imgNames.push_back(WChar2Ansi(file.cFileName));
   imgNames[i].insert(0, tempFileName);
   i++;
  } while (FindNextFile(handle, &file));
 }
 FindClose(handle);
 }
 int main()
 {
 //  Sets the path to read into the image sequence folder 
 char* fileName = "..\\images\\test\\";
 std::vector <string> imgNames;
 //  Gets all file names in the corresponding folder 
 readImgNamefromFile(fileName, imgNames);
 //  Traverse all file names in the corresponding folder 
 for (int i = 0; i < imgNames.size(); i++)
 {
  cv::Mat img = cv::imread(imgNames[i]);
  if (!img.data)
   return -1;
  /*  Image processing algorithms can be added code*/
  cv::imshow("im", img);
  cv::waitKey(0);
 }
 return 0;
 }

Using winWIN32_FIND_DATA to read the files under the folder: first, convert the folder name, using FindFirstFile to get a handle to the current folder name; Then it traverses all the files under the name of the current folder, and converts all the obtained file names and assigns them to the image file vector. Finally, after traversing all the files under the current file, the index name of the corresponding image file is generated, which is used for reading all image files in the folder. After reading a single image file, relevant image processing operations can be carried out.


Related articles: