6

I am new to OpenCV. I want to read XML files in a directory. I am using FindFirstFile, but I am not getting how can I get file names to give as input to cvLoad further. Here is the code I am using:

HANDLE hFind;
WIN32_FIND_DATA FindFileData;
wchar_t* file = L"D:\\zainb_s\\M.phil\\thesis\\dataset\\dataset_3\\RGB_3\\RGB\\s01_e01-   Copy\\1_walking\\depth\\*.xml";
hFind = FindFirstFile(file, &FindFileData);
cout << FindFileData.cFileName[0];
FindClose(hFind);

I want to have filenames in an array to read files further to process.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Zainb
  • 83
  • 1
  • 1
  • 6
  • So your question is about detecting files in a folder? Can you use an additional library like Qt or boost? – Micka Oct 23 '14 at 19:31
  • yes exactly. i want to access files in folder, want to access their names one by one to Load for further processing. How will i use these libraries? as i aid i am very new to Opencv and C – Zainb Oct 23 '14 at 19:43

1 Answers1

13

If you're using a recent version of OpenCV, you're better off avoiding OS-specific methods:

vector<string> fn; // std::string in opencv2.4, but cv::String in 3.0
string path = "e:/code/vlc/faces2/*.png";
cv::glob(path,fn,false);
// Now you got a list of filenames in fn.

(Ohh, and again, avoid deprecated C-API functions like cvLoad like hell, please!!)

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
berak
  • 39,159
  • 9
  • 91
  • 89
  • wow. It is working very fine. thank you so much, you solved my problem so quickly. – Zainb Oct 23 '14 at 20:06
  • Great, didnt know glob yet! – Micka Oct 23 '14 at 20:15
  • @berak cvLoad is the solution i get for my dataset and it is working fine after much of attempts. I need to use this for my data set. Can you please help me for one more thing? This code is working fine, but it must be known to you that cvLoad is expecting a const char * but this code is giving string. can you please help me out what should i do for this? Thank you very much. I am very hopeful that my problem would be solved here. – Zainb Oct 24 '14 at 04:18
  • 1
    each string has a c_str(); member that returns a const char*. – berak Oct 24 '14 at 05:55
  • hey @berak it is working for me. thank you. Can you guide me on a thing. when i use IplImage* depth=(IplImage*)cvLoad("depthImg190.xml"); it is working fine. but when i use IplImage* depth=(IplImage*)cvLoad(filename); where filename = "D:\\zainb_s\\M.phil\\thesis\\dataset\\dataset_3\\RGB_3\\RGB\\s01_e01-Copy\\1_walking\\depth\\depthImg190.xml" cvLoad is not giving me fine results. It is not reading parameter fine. Can you guide me here and the reason for this behavior? – Zainb Oct 24 '14 at 06:19
  • I have placed "depthImg190.xml" inside the project folder for the test but i need to read all the files of the folder. What should i do? – Zainb Oct 24 '14 at 06:26
  • i can't help you with IplImages. you should not use any of that deprecated stuff anyway, but read the xml files using cv::FileStorage, and use cv::Mat instead of the IplImages. i know, it's hard to swallow for noobs, that they have to throw away their hard-worked-for knowledge, but you're clearly on the wrong track there. – berak Oct 24 '14 at 06:27
  • I had actually tried FileStorage first and cv::Mat, but the problem i was facing was my xml files contain type_id "opencv-image" instead of "opencv-matrix" and FileStorage was not returning the data matrix. Is there any solution for this? – Zainb Oct 24 '14 at 06:31
  • Here is the question i had posted first using FIleStorage issue http://stackoverflow.com/questions/26421473/read-xml-file-with-type-id-opencv-image-using-opencv?rq=1 – Zainb Oct 24 '14 at 06:33