Share
Visit

Friday, February 5, 2016

Opencv tutorial, Read all images withim a folder (Windows) and Labeled Data

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 negative
Mat_<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

for (int i = pos_count; i < pos_count + neg_count; ++i)

labels(0, i) = -1;



-----------------------------------------------------------------

#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 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;

                }

}

2 comments:

ad