Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008 #include <opencv2/opencv.hpp>
00009
00010 #include "cvUtils.h"
00011
00012 using namespace std;
00013 using namespace cv;
00014
00015
00016 CvUtils::CvUtils(String f)
00017 {
00018 thefile = f;
00019 feature = "SURF";
00020 descriptor = "SURF";
00021 }
00022
00023
00024 CvUtils::CvUtils(String f, String featureType, String descriptorType){
00025 thefile = f;
00026 feature = featureType;
00027 descriptor = descriptorType;
00028 }
00029 CvUtils::~CvUtils()
00030 {
00031
00032
00033 }
00034
00046 String CvUtils::detect()
00047 {
00048 fromFile();
00049
00050 cout << "Using detector: " << feature << " and descriptor " << descriptor << endl;
00051 Ptr<FeatureDetector> theDetector = FeatureDetector::create(feature);
00052 Ptr<DescriptorExtractor> theDescriptorExtractor = DescriptorExtractor::create(descriptor);
00053
00054 cout << "Extracting keypoints from image..." << endl;
00055 theDetector->detect(image, kps);
00056 cout << "Computing descriptors from image..." << endl;
00057 theDescriptorExtractor->compute(image,kps,descs);
00058 cout << "Finished extracting data from image" << endl;
00059
00060
00061 toFile("xml");
00062
00063 return thefile;
00064 }
00072 void CvUtils::fromFile()
00073 {
00074
00075
00076 int period = thefile.find_last_of(".");
00077 String fileType = thefile.substr(period+1);
00078 cout << "Extension is " << fileType << endl;
00079 if (!strcmp(fileType.c_str(),"xml") || !strcmp(fileType.c_str(),"yml")) {
00080 cout << "Using FileStorage" << endl;
00081 cout << "Reading data from file" << endl;
00082 FileStorage fs(thefile, FileStorage::READ);
00083
00084
00085 fs["image"] >> image;
00086 fs.release();
00087 cout << "Done reading from file" << endl;
00088 }else
00089 {
00090 cout << "Assuming client has sent a bitmap" << endl;
00091 image = imread(thefile.c_str());
00092 }
00093
00094
00095 }
00106 void CvUtils::toFile(String extension)
00107 {
00108 int period = thefile.find_last_of(".");
00109 thefile = thefile.substr(0,period+1)+extension;
00110 cout << "Writing data to file " << thefile << endl;
00111 FileStorage fs(thefile, FileStorage::WRITE);
00112 fs << "detectorType" << feature;
00113 fs << "descriptorType" << descriptor;
00114 fs << "keypoints" << list_keypoint_to_Mat(kps);
00115 fs << "descriptors" << descs;
00116 fs.release();
00117 cout << "Done writing to file" << endl;
00118 }
00131 Mat CvUtils::list_keypoint_to_Mat(vector<KeyPoint> kp)
00132 {
00133 int rows = 0;
00134 if ((int)kp.size() != 0) {
00135 rows = (int)kp.size();
00136 }
00137 Mat res(rows, 7, CV_64FC1);
00138 if (rows > 0) {
00139 for (int i = 0; i < rows; i++) {
00140 KeyPoint mykp = kp.at(i);
00141 res.at<double>(i,0) = mykp.pt.x;
00142 res.at<double>(i,1) = mykp.pt.y;
00143 res.at<double>(i,2) = mykp.size;
00144 res.at<double>(i,3) = mykp.angle;
00145 res.at<double>(i,4) = mykp.response;
00146 res.at<double>(i,5) = mykp.octave;
00147 res.at<double>(i,6) = mykp.class_id;
00148 }
00149 }
00150 return res;
00151 }