Opencv tutorial, Read all images withim a folder (Windows) and Labeled Data
This is an example of how to load all images inside the folder on the Windows system.
I use this to collect vector<Mat> to train the classifier 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>. The code shows 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 the sense that the first part contains only positive samples and when the Iterator is greater than Num of positive samples we can be labeled as negative.
#include "opencv2\highgui.hpp"
#include "opencv2\imgproc.hpp"
#include <vector>
#include <fstream>
#include <stdio.h>
#include <iostream>
#include <Windows.h>
#include <iomanip>
vector<string> getNamesOfFile(string folderPath)
{
// vector of names of file within a folder
vector<string> names_ofFiles;
char search[200];
// convert your string search path into char array
sprintf(search, "%s*.*", folderPath.c_str());
// 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 search path
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
if(Find != INVALID_HANDLE_VALUE) {
do {
// DWORD dwFileAttributes , FILE_ATTRIBUTE_DIRECTORY ident a directory
// If condition is fine lets push file mane into string vector
if(! (Info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) ) {
// Fill vector of names
names_ofFiles.push_back(Info.cFileName);
}
// do - while there is next file
//::FindNextFile the function succeeds, the return value is nonzero and the Info contain //information about the next file
}while(::FindNextFile(Find, &Info));
::FindClose(Find);
}
// Return result
return names_ofFiles;
}
int main(int argc, const char** argv)
{
// your path where you can load all images String Path = "C://NegativeSample/";
// Vector that contain all images file mames
vector<string> vectorOfFileNameWithinPath = getNamesOfFile(Path2);
// Load all images and display
// and store inside the another vector StoredImages
int pos_count = 0;
vector<Mat> StoredImages
for (int i = 0; i < vectorOfFileNameWithinPath.size() -1 ; ++i)
{
// Path "C://NegativeSample/" + i th "Filename.jpg"
String cesta = Path + vectorOfFileNameWithinPath[i];
// Read image into Mat container
Mat img = imread(cesta, IMREAD_COLOR);
// Store images in vector you can use later for example in own ML training
StoredImages.push_back(img)
// Display single image
namedWindow("Display",WINDOW_AUTOSIZE);
imshow( "Display", img);
int key1 = waitKey(10);
// Increase number of positive samples in StoredImages
// Later we push into same vector negative samples
// and we need to know, where the boundary is
pos_count = pos_count + 1;
}
}
This comment has been removed by the author.
visual basic code examples
vbasic code | Display the Open With dialog