Advertisement
tombroskipc

main_sorting

Jul 17th, 2021
162
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.94 KB | None | 0 0
  1. %%writefile main.cpp
  2. #include <iostream>
  3. #include "opencv2/opencv.hpp"
  4. #include "StraightSelectionSortEx.h"
  5. #include "StraightInsertionSortEx.h"
  6. #include "ShellSortEx.h"
  7. #include "HeapSortEx.h"
  8. #include "BubbleSortEx.h"
  9. #include "QuickSortEx.h"
  10. #include "DLinkedListSEEx.h"
  11. //#include "dataframe.h"
  12. #include "util/FileIOLib.h"
  13. //using namespace cv;
  14. using namespace std;
  15.  
  16. typedef unsigned char uint8;
  17. typedef unsigned int  uint32;
  18.  
  19. /////////////////////////////////////////////////////////////////////////////////////////
  20. // Command line parsing functions
  21. /////////////////////////////////////////////////////////////////////////////////////////
  22.  
  23. int get_int(char** begin, char** end, const string& option, int _default){
  24.     char** ptr = std::find(begin, end, option);
  25.     if (ptr != end && ++ptr != end) return stoi(*ptr);
  26.     else return _default;
  27. }
  28.  
  29. string get_string(char** begin, char** end, const string& option, string _default){
  30.     char** ptr = std::find(begin, end, option);
  31.     if (ptr != end && ++ptr != end) return string(*ptr);
  32.     else return _default;
  33. }
  34. bool option_exist(char** begin, char** end, const string& option){
  35.     return std::find(begin, end, option) != end;
  36. }
  37. string trimstring(string str) {
  38.     const char* typeOfWhitespaces = " \t\n\r\f\v";
  39.     str.erase(str.find_last_not_of(typeOfWhitespaces) + 1);
  40.     str.erase(0,str.find_first_not_of(typeOfWhitespaces));
  41.     return str;
  42. }
  43.  
  44. /////////////////////////////////////////////////////////////////////////////////////////
  45. // Command line parsing functions: END
  46. /////////////////////////////////////////////////////////////////////////////////////////
  47.  
  48. void sort_file(string file, string input, string output, bool inrow=true){
  49.     string inputfile = input + "/" + file;
  50.     string outputfile = output + "/" + file;
  51.     cv::Mat image = cv::imread(inputfile, cv::IMREAD_GRAYSCALE);
  52.  
  53.     if (image.empty()) cout << inputfile << ": not found!" << endl;
  54.    
  55.     //Here: valid image:
  56.     cout << "filename: " << inputfile << endl;
  57.     cout << "(nrows, ncols, nchannels) = "
  58.         << "("  << image.rows << ", "
  59.                 << image.cols << ", "
  60.                 << image.channels()
  61.         << ")" << endl;
  62.     cout << "number of pixels: " << image.rows* image.cols * image.channels() << endl;
  63.     int shell_segments[] = {1, 3, 7};
  64.     ISortEx<uint8>* sorter;
  65.     if (output == "straightinsertionsort")              // DONE
  66.         sorter = new StraightInsertionSortEx<uint8>();
  67.     else if (output == "straightselectionsort")         // DONE
  68.         sorter = new StraightSelectionSortEx<uint8>();
  69.     else if (output == "bubblesort")                    // DONE
  70.         sorter = new BubbleSortEx<uint8>();
  71.     else if (output == "shellsort")
  72.         sorter = new ShellSortEx<uint8>(shell_segments, 3);
  73.     else if (output == "heapsort")                      // DONE
  74.         sorter = new HeapSortEx<uint8>();
  75.     else if (output == "quicksort")
  76.         sorter = new QuickSortEx<uint8>();
  77.     else if (output == "dlinkedlistse")
  78.         sorter = new DLinkedListSEEx<uint8>();
  79.  
  80.  
  81.     if(inrow)
  82.     {
  83.         //sort data in rows
  84.         for(int r = 0; r < image.rows; r++)
  85.             sorter->sort(image.ptr<uint8>(r, 0), image.cols, &SortSimpleOrderEx<uint8>::compare4Ascending);
  86.         outputfile = output + "/" + file.substr(0, file.rfind('.')) + "_rows.png";
  87.         cout << "Done rows" << endl;
  88.     }
  89.     else
  90.     {
  91.         //sort data in columns
  92.         for(int c = 0; c < image.cols; c++)
  93.             sorter->sort(image.ptr<uint8>(0, c), image.rows* image.cols * image.channels(), &SortSimpleOrderEx<uint8>::compare4Ascending, image.cols);
  94.         outputfile = output + "/" + file.substr(0, file.rfind('.')) + "_cols.png";
  95.         cout << "Done cols" << endl;
  96.     }
  97.     //cout << outputfile << endl;
  98.     //cout << endl;
  99.     cv::imwrite(outputfile, image); //write image file with extension PNG
  100.  
  101.     delete sorter;
  102. }
  103. /*
  104. 10, 21, 5,  127,
  105. 5,  18, 17, 20,
  106. 25, 35, 40, 50
  107.  
  108. nrows: 3
  109. ncols: 4
  110. */
  111.  
  112. int main(int argc, char** argv)
  113. {
  114.     if(option_exist(argv, argv+argc, "-h"))
  115.     {
  116.         cout << "Show help" << endl;
  117.         return 0;
  118.     }
  119.     ///////////////////////////////////////////////////////////////////////////
  120.     string input = get_string(argv, argv+argc, "-in", "input");
  121.     string output = get_string(argv, argv+argc, "-out", "output");
  122.     string filelist = get_string(argv, argv+argc, "-list", "filelist.csv");
  123.    
  124.     DataFrame master(input + "/" + filelist);
  125.     vector<string> filenames = master["filename"];
  126.     vector<string> outfiles;
  127.     for(auto it=filenames.begin(); it != filenames.end(); it++){
  128.         string filename = *it;
  129.         sort_file(trimstring(filename), input, output, true);
  130.         sort_file(trimstring(filename), input, output, false);
  131.         outfiles.push_back(filename);
  132.     }
  133.  
  134.     DataFrame outmaster;
  135.     outmaster.add("filename", outfiles);
  136.     outmaster.write(output + "/" + filelist);
  137.     return 0;
  138. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement