Opecv C ++ mapping descriptors from file
I extract descriptors
and then save them to a file like this:
detector->detect(img, imgKpts); extractor->compute(img, imgKpts, imgMat); fsKpts << filename << imgMat;
but when i read
go back like this:
std::vector<cv::Mat> filesDVec;
cv::Mat temp;
fs[filename] >> temp;
filesDVec.push_back(temp);
and match
descriptors
with the image loaded:
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 2 cv::Mat
in this way:
matcher->match(filesDVec[i], givenInMat, matchesVector);
AKA match(Scene, Object, ...)
conclusion: minDist = 100 maxDist = 0
(more than one match).
But with them like this:
matcher->match(givenInMat, filesDVec[i], matchesVector);
AKA match(Object, Scene, ...)
this error is triggered:
opencv error:assertion failed (type == src2.type() && src1.cols == src2.cols && (type == CV_32F || type = CV_8U)) in void cv::batchDistance
I want descriptive information from each saved image to be loaded, what am I doing wrong?
Edit
I would add that this is not an example of trying to map and image to oneself as it is known to matcher
fail if the object and original image are equal for testing purposes.
Edit 2
File contents:
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
Reading:
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();
source to share
The problem is you allocate and build filesDVec
with elements imgVec.size()
(they are empty cv::Mat
s). Then each descriptor (you load in a loop for
) is added at the end of the vector filesDVec
.
As a result, you are trying to match some empty ones cv::Mat
with givenInMat
and most likely it will crash the application or assert. Try to read it like below:
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();
source to share