Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- %%writefile main.cpp
- #include <iostream>
- #include "opencv2/opencv.hpp"
- #include "StraightSelectionSortEx.h"
- #include "StraightInsertionSortEx.h"
- #include "ShellSortEx.h"
- #include "HeapSortEx.h"
- #include "BubbleSortEx.h"
- #include "QuickSortEx.h"
- #include "DLinkedListSEEx.h"
- //#include "dataframe.h"
- #include "util/FileIOLib.h"
- //using namespace cv;
- using namespace std;
- typedef unsigned char uint8;
- typedef unsigned int uint32;
- /////////////////////////////////////////////////////////////////////////////////////////
- // Command line parsing functions
- /////////////////////////////////////////////////////////////////////////////////////////
- int get_int(char** begin, char** end, const string& option, int _default){
- char** ptr = std::find(begin, end, option);
- if (ptr != end && ++ptr != end) return stoi(*ptr);
- else return _default;
- }
- string get_string(char** begin, char** end, const string& option, string _default){
- char** ptr = std::find(begin, end, option);
- if (ptr != end && ++ptr != end) return string(*ptr);
- else return _default;
- }
- bool option_exist(char** begin, char** end, const string& option){
- return std::find(begin, end, option) != end;
- }
- string trimstring(string str) {
- const char* typeOfWhitespaces = " \t\n\r\f\v";
- str.erase(str.find_last_not_of(typeOfWhitespaces) + 1);
- str.erase(0,str.find_first_not_of(typeOfWhitespaces));
- return str;
- }
- /////////////////////////////////////////////////////////////////////////////////////////
- // Command line parsing functions: END
- /////////////////////////////////////////////////////////////////////////////////////////
- void sort_file(string file, string input, string output, bool inrow=true){
- string inputfile = input + "/" + file;
- string outputfile = output + "/" + file;
- cv::Mat image = cv::imread(inputfile, cv::IMREAD_GRAYSCALE);
- if (image.empty()) cout << inputfile << ": not found!" << endl;
- //Here: valid image:
- cout << "filename: " << inputfile << endl;
- cout << "(nrows, ncols, nchannels) = "
- << "(" << image.rows << ", "
- << image.cols << ", "
- << image.channels()
- << ")" << endl;
- cout << "number of pixels: " << image.rows* image.cols * image.channels() << endl;
- int shell_segments[] = {1, 3, 7};
- ISortEx<uint8>* sorter;
- if (output == "straightinsertionsort") // DONE
- sorter = new StraightInsertionSortEx<uint8>();
- else if (output == "straightselectionsort") // DONE
- sorter = new StraightSelectionSortEx<uint8>();
- else if (output == "bubblesort") // DONE
- sorter = new BubbleSortEx<uint8>();
- else if (output == "shellsort")
- sorter = new ShellSortEx<uint8>(shell_segments, 3);
- else if (output == "heapsort") // DONE
- sorter = new HeapSortEx<uint8>();
- else if (output == "quicksort")
- sorter = new QuickSortEx<uint8>();
- else if (output == "dlinkedlistse")
- sorter = new DLinkedListSEEx<uint8>();
- if(inrow)
- {
- //sort data in rows
- for(int r = 0; r < image.rows; r++)
- sorter->sort(image.ptr<uint8>(r, 0), image.cols, &SortSimpleOrderEx<uint8>::compare4Ascending);
- outputfile = output + "/" + file.substr(0, file.rfind('.')) + "_rows.png";
- cout << "Done rows" << endl;
- }
- else
- {
- //sort data in columns
- for(int c = 0; c < image.cols; c++)
- sorter->sort(image.ptr<uint8>(0, c), image.rows* image.cols * image.channels(), &SortSimpleOrderEx<uint8>::compare4Ascending, image.cols);
- outputfile = output + "/" + file.substr(0, file.rfind('.')) + "_cols.png";
- cout << "Done cols" << endl;
- }
- //cout << outputfile << endl;
- //cout << endl;
- cv::imwrite(outputfile, image); //write image file with extension PNG
- delete sorter;
- }
- /*
- 10, 21, 5, 127,
- 5, 18, 17, 20,
- 25, 35, 40, 50
- nrows: 3
- ncols: 4
- */
- int main(int argc, char** argv)
- {
- if(option_exist(argv, argv+argc, "-h"))
- {
- cout << "Show help" << endl;
- return 0;
- }
- ///////////////////////////////////////////////////////////////////////////
- string input = get_string(argv, argv+argc, "-in", "input");
- string output = get_string(argv, argv+argc, "-out", "output");
- string filelist = get_string(argv, argv+argc, "-list", "filelist.csv");
- DataFrame master(input + "/" + filelist);
- vector<string> filenames = master["filename"];
- vector<string> outfiles;
- for(auto it=filenames.begin(); it != filenames.end(); it++){
- string filename = *it;
- sort_file(trimstring(filename), input, output, true);
- sort_file(trimstring(filename), input, output, false);
- outfiles.push_back(filename);
- }
- DataFrame outmaster;
- outmaster.add("filename", outfiles);
- outmaster.write(output + "/" + filelist);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement