【问题标题】:detector->detect(img, keypoint); error检测器->检测(img,关键点);错误
【发布时间】:2026-02-17 01:55:02
【问题描述】:

我想在 opencv 中实现词袋。在检测器->检测(img,关键点)之后;检测关键点,当我想使用 keypoint.clear() 清理关键点时;或者当函数要返回时会出现以下错误。

BOW.exe 中 0x011f45bb 处的未处理异常:0xC0000005:访问冲突读取位置 0x42ebe098。

并且还检测到的关键点具有奇异的点坐标,例如 cv::Point_pt{x=-1.5883997e+038y=-1.5883997e+038 }

部分代码

Ptr<DescriptorMatcher> matcher = DescriptorMatcher::create("FlannBased");
Ptr<DescriptorExtractor> extractor = new SurfDescriptorExtractor();
Ptr<FeatureDetector> detector = new SurfFeatureDetector(2000);
    void extractTrainingVocabulary() {
        IplImage *img;
        int i,j;
         CvSeq *imageKeypoints = 0;
        for(j=1;j<=60;j++)
        for(i=1;i<=60;i++){
            sprintf( ch,"%d%s%d%s",j," (",i,").jpg");
                        const char* imageName = ch;
                        Mat img = imread(ch);
                        vector<KeyPoint> keypoint;
                        detector->detect(img, keypoint);
                        Mat features;
                        extractor->compute(img, keypoint, features);
                        bowTrainer.add(features);
                        keypoint.clear();//problem
        }
    return;
    }

【问题讨论】:

    标签: opencv


    【解决方案1】:

    我注意到您的代码在extractTrainingVocabulary() 上声明了IplImage* img; 并在循环内声明了另一个具有相同名称(但类型不同)的变量:Mat img = imread(ch);

    尽管这可能不是问题,但这肯定不是好的做法。我会立即解决这个问题并更新您问题的代码。

    【讨论】:

    • 谢谢卡尔菲利普。我真的不明白是什么问题。我只新建一个项目,复制粘贴代码,执行代码没有任何问题。