尝试在 opencv 4.5.1 中使用 SIFT 提取特征时出现的问题

问题描述

我试图在 opencv 4.5.1 中使用 SIFT 提取图像的特征,但是当我尝试使用 drawKeypoints() 检查结果时,我不断收到这个神秘错误

OpenCV(4.5.1) Error: Assertion Failed (!fixedType() || ((Mat*)obj)->type() == mtype) in cv::debug_build_guard::_OutputArray::create,file C:\build\master_winpack-build-win64-vc14\opencv\modules\core\src\matrix_wrap.cpp,line 1147

D:\School\IP2\OpenCVApplication-VS2019_OCV451_basic\x64\Debug\OpenCVApplication.exe (process 6140) exited with code -1.
To automatically close the console when debugging stops,enable Tools->Options->Debugging->Automatically close the console when debugging stops.

问题似乎出在 drawKeypoints() 函数上,但我不确定是什么导致了问题。

功能

vector<KeyPoint> extractFeatures(String path) {

    Mat_<uchar> source = imread(path,0);

    Mat_<uchar> output(source.rows,source.cols); 
    vector<KeyPoint> keypoints;

    Ptr<SIFT> sift = SIFT::create();
    sift->detect(source,keypoints);

    drawKeypoints(source,keypoints,output);
    imshow("sift_result",output);

    return keypoints;
}

解决方法

您收到异常,因为 outputdrawKeypoints 参数必须是 3 通道彩色图像,并且您正在将 output 初始化为 1 通道(灰度)图像。

当使用:Mat output(source.rows,source.cols);Mat output; 时,drawKeypoints 函数会自动创建一个新的彩色矩阵。
使用派生模板矩阵类 Mat_<uchar> 时,函数 drawKeypoints 引发异常!

您可以将:Mat_<uchar> output(source.rows,source.cols); 替换为:

Mat_<Vec3b> output(source.rows,source.cols); //Create 3 color channels image (matrix).

注意:
您也可以使用 Mat 代替 Mat_

Mat output; //The matrix is going to be dynamically allocated inside drawKeypoints function.  

注意:

  • 我当前的 OpenCV 版本 (4.2.0) 不支持 SIFT,所以我改用 ORB(用于测试)。

这是用于测试的代码示例:

#include "opencv2/opencv.hpp"

using namespace cv;
using namespace std;

int main()
{   
    Mat_<uchar> source = imread("graf.png",0);

    Mat_<Vec3b> output(source.rows,source.cols); //Create 3 color channels image (matrix).
    vector<KeyPoint> keypoints;

    Ptr<ORB> orb = ORB::create();
    orb->detect(source,keypoints);

    drawKeypoints(source,keypoints,output);
    imshow("orb_result",output);

    waitKey(0);
    destroyAllWindows();

    return 0;
}

结果:
enter image description here