Load all pictures inside the Folder into Opencv Mat

Share this for more tutorials and computer vision post from me.. Thanks best Vladimir

This is example how to load all images inside the folder on the Windows system.I use this to collect vector<Mat> to train classificator for image recognition.

Collect positive and negative pictures

Basically, I load all positive samples from one Folder and negative samples from another Folder into one vector<Mat>. Code show only reading from one folder.

Label positive and negative data in Opencv

Also It is easy to count files (images) in one folder and another folder and label the images in your vector in sense that the first part contains only positive samples and when Iterator is greater than Num of positive samples rest we can labeled as negative.

Mat of labels 1 for positive and - 1 for negative you can create like that

This is vector with POSITIVE | NEGATIVE image samples inside

The number of positive images and negative images is count in code at the end of this article.

vector<Mat> samples;

// Create empty Container with INT values 1 for positive and -1 for negativeMat_<int> labels(1, pos_count + neg_count);//Fill the first part by 1 for positive samples intil I < pos_count

for (int i = 0; i < pos_count; ++i)labels(0, i) = 1;//Fill the rest as negative labels with -1

// WIN32_FIND_DATA is a structure // Contains information about the file that is found by the FindFirstFile WIN32_FIND_DATA Info; // Find first file inside searchpath HANDLE Find = ::FindFirstFile(search, &fdInfo);

// If FindFirstFile(search, &fd); fails return INVALID_HANDLE_VALUE,
// If Find is not equal to INVALID_HANDLE_VALUE do the loop inside the
// IF condition