3

我提取descriptors然后将它们保存到这样的文件中:

detector->detect(img, imgKpts);
extractor->compute(img, imgKpts, imgMat);
fsKpts << filename << imgMat;

但是当我read像这样再次回来时:

std::vector<cv::Mat> filesDVec;
cv::Mat temp;
fs[filename] >> temp;
filesDVec.push_back(temp);

并带有加载的图像matchdescriptors

cv::Mat givenIn, givenInMat;
givenIn = cv::imread(dataDirGivenIn, CV_LOAD_IMAGE_GRAYSCALE);
cv::vector<cv::KeyPoint> givenInKpts;
detector->detect(givenIn, givenInKpts);
extractor->compute(givenIn, givenInKpts, givenInMat);
cv::vector<cv::DMatch> matchesVector;

用 2 cv::Mats 这样循环:

matcher->match(filesDVec[i], givenInMat, matchesVector);

AKAmatch(Scene, Object, ...)输出是:(minDist = 100 maxDist = 0不是一个匹配项)。

但是这样对待他们:

matcher->match(givenInMat, filesDVec[i], matchesVector);

AKAmatch(Object, Scene, ...)抛出此错误:

opencv error:assertion failed (type == src2.type() && src1.cols == src2.cols && (type == CV_32F || type = CV_8U)) in void cv::batchDistance

我想保存每个图像的描述性信息,以便可以加载它,我做错了什么?

编辑

我要补充一点,这不是尝试匹配和图像自身的情况,因为众所周知,matcher如果对象和源图像对于测试目的相等,则它不起作用。

编辑 2

文件内容:

Image_0: !!opencv-matrix
   rows: 315
   cols: 32
   dt: u
   data: [ 0, 128, 196, 159, 108, 136, 172, 39, 188, 3, 114, 16, 172,
       234, 0, 66, 74, 43, 46, 128, 64, 172, 67, 239, 4, 54, 218, 8, 84,
       0, 225, 136, 160, 133, 68, 155, 204, 136, 232, 47, 61, 17, 115,
       18, 236, 106, 8, 81, 107, 131, 46, 128, 114, 56, 67, 213, 12, 50,
       218, 64, 21, 8, 209, 136, 180, 69, 70, 142, 28, 130, 238, 96, 141,
       128, 243, 2, 74, 74, 37, 65, 120, 161, 78, 226, 104, 163, 0, 204,
...
etc

读:

std::vector<cv::Mat> filesDVec(imgVec.size());
cv::Mat temp;
cv::FileStorage fs("tileDesc.yml", cv::FileStorage::READ);
for(size_t i = 0; i < (imgVec.size()); i++){
    cv::string iValue = std::to_string(i);
    cv::string filename = "Image_" + iValue;
    fs[filename] >> temp;
    filesDVec.push_back(temp);  
}
fs.release();
4

2 回答 2

1

问题是您filesDVec使用imgVec.size()元素分配和构造(它们是空cv::Mat的)。然后每个描述符(您在for循环中加载)将被添加到 vector 的末尾filesDVec

因此,您尝试将一些空匹配cv::MatgivenInMat并且很可能会导致应用程序崩溃或断言。尝试如下阅读:

std::vector<cv::Mat> filesDVec;
cv::Mat temp;
cv::FileStorage fs("tileDesc.yml", cv::FileStorage::READ);

for(size_t i = 0; i < (imgVec.size()); i++){
    cv::string iValue = std::to_string(i);
    cv::string filename = "Image_" + iValue;
    fs[filename] >> temp;
    filesDVec.push_back(temp);  
}

fs.release();
于 2015-05-05T12:31:02.010 回答
0

您是否以 READ 模式打开文件?否则 opencv 将在读取文件之前清空文件。

FileStorage fsKpts(filename, FileStorage::READ);
fsKpts[filename] >> temp;

代替

FileStorage fsKpts(filename, FileStorage::WRITE);
于 2015-05-02T20:41:56.813 回答