i would to devllop a SVM classifier,for that i have images, and i would to read 80% for training, and 20% for test randomly, using OpenCv 3.0.0 and C++. can you help me please. my images are in the same folder.
Asked
Active
Viewed 351 times
-4
-
Use "glob" to get a vector of strings with the path of all images. Random shuffle the vector, and load the needed number of images – Miki Apr 13 '16 at 18:51
-
note that i read all names of my images, and i put it in a file, than i load it, and now i would to load only 80% of them for training, but not only the fist ones . – nadjet Apr 13 '16 at 19:17
-
i have a nother question please, how can i spicify 80% of the size of my list, and any library i must include (for the shuffle random, and for the percentage) ? – nadjet Apr 13 '16 at 19:27
-
Please note that this is a question and answer site, not a code writing service. If you [edit] your question to describe what you have tried so far and where you are stuck, then we can try to help with specific problems. You should also read [ask]. – Toby Speight Apr 13 '16 at 22:13
-
yes, i tried but i couldn't do it – nadjet Apr 14 '16 at 05:54
1 Answers
0
You can:
read all files in the specified folder with OpenCV glob function. You'll save all filenames into a
std::vector<std::string>
,std::random_shuffle
the vector of file namesKeep only the first XX% names of the shuffled vector
Do something with your images
Code:
#include <opencv2/opencv.hpp>
#include <vector>
#include <string>
#include <algorithm>
int main()
{
// Read all .png files from the specified folder
std::string folder = "D:/SO/img/*.png";
std::vector<std::string> filenames;
cv::glob(folder, filenames);
// Random shuffle
std::random_shuffle(filenames.begin(), filenames.end());
// Keep 80%
float how_many = 0.80;
size_t N = filenames.size() * how_many;
// Do something with N random images
for (size_t i = 0; i < N; ++i)
{
// Load and show image
cv::Mat3b img = cv::imread(filenames[i]);
cv::imshow("Img", img);
cv::waitKey(1);
}
return 0;
}
-
very good, thank you very much, I'm so happy for your help. now can you tell me please how can i read the rest of images (20%) for the test part, without repete (i mean an image used for train, must be not used for test) ? please – nadjet Apr 13 '16 at 21:37
-
Just use images from index `N` to `filenames.size()`. And try to study a little how C++ works – Miki Apr 14 '16 at 09:39
-
-
Miki , can you answer my quesion in the link below http://stackoverflow.com/questions/36655182/roc-curve-in-opencv3-0-0 – nadjet Apr 16 '16 at 14:57
-
can you please help me, I want an answer for my question http://stackoverflow.com/questions/36876093/no-lineaire-svm-in-opencv-3-0-0 – nadjet Apr 26 '16 at 21:39