reading number of images from txt file

asked 2015-12-19 13:21:50 -0500

Maarran gravatar image

updated 2015-12-20 01:00:58 -0500

berak gravatar image

Hi. I managed to read an image from directory and compare it with number of images from txt file.How to convert it as reading number of images from txt file instead reading an image from directory

   static void read_imgList(const string& filename, vector<Mat>& images) {
    std::ifstream file(filename.c_str(), ifstream::in);
    if (!file) {
        string error_message = "No valid input file was given, please check the given filename.";
        CV_Error(CV_StsBadArg, error_message);
    }
    string line;
    while (getline(file, line)) {
        images.push_back(imread(line, 0));
    }
}
int main(int argc, char** argv)
{
    if (argc != 3)
    {
        readme(); return -1;
    }



    string imgList = string(argv[1]);

    vector<Mat> images;

    try {
        read_imgList(imgList, images);
    }
    catch (cv::Exception& e) {
        cerr << "Error opening file \"" << imgList << "\". Reason: " << e.msg << endl;
        exit(1);
    }


    Mat img_2 = imread(imgList[1], CV_LOAD_IMAGE_GRAYSCALE);


    if (!img_2.data)
    {       std::cout << " --(!) Error reading images " << std::endl; return -1;
    }

    //-- Step 1: detect the keypoints using SURF Detector
    int minHessian = 400;
    SurfFeatureDetector detector(minHessian);
    SurfDescriptorExtractor extractor;

    //-- Test img, detect the keypoints
    std::vector<KeyPoint>  keypoints_2;
    detector.detect(img_2, keypoints_2);

    //-- Step 2: calculate descriptors (feature vectors)
    Mat descriptors_2;
    extractor.compute(img_2, keypoints_2, descriptors_2);

    double mindist = 100;
    int matchInd = -1;
    for (unsigned int i = 0; i < images.size(); i++)
    {
        Mat img_1;
        img_1 = images[i];
        if (!img_1.data)
        {
            std::cout << " --(!) Error reading images " << std::endl; return -1;
        }
edit retag flag offensive close merge delete

Comments

where exactly is the problem ?

berak gravatar imageberak ( 2015-12-20 01:02:32 -0500 )edit

I tried to load the image in the txt file in this line. But I couldn't load the image. I'm not sure whether I'm doing the right one. This line of code is to load a single image from directory. Not sure how to make a loop to read all the images listed in the txt file.

 Mat img_2 = imread(imgList[1], CV_LOAD_IMAGE_GRAYSCALE);

if (!img_2.data)
{       std::cout << " --(!) Error reading images " << std::endl; return -1;
}

The loop to read from txt file

for (unsigned int i = 0; i < images.size(); i++)
{
    Mat img_1;
    img_1 = images[i];
    if (!img_1.data)
    {
        std::cout << " --(!) Error reading images " << std::endl; return -1;
    }
Maarran gravatar imageMaarran ( 2015-12-20 02:28:01 -0500 )edit

start with adding a check here:

while (getline(file, line)) {
    Mat im = imread(line, 0);
    if (im.empty()) {
        CV_Error(CV_StsBadArg, line + " could not be read.");
    }
    images.push_back(im);
}
berak gravatar imageberak ( 2015-12-20 02:36:16 -0500 )edit

Hi.. I have sorted out the issue. But the read_imgList function reading both text files giving error.

static void read_imgList(const string& filename, vector<Mat>& trainImages, vector<Mat>& testImages) {
std::ifstream file(filename.c_str(), ifstream::in);
if (!file) {
    string error_message = "No valid input file was given, please check the given filename.";
    CV_Error(CV_StsBadArg, error_message);
}
Maarran gravatar imageMaarran ( 2015-12-22 11:29:56 -0500 )edit