diff options
Diffstat (limited to 'meowpp.test/src/features.cpp')
-rw-r--r-- | meowpp.test/src/features.cpp | 297 |
1 files changed, 135 insertions, 162 deletions
diff --git a/meowpp.test/src/features.cpp b/meowpp.test/src/features.cpp index 2da188a..6972fa3 100644 --- a/meowpp.test/src/features.cpp +++ b/meowpp.test/src/features.cpp @@ -1,34 +1,89 @@ #include <cstdio> +#include <string> +#include <cstdlib> -#include "features__.h" +struct Job; -#include "meowpp/Usage.h" -#include "meowpp/colors/RGB_Space.h" -#include "meowpp/geo/Vectors.h" -#include "meowpp/gra/Bitmap.h" -#include "meowpp/oo/ObjBase.h" -#include "meowpp/oo/ObjSelector.h" +void init ( ); +void setup(int argc, char** argv); +void end ( ); -#include <opencv/cv.h> -#include <opencv/highgui.h> +Job* get(size_t counter); -extern "C"{ -#include <sys/types.h> -#include <dirent.h> +void input (Job* job); +void handle(Job* job); +void output(Job* job); +void clear (Job* job); + +void info0 (Job* job); +void info1 (Job* job); +void info2 (Job* job); + +int main(int argc, char** argv) { + Job* now; + try { + init(); + setup(argc, argv); + for (size_t counter = 0; (now = get(counter)) != NULL; clear(now), ++counter) { + try { + input (now); info0(now); + handle(now); info1(now); + output(now); info2(now); + } + catch (std::string reason) { + throw reason.c_str(); + } + catch (char const* reason) { + printf("fail '%s', ignore.\n\n", reason); + continue; + } + printf("\n"); + } + end(); + } + catch (int num) { + return num; + } + return 0; } +/////////////////////////////////////////////////////////// + +#include "test_utility.h" +#include "features__.h" + +#include "meowpp/utility.h" +#include "meowpp/Usage.h" +#include "meowpp/oo/ObjSelector.h" + #include <vector> #include <algorithm> #include <string> +#include <cstdio> using namespace meow; +/////////////////////////////////////////////////////////// + Usage usg("features"); +FeaturePointsDetectors* detector; + +struct Job { + int id; + std::string file_name; + std::vector<FeaturePoint<double, double> > fps; + Bitmap<RGBf_Space> bmp; +}; + std::vector<std::string> names; -std::vector<std::vector<FeaturePoint<double, double> > > fps; -int setup(int argc, char** argv) { +std::string out_pre; + + +/////////////////////////////////////////////////////////// + +void init() { usg.optionAdd("h" , "Display this help document"); usg.optionAdd("help", "Display this help document"); usg.optionAdd("i", @@ -37,16 +92,13 @@ int setup(int argc, char** argv) { "pathname", "", false); + usg.optionAdd("r", + "recur"); usg.optionAdd("o", "Output images with denoting feature points", "filename", "", false); - usg.optionAdd("f", - "Output text file name", - "filename", - "<<input image name>>.txt", - false); usg.optionAdd("d", "Specify which feature detect algorithm to use", "name", @@ -54,179 +106,100 @@ int setup(int argc, char** argv) { true); std::vector<std::string> algo_list(ObjSelector<kFPSD_ID>::names()); for (size_t i = 0, I = algo_list.size(); i < I; ++i) { - FeaturePointsDetectors const* f; - f = (FeaturePointsDetectors const*)ObjSelector<kFPSD_ID>::get(algo_list[i]); + FeaturePointsDetectors const* f = (FeaturePointsDetectors const*)ObjSelector<kFPSD_ID>::get(algo_list[i]); usg.optionValueAcceptAdd("d", algo_list[i], f->description()); - usg.import(f->usage()); + if (usg.import(f->usage()) == false) throw -1; } +} + +void setup(int argc, char** argv) { std::string err; bool ret = usg.arguments(argc, argv, &err); if (usg.hasOptionSetup("h") || usg.hasOptionSetup("help")) { fprintf(stderr, "%s\n", usg.usage().c_str()); - return 0; + throw 0; } if (ret == false) { fprintf(stderr, "%s\n", err.c_str()); - return -1; + throw -2; } - return 1; -} -int getName() { - if (usg.hasOptionSetup("i") == false) { - names = usg.procArgs(); + detector = (FeaturePointsDetectors*)ObjSelector<kFPSD_ID>::create(usg.optionValue("d", 0)); + if (detector->usage(usg) == false) throw -3; + + if (usg.hasOptionSetup("i")) { + names = cgetFiles(usg.optionValue("i", 0).c_str(), usg.hasOptionSetup("r"), + 4, ".jpg", ".jpeg", ".JPG", ".JPEG"); } else { - std::string base = usg.optionValue("i", 0); - if (base.length() == 0 || base[base.length() - 1] != '/') { - base += "/"; - } - DIR* dir = opendir(base.c_str()); - if (!dir) { - fprintf(stderr, "can't open dir '%s'\n", base.c_str()); - return -1; - } - for (dirent* ent; (ent = readdir(dir)) != NULL; ) { - if (!cstringEndWith(ent->d_name, 4, ".jpeg", ".jpg", ".JPG", ".JPEG")) { - continue; - } - names.push_back(base + std::string(ent->d_name)); - } + names = usg.procArgs(); } - return 1; + std::sort(names.begin(), names.end(), filenameCompare); + out_pre = usg.optionValue("o", 0); } -Bitmap<RGBf_Space> readOne(std::string name) { - Bitmap<RGBf_Space> ret; - messagePrintf(1, "Loading image..."); - cv::Mat img = cv::imread(name, CV_LOAD_IMAGE_COLOR); - if (img.data) { - size_t height = img.size().height; - size_t width = img.size().width ; - ret.size(height, width, RGBf_Space(0)); - for (size_t y = 0; y < height; ++y) { - for (size_t x = 0; x < width; ++x) { - Vector3D<int> v; - for (size_t i = 0; i < 3; ++i) - v.scalar(i, img.at<cv::Vec3b>(y, x)[2 - i]); - RGBf_Space p; - colorTransformate(RGBi_Space(v), &p); - ret.pixel(y, x, p); - } - } - } - messagePrintf(-1, (ret.size() > 0 ? "ok" : "fail")); - return ret; +void end() { + delete detector; } -int features() { - std::string n(usg.optionValue("d", 0)); - FeaturePointsDetectors* fpd((FeaturePointsDetectors*) - ObjSelector<kFPSD_ID>::create(n)); - if (fpd->usage(usg) == false) { - fprintf(stderr, "\nArgument setup error for '%s'\n", n.c_str()); - return -1; - } - for (size_t i = 0, I = names.size(); i < I; ++i) { - messagePrintf(1, "Finding feature points of '%s'", names[i].c_str()); - Bitmap<RGBf_Space> bmp(readOne(names[i])); - if (bmp.size() == 0) { - messagePrintf(-1, "fail"); - continue; - } - messagePrintf(0, "Height x Width = %lux%lu", bmp.height(), bmp.width()); - fps.push_back(fpd->detect(bmp)); - messagePrintf(-1, "ok"); - } - delete fpd; - return 1; -} +/////////////////////////////////////////////////////////// -int writeOne(FILE* f, size_t i) { - if (fprintf(f, "%d\n", (int)fps[i].size()) < 1) return -1; - for (size_t j = 0, J = fps[i].size(); j < J; ++j) { - if (fps[i][j].write(f, false, 0) == false) return -1; - } - return 1; +Job* get(size_t counter) { + if (counter >= names.size()) + return NULL; + Job* job = new Job; + job->id = counter; + job->file_name = names[counter]; + return job; } -int writeText() { - for (size_t i = 0, I = fps.size(); i < I; ++i) { - std::string name2; - if (usg.hasOptionSetup("f")) { name2 = usg.optionValue("f", i); } - else { name2 = names[i] + ".txt"; } - messagePrintf(1, "Write text file to '%s'", name2.c_str()); - FILE* f = fopen(name2.c_str(), "w"); - if (f == NULL) { - messagePrintf(-1, "fail to open file!, ignore"); - continue; - } - if (writeOne(f, i) < 0) { - messagePrintf(-1, "write fail"); - fclose(f); - continue; - } - fclose(f); - messagePrintf(-1, "ok"); - } - return 1; +/////////////////////////////////////////////////////////// + +void input(Job* job) { + if (readBitmap(job->file_name, &(job->bmp)) == false) + throw("cannot open image"); } -int writeBmpOne(std::string name, Bitmap<RGBf_Space> const& bmp) { - size_t height = bmp.height(); - size_t width = bmp.width (); - cv::Mat img(height, width, CV_8UC3); - for (size_t y = 0; y < height; y++) { - for (size_t x = 0; x < width; x++) { - RGBi_Space tmp; - colorTransformate(bmp.pixel(y, x), &tmp); - for (size_t i = 0; i < 3; ++i) - img.at<cv::Vec3b>(y, x)[i] = tmp.rgb(2 - i); - } - } - if (imwrite(name, img) == false) { - return -1; - } - return 1; +void handle(Job* job) { + job->fps = detector->detect(job->bmp); } -int writeBmp() { - if (usg.hasOptionSetup("o") == false) - return 1; - for (size_t i = 0, I = names.size(); i < I; ++i) { - std::string name2; - name2 = stringPrintf("%s%lu.jpg", usg.optionValue("o", 0).c_str(), i); - messagePrintf(1, "Write img file to '%s'", name2.c_str()); - Bitmap<RGBf_Space> bmp(readOne(names[i])); - bool succ; - if ((succ = (bmp.size() > 0))) { - int wh = std::min(bmp.height(), bmp.width()) / 16; - for (size_t j = 0, J = fps[i].size(); j < J; ++j) { - int x0 = fps[i][j].position()(0); - int y0 = fps[i][j].position()(1); - for (int dx = -wh; dx <= wh; ++dx) - if (0 <= x0 + dx && x0 + dx < (int)bmp.width()) - bmp.pixel(y0, x0 + dx, RGBf_Space(Vector3D<double>(1.0, 0.0, 0.0))); - for (int dy = -wh; dy <= wh; ++dy) - if (0 <= y0 + dy && y0 + dy < (int)bmp.height()) - bmp.pixel(y0 + dy, x0, RGBf_Space(Vector3D<double>(1.0, 0.0, 0.0))); - } - succ = (writeBmpOne(name2, bmp) > 0); +void output(Job *job) { + FILE* f = fopen((job->file_name + ".fps").c_str(), "w"); + fprintf(f, "%d %d\n%d\n", + (int)job->bmp.width(), (int)job->bmp.height(), (int)job->fps.size()); + for (size_t i = 0, I = job->fps.size(); i < I; ++i) { + job->fps[i].write(f, false, 0); + } + fclose(f); + if (out_pre.size() > 0) { + for (size_t i = 0, I = job->fps.size(); i < I; ++i) { + int x0 = job->fps[i].position()(0); + int y0 = job->fps[i].position()(1); + int w = std::min(job->bmp.width(), job->bmp.height()) / 32; + for (int x = x0 - w; x <= x0 + w; ++x) + if (0 <= x && x < (int)job->bmp.width()) + job->bmp.pixel(y0, x, RGBf_Space(Vector3D<double>(1.0, 0.0, 0.0))); + for (int y = y0 - w; y <= y0 + w; ++y) + if (0 <= y && y < (int)job->bmp.height()) + job->bmp.pixel(y, x0, RGBf_Space(Vector3D<double>(1.0, 0.0, 0.0))); } - messagePrintf(0, "ok"); + writeBitmap(stringPrintf("%s%d.jpg", out_pre.c_str(), job->id), job->bmp); } - return 1; } -int main(int argc, char** argv) { - int ret; - if ((ret = setup(argc, argv)) <= 0) return ret; - if ((ret = getName()) <= 0) return ret; - if ((ret = features()) <= 0) return ret; - if ((ret = writeText()) <= 0) return ret; - if ((ret = writeBmp()) <= 0) return ret; - return 0; +void clear(Job *job) { + delete job; } +void info0(Job* job) { + printf("file name: %s, %dx%d\n", + job->file_name.c_str(), (int)job->bmp.width(), (int)job->bmp.height()); +} +void info1(Job* job) { + printf("# of feature points: %d\n", (int)job->fps.size()); +} +void info2(Job* job) { +} |