为什么imread(pic_name [i],1)不起作用?

时间:2017-05-10 08:44:49

标签: c++ opencv

我最近使用这段代码来显示那里有多少行进点。 但它不起作用。

我知道问题是我用忧虑的方式使用imread()。 像这样:

Mat srcImage2 = imread(pic_name[i],1);

但是,如果我尝试加载2个JPG图像的代码,它就可以了。 像这样:

 Mat srcImage2 = imread("2.jpg",1);

请帮帮我。 QwQ。 下面有完整的代码。

    #include <opencv2/core/core.hpp>
    #include <opencv2/features2d/features2d.hpp>
    #include "opencv2/highgui/highgui.hpp"
    #include "opencv2/calib3d/calib3d.hpp"
    #include "opencv2/nonfree/nonfree.hpp"
    #include <iostream>
    #include <fstream>
    using namespace cv;
    using namespace std;

    vector<string>& split_input(string& c, vector<string>& filename);

    int main(){

        vector<string> pic_name;
        ifstream infile("PicName.txt");
        string c;

        while (getline(infile, c)) split_input(c, pic_name);
        infile.close();
        for (int i(0);i<pic_name.size(); ++i) {
            Mat srcImage1 = imread("1.jpg", 1);
            Mat srcImage2 = imread(pic_name[i],1);
            if (!srcImage1.data || !srcImage2.data)
            {
                printf("ERROR,cant find picture. \n"); return false;
            }

            int minHessian = 9000;
            SurfFeatureDetector detector(minHessian);
            vector<KeyPoint> keypoints_object, keypoints_scene;

            detector.detect(srcImage1, keypoints_object);
            detector.detect(srcImage2, keypoints_scene);

            //featres
            SurfDescriptorExtractor extractor;
            Mat descriptors_object, descriptors_scene;
            extractor.compute(srcImage1, keypoints_object, descriptors_object);
            extractor.compute(srcImage2, keypoints_scene, descriptors_scene);

            //FLANN
            FlannBasedMatcher matcher;
            vector< DMatch > matches;

            if (descriptors_object.empty())
                cvError(0, "MatchFinder", "1st descriptor empty", __FILE__, __LINE__);
            if (descriptors_scene.empty())
                cvError(0, "MatchFinder", "2nd descriptor empty", __FILE__, __LINE__);

            matcher.match(descriptors_object, descriptors_scene, matches);
            double max_dist = 0; double min_dist = 100;

            for (int i = 0; i < descriptors_object.rows; i++)
            {
                double dist = matches[i].distance;
                if (dist < min_dist) min_dist = dist;
                if (dist > max_dist) max_dist = dist;
            }

            printf(">Max dist : %f \n", max_dist);
            printf(">Min dist : %f \n", min_dist);

            std::vector< DMatch > good_matches;
            for (int i = 0; i < descriptors_object.rows; i++)
            {
                if (matches[i].distance < 3 * min_dist)
                {
                    good_matches.push_back(matches[i]);
                }
            }
            cout << "points:" << good_matches.size()  << endl << endl;

vector<string>& split_input(string& c, vector<string>& filename) {
    string::size_type i(0);
    int flag(0);
    while (i != c.size()) {
        for (; i != c.size() && isspace(c[i]); ++i);
        string::size_type j = i;
        for (; j != c.size() && !isspace(c[j]); ++j);
        filename.push_back(c.substr(i, j - i));
        i = j;
    }
    return filename;
}

PS:PicName.txt只有oneline,即“2.jpg 3.jpg”。 PPS:非常感谢!!

1 个答案:

答案 0 :(得分:0)

您在阅读文件时遇到问题,这就是代码无效的原因,请尝试:

std::ifstream infile("foo.txt");
std::string a;
std::string b;
while (infile >> a >> b)
Mat srcImage1 = imread(a, 1);
Mat srcImage2 = imread(b, 1);

编辑:

您可以使用以下矢量:

std::ifstream infile("foo.txt");
std::string a;

std::vector<std::string> v;
while (infile >> a )
{
    v.push_back(a);
}
if(v.size()!=2) return -1;
Mat srcImage1 = imread(v[0]);
Mat srcImage2 = imread(v[1]);

enter image description here