OpenCV 2.4.5 中的访问冲突读取

2024-01-21

我尝试了有关匹配许多图像的示例代码OpenCV 2.4.5我修改了该代码。我找到了错误代码:

Unhandled exception at 0x585a7090 in testing.exe:
0xC0000005: Access violation reading location 0x00000000.

它的错误在于featureDetector->detect(queryImage, queryKeypoints).

我无法从该问题中找到解决方案。 请帮我。

#include <opencv2\highgui\highgui.hpp>
#include <opencv2\features2d\features2d.hpp>
#include <opencv2\contrib\contrib.hpp>
#include <iostream>
#include <fstream>
#include <conio.h>
#include <string>

using namespace std;
using namespace cv;

static void readTrainFilenames(const string& filename, string& dirName, vector<string>& trainFilenames);
static bool readImages(const string& queryImageName,  Mat& queryImage);
static bool readTrainImages(const string& trainFilename, vector<Mat>& trainImages, vector<string>& trainImageNames);
static void detectKeypoints(const Mat& queryImage, vector<KeyPoint>& queryKeypoints,
    const vector<Mat>& trainImages, vector<vector<KeyPoint>>& trainKeypoints, Ptr<FeatureDetector>& featureDetector);


static void readTrainFilenames(const string& filename, string& dirName, vector<string>& trainFilenames)
{
    trainFilenames.clear();

    ifstream file(filename.c_str());

    if(!file.is_open())
    {
        cout << "File can't open" << endl;
        return;
    }

    size_t pos = filename.rfind("\\");
    char dlmtr = '\\';

    if(pos == String::npos)
    {
        pos = filename.rfind('/');
        dlmtr = '/';
    }

    dirName = pos == string::npos ? "" : filename.substr(0, pos) + dlmtr;

    while(!file.eof())
    {
        string str; getline(file, str);
        if(str.empty()) break;
        trainFilenames.push_back(str);
    } // end while

    file.close();
} // end void readTrainFilenames


static bool readImages(const string& queryImageName,  Mat& queryImage)
{
    cout << "reading  images..." << endl;
    queryImage = imread(queryImageName, CV_LOAD_IMAGE_GRAYSCALE);
    if(queryImage.empty())
    {
        cout << "query image can not be read. \n";
        return false;
    } // end if


    return true;
}

static bool readTrainImages(const string& trainFilename, vector<Mat>& trainImages, vector<string>& trainImageNames)
{
    cout << "reading training images..." << endl;
    string trainDirName = "D:/matching_to_many_images/";

    readTrainFilenames(trainFilename, trainDirName, trainImageNames);
    if(trainImageNames.empty())
    {
        cout << "Train image filenames can not be read." << endl;
        return false;
    } // end if

    int readImageCount = 0;
    for(size_t i = 0; i < trainImageNames.size(); i++)
    {
        string filename = trainDirName + trainImageNames[i];
        Mat img = imread(filename, CV_LOAD_IMAGE_GRAYSCALE);
        if(img.empty())
        {
            cout << "Train image " << filename << " can not be read." << endl;
        }
        else
        {
            readImageCount++;
        }// end if

        trainImages.push_back(img);
    } // end for

    if(!readImageCount)
    {
        cout << "All train images can not be read." << endl;
        return false;
    }
    else
    {
        cout << readImageCount << " train images were read." << endl;
    }

    cout << endl;

    return true;
}

static void detectKeypoints(const Mat& queryImage, vector<KeyPoint>& queryKeypoints,
                            const vector<Mat>& trainImages, 
                            vector<vector<KeyPoint>>& trainKeypoints, 
                            Ptr<FeatureDetector>& featureDetector){
    cout << endl << "Extracting keypoints from images..." << endl;

    try{
        featureDetector->detect(queryImage, queryKeypoints);
    }
    catch(Ptr<FeatureDetector> a)
    {
        cout << "hmm" << endl;
    }

    cout << endl;
} // end void detectKeypoints

int main()
{
    const string defaultDetectorType = "SURF";
    const string defaultDescriptorType = "SURF";
    const string defaultMatcherType = "FlannBased";
    const string defaultQueryImageName = "D:/matching_to_many_images/query.png";
    const string defaultFileWithTrainImages = "D:/matching_to_many_images/train/trainImages.txt";
    const string defaultDirToSaveResImages = "D:/matching_to_many_images/results";

    Ptr<FeatureDetector> featureDetector;
    Ptr<DescriptorExtractor> descriptorExtractor;
    Ptr<DescriptorMatcher> descriptorMatcher;

    Mat queryImages;
    vector<Mat> trainImages;
    vector<string> trainImagesNames;

    vector<KeyPoint> queryKeypoints;
    vector<vector<KeyPoint>> trainKeypoints;

    if(!readImages(defaultQueryImageName, queryImages))
    {
        _getch();
        return -1;
    } // end if

    if(!readTrainImages(defaultFileWithTrainImages, trainImages, trainImagesNames))
    {
        _getch();
        return -1;
    }

    detectKeypoints(queryImages, queryKeypoints, trainImages, trainKeypoints, featureDetector);

    cout << "\n done \n";
    _getch();
    return 0;
} // end main method

Update:

#include <opencv2\highgui\highgui.hpp>
#include <opencv2\features2d\features2d.hpp>
#include <opencv2\contrib\contrib.hpp>
#include <iostream>
#include <fstream>
#include <conio.h>
#include <string>

using namespace std;
using namespace cv;

static void readTrainFilenames(const string& filename, string& dirName, vector<string>& trainFilenames);
static bool readImages(const string& queryImageName,  Mat& queryImage);
static bool readTrainImages(const string& trainFilename, vector<Mat>& trainImages, vector<string>& trainImageNames);
static void detectKeypoints(const Mat& queryImage, vector<KeyPoint>& queryKeypoints, Ptr<FeatureDetector>& featureDetector);


static void readTrainFilenames(const string& filename, string& dirName, vector<string>& trainFilenames)
{
    trainFilenames.clear();

    ifstream file(filename.c_str());

    if(!file.is_open())
    {
        cout << "File can't open" << endl;
        return;
    }

    size_t pos = filename.rfind("\\");
    char dlmtr = '\\';

    if(pos == String::npos)
    {
        pos = filename.rfind('/');
        dlmtr = '/';
    }

    dirName = pos == string::npos ? "" : filename.substr(0, pos) + dlmtr;

    while(!file.eof())
    {
        string str; getline(file, str);
        if(str.empty()) break;
        trainFilenames.push_back(str);
    } // end while

    file.close();
} // end void readTrainFilenames


static bool readImages(const string& queryImageName,  Mat& queryImage)
{
    cout << "reading  images..." << endl;
    queryImage = imread(queryImageName, CV_LOAD_IMAGE_GRAYSCALE);
    if(queryImage.empty())
    {
        cout << "query image can not be read. \n";
        return false;
    } // end if


    return true;
}

static bool readTrainImages(const string& trainFilename, vector<Mat>& trainImages, vector<string>& trainImageNames)
{
    cout << "reading training images..." << endl;
    string trainDirName = "D:/matching_to_many_images/";

    readTrainFilenames(trainFilename, trainDirName, trainImageNames);
    if(trainImageNames.empty())
    {
        cout << "Train image filenames can not be read." << endl;
        return false;
    } // end if

    int readImageCount = 0;
    for(size_t i = 0; i < trainImageNames.size(); i++)
    {
        string filename = trainDirName + trainImageNames[i];
        Mat img = imread(filename, CV_LOAD_IMAGE_GRAYSCALE);
        if(img.empty())
        {
            cout << "Train image " << filename << " can not be read." << endl;
        }
        else
        {
            readImageCount++;
        }// end if

        trainImages.push_back(img);
    } // end for

    if(!readImageCount)
    {
        cout << "All train images can not be read." << endl;
        return false;
    }
    else
    {
        cout << readImageCount << " train images were read." << endl;
    }

    cout << endl;

    return true;
}

static void detectKeypoints(const Mat& queryImage, vector<KeyPoint>& queryKeypoints, Ptr<FeatureDetector>& featureDetector){
    cout << endl << "Extracting keypoints from images..." << endl;

    featureDetector->detect(queryImage, queryKeypoints);


    cout << endl;
} // end void detectKeypoints

int main()
{
    const string defaultDetectorType = "SURF";
    const string defaultDescriptorType = "SURF";
    const string defaultMatcherType = "FlannBased";
    const string defaultQueryImageName = "D:/matching_to_many_images/query.png";
    const string defaultFileWithTrainImages = "D:/matching_to_many_images/train/trainImages.txt";
    const string defaultDirToSaveResImages = "D:/matching_to_many_images/results";

    Ptr<FeatureDetector> featureDetector;
    Ptr<DescriptorExtractor> descriptorExtractor;
    Ptr<DescriptorMatcher> descriptorMatcher;

    Mat queryImages;
    vector<Mat> trainImages;
    vector<string> trainImagesNames;

    vector<KeyPoint> queryKeypoints;
    vector<vector<KeyPoint>> trainKeypoints;

    if(!readImages(defaultQueryImageName, queryImages))
    {
        _getch();
        return -1;
    } // end if

    if(!readTrainImages(defaultFileWithTrainImages, trainImages, trainImagesNames))
    {
        _getch();
        return -1;
    }

    detectKeypoints(queryImages, queryKeypoints, featureDetector);

    cout << "\n done \n";
    _getch();
    return 0;
} // end main method

已解决的问题:

#include <opencv2\highgui\highgui.hpp>
#include <opencv2\features2d\features2d.hpp>
#include <opencv2\contrib\contrib.hpp>
#include <opencv2\nonfree\nonfree.hpp>
#include <iostream>
#include <fstream>
#include <conio.h>
#include <string>

using namespace std;
using namespace cv;

const string defaultDetectorType = "SURF";
const string defaultDescriptorType = "SURF";
const string defaultMatcherType = "FlannBased";
const string defaultQueryImageName = "D:/matching_to_many_images/query.png";
const string defaultFileWithTrainImages = "D:/matching_to_many_images/train/trainImages.txt";
const string defaultDirToSaveResImages = "D:/matching_to_many_images/results";

static void readTrainFilenames(const string& filename, string& dirName, vector<string>& trainFilenames);
static bool readImages(const string& queryImageName,  Mat& queryImage);
static bool readTrainImages(const string& trainFilename, vector<Mat>& trainImages, vector<string>& trainImageNames);
static void detectKeypoints(const Mat& queryImage, vector<KeyPoint>& queryKeypoints, Ptr<FeatureDetector>& featureDetector);
static bool createDetectorDescriptorMatcher(const string& detectorType, 
                                        const string& descriptorType, 
                                        const string& matcherType,
                                        Ptr<FeatureDetector>& featureDetector,
                                        Ptr<DescriptorExtractor>& descriptorExtractor,
                                        Ptr<DescriptorMatcher>& descriptorMatcher);

static void readTrainFilenames(const string& filename, string& dirName, vector<string>& trainFilenames)
{
    trainFilenames.clear();

    ifstream file(filename.c_str());

    if(!file.is_open())
    {
        cout << "File can't open" << endl;
        return;
    }

    size_t pos = filename.rfind("\\");
    char dlmtr = '\\';

    if(pos == String::npos)
    {
        pos = filename.rfind('/');
        dlmtr = '/';
    }

    dirName = pos == string::npos ? "" : filename.substr(0, pos) + dlmtr;

    while(!file.eof())
    {
        string str; getline(file, str);
        if(str.empty()) break;
        trainFilenames.push_back(str);
    } // end while

    file.close();
} // end void readTrainFilenames


static bool readImages(const string& queryImageName,  Mat& queryImage)
{
    cout << "reading  images..." << endl;
    queryImage = imread(queryImageName, CV_LOAD_IMAGE_GRAYSCALE);
    if(queryImage.empty())
    {
        cout << "query image can not be read. \n";
        return false;
    } // end if


    return true;
}

static bool readTrainImages(const string& trainFilename, vector<Mat>& trainImages, vector<string>& trainImageNames)
{
    cout << "reading training images..." << endl;
    string trainDirName = "D:/matching_to_many_images/";

    readTrainFilenames(trainFilename, trainDirName, trainImageNames);
    if(trainImageNames.empty())
    {
        cout << "Train image filenames can not be read." << endl;
        return false;
    } // end if

    int readImageCount = 0;
    for(size_t i = 0; i < trainImageNames.size(); i++)
    {
        string filename = trainDirName + trainImageNames[i];
        Mat img = imread(filename, CV_LOAD_IMAGE_GRAYSCALE);
        if(img.empty())
        {
            cout << "Train image " << filename << " can not be read." << endl;
        }
        else
        {
            readImageCount++;
        }// end if

        trainImages.push_back(img);
    } // end for

    if(!readImageCount)
    {
        cout << "All train images can not be read." << endl;
        return false;
    }
    else
    {
        cout << readImageCount << " train images were read." << endl;
    }

    cout << endl;

    return true;
}

static void detectKeypoints(const Mat& queryImage, vector<KeyPoint>& queryKeypoints, Ptr<FeatureDetector>& featureDetector){
    cout << endl << "Extracting keypoints from images..." << endl;

    if(queryImage.empty())
    {
        cout << "Query Image EMPTY" << endl;
    } 
    else{
        cout << "Query Image FILLED" << endl;
    }

    featureDetector->detect(queryImage, queryKeypoints);


    cout << endl;
} // end void detectKeypoints

static bool createDetectorDescriptorMatcher(const string& detectorType, 
                                        const string& descriptorType, 
                                        const string& matcherType,
                                        Ptr<FeatureDetector>& featureDetector,
                                        Ptr<DescriptorExtractor>& descriptorExtractor,
                                        Ptr<DescriptorMatcher>& descriptorMatcher)
{
    cout << "Creating feature detector, descriptor extractor and descriptor matcher ... " << endl;
    featureDetector = FeatureDetector::create(detectorType);
    descriptorExtractor = DescriptorExtractor::create(descriptorType);
    descriptorMatcher = DescriptorMatcher::create(matcherType);
    cout << endl;

    if(featureDetector.empty())
    {
        cout << "feature detector empty" << endl;
    }

    if(descriptorExtractor.empty())
    {
        cout << "descriptor extractor empty" << endl;
    }

    if(descriptorMatcher.empty())
    {
        cout << "descriptor matcher empty" << endl;
    } 

    bool isCreated = !(featureDetector.empty() || descriptorExtractor.empty() || descriptorMatcher.empty());

    if(!isCreated)
    {
        cout << "can not create feature detector or descriptor extractor or descriptor matcher of given types." << endl;
    } // end if

    return isCreated;
} // end void createDetectorDescriptorMatcher

int main()
{
    initModule_nonfree();
    string detectorType = defaultDetectorType;
    string descriptorType = defaultDetectorType;
    string matcherType = defaultMatcherType;
    string queryImageName = defaultQueryImageName;
    string fileWithTrainImages = defaultFileWithTrainImages;
    string dirToSaveResImages = defaultDirToSaveResImages;

    Ptr<FeatureDetector> featureDetector = FeatureDetector::create("SURF");
    Ptr<DescriptorExtractor> descriptorExtractor = DescriptorExtractor::create("SURF");
    Ptr<DescriptorMatcher> descriptorMatcher;

    if(!createDetectorDescriptorMatcher(detectorType, descriptorType, matcherType, featureDetector, descriptorExtractor, descriptorMatcher))
    {

        _getch();
        return -1;
    }


    Mat queryImages;
    vector<Mat> trainImages;
    vector<string> trainImagesNames;

    vector<KeyPoint> queryKeypoints;
    vector<vector<KeyPoint>> trainKeypoints;

    if(!readImages(defaultQueryImageName, queryImages))
    {
        _getch();
        return -1;
    } // end if

    if(!readTrainImages(defaultFileWithTrainImages, trainImages, trainImagesNames))
    {
        _getch();
        return -1;
    }

    detectKeypoints(queryImages, queryKeypoints, featureDetector);

    cout << "\n done \n";
    _getch();
    return 0;
} // end main method

与许多图像匹配的完整示例代码:

#include <opencv2\highgui\highgui.hpp>
#include <opencv2\features2d\features2d.hpp>
#include <opencv2\contrib\contrib.hpp>
#include <opencv2\nonfree\nonfree.hpp>
#include <iostream>
#include <fstream>
#include <conio.h>
#include <string>

using namespace std;
using namespace cv;

const string defaultDetectorType = "SURF";
const string defaultDescriptorType = "SURF";
const string defaultMatcherType = "FlannBased";
const string defaultQueryImageName = "D:/matching_to_many_images/query.png";
const string defaultFileWithTrainImages = "D:/matching_to_many_images/train/trainImages.txt";
const string defaultDirToSaveResImages = "D:/matching_to_many_images/results";

static void readTrainFilenames(const string& filename, string& dirName, vector<string>& trainFilenames);
static bool readImages(const string& queryImageName,  Mat& queryImage);
static bool readTrainImages(const string& trainFilename, vector<Mat>& trainImages, vector<string>& trainImageNames);
static void detectKeypoints(const Mat& queryImage, vector<KeyPoint>& queryKeypoints, const vector<Mat>& trainImages, vector<vector<KeyPoint>>& trainKeypoints, Ptr<FeatureDetector>& featureDetector);
static bool createDetectorDescriptorMatcher(const string& detectorType, 
                                        const string& descriptorType, 
                                        const string& matcherType,
                                        Ptr<FeatureDetector>& featureDetector,
                                        Ptr<DescriptorExtractor>& descriptorExtractor,
                                        Ptr<DescriptorMatcher>& descriptorMatcher);
static void computeDescriptors(const Mat& queryImage, vector<KeyPoint>& queryKeypoints, Mat& queryDescriptors, 
    const vector<Mat>& trainImages, vector<vector<KeyPoint>>& trainKeypoints, vector<Mat>& trainDescriptors,
    Ptr<DescriptorExtractor>& descriptorExtractor);
static void matchDescriptors(const Mat& queryDescriptors, const vector<Mat>& trainDescriptors, vector<DMatch>& matches, Ptr<DescriptorMatcher>& descriptorMatcher);
static void maskMatchesByTrainImgIdx(const vector<DMatch>& matches, int trainImgIdx, vector<char>& mask);

static void readTrainFilenames(const string& filename, string& dirName, vector<string>& trainFilenames)
{
    trainFilenames.clear();

    ifstream file(filename.c_str());

    if(!file.is_open())
    {
        cout << "File can't open" << endl;
        return;
    }

    size_t pos = filename.rfind("\\");
    char dlmtr = '\\';

    if(pos == String::npos)
    {
        pos = filename.rfind('/');
        dlmtr = '/';
    }

    dirName = pos == string::npos ? "" : filename.substr(0, pos) + dlmtr;

    while(!file.eof())
    {
        string str; getline(file, str);
        if(str.empty()) break;
        trainFilenames.push_back(str);
    } // end while

    file.close();
} // end void readTrainFilenames


static bool readImages(const string& queryImageName,  Mat& queryImage)
{
    cout << "reading  images..." << endl;
    queryImage = imread(queryImageName, CV_LOAD_IMAGE_GRAYSCALE);
    if(queryImage.empty())
    {
        cout << "query image can not be read. \n";
        return false;
    } // end if


    return true;
}

static bool readTrainImages(const string& trainFilename, vector<Mat>& trainImages, vector<string>& trainImageNames)
{
    cout << "reading training images..." << endl;
    string trainDirName = "D:/matching_to_many_images/";

    readTrainFilenames(trainFilename, trainDirName, trainImageNames);
    if(trainImageNames.empty())
    {
        cout << "Train image filenames can not be read." << endl;
        return false;
    } // end if

    int readImageCount = 0;
    for(size_t i = 0; i < trainImageNames.size(); i++)
    {
        string filename = trainDirName + trainImageNames[i];
        Mat img = imread(filename, CV_LOAD_IMAGE_GRAYSCALE);
        if(img.empty())
        {
            cout << "Train image " << filename << " can not be read." << endl;
        }
        else
        {
            readImageCount++;
        }// end if

        trainImages.push_back(img);
    } // end for

    if(!readImageCount)
    {
        cout << "All train images can not be read." << endl;
        return false;
    }
    else
    {
        cout << readImageCount << " train images were read." << endl;
    }

    cout << endl;

    return true;
}

static void detectKeypoints(const Mat& queryImage, vector<KeyPoint>& queryKeypoints, const vector<Mat>& trainImages, vector<vector<KeyPoint>>& trainKeypoints, Ptr<FeatureDetector>& featureDetector){
    cout << endl << "Extracting keypoints from images..." << endl;

    if(queryImage.empty())
    {
        cout << "Query Image EMPTY" << endl;
    } 
    else{
        cout << "Query Image FILLED" << endl;
    }

    featureDetector->detect(queryImage, queryKeypoints);
    featureDetector->detect(trainImages, trainKeypoints);

    cout << endl;
} // end void detectKeypoints

static bool createDetectorDescriptorMatcher(const string& detectorType, 
                                        const string& descriptorType, 
                                        const string& matcherType,
                                        Ptr<FeatureDetector>& featureDetector,
                                        Ptr<DescriptorExtractor>& descriptorExtractor,
                                        Ptr<DescriptorMatcher>& descriptorMatcher)
{
    cout << "Creating feature detector, descriptor extractor and descriptor matcher ... " << endl;
    featureDetector = FeatureDetector::create(detectorType);
    descriptorExtractor = DescriptorExtractor::create(descriptorType);
    descriptorMatcher = DescriptorMatcher::create(matcherType);
    cout << endl;

    if(featureDetector.empty())
    {
        cout << "feature detector empty" << endl;
    }

    if(descriptorExtractor.empty())
    {
        cout << "descriptor extractor empty" << endl;
    }

    if(descriptorMatcher.empty())
    {
        cout << "descriptor matcher empty" << endl;
    } 

    bool isCreated = !(featureDetector.empty() || descriptorExtractor.empty() || descriptorMatcher.empty());

    if(!isCreated)
    {
        cout << "can not create feature detector or descriptor extractor or descriptor matcher of given types." << endl;
    } // end if

    return isCreated;
} // end void createDetectorDescriptorMatcher

static void computeDescriptors(const Mat& queryImage, vector<KeyPoint>& queryKeypoints, Mat& queryDescriptors, 
    const vector<Mat>& trainImages, vector<vector<KeyPoint>>& trainKeypoints, vector<Mat>& trainDescriptors,
    Ptr<DescriptorExtractor>& descriptorExtractor)
{
    cout << "computing descriptors for keypoints..." << endl;
    descriptorExtractor->compute(queryImage, queryKeypoints, queryDescriptors);
    descriptorExtractor->compute(trainImages, trainKeypoints, trainDescriptors);

    int totalTrainDesc = 0;
    for(vector<Mat>::const_iterator tdIter = trainDescriptors.begin(); tdIter != trainDescriptors.end(); tdIter++)
        totalTrainDesc += tdIter->rows;

    cout << "Query descriptors count : " << queryDescriptors.rows << "; Total train descriptors count : " << totalTrainDesc << endl;
    cout << endl;
} // end void computeDescriptors

static void matchDescriptors(const Mat& queryDescriptors, const vector<Mat>& trainDescriptors, vector<DMatch>& matches, Ptr<DescriptorMatcher>& descriptorMatcher)
{
    cout << "Set train descriptors collection in the matcher and match query descriptors to them..." << endl;
    TickMeter tm;
    tm.start();
    descriptorMatcher->add(trainDescriptors);
    descriptorMatcher->train();
    tm.stop();
    double buildTime = tm.getTimeMilli();

    tm.start();
    descriptorMatcher->match(queryDescriptors, matches);
    tm.stop();
    double matchTime = tm.getTimeMilli();

    CV_Assert(queryDescriptors.rows == (int)matches.size() || matches.empty());

    cout << "Number of matches: " << matches.size() << endl;
    cout << "Build time: " << buildTime << " ms; Match time: " << matchTime << " ms" << endl;
    cout << endl;
} // end void matchDescriptors

static void saveResultImages(const Mat& queryImage, const vector<KeyPoint>& queryKeypoints,
    const vector<Mat>& trainImages, const vector<vector<KeyPoint>> &trainKeypoints, const vector<DMatch>& matches, 
    const vector<string>& trainImageNames, const string& resultDir)
{
    cout << "Save results..." << endl;
    Mat drawImg;
    vector<char> mask;
    for(size_t i = 0; i < trainImages.size(); i++)
    {
        if(!trainImages[i].empty())
        {
            maskMatchesByTrainImgIdx(matches, (int)i, mask);
            drawMatches(queryImage, queryKeypoints, trainImages[i], trainKeypoints[i], matches, drawImg, Scalar(255, 0, 0), Scalar(0, 255, 255), mask);
            string filename = resultDir + "/res_" + trainImageNames[i];
            if(!imwrite(filename, drawImg))
            {
                cout << "Image " << filename << " can not be saved (may be because directory " << resultDir << " does not exist" << endl;
            } // end if
        } // end if
    }
} // end void saveResultImages

static void maskMatchesByTrainImgIdx(const vector<DMatch>& matches, int trainImgIdx, vector<char>& mask)
{
    mask.resize(matches.size());
    fill(mask.begin(), mask.end(), 0);
    for(size_t i = 0; i < matches.size(); i++)
    {
        if(matches[i].imgIdx == trainImgIdx)
        {
            mask[i] = 1;
        }
    }
} // end void maskMatchesByTrainImgIdx

int main()
{
    initModule_nonfree();
    string detectorType = defaultDetectorType;
    string descriptorType = defaultDetectorType;
    string matcherType = defaultMatcherType;
    string queryImageName = defaultQueryImageName;
    string fileWithTrainImages = defaultFileWithTrainImages;
    string dirToSaveResImages = defaultDirToSaveResImages;

    Ptr<FeatureDetector> featureDetector = FeatureDetector::create("SURF");
    Ptr<DescriptorExtractor> descriptorExtractor = DescriptorExtractor::create("SURF");
    Ptr<DescriptorMatcher> descriptorMatcher;

    if(!createDetectorDescriptorMatcher(detectorType, descriptorType, matcherType, featureDetector, descriptorExtractor, descriptorMatcher))
    {

        _getch();
        return -1;
    }

    Mat queryImages;
    vector<Mat> trainImages;
    vector<string> trainImagesNames;

    vector<KeyPoint> queryKeypoints;
    vector<vector<KeyPoint>> trainKeypoints;

    if(!readImages(defaultQueryImageName, queryImages))
    {
        _getch();
        return -1;
    } // end if

    if(!readTrainImages(defaultFileWithTrainImages, trainImages, trainImagesNames))
    {
        _getch();
        return -1;
    }

    detectKeypoints(queryImages, queryKeypoints, trainImages, trainKeypoints, featureDetector);

    Mat queryDescriptors;
    vector<Mat> trainDescriptors;

    computeDescriptors(queryImages, queryKeypoints, queryDescriptors, trainImages, trainKeypoints, trainDescriptors, descriptorExtractor);

    vector<DMatch> matches;
    matchDescriptors(queryDescriptors, trainDescriptors, matches, descriptorMatcher);

    saveResultImages(queryImages, queryKeypoints, trainImages, trainKeypoints, matches, trainImagesNames, dirToSaveResImages);

    cout << "\n done \n";
    _getch();
    return 0;
} // end main method

The 文档 http://docs.opencv.org/modules/features2d/doc/common_interfaces_of_feature_detectors.html?highlight=feature%20detector#FeatureDetector%20%3a%20public%20Algorithm上课FeatureDetector说它是一个抽象基类,这意味着您不应该能够创建该类的实例。这是 OpenCV 的错,编译器没有抱怨!

尝试添加:

Ptr<FeatureDetector> featureDetector = FeatureDetector::create(defaultDetectorType);

Update:

我的下一个建议是降低复杂性。简化主程序,直到获得最小的工作版本:

int main()
{
    cv::initModule_nonfree(); // to load SURF/SIFT etc.
    std::vector<cv::KeyPoint> queryKeypoints;
    cv::Mat queryImage = cv::imread(FILENAME, CV_LOAD_IMAGE_GRAYSCALE);
    cv::Ptr<FeatureDetector> featureDetector = cv::FeatureDetector::create("SURF");
    featureDetector->detect(queryImage, queryKeypoints);
}

如果上述版本有效,请开始(慢慢地)添加更多功能,直到达到当前版本。当错误再次出现时,您就知道最后添加的部分是罪魁祸首,您可以专注于此。

如果上述版本不起作用,您至少已经创建了一个SSCCE http://sscce.org/,您可以尝试修复(在其他人的帮助下)。

顺便说一句:错误消息告诉您,您的程序正在尝试读取内存位置0x00000000这是一个指示器,表明您正在使用未初始化的数据结构,但我不确定问题出在您的程序中。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

OpenCV 2.4.5 中的访问冲突读取 的相关文章

  • 删除文件的最后 10 个字符

    我想删除文件的最后 10 个字符 说一个字符串 hello i am a c learner 是文件内的数据 我只是希望该文件是 hello i am a 文件的最后 10 个字符 即字符串 c learner 应在文件内消除 解决方案 将
  • 在一个数据访问层中处理多个连接字符串

    我有一个有趣的困境 我目前有一个数据访问层 它必须与多个域一起使用 并且每个域都有多个数据库存储库 具体取决于所调用的存储过程 目前 我只需使用 SWITCH 语句来确定应用程序正在运行的计算机 并从 Web config 返回适当的连接字
  • 机器Epsilon精度差异

    我正在尝试计算 C 中双精度数和浮点数的机器 epsilon 值 作为学校作业的一部分 我在 Windows 7 64 位中使用 Cygwin 代码如下 include
  • free 和 malloc 在 C 中如何工作?

    我试图弄清楚如果我尝试 从中间 释放指针会发生什么 例如 看下面的代码 char ptr char malloc 10 sizeof char for char i 0 i lt 10 i ptr i i 10 ptr ptr ptr pt
  • 为什么 GCC 不允许我创建“内联静态 std::stringstream”?

    我将直接前往 MCVE include
  • 如何从本机 C(++) DLL 调用 .NET (C#) 代码?

    我有一个 C app exe 和一个 C my dll my dll NET 项目链接到本机 C DLL mynat dll 外部 C DLL 接口 并且从 C 调用 C DLL 可以正常工作 通过使用 DllImport mynat dl
  • -webkit-box-shadow 与 QtWebKit 模糊?

    当时有什么方法可以实现 webkit box shadow 的工作模糊吗 看完这篇评论错误报告 https bugs webkit org show bug cgi id 23291 我认识到这仍然是一个问题 尽管错误报告被标记为RESOL
  • 无限循环与无限递归。两者都是未定义的吗?

    无副作用的无限循环是未定义的行为 看here https coliru stacked crooked com view id 24e0a58778f67cd4举个例子参考参数 https en cppreference com w cpp
  • 如何使从 C# 调用的 C(P/invoke)代码“线程安全”

    我有一些简单的 C 代码 它使用单个全局变量 显然这不是线程安全的 所以当我使用 P invoke 从 C 中的多个线程调用它时 事情就搞砸了 如何为每个线程单独导入此函数 或使其线程安全 我尝试声明变量 declspec thread 但
  • 用于 FTP 的文件系统观察器

    我怎样才能实现FileSystemWatcherFTP 位置 在 C 中 这个想法是 每当 FTP 位置添加任何内容时 我都希望将其复制到我的本地计算机 任何想法都会有所帮助 这是我之前问题的后续使用 NET 进行选择性 FTP 下载 ht
  • 对类 static constexpr 结构的未定义引用,g++ 与 clang

    这是我的代码 a cp p struct int2 int x y struct Foo static constexpr int bar1 1 static constexpr int2 bar2 1 2 int foo1 return
  • C++ 多行字符串原始文字[重复]

    这个问题在这里已经有答案了 我们可以像这样定义一个多行字符串 const char text1 part 1 part 2 part 3 part 4 const char text2 part 1 part 2 part 3 part 4
  • WPF 数据绑定到复合类模式?

    我是第一次尝试 WPF 并且正在努力解决如何将控件绑定到使用其他对象的组合构建的类 例如 如果我有一个由两个单独的类组成的类 Comp 为了清楚起见 请注意省略的各种元素 class One int first int second cla
  • 重载 (c)begin/(c)end

    我试图超载 c begin c end类的函数 以便能够调用 C 11 基于范围的 for 循环 它在大多数情况下都有效 但我无法理解和解决其中一个问题 for auto const point fProjectData gt getPoi
  • LINQ:使用 INNER JOIN、Group 和 SUM

    我正在尝试使用 LINQ 执行以下 SQL 最接近的是执行交叉联接和总和计算 我知道必须有更好的方法来编写它 所以我向堆栈团队寻求帮助 SELECT T1 Column1 T1 Column2 SUM T3 Column1 AS Amoun
  • C 函数 time() 如何处理秒的小数部分?

    The time 函数将返回自 1970 年以来的秒数 我想知道它如何对返回的秒数进行舍入 例如 对于100 4s 它会返回100还是101 有明确的定义吗 ISO C标准没有说太多 它只说time 回报 该实现对当前日历时间的最佳近似 结
  • 相当于Linux中的导入库

    在 Windows C 中 当您想要链接 DLL 时 您必须提供导入库 但是在 GNU 构建系统中 当您想要链接 so 文件 相当于 dll 时 您就不需要链接 为什么是这样 是否有等效的 Windows 导入库 注意 我不会谈论在 Win
  • 对于某些 PDF 文件,LoadIFilter() 返回 -2147467259

    我正在尝试使用 Adob e IFilter 搜索 PDF 文件 我的代码是用 C 编写的 我使用 p invoke 来获取 IFilter 的实例 DllImport query dll SetLastError true CharSet
  • 当文件流没有新数据时如何防止fgets阻塞

    我有一个popen 执行的函数tail f sometextfile 只要文件流中有数据显然我就可以通过fgets 现在 如果没有新数据来自尾部 fgets 挂起 我试过ferror and feof 无济于事 我怎样才能确定fgets 当
  • 类型或命名空间“MyNamespace”不存在等

    我有通常的类型或命名空间名称不存在错误 除了我引用了程序集 using 语句没有显示为不正确 并且我引用的类是公共的 事实上 我在不同的解决方案中引用并使用相同的程序集来执行相同的操作 并且效果很好 顺便说一句 这是VS2010 有人有什么

随机推荐