2015-04-28 84 views
3

匹配的描述我提取descriptors然後將其保存到文件是這樣的:opecv C++從文件

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; 

with th e 2的cv::Mat s此方式輪:

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

A.K.A match(Scene, Object, ...)輸出爲:minDist = 100 maxDist = 0(不是一個單一的匹配)。

但是他們這樣一輪:

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

AKA match(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(); 
+0

'matcher-> match'的兩個錯誤意味着'filesDVec [i]'可能是一個空的Mat。確保'fs [文件名] >> temp;'讀取您指定的文件。 – mcchu

+0

我查過了,確實如此。 – MLMLTL

回答

1

的問題是,你分配,構建filesDVecimgVec.size()元素(它們是空cv::Mat S)。然後,每個描述符(您在for循環中加載)將被添加到向量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(); 
0

你打開READ模式文件?否則,opencv會在讀取文件之前清空文件。

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

而不是

FileStorage fsKpts(filename, FileStorage::WRITE); 
+0

我知道'FileStorage'運行的方式,請參閱我的編輯 – MLMLTL