简体   繁体   中英

how to read and write images in the array?

I got the problem on how to read and write the random images in the array called name[j] into another folder. Currently, i just can read and write images in the whole input directory to another directory instead of random images based on the array name [j]. i do not know how to pass the value in the array to imwrite() function.

This is my full code:

    int main(int argc, char* argv[])
    {
    string homedir = "C:\\Users\\x\\Documents\\Aggressive\\offline workspace\\abc";
    cerr << endl << "path =  " << homedir.c_str() << endl;

    std::string inputDirectory = "C:\\Users\\x\\Documents\\Aggressive\\offline workspace\\abc";
    std::string outputDirectory = "C:\\Users\\x\\Documents\\folder";
    DIR* directory = opendir(inputDirectory.c_str());
    struct dirent* _dirent = NULL;
    int i = 0;
    int total;
    srand(time(0)); //seed random number

    vector<string> name;

    if (directory == NULL)
    {
        printf("Cannot open Input Folder\n");
        return 1;
    }
    while ((_dirent = readdir(directory)) != NULL)
    {
        puts(_dirent->d_name); 
        name.push_back(_dirent->d_name); 
        printf("\n");
        i++;

        std::string fileName = inputDirectory + "\\" + std::string(_dirent->d_name);
        cv::Mat rawImage = cv::imread(fileName.c_str());
        if (rawImage.data == NULL)
        {
            printf("copied\n");
            continue;
        }
        // Add your any image filter here
        fileName = outputDirectory + "\\" + std::string(_dirent->d_name);
        cv::imwrite(fileName.c_str(), rawImage);


    }
    //name.erase(name.begin(), name.begin() + 1); 

    name.erase(name.begin(), name.begin() + 2);
    cout << "file name: " << name[16];
    total = i - 2;

    cout << "\n";
    cout << "There's " << total << " files in the current directory.\n" << endl;

    cout << "Enter array size: \n";
    int a;
    cin >> a;


    for (int j = 0; j < a; j++) {
         //generate random filename
         int index = rand() % a;
         //cout << filename[index] << endl;

         //swap random[j] with random[index]
         string temp = name[j];
         name[j] = name[index];
         name[index] = temp;
     }

     //loop through array and print value
     for (int j = 0; j < a; j++) {
         cout << "Random image selected:" << name[j] << endl; 
     }

    closedir(directory);
}

Assuming name[j] is a string in the format of C:\\...\\img.jpg you can read those images in each loop cycle and write them to your desired directory.

   for (int j = 0; j < a; j++) {
         cout << "Random image selected:" << name[j] << endl; 
         // name[j] should be in the directory format like "C:\\...\\img.jpg"
         //Then read this image
         Mat img = imread(name[j]);

         //Then write to desired directory in every loop you need to change the name to avoid writing on the same name.
         string written_directory = "C:\\Users\\x\\Documents\\folder\\img" + std::to_string(j) + ".jpg";
         imwrite(written_directory,img);                  
     }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM