opencv3.4.5添加contrib后,使用sift算法做特征点识别时报错,之前以为是opencv编译过程中设置不对,亦或是版本问题,又重新编译了好几遍,都还是遇到同样的问题。

#include "opencv2/imgproc.hpp"
#include "opencv2/videoio.hpp"
#include "opencv2/highgui.hpp"
#include "opencv2/xfeatures2d.hpp"
#include <iostream>
#include <ctype.h>
using namespace cv;
using namespace std;
using namespace xfeatures2d;
void main()
{
Mat matSrc = imread("C:\\Users\\Administrator\\Desktop\\lena.jpg");
Mat gray;
Mat draw;
cvtColor(matSrc, gray, CV_RGB2GRAY);
Mat descriptors;
namedWindow("lena");
imshow("lena", matSrc);
std::vector<KeyPoint> keypoints;
// 生产sift结构
Ptr<SiftFeatureDetector> siftFD = SiftFeatureDetector::create();
siftFD->detectAndCompute(gray, Mat(), keypoints, draw);
drawKeypoints(gray, keypoints, gray, Scalar(0, 0, 255), DrawMatchesFlags::DEFAULT);
imshow("gray", gray);
waitKey(0);
system("pause");
return;
};
最后发现问题是出在了imread的路径上,我的路径是从图片属性中找到的

从那里复制图片路径会导致路径文字编码出错
比如复制C:\\lena.jpg
,放到vs代码中,它会被转化为\u202AC:\\lena.jpg
,只是再vs中\u202A
会被自动隐藏,你是看不到的,但是在运行过程中\u202A
就会被加进去,导致无法找到图片,最终上图错误。
解决方法:
动手打路径,或者换个地方复制
又一次被编码问题恶心到。。。。。。
网友评论